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