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