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_MANAGED_MODE_MANAGED_USER_SHARED_SETTINGS_SERVICE_H_
6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SHARED_SETTINGS_SERVICE_H_
8 #include "base/callback.h"
9 #include "base/callback_list.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/managed_mode/managed_users.h"
12 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
13 #include "sync/api/syncable_service.h"
18 class DictionaryValue
;
22 namespace user_prefs
{
23 class PrefRegistrySyncable
;
26 // ManagedUserSharedSettingsService syncs settings (as key-value pairs) that can
27 // be modified both by a supervised user and their manager.
28 // A supervised user can only modify their own settings, whereas a manager can
29 // modify settings for all their supervised users.
31 // The shared settings are stored in the user preferences in a multi-level
32 // dictionary. The first level is the MU ID, the second level is the key for the
33 // setting, and the third level is a dictionary with a "value" key for the value
34 // and an "acknowledged" flag, which is used to wait for the Sync server to
35 // acknowledge that it has seen a setting change (see
36 // ManagedUserSharedSettingsUpdate for how to use this).
37 class ManagedUserSharedSettingsService
: public BrowserContextKeyedService
,
38 public syncer::SyncableService
{
40 // Called whenever a setting changes (see Subscribe() below).
41 typedef base::Callback
<void(const std::string
& /* mu_id */,
42 const std::string
& /* key */)> ChangeCallback
;
43 typedef base::CallbackList
<
44 void(const std::string
& /* mu_id */, const std::string
& /* key */)>
47 // This constructor is public only for testing. Use
48 // |ManagedUserSyncServiceFactory::GetForProfile(...)| instead to get an
49 // instance of this service in production code.
50 explicit ManagedUserSharedSettingsService(PrefService
* prefs
);
51 virtual ~ManagedUserSharedSettingsService();
53 // Returns the value for the given |key| and the supervised user identified by
54 // |mu_id|. If either the supervised user or the key does not exist, NULL is
55 // returned. Note that if the profile that owns this service belongs to a
56 // supervised user, callers will only see settings for their own |mu_id|, i.e.
57 // a non-matching |mu_id| is treated as non-existent.
58 const base::Value
* GetValue(const std::string
& mu_id
, const std::string
& key
);
60 // Sets the value for the given |key| and the supervised user identified by
61 // |mu_id|. If the profile that owns this service belongs to a supervised
62 // user, |mu_id| must be their own.
63 void SetValue(const std::string
& mu_id
,
64 const std::string
& key
,
65 const base::Value
& value
);
67 // Subscribes to changes in the synced settings. The callback will be notified
68 // whenever any setting for any supervised user is changed via Sync (but not
69 // for local changes). Subscribers should filter the settings and users they
70 // are interested in with the |mu_id| and |key| parameters to the callback.
71 scoped_ptr
<ChangeCallbackList::Subscription
> Subscribe(
72 const ChangeCallback
& cb
);
74 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
76 // Public for testing.
77 void SetValueInternal(const std::string
& mu_id
,
78 const std::string
& key
,
79 const base::Value
& value
,
82 // Public for testing.
83 static syncer::SyncData
CreateSyncDataForSetting(const std::string
& mu_id
,
84 const std::string
& key
,
85 const base::Value
& value
,
88 // BrowserContextKeyedService implementation:
89 virtual void Shutdown() OVERRIDE
;
91 // SyncableService implementation:
92 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
93 syncer::ModelType type
,
94 const syncer::SyncDataList
& initial_sync_data
,
95 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
96 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
97 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
98 virtual syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const
100 virtual syncer::SyncError
ProcessSyncChanges(
101 const tracked_objects::Location
& from_here
,
102 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
105 friend class ManagedUserSharedSettingsUpdate
;
107 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
108 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
110 ChangeCallbackList callbacks_
;
115 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SHARED_SETTINGS_SERVICE_H_