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_
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"
20 class DictionaryValue
;
27 class DeviceSettingsService
;
29 // This class manages per-device/global settings.
30 class CrosSettings
: public base::NonThreadSafe
{
32 // Manage singleton instance.
33 static void Initialize();
34 static bool IsInitialized();
35 static void Shutdown();
36 static CrosSettings
* Get();
38 // Checks if the given username is whitelisted and allowed to sign-in to
39 // this device. |wildcard_match| may be NULL. If it's present, it'll be set to
40 // true if the whitelist check was satisfied via a wildcard.
41 static bool IsWhitelisted(const std::string
& username
, bool* wildcard_match
);
43 // Creates a device settings service instance. This is meant for unit tests,
44 // production code uses the singleton returned by Get() above.
45 explicit CrosSettings(DeviceSettingsService
* device_settings_service
);
46 virtual ~CrosSettings();
48 // Helper function to test if the given |path| is a valid cros setting.
49 static bool IsCrosSettings(const std::string
& path
);
51 // Sets |in_value| to given |path| in cros settings.
52 void Set(const std::string
& path
, const base::Value
& in_value
);
54 // Returns setting value for the given |path|.
55 const base::Value
* GetPref(const std::string
& path
) const;
57 // Requests that all providers ensure the values they are serving were read
58 // from a trusted store:
59 // * If all providers are serving trusted values, returns TRUSTED. This
60 // indicates that the cros settings returned by |this| can be trusted during
61 // the current loop cycle.
62 // * If at least one provider ran into a permanent failure while trying to
63 // read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This
64 // indicates that the cros settings will never become trusted.
65 // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least
66 // one provider needs to read values from its trusted store first. The
67 // |callback| will be called back when the read is done.
68 // PrepareTrustedValues() should be called again at that point to determine
69 // whether all providers are serving trusted values now.
70 virtual CrosSettingsProvider::TrustedStatus
PrepareTrustedValues(
71 const base::Closure
& callback
) const;
73 // Convenience forms of Set(). These methods will replace any existing
74 // value at that |path|, even if it has a different type.
75 void SetBoolean(const std::string
& path
, bool in_value
);
76 void SetInteger(const std::string
& path
, int in_value
);
77 void SetDouble(const std::string
& path
, double in_value
);
78 void SetString(const std::string
& path
, const std::string
& in_value
);
80 // Convenience functions for manipulating lists. Note that the following
81 // functions employs a read, modify and write pattern. If underlying settings
82 // provider updates its value asynchronously such as DeviceSettingsProvider,
83 // value cache they read from might not be fresh and multiple calls to those
84 // function would lose data. See http://crbug.com/127215
85 void AppendToList(const std::string
& path
, const base::Value
* value
);
86 void RemoveFromList(const std::string
& path
, const base::Value
* value
);
88 // These are convenience forms of Get(). The value will be retrieved
89 // and the return value will be true if the |path| is valid and the value at
90 // the end of the path can be returned in the form specified.
91 bool GetBoolean(const std::string
& path
, bool* out_value
) const;
92 bool GetInteger(const std::string
& path
, int* out_value
) const;
93 bool GetDouble(const std::string
& path
, double* out_value
) const;
94 bool GetString(const std::string
& path
, std::string
* out_value
) const;
95 bool GetList(const std::string
& path
,
96 const base::ListValue
** out_value
) const;
97 bool GetDictionary(const std::string
& path
,
98 const base::DictionaryValue
** out_value
) const;
100 // Helper function for the whitelist op. Implemented here because we will need
101 // this in a few places. The functions searches for |email| in the pref |path|
102 // It respects whitelists so foo@bar.baz will match *@bar.baz too. If the
103 // match was via a wildcard, |wildcard_match| is set to true.
104 bool FindEmailInList(const std::string
& path
,
105 const std::string
& email
,
106 bool* wildcard_match
) const;
108 // Adding/removing of providers.
109 bool AddSettingsProvider(CrosSettingsProvider
* provider
);
110 bool RemoveSettingsProvider(CrosSettingsProvider
* provider
);
112 // Add an observer Callback for changes for the given |path|.
113 typedef base::CallbackList
<void(void)>::Subscription ObserverSubscription
;
114 scoped_ptr
<ObserverSubscription
> AddSettingsObserver(
115 const std::string
& path
,
116 const base::Closure
& callback
);
118 // Returns the provider that handles settings with the |path| or prefix.
119 CrosSettingsProvider
* GetProvider(const std::string
& path
) const;
122 friend class CrosSettingsTest
;
124 // Fires system setting change callback.
125 void FireObservers(const std::string
& path
);
127 // List of ChromeOS system settings providers.
128 std::vector
<CrosSettingsProvider
*> providers_
;
130 // A map from settings names to a list of observers. Observers get fired in
131 // the order they are added.
132 typedef base::hash_map
<std::string
, base::CallbackList
<void(void)>*>
134 SettingsObserverMap settings_observers_
;
136 DISALLOW_COPY_AND_ASSIGN(CrosSettings
);
139 // Helper class for tests. Initializes the CrosSettings singleton on
140 // construction and tears it down again on destruction.
141 class ScopedTestCrosSettings
{
143 ScopedTestCrosSettings();
144 ~ScopedTestCrosSettings();
147 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings
);
150 } // namespace chromeos
152 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_