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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/singleton.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "chrome/browser/prefs/synced_pref_change_registrar.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
18 #include "components/keyed_service/core/keyed_service.h"
20 class PrefRegistrySimple
;
22 // PrefMetricsService is responsible for recording prefs-related UMA stats.
23 class PrefMetricsService
: public KeyedService
{
25 explicit PrefMetricsService(Profile
* profile
);
26 virtual ~PrefMetricsService();
28 class Factory
: public BrowserContextKeyedServiceFactory
{
30 static Factory
* GetInstance();
31 static PrefMetricsService
* GetForProfile(Profile
* profile
);
33 friend struct DefaultSingletonTraits
<Factory
>;
38 // BrowserContextKeyedServiceFactory implementation
39 virtual KeyedService
* BuildServiceInstanceFor(
40 content::BrowserContext
* profile
) const OVERRIDE
;
41 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE
;
42 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE
;
43 virtual content::BrowserContext
* GetBrowserContextToUse(
44 content::BrowserContext
* context
) const OVERRIDE
;
48 friend class PrefMetricsServiceTest
;
50 // Function to log a Value to a histogram
51 typedef base::Callback
<void(const std::string
&, const base::Value
*)>
52 LogHistogramValueCallback
;
54 // For unit testing only.
55 PrefMetricsService(Profile
* profile
, PrefService
* local_settings
);
57 // Record prefs state on browser context creation.
58 void RecordLaunchPrefs();
60 // Register callbacks for synced pref changes.
61 void RegisterSyncedPrefObservers();
63 // Registers a histogram logging callback for a synced pref change.
64 void AddPrefObserver(const std::string
& path
,
65 const std::string
& histogram_name_prefix
,
66 const LogHistogramValueCallback
& callback
);
68 // Generic callback to observe a synced pref change.
69 void OnPrefChanged(const std::string
& histogram_name_prefix
,
70 const LogHistogramValueCallback
& callback
,
71 const std::string
& path
,
74 // Callback for a boolean pref change histogram.
75 void LogBooleanPrefChange(const std::string
& histogram_name
,
76 const base::Value
* value
);
78 // Callback for an integer pref change histogram.
79 void LogIntegerPrefChange(int boundary_value
,
80 const std::string
& histogram_name
,
81 const base::Value
* value
);
85 PrefService
* local_state_
;
87 PrefChangeRegistrar pref_registrar_
;
88 scoped_ptr
<SyncedPrefChangeRegistrar
> synced_pref_change_registrar_
;
90 base::WeakPtrFactory
<PrefMetricsService
> weak_factory_
;
92 DISALLOW_COPY_AND_ASSIGN(PrefMetricsService
);
95 #endif // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_