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_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SYNC_SERVICE_H_
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "chrome/browser/supervised_user/supervised_user_sync_service_observer.h"
16 #include "chrome/browser/supervised_user/supervised_users.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "sync/api/syncable_service.h"
21 class DictionaryValue
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
30 class SupervisedUserSyncService
: public KeyedService
,
31 public syncer::SyncableService
{
33 // For use with GetSupervisedUsersAsync() below.
34 typedef base::Callback
<void(const base::DictionaryValue
*)>
35 SupervisedUsersCallback
;
37 // Dictionary keys for entry values of |prefs::kSupervisedUsers|.
38 static const char kAcknowledged
[];
39 static const char kChromeAvatar
[];
40 static const char kChromeOsAvatar
[];
41 static const char kMasterKey
[];
42 static const char kPasswordSignatureKey
[];
43 static const char kPasswordEncryptionKey
[];
44 static const char kName
[];
46 // Represents a non-existing avatar on Chrome and Chrome OS.
47 static const int kNoAvatar
;
49 virtual ~SupervisedUserSyncService();
51 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
53 // Extracts the avatar index from the input |avatar_str| and set
54 // |avatar_index| to hold the extracted value. Returns true if the
55 // index was extracted successfully and false otherwise.
56 // |avatar_str| should have the format: "chrome-avatar-index:INDEX"
57 // where INDEX is the integer to be extracted. |avatar_str| can be empty
58 // in case there is no avatar synced for a supervised user in which case
59 // |avatar_index| is set to -1.
60 static bool GetAvatarIndex(const std::string
& avatar_str
,
63 // Given an |avatar_index|, it returns a string of the form:
64 // "chrome-avatar-index:INDEX" where INDEX = |avatar_index|.
65 // It is exposed for testing purposes only.
66 static std::string
BuildAvatarString(int avatar_index
);
68 void AddObserver(SupervisedUserSyncServiceObserver
* observer
);
69 void RemoveObserver(SupervisedUserSyncServiceObserver
* observer
);
71 void AddSupervisedUser(const std::string
& id
,
72 const std::string
& name
,
73 const std::string
& master_key
,
74 const std::string
& signature_key
,
75 const std::string
& encryption_key
,
77 void UpdateSupervisedUser(const std::string
& id
,
78 const std::string
& name
,
79 const std::string
& master_key
,
80 const std::string
& signature_key
,
81 const std::string
& encryption_key
,
84 void DeleteSupervisedUser(const std::string
& id
);
86 // Updates the supervised user avatar only if the supervised user has
87 // no avatar and |avatar_index| is set to some value other than
88 // |kNoAvatar|. If |avatar_index| equals |kNoAvatar| and the
89 // supervised user has an avatar, it will be cleared. However,
90 // to clear an avatar call the convenience method
91 // |ClearSupervisedUserAvatar()|.
92 // Returns true if the avatar value is changed (either updated or cleared)
93 // and false otherwise.
94 bool UpdateSupervisedUserAvatarIfNeeded(const std::string
& id
,
96 void ClearSupervisedUserAvatar(const std::string
& id
);
98 // Returns a dictionary containing all supervised users supervised by this
99 // custodian. This method should only be called once this service has started
100 // syncing supervised users (i.e. has finished its initial merge of local and
101 // server-side data, via MergeDataAndStartSyncing), as the stored data might
102 // be outdated before that.
103 const base::DictionaryValue
* GetSupervisedUsers();
105 // Calls the passed |callback| with a dictionary containing all supervised
106 // users managed by this custodian.
107 void GetSupervisedUsersAsync(const SupervisedUsersCallback
& callback
);
109 // KeyedService implementation:
110 virtual void Shutdown() override
;
112 // SyncableService implementation:
113 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
114 syncer::ModelType type
,
115 const syncer::SyncDataList
& initial_sync_data
,
116 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
117 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) override
;
118 virtual void StopSyncing(syncer::ModelType type
) override
;
119 virtual syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const
121 virtual syncer::SyncError
ProcessSyncChanges(
122 const tracked_objects::Location
& from_here
,
123 const syncer::SyncChangeList
& change_list
) override
;
126 friend class SupervisedUserSyncServiceFactory
;
128 // Use |SupervisedUserSyncServiceFactory::GetForProfile(...)| to get an
129 // instance of this service.
130 explicit SupervisedUserSyncService(PrefService
* prefs
);
132 void OnLastSignedInUsernameChange();
134 scoped_ptr
<base::DictionaryValue
> CreateDictionary(
135 const std::string
& name
,
136 const std::string
& master_key
,
137 const std::string
& signature_key
,
138 const std::string
& encryption_key
,
141 void UpdateSupervisedUserImpl(const std::string
& id
,
142 const std::string
& name
,
143 const std::string
& master_key
,
144 const std::string
& signature_key
,
145 const std::string
& encryption_key
,
149 void NotifySupervisedUserAcknowledged(const std::string
& supervised_user_id
);
150 void NotifySupervisedUsersSyncingStopped();
151 void NotifySupervisedUsersChanged();
153 void DispatchCallbacks();
156 PrefChangeRegistrar pref_change_registrar_
;
158 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
159 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
161 ObserverList
<SupervisedUserSyncServiceObserver
> observers_
;
163 std::vector
<SupervisedUsersCallback
> callbacks_
;
165 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSyncService
);
168 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SYNC_SERVICE_H_