blob: a7b9cccdb751fbdf1737d436e5f0ace5d9ceaf28 [file] [log] [blame]
[email protected]a79f6dc2012-02-23 17:03:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]73bd67b2010-07-16 16:00:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]edaf5962012-08-01 11:31:315#ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
6#define CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
[email protected]73bd67b2010-07-16 16:00:097
brettw84cff3f2017-06-29 18:26:508#include <map>
dcheng24002d02016-04-08 02:42:409#include <memory>
[email protected]73bd67b2010-07-16 16:00:0910#include <string>
[email protected]93a49ae2010-08-03 23:50:4711#include <vector>
[email protected]73bd67b2010-07-16 16:00:0912
[email protected]a79f6dc2012-02-23 17:03:4813#include "base/callback_forward.h"
[email protected]2a7cac02013-09-26 19:20:1814#include "base/callback_list.h"
avi8a07d53892015-12-24 22:13:5315#include "base/macros.h"
gab9e2fda8f2017-05-30 08:18:1116#include "base/sequence_checker.h"
[email protected]833a6bf22013-10-10 21:59:2617#include "chromeos/settings/cros_settings_names.h"
18#include "chromeos/settings/cros_settings_provider.h"
[email protected]73bd67b2010-07-16 16:00:0919
[email protected]dc8caba2010-12-13 16:52:3520namespace base {
[email protected]ced1dd22013-05-02 11:08:4421class DictionaryValue;
[email protected]45d32232011-11-22 11:42:3122class ListValue;
[email protected]c02c853d72010-08-07 06:23:2423class Value;
[email protected]f3a1c642011-07-12 19:15:0324}
[email protected]c02c853d72010-08-07 06:23:2425
[email protected]73bd67b2010-07-16 16:00:0926namespace chromeos {
27
[email protected]08450dd2013-05-22 15:37:2628class DeviceSettingsService;
29
[email protected]a79f6dc2012-02-23 17:03:4830// This class manages per-device/global settings.
gab9e2fda8f2017-05-30 08:18:1131class CrosSettings {
[email protected]73bd67b2010-07-16 16:00:0932 public:
[email protected]7fe93102013-04-17 19:24:2333 // Manage singleton instance.
34 static void Initialize();
35 static bool IsInitialized();
36 static void Shutdown();
[email protected]73bd67b2010-07-16 16:00:0937 static CrosSettings* Get();
38
nkostylev393309f2015-02-05 13:57:3939 // Checks if the given username is whitelisted and allowed to sign-in to
40 // this device. |wildcard_match| may be NULL. If it's present, it'll be set to
41 // true if the whitelist check was satisfied via a wildcard.
Sergey Poromov27fa92532018-01-15 12:43:0442 bool IsUserWhitelisted(const std::string& username,
43 bool* wildcard_match) const;
nkostylev393309f2015-02-05 13:57:3944
[email protected]08450dd2013-05-22 15:37:2645 // Creates a device settings service instance. This is meant for unit tests,
46 // production code uses the singleton returned by Get() above.
47 explicit CrosSettings(DeviceSettingsService* device_settings_service);
48 virtual ~CrosSettings();
49
[email protected]a79f6dc2012-02-23 17:03:4850 // Helper function to test if the given |path| is a valid cros setting.
[email protected]57ecc4b2010-08-11 03:02:5151 static bool IsCrosSettings(const std::string& path);
[email protected]73bd67b2010-07-16 16:00:0952
53 // Sets |in_value| to given |path| in cros settings.
[email protected]6c2b8102011-11-22 13:10:3654 void Set(const std::string& path, const base::Value& in_value);
[email protected]73bd67b2010-07-16 16:00:0955
[email protected]a79f6dc2012-02-23 17:03:4856 // Returns setting value for the given |path|.
[email protected]45d32232011-11-22 11:42:3157 const base::Value* GetPref(const std::string& path) const;
58
bartfab6de794c2014-10-30 15:23:3759 // Requests that all providers ensure the values they are serving were read
60 // from a trusted store:
61 // * If all providers are serving trusted values, returns TRUSTED. This
62 // indicates that the cros settings returned by |this| can be trusted during
63 // the current loop cycle.
64 // * If at least one provider ran into a permanent failure while trying to
65 // read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This
66 // indicates that the cros settings will never become trusted.
67 // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least
68 // one provider needs to read values from its trusted store first. The
69 // |callback| will be called back when the read is done.
70 // PrepareTrustedValues() should be called again at that point to determine
71 // whether all providers are serving trusted values now.
[email protected]ac4ea5f62012-05-25 14:51:4072 virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
73 const base::Closure& callback) const;
[email protected]73bd67b2010-07-16 16:00:0974
75 // Convenience forms of Set(). These methods will replace any existing
[email protected]a79f6dc2012-02-23 17:03:4876 // value at that |path|, even if it has a different type.
[email protected]57ecc4b2010-08-11 03:02:5177 void SetBoolean(const std::string& path, bool in_value);
78 void SetInteger(const std::string& path, int in_value);
[email protected]fb534c92011-02-01 01:02:0779 void SetDouble(const std::string& path, double in_value);
[email protected]57ecc4b2010-08-11 03:02:5180 void SetString(const std::string& path, const std::string& in_value);
[email protected]73bd67b2010-07-16 16:00:0981
[email protected]8f2df86a2012-05-11 00:45:2382 // Convenience functions for manipulating lists. Note that the following
83 // functions employs a read, modify and write pattern. If underlying settings
84 // provider updates its value asynchronously such as DeviceSettingsProvider,
85 // value cache they read from might not be fresh and multiple calls to those
86 // function would lose data. See http://crbug.com/127215
[email protected]6c2b8102011-11-22 13:10:3687 void AppendToList(const std::string& path, const base::Value* value);
88 void RemoveFromList(const std::string& path, const base::Value* value);
89
[email protected]73bd67b2010-07-16 16:00:0990 // These are convenience forms of Get(). The value will be retrieved
[email protected]a79f6dc2012-02-23 17:03:4891 // and the return value will be true if the |path| is valid and the value at
[email protected]73bd67b2010-07-16 16:00:0992 // the end of the path can be returned in the form specified.
[email protected]57ecc4b2010-08-11 03:02:5193 bool GetBoolean(const std::string& path, bool* out_value) const;
94 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0795 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]57ecc4b2010-08-11 03:02:5196 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]45d32232011-11-22 11:42:3197 bool GetList(const std::string& path,
98 const base::ListValue** out_value) const;
[email protected]ced1dd22013-05-02 11:08:4499 bool GetDictionary(const std::string& path,
100 const base::DictionaryValue** out_value) const;
[email protected]46ebf0642010-07-24 02:47:40101
[email protected]6c2b8102011-11-22 13:10:36102 // Helper function for the whitelist op. Implemented here because we will need
103 // this in a few places. The functions searches for |email| in the pref |path|
[email protected]750b08f2014-01-30 04:12:45104 // It respects whitelists so [email protected] will match *@bar.baz too. If the
105 // match was via a wildcard, |wildcard_match| is set to true.
106 bool FindEmailInList(const std::string& path,
107 const std::string& email,
108 bool* wildcard_match) const;
[email protected]6c2b8102011-11-22 13:10:36109
Sergey Poromov27fa92532018-01-15 12:43:04110 // Same as above, but receives already populated user list.
111 static bool FindEmailInList(const base::ListValue* list,
112 const std::string& email,
113 bool* wildcard_match);
114
[email protected]a79f6dc2012-02-23 17:03:48115 // Adding/removing of providers.
avi7438ba72016-10-19 17:33:22116 bool AddSettingsProvider(std::unique_ptr<CrosSettingsProvider> provider);
117 std::unique_ptr<CrosSettingsProvider> RemoveSettingsProvider(
118 CrosSettingsProvider* provider);
[email protected]93a49ae2010-08-03 23:50:47119
[email protected]32ed8542013-09-19 23:27:32120 // Add an observer Callback for changes for the given |path|.
stevenjb43c11caa2017-04-05 16:34:47121 using ObserverSubscription = base::CallbackList<void(void)>::Subscription;
dcheng24002d02016-04-08 02:42:40122 std::unique_ptr<ObserverSubscription> AddSettingsObserver(
[email protected]32ed8542013-09-19 23:27:32123 const std::string& path,
stevenjb43c11caa2017-04-05 16:34:47124 const base::Closure& callback) WARN_UNUSED_RESULT;
[email protected]7096f562010-08-16 21:59:04125
[email protected]a79f6dc2012-02-23 17:03:48126 // Returns the provider that handles settings with the |path| or prefix.
[email protected]c0a2128d92011-07-20 15:04:46127 CrosSettingsProvider* GetProvider(const std::string& path) const;
128
[email protected]7096f562010-08-16 21:59:04129 private:
[email protected]0c4d97c2012-09-01 20:47:20130 friend class CrosSettingsTest;
131
[email protected]32ed8542013-09-19 23:27:32132 // Fires system setting change callback.
[email protected]0c4d97c2012-09-01 20:47:20133 void FireObservers(const std::string& path);
[email protected]279db0c2011-12-13 15:26:12134
[email protected]7096f562010-08-16 21:59:04135 // List of ChromeOS system settings providers.
avi7438ba72016-10-19 17:33:22136 std::vector<std::unique_ptr<CrosSettingsProvider>> providers_;
[email protected]93a49ae2010-08-03 23:50:47137
[email protected]7096f562010-08-16 21:59:04138 // A map from settings names to a list of observers. Observers get fired in
139 // the order they are added.
brettw84cff3f2017-06-29 18:26:50140 std::map<std::string, std::unique_ptr<base::CallbackList<void(void)>>>
avi7438ba72016-10-19 17:33:22141 settings_observers_;
[email protected]7096f562010-08-16 21:59:04142
gab9e2fda8f2017-05-30 08:18:11143 SEQUENCE_CHECKER(sequence_checker_);
144
[email protected]93a49ae2010-08-03 23:50:47145 DISALLOW_COPY_AND_ASSIGN(CrosSettings);
[email protected]73bd67b2010-07-16 16:00:09146};
147
[email protected]e4854dc2013-04-24 00:11:51148// Helper class for tests. Initializes the CrosSettings singleton on
149// construction and tears it down again on destruction.
[email protected]7fe93102013-04-17 19:24:23150class ScopedTestCrosSettings {
151 public:
152 ScopedTestCrosSettings();
153 ~ScopedTestCrosSettings();
154
155 private:
[email protected]7fe93102013-04-17 19:24:23156 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
157};
158
[email protected]73bd67b2010-07-16 16:00:09159} // namespace chromeos
160
[email protected]edaf5962012-08-01 11:31:31161#endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_