1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_
6#define COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_
7
8#include "base/gtest_prod_util.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/weak_ptr.h"
11
12class PrefRegistrySimple;
13class PrefService;
14
15namespace base {
16class SingleThreadTaskRunner;
17}
18
19namespace metrics {
20
21class MachineIdProvider;
22
23// A class for detecting if an install is cloned. It does this by detecting
24// when the hardware running Chrome changes.
25class ClonedInstallDetector {
26 public:
27  explicit ClonedInstallDetector(MachineIdProvider* raw_id_provider);
28  virtual ~ClonedInstallDetector();
29
30  // Posts a task to |task_runner| to generate a machine ID and store it to a
31  // local state pref. If the newly generated ID is different than the
32  // previously stored one, then the install is considered cloned. The ID is a
33  // 24-bit value based off of machine characteristics. This value should never
34  // be sent over the network.
35  // TODO(jwd): Implement change detection.
36  void CheckForClonedInstall(
37      PrefService* local_state,
38      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
39
40  static void RegisterPrefs(PrefRegistrySimple* registry);
41
42 private:
43  FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, SaveId);
44  FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, DetectClone);
45
46  // Converts raw_id into a 24-bit hash and stores the hash in |local_state|.
47  // |raw_id| is not a const ref because it's passed from a cross-thread post
48  // task.
49  void SaveMachineId(PrefService* local_state, std::string raw_id);
50
51  scoped_refptr<MachineIdProvider> raw_id_provider_;
52  base::WeakPtrFactory<ClonedInstallDetector> weak_ptr_factory_;
53
54  DISALLOW_COPY_AND_ASSIGN(ClonedInstallDetector);
55};
56
57}  // namespace metrics
58
59#endif  // COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_
60