[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | edaf596 | 2012-08-01 11:31:31 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_ |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 7 | |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 8 | #include <map> |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 10 | #include <string> |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 11 | #include <vector> |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 12 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 13 | #include "base/callback_forward.h" |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 14 | #include "base/callback_list.h" |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 15 | #include "base/macros.h" |
gab | 9e2fda8f | 2017-05-30 08:18:11 | [diff] [blame] | 16 | #include "base/sequence_checker.h" |
[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 17 | #include "chromeos/settings/cros_settings_names.h" |
| 18 | #include "chromeos/settings/cros_settings_provider.h" |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 19 | |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 20 | namespace base { |
[email protected] | ced1dd2 | 2013-05-02 11:08:44 | [diff] [blame] | 21 | class DictionaryValue; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 22 | class ListValue; |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 23 | class Value; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 24 | } |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 25 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 26 | namespace chromeos { |
| 27 | |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 28 | class DeviceSettingsService; |
| 29 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 30 | // This class manages per-device/global settings. |
gab | 9e2fda8f | 2017-05-30 08:18:11 | [diff] [blame] | 31 | class CrosSettings { |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 32 | public: |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 33 | // Manage singleton instance. |
| 34 | static void Initialize(); |
| 35 | static bool IsInitialized(); |
| 36 | static void Shutdown(); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 37 | static CrosSettings* Get(); |
| 38 | |
nkostylev | 393309f | 2015-02-05 13:57:39 | [diff] [blame] | 39 | // 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 Poromov | 27fa9253 | 2018-01-15 12:43:04 | [diff] [blame^] | 42 | bool IsUserWhitelisted(const std::string& username, |
| 43 | bool* wildcard_match) const; |
nkostylev | 393309f | 2015-02-05 13:57:39 | [diff] [blame] | 44 | |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 45 | // 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] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 50 | // Helper function to test if the given |path| is a valid cros setting. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 51 | static bool IsCrosSettings(const std::string& path); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 52 | |
| 53 | // Sets |in_value| to given |path| in cros settings. |
[email protected] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 54 | void Set(const std::string& path, const base::Value& in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 55 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 56 | // Returns setting value for the given |path|. |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 57 | const base::Value* GetPref(const std::string& path) const; |
| 58 | |
bartfab | 6de794c | 2014-10-30 15:23:37 | [diff] [blame] | 59 | // 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] | ac4ea5f6 | 2012-05-25 14:51:40 | [diff] [blame] | 72 | virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues( |
| 73 | const base::Closure& callback) const; |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 74 | |
| 75 | // Convenience forms of Set(). These methods will replace any existing |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 76 | // value at that |path|, even if it has a different type. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 77 | void SetBoolean(const std::string& path, bool in_value); |
| 78 | void SetInteger(const std::string& path, int in_value); |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 79 | void SetDouble(const std::string& path, double in_value); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 80 | void SetString(const std::string& path, const std::string& in_value); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 81 | |
[email protected] | 8f2df86a | 2012-05-11 00:45:23 | [diff] [blame] | 82 | // 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] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 87 | void AppendToList(const std::string& path, const base::Value* value); |
| 88 | void RemoveFromList(const std::string& path, const base::Value* value); |
| 89 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 90 | // These are convenience forms of Get(). The value will be retrieved |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 91 | // and the return value will be true if the |path| is valid and the value at |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 92 | // the end of the path can be returned in the form specified. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 93 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 94 | bool GetInteger(const std::string& path, int* out_value) const; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 95 | bool GetDouble(const std::string& path, double* out_value) const; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 96 | bool GetString(const std::string& path, std::string* out_value) const; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 97 | bool GetList(const std::string& path, |
| 98 | const base::ListValue** out_value) const; |
[email protected] | ced1dd2 | 2013-05-02 11:08:44 | [diff] [blame] | 99 | bool GetDictionary(const std::string& path, |
| 100 | const base::DictionaryValue** out_value) const; |
[email protected] | 46ebf064 | 2010-07-24 02:47:40 | [diff] [blame] | 101 | |
[email protected] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 102 | // 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] | 750b08f | 2014-01-30 04:12:45 | [diff] [blame] | 104 | // 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] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 109 | |
Sergey Poromov | 27fa9253 | 2018-01-15 12:43:04 | [diff] [blame^] | 110 | // 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] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 115 | // Adding/removing of providers. |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 116 | bool AddSettingsProvider(std::unique_ptr<CrosSettingsProvider> provider); |
| 117 | std::unique_ptr<CrosSettingsProvider> RemoveSettingsProvider( |
| 118 | CrosSettingsProvider* provider); |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 119 | |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 120 | // Add an observer Callback for changes for the given |path|. |
stevenjb | 43c11caa | 2017-04-05 16:34:47 | [diff] [blame] | 121 | using ObserverSubscription = base::CallbackList<void(void)>::Subscription; |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 122 | std::unique_ptr<ObserverSubscription> AddSettingsObserver( |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 123 | const std::string& path, |
stevenjb | 43c11caa | 2017-04-05 16:34:47 | [diff] [blame] | 124 | const base::Closure& callback) WARN_UNUSED_RESULT; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 125 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 126 | // Returns the provider that handles settings with the |path| or prefix. |
[email protected] | c0a2128d9 | 2011-07-20 15:04:46 | [diff] [blame] | 127 | CrosSettingsProvider* GetProvider(const std::string& path) const; |
| 128 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 129 | private: |
[email protected] | 0c4d97c | 2012-09-01 20:47:20 | [diff] [blame] | 130 | friend class CrosSettingsTest; |
| 131 | |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 132 | // Fires system setting change callback. |
[email protected] | 0c4d97c | 2012-09-01 20:47:20 | [diff] [blame] | 133 | void FireObservers(const std::string& path); |
[email protected] | 279db0c | 2011-12-13 15:26:12 | [diff] [blame] | 134 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 135 | // List of ChromeOS system settings providers. |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 136 | std::vector<std::unique_ptr<CrosSettingsProvider>> providers_; |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 137 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 138 | // A map from settings names to a list of observers. Observers get fired in |
| 139 | // the order they are added. |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 140 | std::map<std::string, std::unique_ptr<base::CallbackList<void(void)>>> |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 141 | settings_observers_; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 142 | |
gab | 9e2fda8f | 2017-05-30 08:18:11 | [diff] [blame] | 143 | SEQUENCE_CHECKER(sequence_checker_); |
| 144 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(CrosSettings); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 146 | }; |
| 147 | |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 148 | // Helper class for tests. Initializes the CrosSettings singleton on |
| 149 | // construction and tears it down again on destruction. |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 150 | class ScopedTestCrosSettings { |
| 151 | public: |
| 152 | ScopedTestCrosSettings(); |
| 153 | ~ScopedTestCrosSettings(); |
| 154 | |
| 155 | private: |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings); |
| 157 | }; |
| 158 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 159 | } // namespace chromeos |
| 160 | |
[email protected] | edaf596 | 2012-08-01 11:31:31 | [diff] [blame] | 161 | #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_ |