cloned_install_detector.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_
  5. #define COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_
  6. #include "base/gtest_prod_util.h"
  7. #include "base/memory/weak_ptr.h"
  8. class PrefRegistrySimple;
  9. class PrefService;
  10. namespace metrics {
  11. // A struct that holds cloned install related fields in prefs that need to be
  12. // reported in the system_profile.
  13. struct ClonedInstallInfo {
  14. int64_t last_reset_timestamp;
  15. int64_t first_reset_timestamp;
  16. int reset_count;
  17. };
  18. // A class for detecting if an install is cloned. It does this by detecting
  19. // when the hardware running Chrome changes.
  20. class ClonedInstallDetector {
  21. public:
  22. ClonedInstallDetector();
  23. ClonedInstallDetector(const ClonedInstallDetector&) = delete;
  24. ClonedInstallDetector& operator=(const ClonedInstallDetector&) = delete;
  25. virtual ~ClonedInstallDetector();
  26. // Posts a task to |task_runner| to generate a machine ID and store it to a
  27. // local state pref. If the newly generated ID is different than the
  28. // previously stored one, then the install is considered cloned. The ID is a
  29. // 24-bit value based off of machine characteristics. This value should never
  30. // be sent over the network.
  31. void CheckForClonedInstall(PrefService* local_state);
  32. static void RegisterPrefs(PrefRegistrySimple* registry);
  33. // Reads cloned install info fields from |local_state| and returns them in
  34. // a ClonedInstallInfo.
  35. static ClonedInstallInfo ReadClonedInstallInfo(PrefService* local_state);
  36. // Clears cloned install info fields from |local_state|.
  37. static void ClearClonedInstallInfo(PrefService* local_state);
  38. // Updates cloned install info fields in |local_state| on reset.
  39. static void RecordClonedInstallInfo(PrefService* local_state);
  40. // Returns true for the whole session if we detected a cloned install during
  41. // the construction of a client id.
  42. bool ShouldResetClientIds(PrefService* local_state);
  43. // Returns true for the whole session if we detect a cloned install this
  44. // session.
  45. bool ClonedInstallDetectedInCurrentSession() const;
  46. private:
  47. FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, SaveId);
  48. FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, DetectClone);
  49. FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, ShouldResetClientIds);
  50. FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest,
  51. ClonedInstallDetectedInCurrentSession);
  52. FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, CheckProviderResetIds);
  53. // Converts raw_id into a 24-bit hash and stores the hash in |local_state|.
  54. // |raw_id| is not a const ref because it's passed from a cross-thread post
  55. // task.
  56. void SaveMachineId(PrefService* local_state, const std::string& raw_id);
  57. // Indicates that we detected a cloned install during the current session.
  58. bool detected_this_session_ = false;
  59. // Indicates that we detected a cloned install during the construction of a
  60. // client id and should reset client ids as a result.
  61. bool should_reset_client_ids_ = false;
  62. base::WeakPtrFactory<ClonedInstallDetector> weak_ptr_factory_{this};
  63. };
  64. } // namespace metrics
  65. #endif // COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_