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/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" |
11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
13 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
14 #include "base/prefs/pref_change_registrar.h" | 17 #include "base/prefs/pref_change_registrar.h" |
15 #include "chrome/browser/prefs/synced_pref_change_registrar.h" | 18 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 19 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
18 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 20 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
19 | 21 |
| 22 namespace base { |
| 23 class Value; |
| 24 } // namespace base |
| 25 |
| 26 namespace content { |
| 27 class BrowserContext; |
| 28 } // namespace content |
| 29 |
| 30 class Profile; |
20 class PrefRegistrySimple; | 31 class PrefRegistrySimple; |
| 32 class PrefService; |
21 | 33 |
22 // PrefMetricsService is responsible for recording prefs-related UMA stats. | 34 // PrefMetricsService is responsible for recording prefs-related UMA stats. |
23 class PrefMetricsService : public BrowserContextKeyedService { | 35 class PrefMetricsService : public BrowserContextKeyedService { |
24 public: | 36 public: |
25 enum HashedPrefStyle { | |
26 HASHED_PREF_STYLE_NEW, | |
27 HASHED_PREF_STYLE_DEPRECATED, | |
28 }; | |
29 | |
30 explicit PrefMetricsService(Profile* profile); | 37 explicit PrefMetricsService(Profile* profile); |
31 virtual ~PrefMetricsService(); | 38 virtual ~PrefMetricsService(); |
32 | 39 |
33 class Factory : public BrowserContextKeyedServiceFactory { | 40 class Factory : public BrowserContextKeyedServiceFactory { |
34 public: | 41 public: |
35 static Factory* GetInstance(); | 42 static Factory* GetInstance(); |
36 static PrefMetricsService* GetForProfile(Profile* profile); | 43 static PrefMetricsService* GetForProfile(Profile* profile); |
37 private: | 44 private: |
38 friend struct DefaultSingletonTraits<Factory>; | 45 friend struct DefaultSingletonTraits<Factory>; |
39 | 46 |
40 Factory(); | 47 Factory(); |
41 virtual ~Factory(); | 48 virtual ~Factory(); |
42 | 49 |
43 // BrowserContextKeyedServiceFactory implementation | 50 // BrowserContextKeyedServiceFactory implementation |
44 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 51 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
45 content::BrowserContext* profile) const OVERRIDE; | 52 content::BrowserContext* profile) const OVERRIDE; |
46 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | 53 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; |
47 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; | 54 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; |
48 virtual content::BrowserContext* GetBrowserContextToUse( | 55 virtual content::BrowserContext* GetBrowserContextToUse( |
49 content::BrowserContext* context) const OVERRIDE; | 56 content::BrowserContext* context) const OVERRIDE; |
50 }; | 57 }; |
51 | 58 |
52 // Registers preferences in local state. | 59 // Registers preferences in local state. |
53 static void RegisterPrefs(PrefRegistrySimple* registry); | 60 static void RegisterPrefs(PrefRegistrySimple* registry); |
54 | 61 |
55 private: | 62 private: |
56 friend class PrefMetricsServiceTest; | 63 friend class PrefMetricsServiceTest; |
57 | 64 |
58 // Function to log a Value to a histogram | 65 // Function to log a Value to a histogram |
59 typedef base::Callback<void(const std::string&, const Value*)> | 66 typedef base::Callback<void(const std::string&, const base::Value*)> |
60 LogHistogramValueCallback; | 67 LogHistogramValueCallback; |
61 | 68 |
62 // For unit testing only. | 69 // For unit testing only. |
63 PrefMetricsService(Profile* profile, | 70 PrefMetricsService(Profile* profile, PrefService* local_settings); |
64 PrefService* local_settings, | |
65 const std::string& device_id, | |
66 const char** tracked_pref_paths, | |
67 int tracked_pref_path_count); | |
68 | 71 |
69 // Record prefs state on browser context creation. | 72 // Record prefs state on browser context creation. |
70 void RecordLaunchPrefs(); | 73 void RecordLaunchPrefs(); |
71 | 74 |
72 // Register callbacks for synced pref changes. | 75 // Register callbacks for synced pref changes. |
73 void RegisterSyncedPrefObservers(); | 76 void RegisterSyncedPrefObservers(); |
74 | 77 |
75 // Registers a histogram logging callback for a synced pref change. | 78 // Registers a histogram logging callback for a synced pref change. |
76 void AddPrefObserver(const std::string& path, | 79 void AddPrefObserver(const std::string& path, |
77 const std::string& histogram_name_prefix, | 80 const std::string& histogram_name_prefix, |
78 const LogHistogramValueCallback& callback); | 81 const LogHistogramValueCallback& callback); |
79 | 82 |
80 // Generic callback to observe a synced pref change. | 83 // Generic callback to observe a synced pref change. |
81 void OnPrefChanged(const std::string& histogram_name_prefix, | 84 void OnPrefChanged(const std::string& histogram_name_prefix, |
82 const LogHistogramValueCallback& callback, | 85 const LogHistogramValueCallback& callback, |
83 const std::string& path, | 86 const std::string& path, |
84 bool from_sync); | 87 bool from_sync); |
85 | 88 |
86 // Callback for a boolean pref change histogram. | 89 // Callback for a boolean pref change histogram. |
87 void LogBooleanPrefChange(const std::string& histogram_name, | 90 void LogBooleanPrefChange(const std::string& histogram_name, |
88 const Value* value); | 91 const base::Value* value); |
89 | 92 |
90 // Callback for an integer pref change histogram. | 93 // Callback for an integer pref change histogram. |
91 void LogIntegerPrefChange(int boundary_value, | 94 void LogIntegerPrefChange(int boundary_value, |
92 const std::string& histogram_name, | 95 const std::string& histogram_name, |
93 const Value* value); | 96 const base::Value* value); |
94 | |
95 // Callback to receive a unique device_id. | |
96 void GetDeviceIdCallback(const std::string& device_id); | |
97 | |
98 // Checks the tracked preferences against their last known values and reports | |
99 // any discrepancies. This must be called after |device_id| has been set. | |
100 void CheckTrackedPreferences(); | |
101 | |
102 // Updates the hash of the tracked preference in local state. This must be | |
103 // called after |device_id| has been set. | |
104 void UpdateTrackedPreference(const char* path); | |
105 | |
106 // Computes an MD5 hash for the given preference value. |value| can be | |
107 // NULL which will result in the unique hash representing NULL for the pref | |
108 // at |path|. | |
109 std::string GetHashedPrefValue( | |
110 const char* path, | |
111 const base::Value* value, | |
112 HashedPrefStyle desired_style); | |
113 | |
114 void InitializePrefObservers(); | |
115 | 97 |
116 Profile* profile_; | 98 Profile* profile_; |
117 PrefService* prefs_; | 99 PrefService* prefs_; |
118 PrefService* local_state_; | 100 PrefService* local_state_; |
119 std::string profile_name_; | |
120 std::string pref_hash_seed_; | |
121 std::string device_id_; | |
122 const char** tracked_pref_paths_; | |
123 const int tracked_pref_path_count_; | |
124 bool checked_tracked_prefs_; | |
125 | 101 |
126 PrefChangeRegistrar pref_registrar_; | 102 PrefChangeRegistrar pref_registrar_; |
127 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; | 103 scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_; |
128 | 104 |
129 base::WeakPtrFactory<PrefMetricsService> weak_factory_; | 105 base::WeakPtrFactory<PrefMetricsService> weak_factory_; |
130 | 106 |
131 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); | 107 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService); |
132 }; | 108 }; |
133 | 109 |
134 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ | 110 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_ |
OLD | NEW |