jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 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 | |
blundell@chromium.org | 66d176a | 2014-05-22 13:49:39 | [diff] [blame] | 5 | #ifndef COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_ |
| 6 | #define COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_ |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 7 | |
| 8 | #include "base/gtest_prod_util.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 9 | #include "base/macros.h" |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
| 11 | |
| 12 | class PrefRegistrySimple; |
| 13 | class PrefService; |
| 14 | |
| 15 | namespace metrics { |
| 16 | |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 17 | // A class for detecting if an install is cloned. It does this by detecting |
| 18 | // when the hardware running Chrome changes. |
| 19 | class ClonedInstallDetector { |
| 20 | public: |
Gabriel Charette | 45f796f | 2017-07-12 07:00:54 | [diff] [blame] | 21 | ClonedInstallDetector(); |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 22 | virtual ~ClonedInstallDetector(); |
| 23 | |
blundell@chromium.org | 61b0d48 | 2014-05-20 14:49:10 | [diff] [blame] | 24 | // Posts a task to |task_runner| to generate a machine ID and store it to a |
| 25 | // local state pref. If the newly generated ID is different than the |
| 26 | // previously stored one, then the install is considered cloned. The ID is a |
| 27 | // 24-bit value based off of machine characteristics. This value should never |
| 28 | // be sent over the network. |
Gabriel Charette | 45f796f | 2017-07-12 07:00:54 | [diff] [blame] | 29 | void CheckForClonedInstall(PrefService* local_state); |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 30 | |
| 31 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 32 | |
Joshua Berenhaus | 5235d4fe | 2020-01-23 19:03:45 | [diff] [blame] | 33 | // Returns true for the whole session if we detected a cloned install during |
| 34 | // the construction of a client id. |
| 35 | bool ShouldResetClientIds(PrefService* local_state); |
| 36 | |
| 37 | // Returns true for the whole session if we detect a cloned install this |
| 38 | // session. |
| 39 | bool ClonedInstallDetectedInCurrentSession() const; |
| 40 | |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 41 | private: |
| 42 | FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, SaveId); |
jwd@chromium.org | 37d4709a | 2014-03-29 03:07:40 | [diff] [blame] | 43 | FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, DetectClone); |
Joshua Berenhaus | 5235d4fe | 2020-01-23 19:03:45 | [diff] [blame] | 44 | FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, ShouldResetClientIds); |
| 45 | FRIEND_TEST_ALL_PREFIXES(ClonedInstallDetectorTest, |
| 46 | ClonedInstallDetectedInCurrentSession); |
| 47 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, CheckProviderResetIds); |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 48 | |
| 49 | // Converts raw_id into a 24-bit hash and stores the hash in |local_state|. |
| 50 | // |raw_id| is not a const ref because it's passed from a cross-thread post |
| 51 | // task. |
ki.stfu | 939799a4 | 2015-09-28 04:41:20 | [diff] [blame] | 52 | void SaveMachineId(PrefService* local_state, const std::string& raw_id); |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 53 | |
Joshua Berenhaus | 5235d4fe | 2020-01-23 19:03:45 | [diff] [blame] | 54 | // Indicates that we detected a cloned install during the current session. |
| 55 | bool detected_this_session_ = false; |
| 56 | |
| 57 | // Indicates that we detected a cloned install during the construction of a |
| 58 | // client id and should reset client ids as a result. |
| 59 | bool should_reset_client_ids_ = false; |
| 60 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 61 | base::WeakPtrFactory<ClonedInstallDetector> weak_ptr_factory_{this}; |
jwd@chromium.org | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(ClonedInstallDetector); |
| 64 | }; |
| 65 | |
| 66 | } // namespace metrics |
| 67 | |
blundell@chromium.org | 66d176a | 2014-05-22 13:49:39 | [diff] [blame] | 68 | #endif // COMPONENTS_METRICS_CLONED_INSTALL_DETECTOR_H_ |