cros_settings.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/containers/hash_tables.h" 13#include "base/observer_list.h" 14#include "base/threading/non_thread_safe.h" 15#include "chrome/browser/chromeos/settings/cros_settings_names.h" 16#include "chrome/browser/chromeos/settings/cros_settings_provider.h" 17#include "content/public/browser/notification_observer.h" 18 19namespace base { 20class DictionaryValue; 21class ListValue; 22class Value; 23} 24 25namespace chromeos { 26 27class DeviceSettingsService; 28 29// This class manages per-device/global settings. 30class 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@bar.baz will match *@bar.baz too. 90 bool FindEmailInList(const std::string& path, const std::string& email) const; 91 92 // Adding/removing of providers. 93 bool AddSettingsProvider(CrosSettingsProvider* provider); 94 bool RemoveSettingsProvider(CrosSettingsProvider* provider); 95 96 // If the pref at the given |path| changes, we call the observer's Observe 97 // method with NOTIFICATION_SYSTEM_SETTING_CHANGED. 98 void AddSettingsObserver(const char* path, 99 content::NotificationObserver* obs); 100 void RemoveSettingsObserver(const char* path, 101 content::NotificationObserver* obs); 102 103 // Returns the provider that handles settings with the |path| or prefix. 104 CrosSettingsProvider* GetProvider(const std::string& path) const; 105 106 private: 107 friend class CrosSettingsTest; 108 109 // Fires system setting change notification. 110 void FireObservers(const std::string& path); 111 112 // List of ChromeOS system settings providers. 113 std::vector<CrosSettingsProvider*> providers_; 114 115 // A map from settings names to a list of observers. Observers get fired in 116 // the order they are added. 117 typedef ObserverList<content::NotificationObserver, true> 118 NotificationObserverList; 119 typedef base::hash_map<std::string, NotificationObserverList*> 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. 128class 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