[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 | |
A Olsen | b16a5e5 | 2018-09-21 15:48:46 | [diff] [blame] | 20 | class PrefService; |
| 21 | |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 22 | namespace base { |
[email protected] | ced1dd2 | 2013-05-02 11:08:44 | [diff] [blame] | 23 | class DictionaryValue; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 24 | class ListValue; |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 25 | class Value; |
yilkal | 086bfce | 2019-08-01 17:28:20 | [diff] [blame^] | 26 | } // namespace base |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 27 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 28 | namespace chromeos { |
| 29 | |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 30 | class DeviceSettingsService; |
yilkal | 086bfce | 2019-08-01 17:28:20 | [diff] [blame^] | 31 | class SupervisedUserCrosSettingsProvider; |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 32 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 33 | // This class manages per-device/global settings. |
gab | 9e2fda8f | 2017-05-30 08:18:11 | [diff] [blame] | 34 | class CrosSettings { |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 35 | public: |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 36 | // Manage singleton instance. |
A Olsen | b16a5e5 | 2018-09-21 15:48:46 | [diff] [blame] | 37 | static void Initialize(PrefService* local_state); |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 38 | static bool IsInitialized(); |
| 39 | static void Shutdown(); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 40 | static CrosSettings* Get(); |
| 41 | |
A Olsen | bac20b94 | 2019-01-03 11:18:08 | [diff] [blame] | 42 | // Sets the singleton to |test_instance|. Does not take ownership of the |
| 43 | // instance. Should be matched with a call to |ShutdownForTesting| once the |
| 44 | // test is finished and before the instance is deleted. |
| 45 | static void SetForTesting(CrosSettings* test_instance); |
| 46 | static void ShutdownForTesting(); |
| 47 | |
nkostylev | 393309f | 2015-02-05 13:57:39 | [diff] [blame] | 48 | // Checks if the given username is whitelisted and allowed to sign-in to |
| 49 | // this device. |wildcard_match| may be NULL. If it's present, it'll be set to |
| 50 | // true if the whitelist check was satisfied via a wildcard. |
Sergey Poromov | 27fa9253 | 2018-01-15 12:43:04 | [diff] [blame] | 51 | bool IsUserWhitelisted(const std::string& username, |
| 52 | bool* wildcard_match) const; |
nkostylev | 393309f | 2015-02-05 13:57:39 | [diff] [blame] | 53 | |
A Olsen | bac20b94 | 2019-01-03 11:18:08 | [diff] [blame] | 54 | // Creates an instance with no providers as yet. This is meant for unit tests, |
| 55 | // production code uses the singleton returned by Get() above. |
| 56 | CrosSettings(); |
| 57 | |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 58 | // Creates a device settings service instance. This is meant for unit tests, |
| 59 | // production code uses the singleton returned by Get() above. |
A Olsen | b16a5e5 | 2018-09-21 15:48:46 | [diff] [blame] | 60 | CrosSettings(DeviceSettingsService* device_settings_service, |
| 61 | PrefService* local_state); |
[email protected] | 08450dd | 2013-05-22 15:37:26 | [diff] [blame] | 62 | virtual ~CrosSettings(); |
| 63 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 64 | // Helper function to test if the given |path| is a valid cros setting. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 65 | static bool IsCrosSettings(const std::string& path); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 66 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 67 | // Returns setting value for the given |path|. |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 68 | const base::Value* GetPref(const std::string& path) const; |
| 69 | |
bartfab | 6de794c | 2014-10-30 15:23:37 | [diff] [blame] | 70 | // Requests that all providers ensure the values they are serving were read |
| 71 | // from a trusted store: |
| 72 | // * If all providers are serving trusted values, returns TRUSTED. This |
| 73 | // indicates that the cros settings returned by |this| can be trusted during |
| 74 | // the current loop cycle. |
| 75 | // * If at least one provider ran into a permanent failure while trying to |
| 76 | // read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This |
| 77 | // indicates that the cros settings will never become trusted. |
| 78 | // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least |
| 79 | // one provider needs to read values from its trusted store first. The |
| 80 | // |callback| will be called back when the read is done. |
| 81 | // PrepareTrustedValues() should be called again at that point to determine |
| 82 | // whether all providers are serving trusted values now. |
[email protected] | ac4ea5f6 | 2012-05-25 14:51:40 | [diff] [blame] | 83 | virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues( |
| 84 | const base::Closure& callback) const; |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 85 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 86 | // These are convenience forms of Get(). The value will be retrieved |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 87 | // 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] | 88 | // the end of the path can be returned in the form specified. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 89 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 90 | bool GetInteger(const std::string& path, int* out_value) const; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 91 | bool GetDouble(const std::string& path, double* out_value) const; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 92 | bool GetString(const std::string& path, std::string* out_value) const; |
[email protected] | 45d3223 | 2011-11-22 11:42:31 | [diff] [blame] | 93 | bool GetList(const std::string& path, |
| 94 | const base::ListValue** out_value) const; |
[email protected] | ced1dd2 | 2013-05-02 11:08:44 | [diff] [blame] | 95 | bool GetDictionary(const std::string& path, |
| 96 | const base::DictionaryValue** out_value) const; |
[email protected] | 46ebf064 | 2010-07-24 02:47:40 | [diff] [blame] | 97 | |
[email protected] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 98 | // Helper function for the whitelist op. Implemented here because we will need |
| 99 | // 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] | 100 | // It respects whitelists so [email protected] will match *@bar.baz too. If the |
| 101 | // match was via a wildcard, |wildcard_match| is set to true. |
| 102 | bool FindEmailInList(const std::string& path, |
| 103 | const std::string& email, |
| 104 | bool* wildcard_match) const; |
[email protected] | 6c2b810 | 2011-11-22 13:10:36 | [diff] [blame] | 105 | |
Sergey Poromov | 27fa9253 | 2018-01-15 12:43:04 | [diff] [blame] | 106 | // Same as above, but receives already populated user list. |
| 107 | static bool FindEmailInList(const base::ListValue* list, |
| 108 | const std::string& email, |
| 109 | bool* wildcard_match); |
| 110 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 111 | // Adding/removing of providers. |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 112 | bool AddSettingsProvider(std::unique_ptr<CrosSettingsProvider> provider); |
| 113 | std::unique_ptr<CrosSettingsProvider> RemoveSettingsProvider( |
| 114 | CrosSettingsProvider* provider); |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 115 | |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 116 | // Add an observer Callback for changes for the given |path|. |
stevenjb | 43c11caa | 2017-04-05 16:34:47 | [diff] [blame] | 117 | using ObserverSubscription = base::CallbackList<void(void)>::Subscription; |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 118 | std::unique_ptr<ObserverSubscription> AddSettingsObserver( |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 119 | const std::string& path, |
stevenjb | 43c11caa | 2017-04-05 16:34:47 | [diff] [blame] | 120 | const base::Closure& callback) WARN_UNUSED_RESULT; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 121 | |
[email protected] | a79f6dc | 2012-02-23 17:03:48 | [diff] [blame] | 122 | // Returns the provider that handles settings with the |path| or prefix. |
[email protected] | c0a2128d9 | 2011-07-20 15:04:46 | [diff] [blame] | 123 | CrosSettingsProvider* GetProvider(const std::string& path) const; |
| 124 | |
yilkal | 086bfce | 2019-08-01 17:28:20 | [diff] [blame^] | 125 | const SupervisedUserCrosSettingsProvider* |
| 126 | supervised_user_cros_settings_provider() const { |
| 127 | return supervised_user_cros_settings_provider_; |
| 128 | } |
| 129 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 130 | private: |
[email protected] | 0c4d97c | 2012-09-01 20:47:20 | [diff] [blame] | 131 | friend class CrosSettingsTest; |
| 132 | |
[email protected] | 32ed854 | 2013-09-19 23:27:32 | [diff] [blame] | 133 | // Fires system setting change callback. |
[email protected] | 0c4d97c | 2012-09-01 20:47:20 | [diff] [blame] | 134 | void FireObservers(const std::string& path); |
[email protected] | 279db0c | 2011-12-13 15:26:12 | [diff] [blame] | 135 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 136 | // List of ChromeOS system settings providers. |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 137 | std::vector<std::unique_ptr<CrosSettingsProvider>> providers_; |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 138 | |
yilkal | 086bfce | 2019-08-01 17:28:20 | [diff] [blame^] | 139 | // Owner unique pointer in |providers_|. |
| 140 | SupervisedUserCrosSettingsProvider* supervised_user_cros_settings_provider_; |
| 141 | |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 142 | // A map from settings names to a list of observers. Observers get fired in |
| 143 | // the order they are added. |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 144 | std::map<std::string, std::unique_ptr<base::CallbackList<void(void)>>> |
avi | 7438ba7 | 2016-10-19 17:33:22 | [diff] [blame] | 145 | settings_observers_; |
[email protected] | 7096f56 | 2010-08-16 21:59:04 | [diff] [blame] | 146 | |
gab | 9e2fda8f | 2017-05-30 08:18:11 | [diff] [blame] | 147 | SEQUENCE_CHECKER(sequence_checker_); |
| 148 | |
[email protected] | 93a49ae | 2010-08-03 23:50:47 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(CrosSettings); |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 150 | }; |
| 151 | |
[email protected] | e4854dc | 2013-04-24 00:11:51 | [diff] [blame] | 152 | // Helper class for tests. Initializes the CrosSettings singleton on |
| 153 | // construction and tears it down again on destruction. |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 154 | class ScopedTestCrosSettings { |
| 155 | public: |
A Olsen | b16a5e5 | 2018-09-21 15:48:46 | [diff] [blame] | 156 | explicit ScopedTestCrosSettings(PrefService* local_state); |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 157 | ~ScopedTestCrosSettings(); |
| 158 | |
| 159 | private: |
[email protected] | 7fe9310 | 2013-04-17 19:24:23 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings); |
| 161 | }; |
| 162 | |
[email protected] | 73bd67b | 2010-07-16 16:00:09 | [diff] [blame] | 163 | } // namespace chromeos |
| 164 | |
[email protected] | edaf596 | 2012-08-01 11:31:31 | [diff] [blame] | 165 | #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_ |