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