Home | History | Annotate | Download | only in prefs
      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.
      4 
      5 #ifndef CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
      6 #define CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     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"
     19 
     20 class PrefRegistrySimple;
     21 
     22 // PrefMetricsService is responsible for recording prefs-related UMA stats.
     23 class PrefMetricsService : public KeyedService {
     24  public:
     25   explicit PrefMetricsService(Profile* profile);
     26   virtual ~PrefMetricsService();
     27 
     28   class Factory : public BrowserContextKeyedServiceFactory {
     29    public:
     30     static Factory* GetInstance();
     31     static PrefMetricsService* GetForProfile(Profile* profile);
     32    private:
     33     friend struct DefaultSingletonTraits<Factory>;
     34 
     35     Factory();
     36     virtual ~Factory();
     37 
     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;
     45   };
     46 
     47  private:
     48   friend class PrefMetricsServiceTest;
     49 
     50   // Function to log a Value to a histogram
     51   typedef base::Callback<void(const std::string&, const base::Value*)>
     52       LogHistogramValueCallback;
     53 
     54   // For unit testing only.
     55   PrefMetricsService(Profile* profile, PrefService* local_settings);
     56 
     57   // Record prefs state on browser context creation.
     58   void RecordLaunchPrefs();
     59 
     60   // Register callbacks for synced pref changes.
     61   void RegisterSyncedPrefObservers();
     62 
     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);
     67 
     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,
     72                      bool from_sync);
     73 
     74   // Callback for a boolean pref change histogram.
     75   void LogBooleanPrefChange(const std::string& histogram_name,
     76                             const base::Value* value);
     77 
     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);
     82 
     83   Profile* profile_;
     84   PrefService* prefs_;
     85   PrefService* local_state_;
     86 
     87   PrefChangeRegistrar pref_registrar_;
     88   scoped_ptr<SyncedPrefChangeRegistrar> synced_pref_change_registrar_;
     89 
     90   base::WeakPtrFactory<PrefMetricsService> weak_factory_;
     91 
     92   DISALLOW_COPY_AND_ASSIGN(PrefMetricsService);
     93 };
     94 
     95 #endif  // CHROME_BROWSER_PREFS_PREF_METRICS_SERVICE_H_
     96