blob: 569eb3f083d43713d6058f6dc335f7f672c2ff38 [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
8#include <string>
[email protected]93a49ae2010-08-03 23:50:479#include <vector>
[email protected]73bd67b2010-07-16 16:00:0910
[email protected]a79f6dc2012-02-23 17:03:4811#include "base/callback_forward.h"
[email protected]2a7cac02013-09-26 19:20:1812#include "base/callback_list.h"
[email protected]14c1c232013-06-11 17:52:4413#include "base/containers/hash_tables.h"
avi8a07d53892015-12-24 22:13:5314#include "base/macros.h"
[email protected]32ed8542013-09-19 23:27:3215#include "base/memory/scoped_ptr.h"
[email protected]c9177502011-01-01 04:48:4916#include "base/threading/non_thread_safe.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.
[email protected]c9177502011-01-01 04:48:4931class CrosSettings : public base::NonThreadSafe {
[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.
42 static bool IsWhitelisted(const std::string& username, bool* wildcard_match);
43
[email protected]08450dd2013-05-22 15:37:2644 // Creates a device settings service instance. This is meant for unit tests,
45 // production code uses the singleton returned by Get() above.
46 explicit CrosSettings(DeviceSettingsService* device_settings_service);
47 virtual ~CrosSettings();
48
[email protected]a79f6dc2012-02-23 17:03:4849 // Helper function to test if the given |path| is a valid cros setting.
[email protected]57ecc4b2010-08-11 03:02:5150 static bool IsCrosSettings(const std::string& path);
[email protected]73bd67b2010-07-16 16:00:0951
52 // Sets |in_value| to given |path| in cros settings.
[email protected]6c2b8102011-11-22 13:10:3653 void Set(const std::string& path, const base::Value& in_value);
[email protected]73bd67b2010-07-16 16:00:0954
[email protected]a79f6dc2012-02-23 17:03:4855 // Returns setting value for the given |path|.
[email protected]45d32232011-11-22 11:42:3156 const base::Value* GetPref(const std::string& path) const;
57
bartfab6de794c2014-10-30 15:23:3758 // Requests that all providers ensure the values they are serving were read
59 // from a trusted store:
60 // * If all providers are serving trusted values, returns TRUSTED. This
61 // indicates that the cros settings returned by |this| can be trusted during
62 // the current loop cycle.
63 // * If at least one provider ran into a permanent failure while trying to
64 // read values from its trusted store, returns PERMANENTLY_UNTRUSTED. This
65 // indicates that the cros settings will never become trusted.
66 // * Otherwise, returns TEMPORARILY_UNTRUSTED. This indicates that at least
67 // one provider needs to read values from its trusted store first. The
68 // |callback| will be called back when the read is done.
69 // PrepareTrustedValues() should be called again at that point to determine
70 // whether all providers are serving trusted values now.
[email protected]ac4ea5f62012-05-25 14:51:4071 virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
72 const base::Closure& callback) const;
[email protected]73bd67b2010-07-16 16:00:0973
74 // Convenience forms of Set(). These methods will replace any existing
[email protected]a79f6dc2012-02-23 17:03:4875 // value at that |path|, even if it has a different type.
[email protected]57ecc4b2010-08-11 03:02:5176 void SetBoolean(const std::string& path, bool in_value);
77 void SetInteger(const std::string& path, int in_value);
[email protected]fb534c92011-02-01 01:02:0778 void SetDouble(const std::string& path, double in_value);
[email protected]57ecc4b2010-08-11 03:02:5179 void SetString(const std::string& path, const std::string& in_value);
[email protected]73bd67b2010-07-16 16:00:0980
[email protected]8f2df86a2012-05-11 00:45:2381 // Convenience functions for manipulating lists. Note that the following
82 // functions employs a read, modify and write pattern. If underlying settings
83 // provider updates its value asynchronously such as DeviceSettingsProvider,
84 // value cache they read from might not be fresh and multiple calls to those
85 // function would lose data. See http://crbug.com/127215
[email protected]6c2b8102011-11-22 13:10:3686 void AppendToList(const std::string& path, const base::Value* value);
87 void RemoveFromList(const std::string& path, const base::Value* value);
88
[email protected]73bd67b2010-07-16 16:00:0989 // These are convenience forms of Get(). The value will be retrieved
[email protected]a79f6dc2012-02-23 17:03:4890 // and the return value will be true if the |path| is valid and the value at
[email protected]73bd67b2010-07-16 16:00:0991 // the end of the path can be returned in the form specified.
[email protected]57ecc4b2010-08-11 03:02:5192 bool GetBoolean(const std::string& path, bool* out_value) const;
93 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0794 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]57ecc4b2010-08-11 03:02:5195 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]45d32232011-11-22 11:42:3196 bool GetList(const std::string& path,
97 const base::ListValue** out_value) const;
[email protected]ced1dd22013-05-02 11:08:4498 bool GetDictionary(const std::string& path,
99 const base::DictionaryValue** out_value) const;
[email protected]46ebf0642010-07-24 02:47:40100
[email protected]6c2b8102011-11-22 13:10:36101 // Helper function for the whitelist op. Implemented here because we will need
102 // this in a few places. The functions searches for |email| in the pref |path|
[email protected]750b08f2014-01-30 04:12:45103 // It respects whitelists so [email protected] will match *@bar.baz too. If the
104 // match was via a wildcard, |wildcard_match| is set to true.
105 bool FindEmailInList(const std::string& path,
106 const std::string& email,
107 bool* wildcard_match) const;
[email protected]6c2b8102011-11-22 13:10:36108
[email protected]a79f6dc2012-02-23 17:03:48109 // Adding/removing of providers.
[email protected]7096f562010-08-16 21:59:04110 bool AddSettingsProvider(CrosSettingsProvider* provider);
111 bool RemoveSettingsProvider(CrosSettingsProvider* provider);
[email protected]93a49ae2010-08-03 23:50:47112
[email protected]32ed8542013-09-19 23:27:32113 // Add an observer Callback for changes for the given |path|.
[email protected]2a7cac02013-09-26 19:20:18114 typedef base::CallbackList<void(void)>::Subscription ObserverSubscription;
[email protected]32ed8542013-09-19 23:27:32115 scoped_ptr<ObserverSubscription> AddSettingsObserver(
116 const std::string& path,
117 const base::Closure& callback);
[email protected]7096f562010-08-16 21:59:04118
[email protected]a79f6dc2012-02-23 17:03:48119 // Returns the provider that handles settings with the |path| or prefix.
[email protected]c0a2128d92011-07-20 15:04:46120 CrosSettingsProvider* GetProvider(const std::string& path) const;
121
[email protected]7096f562010-08-16 21:59:04122 private:
[email protected]0c4d97c2012-09-01 20:47:20123 friend class CrosSettingsTest;
124
[email protected]32ed8542013-09-19 23:27:32125 // Fires system setting change callback.
[email protected]0c4d97c2012-09-01 20:47:20126 void FireObservers(const std::string& path);
[email protected]279db0c2011-12-13 15:26:12127
[email protected]7096f562010-08-16 21:59:04128 // List of ChromeOS system settings providers.
[email protected]93a49ae2010-08-03 23:50:47129 std::vector<CrosSettingsProvider*> providers_;
130
[email protected]7096f562010-08-16 21:59:04131 // A map from settings names to a list of observers. Observers get fired in
132 // the order they are added.
[email protected]2a7cac02013-09-26 19:20:18133 typedef base::hash_map<std::string, base::CallbackList<void(void)>*>
[email protected]7096f562010-08-16 21:59:04134 SettingsObserverMap;
135 SettingsObserverMap settings_observers_;
136
[email protected]93a49ae2010-08-03 23:50:47137 DISALLOW_COPY_AND_ASSIGN(CrosSettings);
[email protected]73bd67b2010-07-16 16:00:09138};
139
[email protected]e4854dc2013-04-24 00:11:51140// Helper class for tests. Initializes the CrosSettings singleton on
141// construction and tears it down again on destruction.
[email protected]7fe93102013-04-17 19:24:23142class ScopedTestCrosSettings {
143 public:
144 ScopedTestCrosSettings();
145 ~ScopedTestCrosSettings();
146
147 private:
[email protected]7fe93102013-04-17 19:24:23148 DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
149};
150
[email protected]73bd67b2010-07-16 16:00:09151} // namespace chromeos
152
[email protected]edaf5962012-08-01 11:31:31153#endif // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_