[email protected] | 3907664 | 2014-05-05 20:32:55 | [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 | |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 5 | #ifndef COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
| 6 | #define COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/metrics/field_trial.h" |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 16 | #include "components/metrics/client_info.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 17 | |
| 18 | class PrefService; |
| 19 | class PrefRegistrySimple; |
| 20 | |
| 21 | namespace metrics { |
| 22 | |
| 23 | class ClonedInstallDetector; |
| 24 | |
| 25 | // Responsible for managing MetricsService state prefs, specifically the UMA |
| 26 | // client id and low entropy source. Code outside the metrics directory should |
| 27 | // not be instantiating or using this class directly. |
| 28 | class MetricsStateManager { |
| 29 | public: |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 30 | // A callback that can be invoked to store client info to persistent storage. |
| 31 | // Storing an empty client_id will resulted in the backup being voided. |
| 32 | typedef base::Callback<void(const ClientInfo& client_info)> |
| 33 | StoreClientInfoCallback; |
| 34 | |
| 35 | // A callback that can be invoked to load client info stored through the |
| 36 | // StoreClientInfoCallback. |
| 37 | typedef base::Callback<scoped_ptr<ClientInfo>(void)> LoadClientInfoCallback; |
| 38 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 39 | virtual ~MetricsStateManager(); |
| 40 | |
| 41 | // Returns true if the user opted in to sending metric reports. |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 42 | bool IsMetricsReportingEnabled(); |
| 43 | |
| 44 | // Returns the client ID for this client, or the empty string if the user is |
| 45 | // not opted in to metrics reporting. |
| 46 | const std::string& client_id() const { return client_id_; } |
| 47 | |
| 48 | // Forces the client ID to be generated. This is useful in case it's needed |
| 49 | // before recording. |
| 50 | void ForceClientIdCreation(); |
| 51 | |
| 52 | // Checks if this install was cloned or imaged from another machine. If a |
| 53 | // clone is detected, resets the client id and low entropy source. This |
| 54 | // should not be called more than once. |
[email protected] | 61b0d48 | 2014-05-20 14:49:10 | [diff] [blame] | 55 | void CheckForClonedInstall( |
| 56 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 57 | |
| 58 | // Returns the preferred entropy provider used to seed persistent activities |
| 59 | // based on whether or not metrics reporting is permitted on this client. |
| 60 | // |
| 61 | // If metrics reporting is enabled, this method returns an entropy provider |
| 62 | // 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 | // source. |
| 65 | scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider(); |
| 66 | |
| 67 | // Creates the MetricsStateManager, enforcing that only a single instance |
| 68 | // of the class exists at a time. Returns NULL if an instance exists already. |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 69 | static scoped_ptr<MetricsStateManager> Create( |
| 70 | PrefService* local_state, |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 71 | const base::Callback<bool(void)>& is_reporting_enabled_callback, |
| 72 | const StoreClientInfoCallback& store_client_info, |
| 73 | const LoadClientInfoCallback& load_client_info); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 74 | |
| 75 | // Registers local state prefs used by this class. |
| 76 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 77 | |
| 78 | private: |
| 79 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); |
| 80 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); |
| 81 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); |
| 82 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, |
| 83 | PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 84 | FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); |
| 85 | |
| 86 | // Designates which entropy source was returned from this class. |
| 87 | // This is used for testing to validate that we return the correct source |
| 88 | // depending on the state of the service. |
| 89 | enum EntropySourceType { |
| 90 | ENTROPY_SOURCE_NONE, |
| 91 | ENTROPY_SOURCE_LOW, |
| 92 | ENTROPY_SOURCE_HIGH, |
asvitkine | 20fd1db | 2015-02-25 19:06:22 | [diff] [blame] | 93 | ENTROPY_SOURCE_ENUM_SIZE, |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 96 | // Creates the MetricsStateManager with the given |local_state|. Calls |
| 97 | // |is_reporting_enabled_callback| to query whether metrics reporting is |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 98 | // enabled. Clients should instead use Create(), which enforces that a single |
| 99 | // instance of this class be alive at any given time. |
| 100 | // |store_client_info| should back up client info to persistent storage such |
| 101 | // that it is later retrievable by |load_client_info|. |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 102 | MetricsStateManager( |
| 103 | PrefService* local_state, |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 104 | const base::Callback<bool(void)>& is_reporting_enabled_callback, |
| 105 | const StoreClientInfoCallback& store_client_info, |
| 106 | const LoadClientInfoCallback& load_client_info); |
| 107 | |
| 108 | // Backs up the current client info via |store_client_info_|. |
| 109 | void BackUpCurrentClientInfo(); |
| 110 | |
| 111 | // Loads the client info via |load_client_info_| and potentially migrates it |
| 112 | // before returning it if it comes back in its old form. |
| 113 | scoped_ptr<ClientInfo> LoadClientInfoAndMaybeMigrate(); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 114 | |
| 115 | // Returns the low entropy source for this client. This is a random value |
| 116 | // that is non-identifying amongst browser clients. This method will |
| 117 | // generate the entropy source value if it has not been called before. |
| 118 | int GetLowEntropySource(); |
| 119 | |
isherman | 6f910f1 | 2015-08-07 22:53:55 | [diff] [blame] | 120 | // Generates the low entropy source value for this client if it is not |
| 121 | // already set. |
| 122 | void UpdateLowEntropySource(); |
| 123 | |
asvitkine | 20fd1db | 2015-02-25 19:06:22 | [diff] [blame] | 124 | // Updates |entropy_source_returned_| with |type| iff the current value is |
| 125 | // ENTROPY_SOURCE_NONE and logs the new value in a histogram. |
| 126 | void UpdateEntropySourceReturnedValue(EntropySourceType type); |
| 127 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 128 | // Returns the first entropy source that was returned by this service since |
| 129 | // start up, or NONE if neither was returned yet. This is exposed for testing |
| 130 | // only. |
| 131 | EntropySourceType entropy_source_returned() const { |
| 132 | return entropy_source_returned_; |
| 133 | } |
| 134 | |
| 135 | // Reset the client id and low entropy source if the kMetricsResetMetricIDs |
| 136 | // pref is true. |
| 137 | void ResetMetricsIDsIfNecessary(); |
| 138 | |
| 139 | // Whether an instance of this class exists. Used to enforce that there aren't |
| 140 | // multiple instances of this class at a given time. |
| 141 | static bool instance_exists_; |
| 142 | |
| 143 | // Weak pointer to the local state prefs store. |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 144 | PrefService* const local_state_; |
| 145 | |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 146 | // A callback run by this MetricsStateManager to know whether reporting is |
| 147 | // enabled. |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 148 | const base::Callback<bool(void)> is_reporting_enabled_callback_; |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 149 | |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 150 | // A callback run during client id creation so this MetricsStateManager can |
| 151 | // store a backup of the newly generated ID. |
| 152 | const StoreClientInfoCallback store_client_info_; |
| 153 | |
| 154 | // A callback run if this MetricsStateManager can't get the client id from |
| 155 | // its typical location and wants to attempt loading it from this backup. |
| 156 | const LoadClientInfoCallback load_client_info_; |
| 157 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 158 | // The identifier that's sent to the server with the log reports. |
| 159 | std::string client_id_; |
| 160 | |
| 161 | // The non-identifying low entropy source value. |
| 162 | int low_entropy_source_; |
| 163 | |
| 164 | // The last entropy source returned by this service, used for testing. |
| 165 | EntropySourceType entropy_source_returned_; |
| 166 | |
| 167 | scoped_ptr<ClonedInstallDetector> cloned_install_detector_; |
| 168 | |
| 169 | DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); |
| 170 | }; |
| 171 | |
| 172 | } // namespace metrics |
| 173 | |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 174 | #endif // COMPONENTS_METRICS_METRICS_STATE_MANAGER_H_ |