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 ~SupervisedUserSyncService() override;
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 void Shutdown() override;
112 // SyncableService implementation:
113 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 void StopSyncing(syncer::ModelType type) override;
119 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
120 syncer::SyncError ProcessSyncChanges(
121 const tracked_objects::Location& from_here,
122 const syncer::SyncChangeList& change_list) override;
125 friend class SupervisedUserSyncServiceFactory;
127 // Use |SupervisedUserSyncServiceFactory::GetForProfile(...)| to get an
128 // instance of this service.
129 explicit SupervisedUserSyncService(PrefService* prefs);
131 void OnLastSignedInUsernameChange();
133 scoped_ptr<base::DictionaryValue> CreateDictionary(
134 const std::string& name,
135 const std::string& master_key,
136 const std::string& signature_key,
137 const std::string& encryption_key,
140 void UpdateSupervisedUserImpl(const std::string& id,
141 const std::string& name,
142 const std::string& master_key,
143 const std::string& signature_key,
144 const std::string& encryption_key,
148 void NotifySupervisedUserAcknowledged(const std::string& supervised_user_id);
149 void NotifySupervisedUsersSyncingStopped();
150 void NotifySupervisedUsersChanged();
152 void DispatchCallbacks();
155 PrefChangeRegistrar pref_change_registrar_;
157 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
158 scoped_ptr<syncer::SyncErrorFactory> error_handler_;
160 ObserverList<SupervisedUserSyncServiceObserver> observers_;
162 std::vector<SupervisedUsersCallback> callbacks_;
164 DISALLOW_COPY_AND_ASSIGN(SupervisedUserSyncService);
167 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SYNC_SERVICE_H_