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_
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
;
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
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
46 // As an example, a split setting with key "Moose" and value
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
{
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
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 SupervisedUserPrefStore is (partially at
89 // least) a KeyedService, see TODO in SupervisedUserPrefStore.
90 Profile
* GetProfile();
92 // Activates/deactivates the service. This is called by the
93 // SupervisedUserService when it is (de)activated.
94 void SetActive(bool active
);
96 // Whether supervised user settings are available.
99 // Clears all supervised user settings and items.
102 // Constructs a key for a split supervised user setting from a prefix and a
104 static std::string
MakeSplitSettingKey(const std::string
& prefix
,
105 const std::string
& key
);
107 // Uploads an item to the Sync server. Items are the same data structure as
108 // supervised user settings (i.e. key-value pairs, as described at the top of
109 // the file), but they are only uploaded (whereas supervised user settings are
110 // only downloaded), and never passed to the preference system.
111 // An example of an uploaded item is an access request to a blocked URL.
112 void UploadItem(const std::string
& key
, scoped_ptr
<base::Value
> value
);
114 // Sets the setting with the given |key| to a copy of the given |value|.
115 void SetLocalSetting(const std::string
& key
, scoped_ptr
<base::Value
> value
);
117 // Public for testing.
118 static syncer::SyncData
CreateSyncDataForSetting(const std::string
& name
,
119 const base::Value
& value
);
121 // KeyedService implementation:
122 void Shutdown() override
;
124 // SyncableService implementation:
125 syncer::SyncMergeResult
MergeDataAndStartSyncing(
126 syncer::ModelType type
,
127 const syncer::SyncDataList
& initial_sync_data
,
128 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
129 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) override
;
130 void StopSyncing(syncer::ModelType type
) override
;
131 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
132 syncer::SyncError
ProcessSyncChanges(
133 const tracked_objects::Location
& from_here
,
134 const syncer::SyncChangeList
& change_list
) override
;
136 // PrefStore::Observer implementation:
137 void OnPrefValueChanged(const std::string
& key
) override
;
138 void OnInitializationCompleted(bool success
) override
;
141 base::DictionaryValue
* GetOrCreateDictionary(const std::string
& key
) const;
142 base::DictionaryValue
* GetAtomicSettings() const;
143 base::DictionaryValue
* GetSplitSettings() const;
144 base::DictionaryValue
* GetQueuedItems() const;
146 // Returns the dictionary where a given Sync item should be stored, depending
147 // on whether the supervised user setting is atomic or split. In case of a
148 // split setting, the split setting prefix of |key| is removed, so that |key|
149 // can be used to update the returned dictionary.
150 base::DictionaryValue
* GetDictionaryAndSplitKey(std::string
* key
) const;
152 // Returns a dictionary with all supervised user settings if the service is
153 // active, or NULL otherwise.
154 scoped_ptr
<base::DictionaryValue
> GetSettings();
156 // Sends the settings to all subscribers. This method should be called by the
157 // subclass whenever the settings change.
158 void InformSubscribers();
160 // Used for persisting the settings. Unlike other PrefStores, this one is not
161 // directly hooked up to the PrefService.
162 scoped_refptr
<PersistentPrefStore
> store_
;
168 bool initialization_failed_
;
170 // A set of local settings that are fixed and not configured remotely.
171 scoped_ptr
<base::DictionaryValue
> local_settings_
;
173 SettingsCallbackList callback_list_
;
175 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
176 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
178 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSettingsService
);
181 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SETTINGS_SERVICE_H_