[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 1 | // Copyright (c) 2010 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_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" |
| 13 | #include "base/non_thread_safe.h" |
| 14 | #include "base/observer_list.h" |
| 15 | #include "base/singleton.h" |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/cros_settings_names.h" |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 17 | #include "chrome/common/notification_observer.h" |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 18 | |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 19 | class Value; |
| 20 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 21 | namespace chromeos { |
| 22 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 23 | class CrosSettingsProvider; |
| 24 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 25 | // A class manages per-device/global settings. |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 26 | class CrosSettings : public NonThreadSafe { |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 27 | public: |
| 28 | // Class factory. |
| 29 | static CrosSettings* Get(); |
| 30 | |
| 31 | // Helper function to test if given path is a value cros settings name. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 32 | static bool IsCrosSettings(const std::string& path); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 33 | |
| 34 | // Sets |in_value| to given |path| in cros settings. |
| 35 | // Note that this takes ownership of |in_value|. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 36 | void Set(const std::string& path, Value* in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 37 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 38 | // Fires system setting change notification. |
| 39 | void FireObservers(const char* path); |
| 40 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 41 | // Gets settings value of given |path| to |out_value|. |
| 42 | // Note that |out_value| is still owned by this class. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 43 | bool Get(const std::string& path, Value** out_value) const; |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 44 | |
| 45 | // Convenience forms of Set(). These methods will replace any existing |
| 46 | // value at that path, even if it has a different type. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 47 | void SetBoolean(const std::string& path, bool in_value); |
| 48 | void SetInteger(const std::string& path, int in_value); |
| 49 | void SetReal(const std::string& path, double in_value); |
| 50 | void SetString(const std::string& path, const std::string& in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 51 | |
| 52 | // These are convenience forms of Get(). The value will be retrieved |
| 53 | // and the return value will be true if the path is valid and the value at |
| 54 | // the end of the path can be returned in the form specified. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 55 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 56 | bool GetInteger(const std::string& path, int* out_value) const; |
| 57 | bool GetReal(const std::string& path, double* out_value) const; |
| 58 | bool GetString(const std::string& path, std::string* out_value) const; |
[email protected] | 46ebf064 | 2010-07-24 02:47:40 | [diff] [blame] | 59 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 60 | // adding/removing of providers |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 61 | bool AddSettingsProvider(CrosSettingsProvider* provider); |
| 62 | bool RemoveSettingsProvider(CrosSettingsProvider* provider); |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 63 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 64 | // If the pref at the given path changes, we call the observer's Observe |
| 65 | // method with NOTIFY_PREF_CHANGED. |
| 66 | void AddSettingsObserver(const char* path, NotificationObserver* obs); |
| 67 | void RemoveSettingsObserver(const char* path, NotificationObserver* obs); |
| 68 | |
| 69 | private: |
| 70 | // List of ChromeOS system settings providers. |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 71 | std::vector<CrosSettingsProvider*> providers_; |
| 72 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 73 | // A map from settings names to a list of observers. Observers get fired in |
| 74 | // the order they are added. |
| 75 | typedef ObserverList<NotificationObserver> NotificationObserverList; |
| 76 | typedef base::hash_map<std::string, NotificationObserverList*> |
| 77 | SettingsObserverMap; |
| 78 | SettingsObserverMap settings_observers_; |
| 79 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 80 | CrosSettings(); |
| 81 | ~CrosSettings(); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 82 | CrosSettingsProvider* GetProvider(const std::string& path) const; |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 83 | friend struct DefaultSingletonTraits<CrosSettings>; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame^] | 84 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 85 | DISALLOW_COPY_AND_ASSIGN(CrosSettings); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace chromeos |
| 89 | |
| 90 | #endif // CHROME_BROWSER_CHROMEOS_CROS_SETTINGS_H_ |