OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ | 5 #ifndef COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
6 #define COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ | 6 #define COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
15 #include "components/metrics/client_info.h" | 15 #include "components/metrics/client_info.h" |
16 | 16 |
17 class PrefService; | 17 class PrefService; |
18 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
19 | 19 |
20 namespace metrics { | 20 namespace metrics { |
21 | 21 |
22 class ClonedInstallDetector; | 22 class ClonedInstallDetector; |
| 23 class EnabledStateProvider; |
23 | 24 |
24 // Responsible for managing MetricsService state prefs, specifically the UMA | 25 // Responsible for managing MetricsService state prefs, specifically the UMA |
25 // client id and low entropy source. Code outside the metrics directory should | 26 // client id and low entropy source. Code outside the metrics directory should |
26 // not be instantiating or using this class directly. | 27 // not be instantiating or using this class directly. |
27 class MetricsStateManager { | 28 class MetricsStateManager { |
28 public: | 29 public: |
29 // A callback that can be invoked to store client info to persistent storage. | 30 // A callback that can be invoked to store client info to persistent storage. |
30 // Storing an empty client_id will resulted in the backup being voided. | 31 // Storing an empty client_id will resulted in the backup being voided. |
31 typedef base::Callback<void(const ClientInfo& client_info)> | 32 typedef base::Callback<void(const ClientInfo& client_info)> |
32 StoreClientInfoCallback; | 33 StoreClientInfoCallback; |
(...skipping 29 matching lines...) Expand all Loading... |
62 // that has a high source of entropy, partially based on the client ID. | 63 // that has a high source of entropy, partially based on the client ID. |
63 // Otherwise, it returns an entropy provider that is based on a low entropy | 64 // Otherwise, it returns an entropy provider that is based on a low entropy |
64 // source. | 65 // source. |
65 std::unique_ptr<const base::FieldTrial::EntropyProvider> | 66 std::unique_ptr<const base::FieldTrial::EntropyProvider> |
66 CreateEntropyProvider(); | 67 CreateEntropyProvider(); |
67 | 68 |
68 // Creates the MetricsStateManager, enforcing that only a single instance | 69 // Creates the MetricsStateManager, enforcing that only a single instance |
69 // of the class exists at a time. Returns NULL if an instance exists already. | 70 // of the class exists at a time. Returns NULL if an instance exists already. |
70 static std::unique_ptr<MetricsStateManager> Create( | 71 static std::unique_ptr<MetricsStateManager> Create( |
71 PrefService* local_state, | 72 PrefService* local_state, |
72 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 73 EnabledStateProvider* enabled_state_provider, |
73 const StoreClientInfoCallback& store_client_info, | 74 const StoreClientInfoCallback& store_client_info, |
74 const LoadClientInfoCallback& load_client_info); | 75 const LoadClientInfoCallback& load_client_info); |
75 | 76 |
76 // Registers local state prefs used by this class. | 77 // Registers local state prefs used by this class. |
77 static void RegisterPrefs(PrefRegistrySimple* registry); | 78 static void RegisterPrefs(PrefRegistrySimple* registry); |
78 | 79 |
79 private: | 80 private: |
80 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); | 81 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); |
81 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); | 82 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); |
82 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); | 83 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); |
83 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, | 84 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, |
84 PermutedEntropyCacheClearedWhenLowEntropyReset); | 85 PermutedEntropyCacheClearedWhenLowEntropyReset); |
85 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); | 86 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); |
86 | 87 |
87 // Designates which entropy source was returned from this class. | 88 // Designates which entropy source was returned from this class. |
88 // This is used for testing to validate that we return the correct source | 89 // This is used for testing to validate that we return the correct source |
89 // depending on the state of the service. | 90 // depending on the state of the service. |
90 enum EntropySourceType { | 91 enum EntropySourceType { |
91 ENTROPY_SOURCE_NONE, | 92 ENTROPY_SOURCE_NONE, |
92 ENTROPY_SOURCE_LOW, | 93 ENTROPY_SOURCE_LOW, |
93 ENTROPY_SOURCE_HIGH, | 94 ENTROPY_SOURCE_HIGH, |
94 ENTROPY_SOURCE_ENUM_SIZE, | 95 ENTROPY_SOURCE_ENUM_SIZE, |
95 }; | 96 }; |
96 | 97 |
97 // Creates the MetricsStateManager with the given |local_state|. Calls | 98 // Creates the MetricsStateManager with the given |local_state|. Uses |
98 // |is_reporting_enabled_callback| to query whether metrics reporting is | 99 // |enabled_state_provider| to query whether there is consent for metrics |
99 // enabled. Clients should instead use Create(), which enforces that a single | 100 // reporting, and if it is enabled. Clients should instead use Create(), which |
100 // instance of this class be alive at any given time. | 101 // enforces that a single instance of this class be alive at any given time. |
101 // |store_client_info| should back up client info to persistent storage such | 102 // |store_client_info| should back up client info to persistent storage such |
102 // that it is later retrievable by |load_client_info|. | 103 // that it is later retrievable by |load_client_info|. |
103 MetricsStateManager( | 104 MetricsStateManager(PrefService* local_state, |
104 PrefService* local_state, | 105 EnabledStateProvider* enabled_state_provider, |
105 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 106 const StoreClientInfoCallback& store_client_info, |
106 const StoreClientInfoCallback& store_client_info, | 107 const LoadClientInfoCallback& load_client_info); |
107 const LoadClientInfoCallback& load_client_info); | |
108 | 108 |
109 // Backs up the current client info via |store_client_info_|. | 109 // Backs up the current client info via |store_client_info_|. |
110 void BackUpCurrentClientInfo(); | 110 void BackUpCurrentClientInfo(); |
111 | 111 |
112 // Loads the client info via |load_client_info_| and potentially migrates it | 112 // Loads the client info via |load_client_info_| and potentially migrates it |
113 // before returning it if it comes back in its old form. | 113 // before returning it if it comes back in its old form. |
114 std::unique_ptr<ClientInfo> LoadClientInfoAndMaybeMigrate(); | 114 std::unique_ptr<ClientInfo> LoadClientInfoAndMaybeMigrate(); |
115 | 115 |
116 // Returns the low entropy source for this client. This is a random value | 116 // Returns the low entropy source for this client. This is a random value |
117 // that is non-identifying amongst browser clients. This method will | 117 // that is non-identifying amongst browser clients. This method will |
(...skipping 19 matching lines...) Expand all Loading... |
137 // pref is true. | 137 // pref is true. |
138 void ResetMetricsIDsIfNecessary(); | 138 void ResetMetricsIDsIfNecessary(); |
139 | 139 |
140 // Whether an instance of this class exists. Used to enforce that there aren't | 140 // Whether an instance of this class exists. Used to enforce that there aren't |
141 // multiple instances of this class at a given time. | 141 // multiple instances of this class at a given time. |
142 static bool instance_exists_; | 142 static bool instance_exists_; |
143 | 143 |
144 // Weak pointer to the local state prefs store. | 144 // Weak pointer to the local state prefs store. |
145 PrefService* const local_state_; | 145 PrefService* const local_state_; |
146 | 146 |
147 // A callback run by this MetricsStateManager to know whether reporting is | 147 // Weak pointer to an enabled state provider. Used to know whether the user |
148 // enabled. | 148 // has consented to reporting, and if reporting should be done. |
149 const base::Callback<bool(void)> is_reporting_enabled_callback_; | 149 EnabledStateProvider* enabled_state_provider_; |
150 | 150 |
151 // A callback run during client id creation so this MetricsStateManager can | 151 // A callback run during client id creation so this MetricsStateManager can |
152 // store a backup of the newly generated ID. | 152 // store a backup of the newly generated ID. |
153 const StoreClientInfoCallback store_client_info_; | 153 const StoreClientInfoCallback store_client_info_; |
154 | 154 |
155 // A callback run if this MetricsStateManager can't get the client id from | 155 // A callback run if this MetricsStateManager can't get the client id from |
156 // its typical location and wants to attempt loading it from this backup. | 156 // its typical location and wants to attempt loading it from this backup. |
157 const LoadClientInfoCallback load_client_info_; | 157 const LoadClientInfoCallback load_client_info_; |
158 | 158 |
159 // The identifier that's sent to the server with the log reports. | 159 // The identifier that's sent to the server with the log reports. |
160 std::string client_id_; | 160 std::string client_id_; |
161 | 161 |
162 // The non-identifying low entropy source value. | 162 // The non-identifying low entropy source value. |
163 int low_entropy_source_; | 163 int low_entropy_source_; |
164 | 164 |
165 // The last entropy source returned by this service, used for testing. | 165 // The last entropy source returned by this service, used for testing. |
166 EntropySourceType entropy_source_returned_; | 166 EntropySourceType entropy_source_returned_; |
167 | 167 |
168 std::unique_ptr<ClonedInstallDetector> cloned_install_detector_; | 168 std::unique_ptr<ClonedInstallDetector> cloned_install_detector_; |
169 | 169 |
170 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); | 170 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); |
171 }; | 171 }; |
172 | 172 |
173 } // namespace metrics | 173 } // namespace metrics |
174 | 174 |
175 #endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ | 175 #endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
OLD | NEW |