| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/managed_mode/managed_user_sync_service.h" | 5 #include "chrome/browser/managed_mode/managed_user_sync_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 11 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "components/user_prefs/pref_registry_syncable.h" | 14 #include "components/user_prefs/pref_registry_syncable.h" |
| 14 #include "sync/api/sync_change.h" | 15 #include "sync/api/sync_change.h" |
| 15 #include "sync/api/sync_data.h" | 16 #include "sync/api/sync_data.h" |
| 16 #include "sync/api/sync_error.h" | 17 #include "sync/api/sync_error.h" |
| 17 #include "sync/api/sync_error_factory.h" | 18 #include "sync/api/sync_error_factory.h" |
| 18 #include "sync/api/sync_merge_result.h" | 19 #include "sync/api/sync_merge_result.h" |
| 19 #include "sync/protocol/sync.pb.h" | 20 #include "sync/protocol/sync.pb.h" |
| 20 | 21 |
| 21 using base::DictionaryValue; | 22 using base::DictionaryValue; |
| 22 using user_prefs::PrefRegistrySyncable; | 23 using user_prefs::PrefRegistrySyncable; |
| 23 using syncer::MANAGED_USERS; | 24 using syncer::MANAGED_USERS; |
| 24 using syncer::ModelType; | 25 using syncer::ModelType; |
| 25 using syncer::SyncChange; | 26 using syncer::SyncChange; |
| 26 using syncer::SyncChangeList; | 27 using syncer::SyncChangeList; |
| 27 using syncer::SyncChangeProcessor; | 28 using syncer::SyncChangeProcessor; |
| 28 using syncer::SyncData; | 29 using syncer::SyncData; |
| 29 using syncer::SyncDataList; | 30 using syncer::SyncDataList; |
| 30 using syncer::SyncError; | 31 using syncer::SyncError; |
| 31 using syncer::SyncErrorFactory; | 32 using syncer::SyncErrorFactory; |
| 32 using syncer::SyncMergeResult; | 33 using syncer::SyncMergeResult; |
| 33 using sync_pb::ManagedUserSpecifics; | 34 using sync_pb::ManagedUserSpecifics; |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const char kAcknowledged[] = "acknowledged"; | 38 const char kAcknowledged[] = "acknowledged"; |
| 39 const char kChromeAvatar[] = "chromeAvatar"; |
| 40 const char kChromeOsAvatar[] = "chromeOsAvatar"; |
| 38 const char kName[] = "name"; | 41 const char kName[] = "name"; |
| 39 const char kMasterKey[] = "masterKey"; | 42 const char kMasterKey[] = "masterKey"; |
| 40 | 43 |
| 41 SyncData CreateLocalSyncData(const std::string& id, | 44 SyncData CreateLocalSyncData(const std::string& id, |
| 42 const std::string& name, | 45 const std::string& name, |
| 43 bool acknowledged, | 46 bool acknowledged, |
| 44 const std::string& master_key) { | 47 const std::string& master_key, |
| 48 const std::string& chrome_avatar, |
| 49 const std::string& chromeos_avatar) { |
| 45 ::sync_pb::EntitySpecifics specifics; | 50 ::sync_pb::EntitySpecifics specifics; |
| 46 specifics.mutable_managed_user()->set_id(id); | 51 specifics.mutable_managed_user()->set_id(id); |
| 47 specifics.mutable_managed_user()->set_name(name); | 52 specifics.mutable_managed_user()->set_name(name); |
| 53 if (!chrome_avatar.empty()) |
| 54 specifics.mutable_managed_user()->set_chrome_avatar(chrome_avatar); |
| 55 if (!chromeos_avatar.empty()) |
| 56 specifics.mutable_managed_user()->set_chromeos_avatar(chromeos_avatar); |
| 48 if (!master_key.empty()) | 57 if (!master_key.empty()) |
| 49 specifics.mutable_managed_user()->set_master_key(master_key); | 58 specifics.mutable_managed_user()->set_master_key(master_key); |
| 50 if (acknowledged) | 59 if (acknowledged) |
| 51 specifics.mutable_managed_user()->set_acknowledged(true); | 60 specifics.mutable_managed_user()->set_acknowledged(true); |
| 52 return SyncData::CreateLocalData(id, name, specifics); | 61 return SyncData::CreateLocalData(id, name, specifics); |
| 53 } | 62 } |
| 54 | 63 |
| 64 SyncData CreateSyncDataFromDictionaryEntry( |
| 65 const DictionaryValue::Iterator& it) { |
| 66 const DictionaryValue* dict = NULL; |
| 67 bool success = it.value().GetAsDictionary(&dict); |
| 68 DCHECK(success); |
| 69 bool acknowledged = false; |
| 70 dict->GetBoolean(kAcknowledged, &acknowledged); |
| 71 std::string name; |
| 72 dict->GetString(kName, &name); |
| 73 DCHECK(!name.empty()); |
| 74 std::string master_key; |
| 75 dict->GetString(kMasterKey, &master_key); |
| 76 std::string chrome_avatar; |
| 77 dict->GetString(kChromeAvatar, &chrome_avatar); |
| 78 std::string chromeos_avatar; |
| 79 dict->GetString(kChromeOsAvatar, &chromeos_avatar); |
| 80 |
| 81 return CreateLocalSyncData(it.key(), name, acknowledged, master_key, |
| 82 chrome_avatar, chromeos_avatar); |
| 83 } |
| 84 |
| 55 } // namespace | 85 } // namespace |
| 56 | 86 |
| 57 ManagedUserSyncService::ManagedUserSyncService(PrefService* prefs) | 87 ManagedUserSyncService::ManagedUserSyncService(PrefService* prefs) |
| 58 : prefs_(prefs) { | 88 : prefs_(prefs) { |
| 59 pref_change_registrar_.Init(prefs_); | 89 pref_change_registrar_.Init(prefs_); |
| 60 pref_change_registrar_.Add( | 90 pref_change_registrar_.Add( |
| 61 prefs::kGoogleServicesLastUsername, | 91 prefs::kGoogleServicesLastUsername, |
| 62 base::Bind(&ManagedUserSyncService::OnLastSignedInUsernameChange, | 92 base::Bind(&ManagedUserSyncService::OnLastSignedInUsernameChange, |
| 63 base::Unretained(this))); | 93 base::Unretained(this))); |
| 64 } | 94 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 observers_.AddObserver(observer); | 108 observers_.AddObserver(observer); |
| 79 } | 109 } |
| 80 | 110 |
| 81 void ManagedUserSyncService::RemoveObserver( | 111 void ManagedUserSyncService::RemoveObserver( |
| 82 ManagedUserSyncServiceObserver* observer) { | 112 ManagedUserSyncServiceObserver* observer) { |
| 83 observers_.RemoveObserver(observer); | 113 observers_.RemoveObserver(observer); |
| 84 } | 114 } |
| 85 | 115 |
| 86 void ManagedUserSyncService::AddManagedUser(const std::string& id, | 116 void ManagedUserSyncService::AddManagedUser(const std::string& id, |
| 87 const std::string& name, | 117 const std::string& name, |
| 88 const std::string& master_key) { | 118 const std::string& master_key, |
| 119 int avatar_index) { |
| 89 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 120 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 90 DictionaryValue* dict = update.Get(); | 121 DictionaryValue* dict = update.Get(); |
| 91 DictionaryValue* value = new DictionaryValue; | 122 DictionaryValue* value = new DictionaryValue; |
| 92 value->SetString(kName, name); | 123 value->SetString(kName, name); |
| 93 value->SetString(kMasterKey, master_key); | 124 value->SetString(kMasterKey, master_key); |
| 125 std::string chrome_avatar; |
| 126 #if defined(CHROME_OS) |
| 127 // This is a dummy value that is passed when a supervised user is created on |
| 128 // Chrome OS. |
| 129 // TODO(ibraaaa): update this to use the correct avatar index |
| 130 // once avatar syncing for supervised users is implemented on Chrome OS. |
| 131 DCHECK_EQ(avatar_index, -111); |
| 132 #else |
| 133 chrome_avatar = base::StringPrintf("chrome-avatar-index:%d", avatar_index); |
| 134 #endif |
| 135 value->SetString(kChromeAvatar, chrome_avatar); |
| 136 // TODO(ibraaaa): this should be updated to allow supervised |
| 137 // users avatar syncing on Chrome OS. |
| 138 value->SetString(kChromeOsAvatar, std::string()); |
| 94 DCHECK(!dict->HasKey(id)); | 139 DCHECK(!dict->HasKey(id)); |
| 95 dict->SetWithoutPathExpansion(id, value); | 140 dict->SetWithoutPathExpansion(id, value); |
| 96 | 141 |
| 97 if (!sync_processor_) | 142 if (!sync_processor_) |
| 98 return; | 143 return; |
| 99 | 144 |
| 100 // If we're already syncing, create a new change and upload it. | 145 // If we're already syncing, create a new change and upload it. |
| 101 SyncChangeList change_list; | 146 SyncChangeList change_list; |
| 102 change_list.push_back(SyncChange( | 147 change_list.push_back(SyncChange( |
| 103 FROM_HERE, | 148 FROM_HERE, |
| 104 SyncChange::ACTION_ADD, | 149 SyncChange::ACTION_ADD, |
| 105 CreateLocalSyncData(id, name, false, master_key))); | 150 CreateLocalSyncData(id, name, false, master_key, |
| 151 chrome_avatar, std::string()))); |
| 106 SyncError error = | 152 SyncError error = |
| 107 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); | 153 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 108 DCHECK(!error.IsSet()) << error.ToString(); | 154 DCHECK(!error.IsSet()) << error.ToString(); |
| 109 } | 155 } |
| 110 | 156 |
| 111 void ManagedUserSyncService::DeleteManagedUser(const std::string& id) { | 157 void ManagedUserSyncService::DeleteManagedUser(const std::string& id) { |
| 112 if (!sync_processor_) | 158 if (!sync_processor_) |
| 113 return; | 159 return; |
| 114 | 160 |
| 115 SyncChangeList change_list; | 161 SyncChangeList change_list; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 int num_items_modified = 0; | 209 int num_items_modified = 0; |
| 164 for (SyncDataList::const_iterator it = initial_sync_data.begin(); | 210 for (SyncDataList::const_iterator it = initial_sync_data.begin(); |
| 165 it != initial_sync_data.end(); ++it) { | 211 it != initial_sync_data.end(); ++it) { |
| 166 DCHECK_EQ(MANAGED_USERS, it->GetDataType()); | 212 DCHECK_EQ(MANAGED_USERS, it->GetDataType()); |
| 167 const ManagedUserSpecifics& managed_user = | 213 const ManagedUserSpecifics& managed_user = |
| 168 it->GetSpecifics().managed_user(); | 214 it->GetSpecifics().managed_user(); |
| 169 DictionaryValue* value = new DictionaryValue(); | 215 DictionaryValue* value = new DictionaryValue(); |
| 170 value->SetString(kName, managed_user.name()); | 216 value->SetString(kName, managed_user.name()); |
| 171 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); | 217 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); |
| 172 value->SetString(kMasterKey, managed_user.master_key()); | 218 value->SetString(kMasterKey, managed_user.master_key()); |
| 219 value->SetString(kChromeAvatar, managed_user.chrome_avatar()); |
| 220 value->SetString(kChromeOsAvatar, managed_user.chromeos_avatar()); |
| 173 if (dict->HasKey(managed_user.id())) | 221 if (dict->HasKey(managed_user.id())) |
| 174 num_items_modified++; | 222 num_items_modified++; |
| 175 else | 223 else |
| 176 num_items_added++; | 224 num_items_added++; |
| 177 dict->SetWithoutPathExpansion(managed_user.id(), value); | 225 dict->SetWithoutPathExpansion(managed_user.id(), value); |
| 178 seen_ids.insert(managed_user.id()); | 226 seen_ids.insert(managed_user.id()); |
| 179 } | 227 } |
| 180 | 228 |
| 181 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 229 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| 182 if (seen_ids.find(it.key()) != seen_ids.end()) | 230 if (seen_ids.find(it.key()) != seen_ids.end()) |
| 183 continue; | 231 continue; |
| 184 | 232 |
| 185 const DictionaryValue* dict = NULL; | 233 change_list.push_back(SyncChange(FROM_HERE, SyncChange::ACTION_ADD, |
| 186 bool success = it.value().GetAsDictionary(&dict); | 234 CreateSyncDataFromDictionaryEntry(it))); |
| 187 DCHECK(success); | |
| 188 bool acknowledged = false; | |
| 189 dict->GetBoolean(kAcknowledged, &acknowledged); | |
| 190 std::string name; | |
| 191 dict->GetString(kName, &name); | |
| 192 std::string master_key; | |
| 193 dict->GetString(kMasterKey, &master_key); | |
| 194 DCHECK(!name.empty()); | |
| 195 change_list.push_back( | |
| 196 SyncChange(FROM_HERE, SyncChange::ACTION_ADD, | |
| 197 CreateLocalSyncData(it.key(), name, acknowledged, master_key))); | |
| 198 } | 235 } |
| 199 result.set_error(sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); | 236 result.set_error(sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); |
| 200 | 237 |
| 201 result.set_num_items_modified(num_items_modified); | 238 result.set_num_items_modified(num_items_modified); |
| 202 result.set_num_items_added(num_items_added); | 239 result.set_num_items_added(num_items_added); |
| 203 result.set_num_items_after_association(dict->size()); | 240 result.set_num_items_after_association(dict->size()); |
| 204 | 241 |
| 205 DispatchCallbacks(); | 242 DispatchCallbacks(); |
| 206 | 243 |
| 207 return result; | 244 return result; |
| 208 } | 245 } |
| 209 | 246 |
| 210 void ManagedUserSyncService::StopSyncing(ModelType type) { | 247 void ManagedUserSyncService::StopSyncing(ModelType type) { |
| 211 DCHECK_EQ(MANAGED_USERS, type); | 248 DCHECK_EQ(MANAGED_USERS, type); |
| 212 // The observers may want to change the Sync data, so notify them before | 249 // The observers may want to change the Sync data, so notify them before |
| 213 // resetting the |sync_processor_|. | 250 // resetting the |sync_processor_|. |
| 214 NotifyManagedUsersSyncingStopped(); | 251 NotifyManagedUsersSyncingStopped(); |
| 215 sync_processor_.reset(); | 252 sync_processor_.reset(); |
| 216 error_handler_.reset(); | 253 error_handler_.reset(); |
| 217 } | 254 } |
| 218 | 255 |
| 219 SyncDataList ManagedUserSyncService::GetAllSyncData( | 256 SyncDataList ManagedUserSyncService::GetAllSyncData( |
| 220 ModelType type) const { | 257 ModelType type) const { |
| 221 SyncDataList data; | 258 SyncDataList data; |
| 222 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 259 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 223 DictionaryValue* dict = update.Get(); | 260 DictionaryValue* dict = update.Get(); |
| 224 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 261 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) |
| 225 const DictionaryValue* dict = NULL; | 262 data.push_back(CreateSyncDataFromDictionaryEntry(it)); |
| 226 bool success = it.value().GetAsDictionary(&dict); | 263 |
| 227 DCHECK(success); | |
| 228 std::string name; | |
| 229 dict->GetString(kName, &name); | |
| 230 std::string master_key; | |
| 231 dict->GetString(kMasterKey, &master_key); | |
| 232 bool acknowledged = false; | |
| 233 dict->GetBoolean(kAcknowledged, &acknowledged); | |
| 234 data.push_back( | |
| 235 CreateLocalSyncData(it.key(), name, acknowledged, master_key)); | |
| 236 } | |
| 237 return data; | 264 return data; |
| 238 } | 265 } |
| 239 | 266 |
| 240 SyncError ManagedUserSyncService::ProcessSyncChanges( | 267 SyncError ManagedUserSyncService::ProcessSyncChanges( |
| 241 const tracked_objects::Location& from_here, | 268 const tracked_objects::Location& from_here, |
| 242 const SyncChangeList& change_list) { | 269 const SyncChangeList& change_list) { |
| 243 SyncError error; | 270 SyncError error; |
| 244 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 271 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 245 DictionaryValue* dict = update.Get(); | 272 DictionaryValue* dict = update.Get(); |
| 246 for (SyncChangeList::const_iterator it = change_list.begin(); | 273 for (SyncChangeList::const_iterator it = change_list.begin(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 265 | 292 |
| 266 // If the managed user switched from unacknowledged to acknowledged, | 293 // If the managed user switched from unacknowledged to acknowledged, |
| 267 // we might need to continue with a registration. | 294 // we might need to continue with a registration. |
| 268 if (old_value && !old_value->HasKey(kAcknowledged)) | 295 if (old_value && !old_value->HasKey(kAcknowledged)) |
| 269 NotifyManagedUserAcknowledged(managed_user.id()); | 296 NotifyManagedUserAcknowledged(managed_user.id()); |
| 270 | 297 |
| 271 DictionaryValue* value = new DictionaryValue; | 298 DictionaryValue* value = new DictionaryValue; |
| 272 value->SetString(kName, managed_user.name()); | 299 value->SetString(kName, managed_user.name()); |
| 273 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); | 300 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); |
| 274 value->SetString(kMasterKey, managed_user.master_key()); | 301 value->SetString(kMasterKey, managed_user.master_key()); |
| 302 value->SetString(kChromeAvatar, managed_user.chrome_avatar()); |
| 303 value->SetString(kChromeOsAvatar, managed_user.chromeos_avatar()); |
| 275 dict->SetWithoutPathExpansion(managed_user.id(), value); | 304 dict->SetWithoutPathExpansion(managed_user.id(), value); |
| 276 break; | 305 break; |
| 277 } | 306 } |
| 278 case SyncChange::ACTION_DELETE: { | 307 case SyncChange::ACTION_DELETE: { |
| 279 DCHECK(dict->HasKey(managed_user.id())) << managed_user.id(); | 308 DCHECK(dict->HasKey(managed_user.id())) << managed_user.id(); |
| 280 dict->RemoveWithoutPathExpansion(managed_user.id(), NULL); | 309 dict->RemoveWithoutPathExpansion(managed_user.id(), NULL); |
| 281 break; | 310 break; |
| 282 } | 311 } |
| 283 case SyncChange::ACTION_INVALID: { | 312 case SyncChange::ACTION_INVALID: { |
| 284 NOTREACHED(); | 313 NOTREACHED(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 310 | 339 |
| 311 void ManagedUserSyncService::DispatchCallbacks() { | 340 void ManagedUserSyncService::DispatchCallbacks() { |
| 312 const DictionaryValue* managed_users = | 341 const DictionaryValue* managed_users = |
| 313 prefs_->GetDictionary(prefs::kManagedUsers); | 342 prefs_->GetDictionary(prefs::kManagedUsers); |
| 314 for (std::vector<ManagedUsersCallback>::iterator it = callbacks_.begin(); | 343 for (std::vector<ManagedUsersCallback>::iterator it = callbacks_.begin(); |
| 315 it != callbacks_.end(); ++it) { | 344 it != callbacks_.end(); ++it) { |
| 316 it->Run(managed_users); | 345 it->Run(managed_users); |
| 317 } | 346 } |
| 318 callbacks_.clear(); | 347 callbacks_.clear(); |
| 319 } | 348 } |
| OLD | NEW |