OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_PREFS_PREF_METRICS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/prefs/pref_change_registrar.h" |
13 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
16 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
17 | 19 |
| 20 class PrefRegistrySimple; |
| 21 |
18 // PrefMetricsService is responsible for recording prefs-related UMA stats. | 22 // PrefMetricsService is responsible for recording prefs-related UMA stats. |
19 class PrefMetricsService : public BrowserContextKeyedService { | 23 class PrefMetricsService : public BrowserContextKeyedService { |
20 public: | 24 public: |
21 explicit PrefMetricsService(Profile* profile); | 25 explicit PrefMetricsService(Profile* profile); |
| 26 // For unit testing only. |
| 27 PrefMetricsService(Profile* profile, |
| 28 PrefService* local_settings, |
| 29 const std::string& device_id, |
| 30 const char** tracked_pref_paths, |
| 31 int tracked_pref_path_count); |
| 32 |
22 virtual ~PrefMetricsService(); | 33 virtual ~PrefMetricsService(); |
23 | 34 |
24 class Factory : public BrowserContextKeyedServiceFactory { | 35 class Factory : public BrowserContextKeyedServiceFactory { |
25 public: | 36 public: |
26 static Factory* GetInstance(); | 37 static Factory* GetInstance(); |
27 static PrefMetricsService* GetForProfile(Profile* profile); | 38 static PrefMetricsService* GetForProfile(Profile* profile); |
28 private: | 39 private: |
29 friend struct DefaultSingletonTraits<Factory>; | 40 friend struct DefaultSingletonTraits<Factory>; |
30 | 41 |
31 Factory(); | 42 Factory(); |
32 virtual ~Factory(); | 43 virtual ~Factory(); |
33 | 44 |
34 // BrowserContextKeyedServiceFactory implementation | 45 // BrowserContextKeyedServiceFactory implementation |
35 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 46 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
36 content::BrowserContext* profile) const OVERRIDE; | 47 content::BrowserContext* profile) const OVERRIDE; |
37 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | 48 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
38 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; | 49 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
39 virtual content::BrowserContext* GetBrowserContextToUse( | 50 virtual content::BrowserContext* GetBrowserContextToUse( |
40 content::BrowserContext* context) const OVERRIDE; | 51 content::BrowserContext* context) const OVERRIDE; |
41 }; | 52 }; |
42 | 53 |
| 54 // Registers preferences in local state. |
| 55 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 56 |
43 private: | 57 private: |
44 // Use a map to convert domains to their histogram identifiers. Ids are | 58 friend class PrefMetricsServiceTest; |
45 // defined in tools/metrics/histograms/histograms.xml and (usually) also in | |
46 // chrome/browser/search_engines/prepopulated_engines.json. | |
47 typedef std::map<std::string, int> DomainIdMap; | |
48 | 59 |
49 // Function to log a Value to a histogram | 60 // Function to log a Value to a histogram |
50 typedef base::Callback<void(const std::string&, const Value*)> | 61 typedef base::Callback<void(const std::string&, const Value*)> |
51 LogHistogramValueCallback; | 62 LogHistogramValueCallback; |
52 | 63 |
53 // Record prefs state on browser context creation. | 64 // Record prefs state on browser context creation. |
54 void RecordLaunchPrefs(); | 65 void RecordLaunchPrefs(); |
55 | 66 |
56 // Register callbacks for synced pref changes. | 67 // Register callbacks for synced pref changes. |
57 void RegisterSyncedPrefObservers(); | 68 void RegisterSyncedPrefObservers(); |
(...skipping 17 matching lines...) Expand all Loading... |
75 void LogIntegerPrefChange(int boundary_value, | 86 void LogIntegerPrefChange(int boundary_value, |
76 const std::string& histogram_name, | 87 const std::string& histogram_name, |
77 const Value* value); | 88 const Value* value); |
78 | 89 |
79 // Callback for a list pref change. Each item in the list | 90 // Callback for a list pref change. Each item in the list |
80 // is logged using the given histogram callback. | 91 // is logged using the given histogram callback. |
81 void LogListPrefChange(const LogHistogramValueCallback& item_callback, | 92 void LogListPrefChange(const LogHistogramValueCallback& item_callback, |
82 const std::string& histogram_name, | 93 const std::string& histogram_name, |
83 const Value* value); | 94 const Value* value); |
84 | 95 |
| 96 // Callback to receive a unique device_id. This is used to make it harder to |
| 97 // spoof the preference value checks. |
| 98 void GetDeviceIdCallback(const std::string& device_id); |
| 99 |
| 100 // Checks the tracked preferences against their last known values and reports |
| 101 // any discrepancies. This must be called after |device_id| has been set. |
| 102 void CheckTrackedPreferences(); |
| 103 |
| 104 // Updates the hash of the tracked preference in local state. This must be |
| 105 // called after |device_id| has been set. |
| 106 void UpdateTrackedPreference(const char* path); |
| 107 |
| 108 // Removes the tracked preference from local state. Returns 'true' iff. the |
| 109 // value was present. |
| 110 bool RemoveTrackedPreference(const char* path); |
| 111 |
| 112 // Gets the path to the preference value hash in local state. |
| 113 std::string GetHashedPrefPath(const char* path); |
| 114 |
| 115 // Computes an MD5 hash for the given preference value. Salts the hash with |
| 116 // the pref name and the device id to make it harder to spoof. |
| 117 std::string GetHashedPrefValue(const char* path, const base::Value* value); |
| 118 |
| 119 void InitializePrefObservers(); |
| 120 |
| 121 base::WeakPtrFactory<PrefMetricsService> weak_factory_; |
| 122 |
85 Profile* profile_; | 123 Profile* profile_; |
| 124 PrefService* prefs_; |
| 125 PrefService* local_state_; |
| 126 std::string profile_name_; |
| 127 std::string device_id_; |
| 128 const char** tracked_pref_paths_; |
| 129 const int tracked_pref_path_count_; |
| 130 |
| 131 PrefChangeRegistrar pref_registrar_; |
86 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; | 132 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; |
87 }; | 133 }; |
88 | 134 |
89 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 135 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
OLD | NEW |