| 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 CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ |
| 6 #define CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ | 6 #define CHROME_BROWSER_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/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 14 | 15 |
| 15 class PrefService; | 16 class PrefService; |
| 16 class PrefRegistrySimple; | 17 class PrefRegistrySimple; |
| 17 | 18 |
| 18 namespace metrics { | 19 namespace metrics { |
| 19 | 20 |
| 20 class ClonedInstallDetector; | 21 class ClonedInstallDetector; |
| 21 | 22 |
| 22 // Responsible for managing MetricsService state prefs, specifically the UMA | 23 // Responsible for managing MetricsService state prefs, specifically the UMA |
| 23 // client id and low entropy source. Code outside the metrics directory should | 24 // client id and low entropy source. Code outside the metrics directory should |
| 24 // not be instantiating or using this class directly. | 25 // not be instantiating or using this class directly. |
| 25 class MetricsStateManager { | 26 class MetricsStateManager { |
| 26 public: | 27 public: |
| 27 virtual ~MetricsStateManager(); | 28 virtual ~MetricsStateManager(); |
| 28 | 29 |
| 29 // Returns true if the user opted in to sending metric reports. | 30 // Returns true if the user opted in to sending metric reports. |
| 30 // TODO(asvitkine): This function does not report the correct value on | |
| 31 // Android, see http://crbug.com/362192. | |
| 32 bool IsMetricsReportingEnabled(); | 31 bool IsMetricsReportingEnabled(); |
| 33 | 32 |
| 34 // Returns the client ID for this client, or the empty string if the user is | 33 // Returns the client ID for this client, or the empty string if the user is |
| 35 // not opted in to metrics reporting. | 34 // not opted in to metrics reporting. |
| 36 const std::string& client_id() const { return client_id_; } | 35 const std::string& client_id() const { return client_id_; } |
| 37 | 36 |
| 38 // Forces the client ID to be generated. This is useful in case it's needed | 37 // Forces the client ID to be generated. This is useful in case it's needed |
| 39 // before recording. | 38 // before recording. |
| 40 void ForceClientIdCreation(); | 39 void ForceClientIdCreation(); |
| 41 | 40 |
| 42 // Checks if this install was cloned or imaged from another machine. If a | 41 // Checks if this install was cloned or imaged from another machine. If a |
| 43 // clone is detected, resets the client id and low entropy source. This | 42 // clone is detected, resets the client id and low entropy source. This |
| 44 // should not be called more than once. | 43 // should not be called more than once. |
| 45 void CheckForClonedInstall( | 44 void CheckForClonedInstall( |
| 46 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 47 | 46 |
| 48 // Returns the preferred entropy provider used to seed persistent activities | 47 // Returns the preferred entropy provider used to seed persistent activities |
| 49 // based on whether or not metrics reporting is permitted on this client. | 48 // based on whether or not metrics reporting is permitted on this client. |
| 50 // | 49 // |
| 51 // If metrics reporting is enabled, this method returns an entropy provider | 50 // If metrics reporting is enabled, this method returns an entropy provider |
| 52 // that has a high source of entropy, partially based on the client ID. | 51 // that has a high source of entropy, partially based on the client ID. |
| 53 // Otherwise, it returns an entropy provider that is based on a low entropy | 52 // Otherwise, it returns an entropy provider that is based on a low entropy |
| 54 // source. | 53 // source. |
| 55 scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider(); | 54 scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider(); |
| 56 | 55 |
| 57 // Creates the MetricsStateManager, enforcing that only a single instance | 56 // Creates the MetricsStateManager, enforcing that only a single instance |
| 58 // of the class exists at a time. Returns NULL if an instance exists already. | 57 // of the class exists at a time. Returns NULL if an instance exists already. |
| 59 static scoped_ptr<MetricsStateManager> Create(PrefService* local_state); | 58 static scoped_ptr<MetricsStateManager> Create( |
| 59 PrefService* local_state, |
| 60 const base::Callback<bool(void)>& is_reporting_enabled_callback); |
| 60 | 61 |
| 61 // Registers local state prefs used by this class. | 62 // Registers local state prefs used by this class. |
| 62 static void RegisterPrefs(PrefRegistrySimple* registry); | 63 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 63 | 64 |
| 64 private: | 65 private: |
| 65 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); | 66 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); |
| 66 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); | 67 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); |
| 67 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); | 68 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); |
| 68 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, | 69 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, |
| 69 PermutedEntropyCacheClearedWhenLowEntropyReset); | 70 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 70 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); | 71 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); |
| 71 | 72 |
| 72 // Designates which entropy source was returned from this class. | 73 // Designates which entropy source was returned from this class. |
| 73 // This is used for testing to validate that we return the correct source | 74 // This is used for testing to validate that we return the correct source |
| 74 // depending on the state of the service. | 75 // depending on the state of the service. |
| 75 enum EntropySourceType { | 76 enum EntropySourceType { |
| 76 ENTROPY_SOURCE_NONE, | 77 ENTROPY_SOURCE_NONE, |
| 77 ENTROPY_SOURCE_LOW, | 78 ENTROPY_SOURCE_LOW, |
| 78 ENTROPY_SOURCE_HIGH, | 79 ENTROPY_SOURCE_HIGH, |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 // Creates the MetricsStateManager with the given |local_state|. Clients | 82 // Creates the MetricsStateManager with the given |local_state|. Calls |
| 82 // should instead use Create(), which enforces a single instance of this class | 83 // |is_reporting_enabled_callback| to query whether metrics reporting is |
| 83 // is alive at any given time. | 84 // enabled. Clients should instead use Create(), which enforces a single |
| 84 explicit MetricsStateManager(PrefService* local_state); | 85 // instance of this class is alive at any given time. |
| 86 MetricsStateManager( |
| 87 PrefService* local_state, |
| 88 const base::Callback<bool(void)>& is_reporting_enabled_callback); |
| 85 | 89 |
| 86 // Returns the low entropy source for this client. This is a random value | 90 // Returns the low entropy source for this client. This is a random value |
| 87 // that is non-identifying amongst browser clients. This method will | 91 // that is non-identifying amongst browser clients. This method will |
| 88 // generate the entropy source value if it has not been called before. | 92 // generate the entropy source value if it has not been called before. |
| 89 int GetLowEntropySource(); | 93 int GetLowEntropySource(); |
| 90 | 94 |
| 91 // Returns the first entropy source that was returned by this service since | 95 // Returns the first entropy source that was returned by this service since |
| 92 // start up, or NONE if neither was returned yet. This is exposed for testing | 96 // start up, or NONE if neither was returned yet. This is exposed for testing |
| 93 // only. | 97 // only. |
| 94 EntropySourceType entropy_source_returned() const { | 98 EntropySourceType entropy_source_returned() const { |
| 95 return entropy_source_returned_; | 99 return entropy_source_returned_; |
| 96 } | 100 } |
| 97 | 101 |
| 98 // Reset the client id and low entropy source if the kMetricsResetMetricIDs | 102 // Reset the client id and low entropy source if the kMetricsResetMetricIDs |
| 99 // pref is true. | 103 // pref is true. |
| 100 void ResetMetricsIDsIfNecessary(); | 104 void ResetMetricsIDsIfNecessary(); |
| 101 | 105 |
| 102 // Whether an instance of this class exists. Used to enforce that there aren't | 106 // Whether an instance of this class exists. Used to enforce that there aren't |
| 103 // multiple instances of this class at a given time. | 107 // multiple instances of this class at a given time. |
| 104 static bool instance_exists_; | 108 static bool instance_exists_; |
| 105 | 109 |
| 106 // Weak pointer to the local state prefs store. | 110 // Weak pointer to the local state prefs store. |
| 107 PrefService* local_state_; | 111 PrefService* const local_state_; |
| 112 |
| 113 const base::Callback<bool(void)> is_reporting_enabled_callback_; |
| 108 | 114 |
| 109 // The identifier that's sent to the server with the log reports. | 115 // The identifier that's sent to the server with the log reports. |
| 110 std::string client_id_; | 116 std::string client_id_; |
| 111 | 117 |
| 112 // The non-identifying low entropy source value. | 118 // The non-identifying low entropy source value. |
| 113 int low_entropy_source_; | 119 int low_entropy_source_; |
| 114 | 120 |
| 115 // The last entropy source returned by this service, used for testing. | 121 // The last entropy source returned by this service, used for testing. |
| 116 EntropySourceType entropy_source_returned_; | 122 EntropySourceType entropy_source_returned_; |
| 117 | 123 |
| 118 scoped_ptr<ClonedInstallDetector> cloned_install_detector_; | 124 scoped_ptr<ClonedInstallDetector> cloned_install_detector_; |
| 119 | 125 |
| 120 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); | 126 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); |
| 121 }; | 127 }; |
| 122 | 128 |
| 123 } // namespace metrics | 129 } // namespace metrics |
| 124 | 130 |
| 125 #endif // CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ | 131 #endif // CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ |
| OLD | NEW |