Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / cros_settings.h
blob2fa5e5064ef1f484f8818b76a0b14e15e095d3d6
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 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;
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);
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);
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;
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;
92 // Adding/removing of providers.
93 bool AddSettingsProvider(CrosSettingsProvider* provider);
94 bool RemoveSettingsProvider(CrosSettingsProvider* provider);
96 // Add an observer Callback for changes for the given |path|.
97 typedef base::CallbackList<void(void)>::Subscription ObserverSubscription;
98 scoped_ptr<ObserverSubscription> AddSettingsObserver(
99 const std::string& path,
100 const base::Closure& callback);
102 // Returns the provider that handles settings with the |path| or prefix.
103 CrosSettingsProvider* GetProvider(const std::string& path) const;
105 private:
106 friend class CrosSettingsTest;
108 // Fires system setting change callback.
109 void FireObservers(const std::string& path);
111 // List of ChromeOS system settings providers.
112 std::vector<CrosSettingsProvider*> providers_;
114 // A map from settings names to a list of observers. Observers get fired in
115 // the order they are added.
116 typedef base::hash_map<std::string, base::CallbackList<void(void)>*>
117 SettingsObserverMap;
118 SettingsObserverMap settings_observers_;
120 DISALLOW_COPY_AND_ASSIGN(CrosSettings);
123 // Helper class for tests. Initializes the CrosSettings singleton on
124 // construction and tears it down again on destruction.
125 class ScopedTestCrosSettings {
126 public:
127 ScopedTestCrosSettings();
128 ~ScopedTestCrosSettings();
130 private:
131 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
134 } // namespace chromeos
136 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_