[email protected] | 2b2820a | 2011-03-07 22:28:40 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 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_CROS_SETTINGS_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_CROS_SETTINGS_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 8 | |
| 9 | #include <string> |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 11 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 12 | #include "base/hash_tables.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 14 | #include "base/observer_list.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 15 | #include "base/threading/non_thread_safe.h" |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/cros_settings_names.h" |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame^] | 17 | #include "chrome/browser/chromeos/cros_settings_provider.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 19 | |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 20 | namespace base { |
| 21 | template <typename T> struct DefaultLazyInstanceTraits; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame^] | 22 | class ListValue; |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 23 | class Value; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 24 | } |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 25 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 26 | namespace chromeos { |
| 27 | |
| 28 | // A class manages per-device/global settings. |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 29 | class CrosSettings : public base::NonThreadSafe { |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 30 | public: |
| 31 | // Class factory. |
| 32 | static CrosSettings* Get(); |
| 33 | |
| 34 | // Helper function to test if given path is a value cros settings name. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 35 | static bool IsCrosSettings(const std::string& path); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 36 | |
| 37 | // Sets |in_value| to given |path| in cros settings. |
| 38 | // Note that this takes ownership of |in_value|. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 39 | void Set(const std::string& path, base::Value* in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 40 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 41 | // Fires system setting change notification. |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame^] | 42 | // TODO(pastarmovj): Consider to remove this function from the public |
| 43 | // interface. |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 44 | void FireObservers(const char* path); |
| 45 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 46 | // Gets settings value of given |path| to |out_value|. |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame^] | 47 | const base::Value* GetPref(const std::string& path) const; |
| 48 | |
| 49 | // Starts a fetch from the trusted store for the value of |path| if not loaded |
| 50 | // yet. It will call the |callback| function upon completion if a new fetch |
| 51 | // was needed in which case the return value is false. Else it will return |
| 52 | // true and won't call the |callback|. |
| 53 | bool GetTrusted(const std::string& path, |
| 54 | const base::Closure& callback) const; |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 55 | |
| 56 | // Convenience forms of Set(). These methods will replace any existing |
| 57 | // value at that path, even if it has a different type. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 58 | void SetBoolean(const std::string& path, bool in_value); |
| 59 | void SetInteger(const std::string& path, int in_value); |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 60 | void SetDouble(const std::string& path, double in_value); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 61 | void SetString(const std::string& path, const std::string& in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 62 | |
| 63 | // These are convenience forms of Get(). The value will be retrieved |
| 64 | // and the return value will be true if the path is valid and the value at |
| 65 | // the end of the path can be returned in the form specified. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 66 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 67 | bool GetInteger(const std::string& path, int* out_value) const; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 68 | bool GetDouble(const std::string& path, double* out_value) const; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 69 | bool GetString(const std::string& path, std::string* out_value) const; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame^] | 70 | bool GetList(const std::string& path, |
| 71 | const base::ListValue** out_value) const; |
[email protected] | 46ebf064 | 2010-07-24 02:47:40 | [diff] [blame] | 72 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 73 | // adding/removing of providers |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 74 | bool AddSettingsProvider(CrosSettingsProvider* provider); |
| 75 | bool RemoveSettingsProvider(CrosSettingsProvider* provider); |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 76 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 77 | // If the pref at the given path changes, we call the observer's Observe |
[email protected] | 10abd19 | 2010-09-30 02:03:49 | [diff] [blame] | 78 | // method with PREF_CHANGED. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 79 | void AddSettingsObserver(const char* path, |
| 80 | content::NotificationObserver* obs); |
| 81 | void RemoveSettingsObserver(const char* path, |
| 82 | content::NotificationObserver* obs); |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 83 | |
[email protected] | c0a2128d9 | 2011-07-20 15:04:46 | [diff] [blame] | 84 | // Returns the provider that handles settings with the path or prefix. |
| 85 | CrosSettingsProvider* GetProvider(const std::string& path) const; |
| 86 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 87 | private: |
| 88 | // List of ChromeOS system settings providers. |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 89 | std::vector<CrosSettingsProvider*> providers_; |
| 90 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 91 | // A map from settings names to a list of observers. Observers get fired in |
| 92 | // the order they are added. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 93 | typedef ObserverList<content::NotificationObserver> NotificationObserverList; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 94 | typedef base::hash_map<std::string, NotificationObserverList*> |
| 95 | SettingsObserverMap; |
| 96 | SettingsObserverMap settings_observers_; |
| 97 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 98 | CrosSettings(); |
| 99 | ~CrosSettings(); |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 100 | friend struct base::DefaultLazyInstanceTraits<CrosSettings>; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 101 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(CrosSettings); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace chromeos |
| 106 | |
| 107 | #endif // CHROME_BROWSER_CHROMEOS_CROS_SETTINGS_H_ |