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