| 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; |
| 33 | 34 |
| 34 // A callback that can be invoked to load client info stored through the | 35 // A callback that can be invoked to load client info stored through the |
| 35 // StoreClientInfoCallback. | 36 // StoreClientInfoCallback. |
| 36 typedef base::Callback<std::unique_ptr<ClientInfo>(void)> | 37 typedef base::Callback<std::unique_ptr<ClientInfo>(void)> |
| 37 LoadClientInfoCallback; | 38 LoadClientInfoCallback; |
| 38 | 39 |
| 39 virtual ~MetricsStateManager(); | 40 virtual ~MetricsStateManager(); |
| 40 | 41 |
| 41 // Returns true if the user opted in to sending metric reports. | 42 // Returns true if the user has consented to sending metric reports, and there |
| 43 // is no other reason to disable reporting. One such reason is client |
| 44 // sampling, and this client isn't in the sample. |
| 42 bool IsMetricsReportingEnabled(); | 45 bool IsMetricsReportingEnabled(); |
| 43 | 46 |
| 44 // Returns the client ID for this client, or the empty string if the user is | 47 // Returns the client ID for this client, or the empty string if the user is |
| 45 // not opted in to metrics reporting. | 48 // not opted in to metrics reporting. |
| 46 const std::string& client_id() const { return client_id_; } | 49 const std::string& client_id() const { return client_id_; } |
| 47 | 50 |
| 48 // Forces the client ID to be generated. This is useful in case it's needed | 51 // Forces the client ID to be generated. This is useful in case it's needed |
| 49 // before recording. | 52 // before recording. |
| 50 void ForceClientIdCreation(); | 53 void ForceClientIdCreation(); |
| 51 | 54 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 62 // that has a high source of entropy, partially based on the client ID. | 65 // 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 | 66 // Otherwise, it returns an entropy provider that is based on a low entropy |
| 64 // source. | 67 // source. |
| 65 std::unique_ptr<const base::FieldTrial::EntropyProvider> | 68 std::unique_ptr<const base::FieldTrial::EntropyProvider> |
| 66 CreateEntropyProvider(); | 69 CreateEntropyProvider(); |
| 67 | 70 |
| 68 // Creates the MetricsStateManager, enforcing that only a single instance | 71 // Creates the MetricsStateManager, enforcing that only a single instance |
| 69 // of the class exists at a time. Returns NULL if an instance exists already. | 72 // of the class exists at a time. Returns NULL if an instance exists already. |
| 70 static std::unique_ptr<MetricsStateManager> Create( | 73 static std::unique_ptr<MetricsStateManager> Create( |
| 71 PrefService* local_state, | 74 PrefService* local_state, |
| 72 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 75 EnabledStateProvider* enabled_state_provider, |
| 73 const StoreClientInfoCallback& store_client_info, | 76 const StoreClientInfoCallback& store_client_info, |
| 74 const LoadClientInfoCallback& load_client_info); | 77 const LoadClientInfoCallback& load_client_info); |
| 75 | 78 |
| 76 // Registers local state prefs used by this class. | 79 // Registers local state prefs used by this class. |
| 77 static void RegisterPrefs(PrefRegistrySimple* registry); | 80 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 78 | 81 |
| 79 private: | 82 private: |
| 80 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); | 83 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); |
| 81 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); | 84 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); |
| 82 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); | 85 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); |
| 83 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, | 86 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, |
| 84 PermutedEntropyCacheClearedWhenLowEntropyReset); | 87 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 85 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); | 88 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); |
| 86 | 89 |
| 87 // Designates which entropy source was returned from this class. | 90 // Designates which entropy source was returned from this class. |
| 88 // This is used for testing to validate that we return the correct source | 91 // This is used for testing to validate that we return the correct source |
| 89 // depending on the state of the service. | 92 // depending on the state of the service. |
| 90 enum EntropySourceType { | 93 enum EntropySourceType { |
| 91 ENTROPY_SOURCE_NONE, | 94 ENTROPY_SOURCE_NONE, |
| 92 ENTROPY_SOURCE_LOW, | 95 ENTROPY_SOURCE_LOW, |
| 93 ENTROPY_SOURCE_HIGH, | 96 ENTROPY_SOURCE_HIGH, |
| 94 ENTROPY_SOURCE_ENUM_SIZE, | 97 ENTROPY_SOURCE_ENUM_SIZE, |
| 95 }; | 98 }; |
| 96 | 99 |
| 97 // Creates the MetricsStateManager with the given |local_state|. Calls | 100 // Creates the MetricsStateManager with the given |local_state|. Uses |
| 98 // |is_reporting_enabled_callback| to query whether metrics reporting is | 101 // |enabled_state_provider| to query whether there is consent for metrics |
| 99 // enabled. Clients should instead use Create(), which enforces that a single | 102 // reporting, and if it is enabled. Clients should instead use Create(), which |
| 100 // instance of this class be alive at any given time. | 103 // 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 | 104 // |store_client_info| should back up client info to persistent storage such |
| 102 // that it is later retrievable by |load_client_info|. | 105 // that it is later retrievable by |load_client_info|. |
| 103 MetricsStateManager( | 106 MetricsStateManager(PrefService* local_state, |
| 104 PrefService* local_state, | 107 EnabledStateProvider* enabled_state_provider, |
| 105 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 108 const StoreClientInfoCallback& store_client_info, |
| 106 const StoreClientInfoCallback& store_client_info, | 109 const LoadClientInfoCallback& load_client_info); |
| 107 const LoadClientInfoCallback& load_client_info); | |
| 108 | 110 |
| 109 // Backs up the current client info via |store_client_info_|. | 111 // Backs up the current client info via |store_client_info_|. |
| 110 void BackUpCurrentClientInfo(); | 112 void BackUpCurrentClientInfo(); |
| 111 | 113 |
| 112 // Loads the client info via |load_client_info_| and potentially migrates it | 114 // Loads the client info via |load_client_info_| and potentially migrates it |
| 113 // before returning it if it comes back in its old form. | 115 // before returning it if it comes back in its old form. |
| 114 std::unique_ptr<ClientInfo> LoadClientInfoAndMaybeMigrate(); | 116 std::unique_ptr<ClientInfo> LoadClientInfoAndMaybeMigrate(); |
| 115 | 117 |
| 116 // Returns the low entropy source for this client. This is a random value | 118 // Returns the low entropy source for this client. This is a random value |
| 117 // that is non-identifying amongst browser clients. This method will | 119 // that is non-identifying amongst browser clients. This method will |
| (...skipping 19 matching lines...) Expand all Loading... |
| 137 // pref is true. | 139 // pref is true. |
| 138 void ResetMetricsIDsIfNecessary(); | 140 void ResetMetricsIDsIfNecessary(); |
| 139 | 141 |
| 140 // Whether an instance of this class exists. Used to enforce that there aren't | 142 // Whether an instance of this class exists. Used to enforce that there aren't |
| 141 // multiple instances of this class at a given time. | 143 // multiple instances of this class at a given time. |
| 142 static bool instance_exists_; | 144 static bool instance_exists_; |
| 143 | 145 |
| 144 // Weak pointer to the local state prefs store. | 146 // Weak pointer to the local state prefs store. |
| 145 PrefService* const local_state_; | 147 PrefService* const local_state_; |
| 146 | 148 |
| 147 // A callback run by this MetricsStateManager to know whether reporting is | 149 // Weak pointer to an enabled state provider. Used to know whether the user |
| 148 // enabled. | 150 // has consented to reporting, and if reporting should be done. |
| 149 const base::Callback<bool(void)> is_reporting_enabled_callback_; | 151 EnabledStateProvider* enabled_state_provider_; |
| 150 | 152 |
| 151 // A callback run during client id creation so this MetricsStateManager can | 153 // A callback run during client id creation so this MetricsStateManager can |
| 152 // store a backup of the newly generated ID. | 154 // store a backup of the newly generated ID. |
| 153 const StoreClientInfoCallback store_client_info_; | 155 const StoreClientInfoCallback store_client_info_; |
| 154 | 156 |
| 155 // A callback run if this MetricsStateManager can't get the client id from | 157 // 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. | 158 // its typical location and wants to attempt loading it from this backup. |
| 157 const LoadClientInfoCallback load_client_info_; | 159 const LoadClientInfoCallback load_client_info_; |
| 158 | 160 |
| 159 // The identifier that's sent to the server with the log reports. | 161 // The identifier that's sent to the server with the log reports. |
| 160 std::string client_id_; | 162 std::string client_id_; |
| 161 | 163 |
| 162 // The non-identifying low entropy source value. | 164 // The non-identifying low entropy source value. |
| 163 int low_entropy_source_; | 165 int low_entropy_source_; |
| 164 | 166 |
| 165 // The last entropy source returned by this service, used for testing. | 167 // The last entropy source returned by this service, used for testing. |
| 166 EntropySourceType entropy_source_returned_; | 168 EntropySourceType entropy_source_returned_; |
| 167 | 169 |
| 168 std::unique_ptr<ClonedInstallDetector> cloned_install_detector_; | 170 std::unique_ptr<ClonedInstallDetector> cloned_install_detector_; |
| 169 | 171 |
| 170 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); | 172 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); |
| 171 }; | 173 }; |
| 172 | 174 |
| 173 } // namespace metrics | 175 } // namespace metrics |
| 174 | 176 |
| 175 #endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ | 177 #endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
| OLD | NEW |