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_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_LEGACY_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 "chrome/browser/supervised_user/legacy/supervised_user_sync_service_observer.h"
15 #include "chrome/browser/supervised_user/supervised_users.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/signin/core/browser/signin_manager_base.h"
18 #include "sync/api/syncable_service.h"
21 class DictionaryValue
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
31 class SupervisedUserSyncService
: public KeyedService
,
32 public SigninManagerBase::Observer
,
33 public syncer::SyncableService
{
35 // For use with GetSupervisedUsersAsync() below.
36 typedef base::Callback
<void(const base::DictionaryValue
*)>
37 SupervisedUsersCallback
;
39 // Dictionary keys for entry values of |prefs::kSupervisedUsers|.
40 static const char kAcknowledged
[];
41 static const char kChromeAvatar
[];
42 static const char kChromeOsAvatar
[];
43 static const char kMasterKey
[];
44 static const char kPasswordSignatureKey
[];
45 static const char kPasswordEncryptionKey
[];
46 static const char kName
[];
48 // Represents a non-existing avatar on Chrome and Chrome OS.
49 static const int kNoAvatar
;
51 ~SupervisedUserSyncService() override
;
53 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
55 // Extracts the avatar index from the input |avatar_str| and set
56 // |avatar_index| to hold the extracted value. Returns true if the
57 // index was extracted successfully and false otherwise.
58 // |avatar_str| should have the format: "chrome-avatar-index:INDEX"
59 // where INDEX is the integer to be extracted. |avatar_str| can be empty
60 // in case there is no avatar synced for a supervised user in which case
61 // |avatar_index| is set to -1.
62 static bool GetAvatarIndex(const std::string
& avatar_str
,
65 // Given an |avatar_index|, it returns a string of the form:
66 // "chrome-avatar-index:INDEX" where INDEX = |avatar_index|.
67 // It is exposed for testing purposes only.
68 static std::string
BuildAvatarString(int avatar_index
);
70 void AddObserver(SupervisedUserSyncServiceObserver
* observer
);
71 void RemoveObserver(SupervisedUserSyncServiceObserver
* observer
);
73 void AddSupervisedUser(const std::string
& id
,
74 const std::string
& name
,
75 const std::string
& master_key
,
76 const std::string
& signature_key
,
77 const std::string
& encryption_key
,
79 void UpdateSupervisedUser(const std::string
& id
,
80 const std::string
& name
,
81 const std::string
& master_key
,
82 const std::string
& signature_key
,
83 const std::string
& encryption_key
,
86 void DeleteSupervisedUser(const std::string
& id
);
88 // Updates the supervised user avatar only if the supervised user has
89 // no avatar and |avatar_index| is set to some value other than
90 // |kNoAvatar|. If |avatar_index| equals |kNoAvatar| and the
91 // supervised user has an avatar, it will be cleared. However,
92 // to clear an avatar call the convenience method
93 // |ClearSupervisedUserAvatar()|.
94 // Returns true if the avatar value is changed (either updated or cleared)
95 // and false otherwise.
96 bool UpdateSupervisedUserAvatarIfNeeded(const std::string
& id
,
98 void ClearSupervisedUserAvatar(const std::string
& id
);
100 // Returns a dictionary containing all supervised users supervised by this
101 // custodian. This method should only be called once this service has started
102 // syncing supervised users (i.e. has finished its initial merge of local and
103 // server-side data, via MergeDataAndStartSyncing), as the stored data might
104 // be outdated before that.
105 const base::DictionaryValue
* GetSupervisedUsers();
107 // Calls the passed |callback| with a dictionary containing all supervised
108 // users managed by this custodian.
109 void GetSupervisedUsersAsync(const SupervisedUsersCallback
& callback
);
111 // KeyedService implementation:
112 void Shutdown() override
;
114 // SyncableService implementation:
115 syncer::SyncMergeResult
MergeDataAndStartSyncing(
116 syncer::ModelType type
,
117 const syncer::SyncDataList
& initial_sync_data
,
118 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
119 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) override
;
120 void StopSyncing(syncer::ModelType type
) override
;
121 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
122 syncer::SyncError
ProcessSyncChanges(
123 const tracked_objects::Location
& from_here
,
124 const syncer::SyncChangeList
& change_list
) override
;
127 friend class SupervisedUserSyncServiceFactory
;
129 // Use |SupervisedUserSyncServiceFactory::GetForProfile(...)| to get an
130 // instance of this service.
131 explicit SupervisedUserSyncService(Profile
* profile
);
133 // SigninManagerBase::Observer implementation.
134 void GoogleSignedOut(const std::string
& account_id
,
135 const std::string
& username
) override
;
137 scoped_ptr
<base::DictionaryValue
> CreateDictionary(
138 const std::string
& name
,
139 const std::string
& master_key
,
140 const std::string
& signature_key
,
141 const std::string
& encryption_key
,
144 void UpdateSupervisedUserImpl(const std::string
& id
,
145 const std::string
& name
,
146 const std::string
& master_key
,
147 const std::string
& signature_key
,
148 const std::string
& encryption_key
,
152 void NotifySupervisedUserAcknowledged(const std::string
& supervised_user_id
);
153 void NotifySupervisedUsersSyncingStopped();
154 void NotifySupervisedUsersChanged();
156 void DispatchCallbacks();
161 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
162 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
164 base::ObserverList
<SupervisedUserSyncServiceObserver
> observers_
;
166 std::vector
<SupervisedUsersCallback
> callbacks_
;
168 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSyncService
);
171 #endif // CHROME_BROWSER_SUPERVISED_USER_LEGACY_SUPERVISED_USER_SYNC_SERVICE_H_