Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_settings_service.h
blobfce6585bc9b54f753734d0b2d487eca9fc1887c9
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/callback_list.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_store.h"
16 #include "base/values.h"
17 #include "chrome/browser/supervised_user/supervised_users.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "sync/api/syncable_service.h"
21 class PersistentPrefStore;
22 class Profile;
24 namespace base {
25 class FilePath;
26 class SequencedTaskRunner;
29 // This class syncs supervised user settings from a server, which are mapped to
30 // preferences. The downloaded settings are persisted in a PrefStore (which is
31 // not directly hooked up to the PrefService; it's just used internally).
32 // Settings are key-value pairs, where the key uniquely identifies the setting.
33 // The value is a string containing a JSON serialization of an arbitrary value,
34 // which is the value of the setting.
36 // There are two kinds of settings handled by this class: Atomic and split
37 // settings.
38 // Atomic settings consist of a single key (which will be mapped to a pref key)
39 // and a single (arbitrary) value.
40 // Split settings encode a dictionary value and are stored as multiple Sync
41 // items, one for each dictionary entry. The key for each of these Sync items
42 // is the key of the split setting, followed by a separator (':') and the key
43 // for the dictionary entry. The value of the Sync item is the value of the
44 // dictionary entry.
46 // As an example, a split setting with key "Moose" and value
47 // {
48 // "foo": "bar",
49 // "baz": "blurp"
50 // }
51 // would be encoded as two sync items, one with key "Moose:foo" and value "bar",
52 // and one with key "Moose:baz" and value "blurp".
53 class SupervisedUserSettingsService : public KeyedService,
54 public syncer::SyncableService,
55 public PrefStore::Observer {
56 public:
57 // A callback whose first parameter is a dictionary containing all supervised
58 // user settings. If the dictionary is NULL, it means that the service is
59 // inactive, i.e. the user is not supervised.
60 using SettingsCallbackType = void(const base::DictionaryValue*);
61 using SettingsCallback = base::Callback<SettingsCallbackType>;
62 using SettingsCallbackList = base::CallbackList<SettingsCallbackType>;
64 explicit SupervisedUserSettingsService(Profile *profile);
65 ~SupervisedUserSettingsService() override;
67 // Initializes the service by loading its settings from a file underneath the
68 // |profile_path|. File I/O will be serialized via the
69 // |sequenced_task_runner|. If |load_synchronously| is true, the settings will
70 // be loaded synchronously, otherwise asynchronously.
71 void Init(base::FilePath profile_path,
72 base::SequencedTaskRunner* sequenced_task_runner,
73 bool load_synchronously);
75 // Initializes the service by loading its settings from the |pref_store|.
76 // Use this method in tests to inject a different PrefStore than the
77 // default one.
78 void Init(scoped_refptr<PersistentPrefStore> pref_store);
80 // Adds a callback to be called when supervised user settings are initially
81 // available, or when they change.
82 scoped_ptr<SettingsCallbackList::Subscription> Subscribe(
83 const SettingsCallback& callback) WARN_UNUSED_RESULT;
85 // Gets the associated profile
86 // This is currently only used for subscribing to notifications, it will be
87 // nullptr in tests and will soon be removed.
88 // TODO(peconn): Remove this once HostContentSettingsMap is a KeyedService
89 Profile* GetProfile();
91 // Activates/deactivates the service. This is called by the
92 // SupervisedUserService when it is (de)activated.
93 void SetActive(bool active);
95 // Whether supervised user settings are available.
96 bool IsReady();
98 // Clears all supervised user settings and items.
99 void Clear();
101 // Constructs a key for a split supervised user setting from a prefix and a
102 // variable key.
103 static std::string MakeSplitSettingKey(const std::string& prefix,
104 const std::string& key);
106 // Uploads an item to the Sync server. Items are the same data structure as
107 // supervised user settings (i.e. key-value pairs, as described at the top of
108 // the file), but they are only uploaded (whereas supervised user settings are
109 // only downloaded), and never passed to the preference system.
110 // An example of an uploaded item is an access request to a blocked URL.
111 void UploadItem(const std::string& key, scoped_ptr<base::Value> value);
113 // Sets the setting with the given |key| to a copy of the given |value|.
114 void SetLocalSetting(const std::string& key, scoped_ptr<base::Value> value);
116 // Public for testing.
117 static syncer::SyncData CreateSyncDataForSetting(const std::string& name,
118 const base::Value& value);
120 // KeyedService implementation:
121 void Shutdown() override;
123 // SyncableService implementation:
124 syncer::SyncMergeResult MergeDataAndStartSyncing(
125 syncer::ModelType type,
126 const syncer::SyncDataList& initial_sync_data,
127 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
128 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
129 void StopSyncing(syncer::ModelType type) override;
130 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
131 syncer::SyncError ProcessSyncChanges(
132 const tracked_objects::Location& from_here,
133 const syncer::SyncChangeList& change_list) override;
135 // PrefStore::Observer implementation:
136 void OnPrefValueChanged(const std::string& key) override;
137 void OnInitializationCompleted(bool success) override;
139 private:
140 base::DictionaryValue* GetOrCreateDictionary(const std::string& key) const;
141 base::DictionaryValue* GetAtomicSettings() const;
142 base::DictionaryValue* GetSplitSettings() const;
143 base::DictionaryValue* GetQueuedItems() const;
145 // Returns the dictionary where a given Sync item should be stored, depending
146 // on whether the supervised user setting is atomic or split. In case of a
147 // split setting, the split setting prefix of |key| is removed, so that |key|
148 // can be used to update the returned dictionary.
149 base::DictionaryValue* GetDictionaryAndSplitKey(std::string* key) const;
151 // Returns a dictionary with all supervised user settings if the service is
152 // active, or NULL otherwise.
153 scoped_ptr<base::DictionaryValue> GetSettings();
155 // Sends the settings to all subscribers. This method should be called by the
156 // subclass whenever the settings change.
157 void InformSubscribers();
159 // Used for persisting the settings. Unlike other PrefStores, this one is not
160 // directly hooked up to the PrefService.
161 scoped_refptr<PersistentPrefStore> store_;
163 Profile* profile_;
165 bool active_;
167 bool initialization_failed_;
169 // A set of local settings that are fixed and not configured remotely.
170 scoped_ptr<base::DictionaryValue> local_settings_;
172 SettingsCallbackList callback_list_;
174 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
175 scoped_ptr<syncer::SyncErrorFactory> error_handler_;
177 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSettingsService);
180 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_