Home | History | Annotate | Download | only in settings
      1 // Copyright (c) 2012 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_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
      6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback_forward.h"
     12 #include "base/callback_list.h"
     13 #include "base/containers/hash_tables.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/threading/non_thread_safe.h"
     16 #include "chromeos/settings/cros_settings_names.h"
     17 #include "chromeos/settings/cros_settings_provider.h"
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 class ListValue;
     22 class Value;
     23 }
     24 
     25 namespace chromeos {
     26 
     27 class DeviceSettingsService;
     28 
     29 // This class manages per-device/global settings.
     30 class CrosSettings : public base::NonThreadSafe {
     31  public:
     32   // Manage singleton instance.
     33   static void Initialize();
     34   static bool IsInitialized();
     35   static void Shutdown();
     36   static CrosSettings* Get();
     37 
     38   // Creates a device settings service instance. This is meant for unit tests,
     39   // production code uses the singleton returned by Get() above.
     40   explicit CrosSettings(DeviceSettingsService* device_settings_service);
     41   virtual ~CrosSettings();
     42 
     43   // Helper function to test if the given |path| is a valid cros setting.
     44   static bool IsCrosSettings(const std::string& path);
     45 
     46   // Sets |in_value| to given |path| in cros settings.
     47   void Set(const std::string& path, const base::Value& in_value);
     48 
     49   // Returns setting value for the given |path|.
     50   const base::Value* GetPref(const std::string& path) const;
     51 
     52   // Requests all providers to fetch their values from a trusted store, if they
     53   // haven't done so yet. Returns true if the cros settings returned by |this|
     54   // are trusted during the current loop cycle; otherwise returns false, and
     55   // |callback| will be invoked later when trusted values become available.
     56   // PrepareTrustedValues() should be tried again in that case.
     57   virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
     58       const base::Closure& callback) const;
     59 
     60   // Convenience forms of Set().  These methods will replace any existing
     61   // value at that |path|, even if it has a different type.
     62   void SetBoolean(const std::string& path, bool in_value);
     63   void SetInteger(const std::string& path, int in_value);
     64   void SetDouble(const std::string& path, double in_value);
     65   void SetString(const std::string& path, const std::string& in_value);
     66 
     67   // Convenience functions for manipulating lists. Note that the following
     68   // functions employs a read, modify and write pattern. If underlying settings
     69   // provider updates its value asynchronously such as DeviceSettingsProvider,
     70   // value cache they read from might not be fresh and multiple calls to those
     71   // function would lose data. See http://crbug.com/127215
     72   void AppendToList(const std::string& path, const base::Value* value);
     73   void RemoveFromList(const std::string& path, const base::Value* value);
     74 
     75   // These are convenience forms of Get().  The value will be retrieved
     76   // and the return value will be true if the |path| is valid and the value at
     77   // the end of the path can be returned in the form specified.
     78   bool GetBoolean(const std::string& path, bool* out_value) const;
     79   bool GetInteger(const std::string& path, int* out_value) const;
     80   bool GetDouble(const std::string& path, double* out_value) const;
     81   bool GetString(const std::string& path, std::string* out_value) const;
     82   bool GetList(const std::string& path,
     83                const base::ListValue** out_value) const;
     84   bool GetDictionary(const std::string& path,
     85                      const base::DictionaryValue** out_value) const;
     86 
     87   // Helper function for the whitelist op. Implemented here because we will need
     88   // this in a few places. The functions searches for |email| in the pref |path|
     89   // It respects whitelists so foo (at) bar.baz will match *@bar.baz too. If the
     90   // match was via a wildcard, |wildcard_match| is set to true.
     91   bool FindEmailInList(const std::string& path,
     92                        const std::string& email,
     93                        bool* wildcard_match) const;
     94 
     95   // Adding/removing of providers.
     96   bool AddSettingsProvider(CrosSettingsProvider* provider);
     97   bool RemoveSettingsProvider(CrosSettingsProvider* provider);
     98 
     99   // Add an observer Callback for changes for the given |path|.
    100   typedef base::CallbackList<void(void)>::Subscription ObserverSubscription;
    101   scoped_ptr<ObserverSubscription> AddSettingsObserver(
    102       const std::string& path,
    103       const base::Closure& callback);
    104 
    105   // Returns the provider that handles settings with the |path| or prefix.
    106   CrosSettingsProvider* GetProvider(const std::string& path) const;
    107 
    108  private:
    109   friend class CrosSettingsTest;
    110 
    111   // Fires system setting change callback.
    112   void FireObservers(const std::string& path);
    113 
    114   // List of ChromeOS system settings providers.
    115   std::vector<CrosSettingsProvider*> providers_;
    116 
    117   // A map from settings names to a list of observers. Observers get fired in
    118   // the order they are added.
    119   typedef base::hash_map<std::string, base::CallbackList<void(void)>*>
    120       SettingsObserverMap;
    121   SettingsObserverMap settings_observers_;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(CrosSettings);
    124 };
    125 
    126 // Helper class for tests. Initializes the CrosSettings singleton on
    127 // construction and tears it down again on destruction.
    128 class ScopedTestCrosSettings {
    129  public:
    130   ScopedTestCrosSettings();
    131   ~ScopedTestCrosSettings();
    132 
    133  private:
    134   DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
    135 };
    136 
    137 }  // namespace chromeos
    138 
    139 #endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
    140