[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / cros_settings.h
blobf7becbe43a0eb92f6347e64dab3859616e212c08
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
8 #include <string>
9 #include <vector>
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"
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
22 class Value;
25 namespace chromeos {
27 class DeviceSettingsService;
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();
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();
43 // Helper function to test if the given |path| is a valid cros setting.
44 static bool IsCrosSettings(const std::string& path);
46 // Sets |in_value| to given |path| in cros settings.
47 void Set(const std::string& path, const base::Value& in_value);
49 // Returns setting value for the given |path|.
50 const base::Value* GetPref(const std::string& path) const;
52 // Requests that all providers ensure the values they are serving were read
53 // from a trusted store:
54 // * If all providers are serving trusted values, returns TRUSTED. This
55 // indicates that the cros settings returned by |this| can be trusted during
56 // the current loop cycle.
57 // * If at least one provider ran into a permanent failure while trying to
58 // read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This
59 // indicates that the cros settings will never become trusted.
60 // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least
61 // one provider needs to read values from its trusted store first. The
62 // |callback| will be called back when the read is done.
63 // PrepareTrustedValues() should be called again at that point to determine
64 // whether all providers are serving trusted values now.
65 virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
66 const base::Closure& callback) const;
68 // Convenience forms of Set(). These methods will replace any existing
69 // value at that |path|, even if it has a different type.
70 void SetBoolean(const std::string& path, bool in_value);
71 void SetInteger(const std::string& path, int in_value);
72 void SetDouble(const std::string& path, double in_value);
73 void SetString(const std::string& path, const std::string& in_value);
75 // Convenience functions for manipulating lists. Note that the following
76 // functions employs a read, modify and write pattern. If underlying settings
77 // provider updates its value asynchronously such as DeviceSettingsProvider,
78 // value cache they read from might not be fresh and multiple calls to those
79 // function would lose data. See http://crbug.com/127215
80 void AppendToList(const std::string& path, const base::Value* value);
81 void RemoveFromList(const std::string& path, const base::Value* value);
83 // These are convenience forms of Get(). The value will be retrieved
84 // and the return value will be true if the |path| is valid and the value at
85 // the end of the path can be returned in the form specified.
86 bool GetBoolean(const std::string& path, bool* out_value) const;
87 bool GetInteger(const std::string& path, int* out_value) const;
88 bool GetDouble(const std::string& path, double* out_value) const;
89 bool GetString(const std::string& path, std::string* out_value) const;
90 bool GetList(const std::string& path,
91 const base::ListValue** out_value) const;
92 bool GetDictionary(const std::string& path,
93 const base::DictionaryValue** out_value) const;
95 // Helper function for the whitelist op. Implemented here because we will need
96 // this in a few places. The functions searches for |email| in the pref |path|
97 // It respects whitelists so foo@bar.baz will match *@bar.baz too. If the
98 // match was via a wildcard, |wildcard_match| is set to true.
99 bool FindEmailInList(const std::string& path,
100 const std::string& email,
101 bool* wildcard_match) const;
103 // Adding/removing of providers.
104 bool AddSettingsProvider(CrosSettingsProvider* provider);
105 bool RemoveSettingsProvider(CrosSettingsProvider* provider);
107 // Add an observer Callback for changes for the given |path|.
108 typedef base::CallbackList<void(void)>::Subscription ObserverSubscription;
109 scoped_ptr<ObserverSubscription> AddSettingsObserver(
110 const std::string& path,
111 const base::Closure& callback);
113 // Returns the provider that handles settings with the |path| or prefix.
114 CrosSettingsProvider* GetProvider(const std::string& path) const;
116 private:
117 friend class CrosSettingsTest;
119 // Fires system setting change callback.
120 void FireObservers(const std::string& path);
122 // List of ChromeOS system settings providers.
123 std::vector<CrosSettingsProvider*> providers_;
125 // A map from settings names to a list of observers. Observers get fired in
126 // the order they are added.
127 typedef base::hash_map<std::string, base::CallbackList<void(void)>*>
128 SettingsObserverMap;
129 SettingsObserverMap settings_observers_;
131 DISALLOW_COPY_AND_ASSIGN(CrosSettings);
134 // Helper class for tests. Initializes the CrosSettings singleton on
135 // construction and tears it down again on destruction.
136 class ScopedTestCrosSettings {
137 public:
138 ScopedTestCrosSettings();
139 ~ScopedTestCrosSettings();
141 private:
142 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
145 } // namespace chromeos
147 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_