blob: 200451486f20730235fabe2711e9d28b9ebadc9c [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
A Olsenb16a5e52018-09-21 15:48:4620class PrefService;
21
[email protected]dc8caba2010-12-13 16:52:3522namespace base {
[email protected]ced1dd22013-05-02 11:08:4423class DictionaryValue;
[email protected]45d32232011-11-22 11:42:3124class ListValue;
[email protected]c02c853d72010-08-07 06:23:2425class Value;
yilkal086bfce2019-08-01 17:28:2026} // namespace base
[email protected]c02c853d72010-08-07 06:23:2427
[email protected]73bd67b2010-07-16 16:00:0928namespace chromeos {
29
[email protected]08450dd2013-05-22 15:37:2630class DeviceSettingsService;
yilkal086bfce2019-08-01 17:28:2031class SupervisedUserCrosSettingsProvider;
[email protected]08450dd2013-05-22 15:37:2632
[email protected]a79f6dc2012-02-23 17:03:4833// This class manages per-device/global settings.
gab9e2fda8f2017-05-30 08:18:1134class CrosSettings {
[email protected]73bd67b2010-07-16 16:00:0935 public:
[email protected]7fe93102013-04-17 19:24:2336 // Manage singleton instance.
A Olsenb16a5e52018-09-21 15:48:4637 static void Initialize(PrefService* local_state);
[email protected]7fe93102013-04-17 19:24:2338 static bool IsInitialized();
39 static void Shutdown();
[email protected]73bd67b2010-07-16 16:00:0940 static CrosSettings* Get();
41
A Olsenbac20b942019-01-03 11:18:0842 // 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
nkostylev393309f2015-02-05 13:57:3948 // 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 Poromov27fa92532018-01-15 12:43:0451 bool IsUserWhitelisted(const std::string& username,
52 bool* wildcard_match) const;
nkostylev393309f2015-02-05 13:57:3953
A Olsenbac20b942019-01-03 11:18:0854 // 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]08450dd2013-05-22 15:37:2658 // Creates a device settings service instance. This is meant for unit tests,
59 // production code uses the singleton returned by Get() above.
A Olsenb16a5e52018-09-21 15:48:4660 CrosSettings(DeviceSettingsService* device_settings_service,
61 PrefService* local_state);
[email protected]08450dd2013-05-22 15:37:2662 virtual ~CrosSettings();
63
[email protected]a79f6dc2012-02-23 17:03:4864 // Helper function to test if the given |path| is a valid cros setting.
[email protected]57ecc4b2010-08-11 03:02:5165 static bool IsCrosSettings(const std::string& path);
[email protected]73bd67b2010-07-16 16:00:0966
[email protected]a79f6dc2012-02-23 17:03:4867 // Returns setting value for the given |path|.
[email protected]45d32232011-11-22 11:42:3168 const base::Value* GetPref(const std::string& path) const;
69
bartfab6de794c2014-10-30 15:23:3770 // 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]ac4ea5f62012-05-25 14:51:4083 virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
84 const base::Closure& callback) const;
[email protected]73bd67b2010-07-16 16:00:0985
[email protected]73bd67b2010-07-16 16:00:0986 // These are convenience forms of Get(). The value will be retrieved
[email protected]a79f6dc2012-02-23 17:03:4887 // and the return value will be true if the |path| is valid and the value at
[email protected]73bd67b2010-07-16 16:00:0988 // the end of the path can be returned in the form specified.
[email protected]57ecc4b2010-08-11 03:02:5189 bool GetBoolean(const std::string& path, bool* out_value) const;
90 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0791 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]57ecc4b2010-08-11 03:02:5192 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]45d32232011-11-22 11:42:3193 bool GetList(const std::string& path,
94 const base::ListValue** out_value) const;
[email protected]ced1dd22013-05-02 11:08:4495 bool GetDictionary(const std::string& path,
96 const base::DictionaryValue** out_value) const;
[email protected]46ebf0642010-07-24 02:47:4097
[email protected]6c2b8102011-11-22 13:10:3698 // 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]750b08f2014-01-30 04:12:45100 // 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]6c2b8102011-11-22 13:10:36105
Sergey Poromov27fa92532018-01-15 12:43:04106 // 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]a79f6dc2012-02-23 17:03:48111 // Adding/removing of providers.
avi7438ba72016-10-19 17:33:22112 bool AddSettingsProvider(std::unique_ptr<CrosSettingsProvider> provider);
113 std::unique_ptr<CrosSettingsProvider> RemoveSettingsProvider(
114 CrosSettingsProvider* provider);
[email protected]93a49ae2010-08-03 23:50:47115
[email protected]32ed8542013-09-19 23:27:32116 // Add an observer Callback for changes for the given |path|.
stevenjb43c11caa2017-04-05 16:34:47117 using ObserverSubscription = base::CallbackList<void(void)>::Subscription;
dcheng24002d02016-04-08 02:42:40118 std::unique_ptr<ObserverSubscription> AddSettingsObserver(
[email protected]32ed8542013-09-19 23:27:32119 const std::string& path,
stevenjb43c11caa2017-04-05 16:34:47120 const base::Closure& callback) WARN_UNUSED_RESULT;
[email protected]7096f562010-08-16 21:59:04121
[email protected]a79f6dc2012-02-23 17:03:48122 // Returns the provider that handles settings with the |path| or prefix.
[email protected]c0a2128d92011-07-20 15:04:46123 CrosSettingsProvider* GetProvider(const std::string& path) const;
124
yilkal086bfce2019-08-01 17:28:20125 const SupervisedUserCrosSettingsProvider*
126 supervised_user_cros_settings_provider() const {
127 return supervised_user_cros_settings_provider_;
128 }
129
[email protected]7096f562010-08-16 21:59:04130 private:
[email protected]0c4d97c2012-09-01 20:47:20131 friend class CrosSettingsTest;
132
[email protected]32ed8542013-09-19 23:27:32133 // Fires system setting change callback.
[email protected]0c4d97c2012-09-01 20:47:20134 void FireObservers(const std::string& path);
[email protected]279db0c2011-12-13 15:26:12135
[email protected]7096f562010-08-16 21:59:04136 // List of ChromeOS system settings providers.
avi7438ba72016-10-19 17:33:22137 std::vector<std::unique_ptr<CrosSettingsProvider>> providers_;
[email protected]93a49ae2010-08-03 23:50:47138
yilkal086bfce2019-08-01 17:28:20139 // Owner unique pointer in |providers_|.
140 SupervisedUserCrosSettingsProvider* supervised_user_cros_settings_provider_;
141
[email protected]7096f562010-08-16 21:59:04142 // A map from settings names to a list of observers. Observers get fired in
143 // the order they are added.
brettw84cff3f2017-06-29 18:26:50144 std::map<std::string, std::unique_ptr<base::CallbackList<void(void)>>>
avi7438ba72016-10-19 17:33:22145 settings_observers_;
[email protected]7096f562010-08-16 21:59:04146
gab9e2fda8f2017-05-30 08:18:11147 SEQUENCE_CHECKER(sequence_checker_);
148
[email protected]93a49ae2010-08-03 23:50:47149 DISALLOW_COPY_AND_ASSIGN(CrosSettings);
[email protected]73bd67b2010-07-16 16:00:09150};
151
[email protected]e4854dc2013-04-24 00:11:51152// Helper class for tests. Initializes the CrosSettings singleton on
153// construction and tears it down again on destruction.
[email protected]7fe93102013-04-17 19:24:23154class ScopedTestCrosSettings {
155 public:
A Olsenb16a5e52018-09-21 15:48:46156 explicit ScopedTestCrosSettings(PrefService* local_state);
[email protected]7fe93102013-04-17 19:24:23157 ~ScopedTestCrosSettings();
158
159 private:
[email protected]7fe93102013-04-17 19:24:23160 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
161};
162
[email protected]73bd67b2010-07-16 16:00:09163} // namespace chromeos
164
[email protected]edaf5962012-08-01 11:31:31165#endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_