OLD | NEW |
(Empty) | |
| 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 explicit MetricsStateManager(PrefService* local_state); |
| 28 virtual ~MetricsStateManager(); |
| 29 |
| 30 // Returns true if the user opted in to sending metric reports. |
| 31 // TODO(asvitkine): This function does not report the correct value on |
| 32 // Android, see http://crbug.com/362192. |
| 33 bool IsMetricsReportingEnabled(); |
| 34 |
| 35 // Returns the client ID for this client, or the empty string if the user is |
| 36 // not opted in to metrics reporting. |
| 37 const std::string& client_id() const { return client_id_; } |
| 38 |
| 39 // Forces the client ID to be generated. This is useful in case it's needed |
| 40 // before recording. |
| 41 void ForceClientIdCreation(); |
| 42 |
| 43 // Checks if this install was cloned or imaged from another machine. If a |
| 44 // clone is detected, resets the client id and low entropy source. This |
| 45 // should not be called more than once. |
| 46 void CheckForClonedInstall(); |
| 47 |
| 48 // Returns the preferred entropy provider used to seed persistent activities |
| 49 // based on whether or not metrics reporting is permitted on this client. |
| 50 // |
| 51 // 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. |
| 53 // Otherwise, an entropy provider that is based on a low entropy source is |
| 54 // returned. |
| 55 scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider(); |
| 56 |
| 57 // Registers local state prefs used by this class. |
| 58 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 59 |
| 60 private: |
| 61 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_Low); |
| 62 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, EntropySourceUsed_High); |
| 63 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, LowEntropySource0NotReset); |
| 64 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, |
| 65 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 66 FRIEND_TEST_ALL_PREFIXES(MetricsStateManagerTest, ResetMetricsIDs); |
| 67 |
| 68 // Designates which entropy source was returned from this class. |
| 69 // This is used for testing to validate that we return the correct source |
| 70 // depending on the state of the service. |
| 71 enum EntropySourceType { |
| 72 ENTROPY_SOURCE_NONE, |
| 73 ENTROPY_SOURCE_LOW, |
| 74 ENTROPY_SOURCE_HIGH, |
| 75 }; |
| 76 |
| 77 // Returns the low entropy source for this client. This is a random value |
| 78 // that is non-identifying amongst browser clients. This method will |
| 79 // generate the entropy source value if it has not been called before. |
| 80 int GetLowEntropySource(); |
| 81 |
| 82 // Returns the first entropy source that was returned by this service since |
| 83 // start up, or NONE if neither was returned yet. This is exposed for testing |
| 84 // only. |
| 85 EntropySourceType entropy_source_returned() const { |
| 86 return entropy_source_returned_; |
| 87 } |
| 88 |
| 89 // Reset the client id and low entropy source if the kMetricsResetMetricIDs |
| 90 // pref is true. |
| 91 void ResetMetricsIDsIfNecessary(); |
| 92 |
| 93 // Weak pointer to the local state prefs store. |
| 94 PrefService* local_state_; |
| 95 |
| 96 // The identifier that's sent to the server with the log reports. |
| 97 std::string client_id_; |
| 98 |
| 99 // The non-identifying low entropy source value. |
| 100 int low_entropy_source_; |
| 101 |
| 102 // The last entropy source returned by this service, used for testing. |
| 103 EntropySourceType entropy_source_returned_; |
| 104 |
| 105 scoped_ptr<ClonedInstallDetector> cloned_install_detector_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(MetricsStateManager); |
| 108 }; |
| 109 |
| 110 } // namespace metrics |
| 111 |
| 112 #endif // CHROME_BROWSER_METRICS_METRICS_STATE_MANAGER_H_ |
OLD | NEW |