Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / supervised_user_shared_settings_service.h
blobaeaabf3d5393100f66bb067869a3006dd9e8329b
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_LEGACY_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_LEGACY_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/callback_list.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/supervised_user/supervised_users.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "sync/api/syncable_service.h"
17 class PrefService;
19 namespace base {
20 class DictionaryValue;
21 class Value;
24 namespace user_prefs {
25 class PrefRegistrySyncable;
28 // SupervisedUserSharedSettingsService syncs settings (as key-value pairs) that
29 // can be modified both by a supervised user and their manager.
30 // A supervised user can only modify their own settings, whereas a manager can
31 // modify settings for all their supervised users.
33 // The shared settings are stored in the user preferences in a multi-level
34 // dictionary. The first level is the SU ID (called "managed user ID" on the
35 // server for historic reasons), the second level is the key for the setting,
36 // and the third level is a dictionary with a "value" key for the value and an
37 // "acknowledged" flag, which is used to wait for the Sync server to acknowledge
38 // that it has seen a setting change (see SupervisedUserSharedSettingsUpdate for
39 // how to use this).
40 class SupervisedUserSharedSettingsService : public KeyedService,
41 public syncer::SyncableService {
42 public:
43 // Called whenever a setting changes (see Subscribe() below).
44 typedef base::Callback<void(const std::string& /* su_id */,
45 const std::string& /* key */)> ChangeCallback;
46 typedef base::CallbackList<
47 void(const std::string& /* su_id */, const std::string& /* key */)>
48 ChangeCallbackList;
50 // This constructor is public only for testing. Use
51 // |SupervisedUserSharedSettingsServiceFactory::GetForProfile(...)| instead to
52 // get an instance of this service in production code.
53 explicit SupervisedUserSharedSettingsService(PrefService* prefs);
54 ~SupervisedUserSharedSettingsService() override;
56 // Returns the value for the given |key| and the supervised user identified by
57 // |su_id|. If either the supervised user or the key does not exist, NULL is
58 // returned. Note that if the profile that owns this service belongs to a
59 // supervised user, callers will only see settings for their own |su_id|, i.e.
60 // a non-matching |su_id| is treated as non-existent.
61 const base::Value* GetValue(const std::string& su_id, const std::string& key);
63 // Sets the value for the given |key| and the supervised user identified by
64 // |su_id|. If the profile that owns this service belongs to a supervised
65 // user, |su_id| must be their own.
66 void SetValue(const std::string& su_id,
67 const std::string& key,
68 const base::Value& value);
70 // Subscribes to changes in the synced settings. The callback will be notified
71 // whenever any setting for any supervised user is changed via Sync (but not
72 // for local changes). Subscribers should filter the settings and users they
73 // are interested in with the |su_id| and |key| parameters to the callback.
74 scoped_ptr<ChangeCallbackList::Subscription> Subscribe(
75 const ChangeCallback& cb);
77 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
79 // Public for testing.
80 void SetValueInternal(const std::string& su_id,
81 const std::string& key,
82 const base::Value& value,
83 bool acknowledged);
85 // Public for testing.
86 static syncer::SyncData CreateSyncDataForSetting(const std::string& su_id,
87 const std::string& key,
88 const base::Value& value,
89 bool acknowledged);
91 // KeyedService implementation:
92 void Shutdown() override;
94 // SyncableService implementation:
95 syncer::SyncMergeResult MergeDataAndStartSyncing(
96 syncer::ModelType type,
97 const syncer::SyncDataList& initial_sync_data,
98 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
99 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
100 void StopSyncing(syncer::ModelType type) override;
101 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
102 syncer::SyncError ProcessSyncChanges(
103 const tracked_objects::Location& from_here,
104 const syncer::SyncChangeList& change_list) override;
106 private:
107 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
108 scoped_ptr<syncer::SyncErrorFactory> error_handler_;
110 ChangeCallbackList callbacks_;
112 PrefService* prefs_;
115 #endif // CHROME_BROWSER_SUPERVISED_USER_LEGACY_SUPERVISED_USER_SHARED_SETTINGS_SERVICE_H_