Chromium Code Reviews| 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/strings/stringprintf.h" | |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 12 #include "components/user_prefs/pref_registry_syncable.h" | 13 #include "components/user_prefs/pref_registry_syncable.h" |
| 13 #include "sync/api/sync_change.h" | 14 #include "sync/api/sync_change.h" |
| 14 #include "sync/api/sync_data.h" | 15 #include "sync/api/sync_data.h" |
| 15 #include "sync/api/sync_error.h" | 16 #include "sync/api/sync_error.h" |
| 16 #include "sync/api/sync_error_factory.h" | 17 #include "sync/api/sync_error_factory.h" |
| 17 #include "sync/api/sync_merge_result.h" | 18 #include "sync/api/sync_merge_result.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 29 using syncer::SyncError; | 30 using syncer::SyncError; |
| 30 using syncer::SyncErrorFactory; | 31 using syncer::SyncErrorFactory; |
| 31 using syncer::SyncMergeResult; | 32 using syncer::SyncMergeResult; |
| 32 using sync_pb::ManagedUserSpecifics; | 33 using sync_pb::ManagedUserSpecifics; |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 const char kAcknowledged[] = "acknowledged"; | 37 const char kAcknowledged[] = "acknowledged"; |
| 37 const char kName[] = "name"; | 38 const char kName[] = "name"; |
| 38 const char kMasterKey[] = "masterKey"; | 39 const char kMasterKey[] = "masterKey"; |
| 40 const char kAvatar[] = "avatar"; | |
|
Bernhard Bauer
2013/09/04 12:28:33
Keep these alphabetized please.
ibra
2013/09/04 16:19:45
Done.
| |
| 41 const char kAvatarPrefix[] = "chrome-avatar-index:"; | |
|
Bernhard Bauer
2013/09/04 12:28:33
How would this be used on Chrome OS?
ibra
2013/09/04 16:19:45
My understanding is that when the Chrome OS guys w
Bernhard Bauer
2013/09/04 16:33:10
But this would mean that Chrome on different platf
| |
| 39 | 42 |
| 40 SyncData CreateLocalSyncData(const std::string& id, | 43 SyncData CreateLocalSyncData(const std::string& id, |
| 41 const std::string& name, | 44 const std::string& name, |
| 42 bool acknowledged, | 45 bool acknowledged, |
| 43 const std::string& master_key) { | 46 const std::string& master_key, |
| 47 const std::string& avatar) { | |
| 44 ::sync_pb::EntitySpecifics specifics; | 48 ::sync_pb::EntitySpecifics specifics; |
| 45 specifics.mutable_managed_user()->set_id(id); | 49 specifics.mutable_managed_user()->set_id(id); |
| 46 specifics.mutable_managed_user()->set_name(name); | 50 specifics.mutable_managed_user()->set_name(name); |
| 51 if (!avatar.empty()) | |
| 52 specifics.mutable_managed_user()->set_avatar(avatar); | |
| 47 if (!master_key.empty()) | 53 if (!master_key.empty()) |
| 48 specifics.mutable_managed_user()->set_master_key(master_key); | 54 specifics.mutable_managed_user()->set_master_key(master_key); |
| 49 if (acknowledged) | 55 if (acknowledged) |
| 50 specifics.mutable_managed_user()->set_acknowledged(true); | 56 specifics.mutable_managed_user()->set_acknowledged(true); |
| 51 return SyncData::CreateLocalData(id, name, specifics); | 57 return SyncData::CreateLocalData(id, name, specifics); |
| 52 } | 58 } |
| 53 | 59 |
| 60 SyncData CreateSyncDataFromDictionaryEntry( | |
| 61 const DictionaryValue::Iterator& it) { | |
| 62 const DictionaryValue* dict = NULL; | |
| 63 bool success = it.value().GetAsDictionary(&dict); | |
| 64 DCHECK(success); | |
| 65 bool acknowledged = false; | |
| 66 dict->GetBoolean(kAcknowledged, &acknowledged); | |
| 67 std::string name; | |
| 68 dict->GetString(kName, &name); | |
| 69 DCHECK(!name.empty()); | |
| 70 std::string master_key; | |
| 71 dict->GetString(kMasterKey, &master_key); | |
| 72 std::string avatar; | |
| 73 dict->GetString(kAvatar, &avatar); | |
| 74 | |
| 75 return CreateLocalSyncData(it.key(), name, acknowledged, | |
| 76 master_key, avatar); | |
| 77 } | |
| 78 | |
| 54 } // namespace | 79 } // namespace |
| 55 | 80 |
| 56 ManagedUserSyncService::ManagedUserSyncService(PrefService* prefs) | 81 ManagedUserSyncService::ManagedUserSyncService(PrefService* prefs) |
| 57 : prefs_(prefs) { | 82 : prefs_(prefs) { |
| 58 pref_change_registrar_.Init(prefs_); | 83 pref_change_registrar_.Init(prefs_); |
| 59 pref_change_registrar_.Add( | 84 pref_change_registrar_.Add( |
| 60 prefs::kGoogleServicesLastUsername, | 85 prefs::kGoogleServicesLastUsername, |
| 61 base::Bind(&ManagedUserSyncService::OnLastSignedInUsernameChange, | 86 base::Bind(&ManagedUserSyncService::OnLastSignedInUsernameChange, |
| 62 base::Unretained(this))); | 87 base::Unretained(this))); |
| 63 } | 88 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 77 observers_.AddObserver(observer); | 102 observers_.AddObserver(observer); |
| 78 } | 103 } |
| 79 | 104 |
| 80 void ManagedUserSyncService::RemoveObserver( | 105 void ManagedUserSyncService::RemoveObserver( |
| 81 ManagedUserSyncServiceObserver* observer) { | 106 ManagedUserSyncServiceObserver* observer) { |
| 82 observers_.RemoveObserver(observer); | 107 observers_.RemoveObserver(observer); |
| 83 } | 108 } |
| 84 | 109 |
| 85 void ManagedUserSyncService::AddManagedUser(const std::string& id, | 110 void ManagedUserSyncService::AddManagedUser(const std::string& id, |
| 86 const std::string& name, | 111 const std::string& name, |
| 87 const std::string& master_key) { | 112 const std::string& master_key, |
| 113 int avatar_index) { | |
| 88 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 114 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 89 DictionaryValue* dict = update.Get(); | 115 DictionaryValue* dict = update.Get(); |
| 90 DictionaryValue* value = new DictionaryValue; | 116 DictionaryValue* value = new DictionaryValue; |
| 91 value->SetString(kName, name); | 117 value->SetString(kName, name); |
| 92 value->SetString(kMasterKey, master_key); | 118 value->SetString(kMasterKey, master_key); |
| 119 std::string avatar = base::StringPrintf("%s%d", kAvatarPrefix, avatar_index); | |
|
Bernhard Bauer
2013/09/04 12:28:33
Are you planning to extract the avatar index here
ibra
2013/09/04 16:19:45
The above comment basically replies to this also.
Bernhard Bauer
2013/09/04 16:33:10
Sure, that sounds reasonable. It's not required fo
| |
| 120 value->SetString(kAvatar, avatar); | |
| 93 DCHECK(!dict->HasKey(id)); | 121 DCHECK(!dict->HasKey(id)); |
| 94 dict->SetWithoutPathExpansion(id, value); | 122 dict->SetWithoutPathExpansion(id, value); |
| 95 | 123 |
| 96 if (!sync_processor_) | 124 if (!sync_processor_) |
| 97 return; | 125 return; |
| 98 | 126 |
| 99 // If we're already syncing, create a new change and upload it. | 127 // If we're already syncing, create a new change and upload it. |
| 100 SyncChangeList change_list; | 128 SyncChangeList change_list; |
| 101 change_list.push_back(SyncChange( | 129 change_list.push_back(SyncChange( |
| 102 FROM_HERE, | 130 FROM_HERE, |
| 103 SyncChange::ACTION_ADD, | 131 SyncChange::ACTION_ADD, |
| 104 CreateLocalSyncData(id, name, false, master_key))); | 132 CreateLocalSyncData(id, name, false, master_key, avatar))); |
| 105 SyncError error = | 133 SyncError error = |
| 106 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); | 134 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 107 DCHECK(!error.IsSet()) << error.ToString(); | 135 DCHECK(!error.IsSet()) << error.ToString(); |
| 108 } | 136 } |
| 109 | 137 |
| 110 void ManagedUserSyncService::DeleteManagedUser(const std::string& id) { | 138 void ManagedUserSyncService::DeleteManagedUser(const std::string& id) { |
| 111 if (!sync_processor_) | 139 if (!sync_processor_) |
| 112 return; | 140 return; |
| 113 | 141 |
| 114 SyncChangeList change_list; | 142 SyncChangeList change_list; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 145 int num_items_modified = 0; | 173 int num_items_modified = 0; |
| 146 for (SyncDataList::const_iterator it = initial_sync_data.begin(); | 174 for (SyncDataList::const_iterator it = initial_sync_data.begin(); |
| 147 it != initial_sync_data.end(); ++it) { | 175 it != initial_sync_data.end(); ++it) { |
| 148 DCHECK_EQ(MANAGED_USERS, it->GetDataType()); | 176 DCHECK_EQ(MANAGED_USERS, it->GetDataType()); |
| 149 const ManagedUserSpecifics& managed_user = | 177 const ManagedUserSpecifics& managed_user = |
| 150 it->GetSpecifics().managed_user(); | 178 it->GetSpecifics().managed_user(); |
| 151 DictionaryValue* value = new DictionaryValue(); | 179 DictionaryValue* value = new DictionaryValue(); |
| 152 value->SetString(kName, managed_user.name()); | 180 value->SetString(kName, managed_user.name()); |
| 153 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); | 181 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); |
| 154 value->SetString(kMasterKey, managed_user.master_key()); | 182 value->SetString(kMasterKey, managed_user.master_key()); |
| 183 value->SetString(kAvatar, managed_user.avatar()); | |
| 155 if (dict->HasKey(managed_user.id())) | 184 if (dict->HasKey(managed_user.id())) |
| 156 num_items_modified++; | 185 num_items_modified++; |
| 157 else | 186 else |
| 158 num_items_added++; | 187 num_items_added++; |
| 159 dict->SetWithoutPathExpansion(managed_user.id(), value); | 188 dict->SetWithoutPathExpansion(managed_user.id(), value); |
| 160 seen_ids.insert(managed_user.id()); | 189 seen_ids.insert(managed_user.id()); |
| 161 } | 190 } |
| 162 | 191 |
| 163 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 192 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| 164 if (seen_ids.find(it.key()) != seen_ids.end()) | 193 if (seen_ids.find(it.key()) != seen_ids.end()) |
| 165 continue; | 194 continue; |
| 166 | 195 |
| 167 const DictionaryValue* dict = NULL; | 196 change_list.push_back(SyncChange(FROM_HERE, SyncChange::ACTION_ADD, |
| 168 bool success = it.value().GetAsDictionary(&dict); | 197 CreateSyncDataFromDictionaryEntry(it))); |
| 169 DCHECK(success); | |
| 170 bool acknowledged = false; | |
| 171 dict->GetBoolean(kAcknowledged, &acknowledged); | |
| 172 std::string name; | |
| 173 dict->GetString(kName, &name); | |
| 174 std::string master_key; | |
| 175 dict->GetString(kMasterKey, &master_key); | |
| 176 DCHECK(!name.empty()); | |
| 177 change_list.push_back( | |
| 178 SyncChange(FROM_HERE, SyncChange::ACTION_ADD, | |
| 179 CreateLocalSyncData(it.key(), name, acknowledged, master_key))); | |
| 180 } | 198 } |
| 181 result.set_error(sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); | 199 result.set_error(sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); |
| 182 | 200 |
| 183 result.set_num_items_modified(num_items_modified); | 201 result.set_num_items_modified(num_items_modified); |
| 184 result.set_num_items_added(num_items_added); | 202 result.set_num_items_added(num_items_added); |
| 185 result.set_num_items_after_association(dict->size()); | 203 result.set_num_items_after_association(dict->size()); |
| 186 | 204 |
| 187 return result; | 205 return result; |
| 188 } | 206 } |
| 189 | 207 |
| 190 void ManagedUserSyncService::StopSyncing(ModelType type) { | 208 void ManagedUserSyncService::StopSyncing(ModelType type) { |
| 191 DCHECK_EQ(MANAGED_USERS, type); | 209 DCHECK_EQ(MANAGED_USERS, type); |
| 192 // The observers may want to change the Sync data, so notify them before | 210 // The observers may want to change the Sync data, so notify them before |
| 193 // resetting the |sync_processor_|. | 211 // resetting the |sync_processor_|. |
| 194 NotifyManagedUsersSyncingStopped(); | 212 NotifyManagedUsersSyncingStopped(); |
| 195 sync_processor_.reset(); | 213 sync_processor_.reset(); |
| 196 error_handler_.reset(); | 214 error_handler_.reset(); |
| 197 } | 215 } |
| 198 | 216 |
| 199 SyncDataList ManagedUserSyncService::GetAllSyncData( | 217 SyncDataList ManagedUserSyncService::GetAllSyncData( |
| 200 ModelType type) const { | 218 ModelType type) const { |
| 201 SyncDataList data; | 219 SyncDataList data; |
| 202 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 220 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 203 DictionaryValue* dict = update.Get(); | 221 DictionaryValue* dict = update.Get(); |
| 204 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 222 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) |
| 205 const DictionaryValue* dict = NULL; | 223 data.push_back(CreateSyncDataFromDictionaryEntry(it)); |
| 206 bool success = it.value().GetAsDictionary(&dict); | 224 |
| 207 DCHECK(success); | |
| 208 std::string name; | |
| 209 dict->GetString(kName, &name); | |
| 210 std::string master_key; | |
| 211 dict->GetString(kMasterKey, &master_key); | |
| 212 bool acknowledged = false; | |
| 213 dict->GetBoolean(kAcknowledged, &acknowledged); | |
| 214 data.push_back( | |
| 215 CreateLocalSyncData(it.key(), name, acknowledged, master_key)); | |
| 216 } | |
| 217 return data; | 225 return data; |
| 218 } | 226 } |
| 219 | 227 |
| 220 SyncError ManagedUserSyncService::ProcessSyncChanges( | 228 SyncError ManagedUserSyncService::ProcessSyncChanges( |
| 221 const tracked_objects::Location& from_here, | 229 const tracked_objects::Location& from_here, |
| 222 const SyncChangeList& change_list) { | 230 const SyncChangeList& change_list) { |
| 223 SyncError error; | 231 SyncError error; |
| 224 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 232 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 225 DictionaryValue* dict = update.Get(); | 233 DictionaryValue* dict = update.Get(); |
| 226 for (SyncChangeList::const_iterator it = change_list.begin(); | 234 for (SyncChangeList::const_iterator it = change_list.begin(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 245 | 253 |
| 246 // If the managed user switched from unacknowledged to acknowledged, | 254 // If the managed user switched from unacknowledged to acknowledged, |
| 247 // we might need to continue with a registration. | 255 // we might need to continue with a registration. |
| 248 if (old_value && !old_value->HasKey(kAcknowledged)) | 256 if (old_value && !old_value->HasKey(kAcknowledged)) |
| 249 NotifyManagedUserAcknowledged(managed_user.id()); | 257 NotifyManagedUserAcknowledged(managed_user.id()); |
| 250 | 258 |
| 251 DictionaryValue* value = new DictionaryValue; | 259 DictionaryValue* value = new DictionaryValue; |
| 252 value->SetString(kName, managed_user.name()); | 260 value->SetString(kName, managed_user.name()); |
| 253 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); | 261 value->SetBoolean(kAcknowledged, managed_user.acknowledged()); |
| 254 value->SetString(kMasterKey, managed_user.master_key()); | 262 value->SetString(kMasterKey, managed_user.master_key()); |
| 263 value->SetString(kAvatar, managed_user.avatar()); | |
| 255 dict->SetWithoutPathExpansion(managed_user.id(), value); | 264 dict->SetWithoutPathExpansion(managed_user.id(), value); |
| 256 break; | 265 break; |
| 257 } | 266 } |
| 258 case SyncChange::ACTION_DELETE: { | 267 case SyncChange::ACTION_DELETE: { |
| 259 DCHECK(dict->HasKey(managed_user.id())) << managed_user.id(); | 268 DCHECK(dict->HasKey(managed_user.id())) << managed_user.id(); |
| 260 dict->RemoveWithoutPathExpansion(managed_user.id(), NULL); | 269 dict->RemoveWithoutPathExpansion(managed_user.id(), NULL); |
| 261 break; | 270 break; |
| 262 } | 271 } |
| 263 case SyncChange::ACTION_INVALID: { | 272 case SyncChange::ACTION_INVALID: { |
| 264 NOTREACHED(); | 273 NOTREACHED(); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 280 void ManagedUserSyncService::NotifyManagedUserAcknowledged( | 289 void ManagedUserSyncService::NotifyManagedUserAcknowledged( |
| 281 const std::string& managed_user_id) { | 290 const std::string& managed_user_id) { |
| 282 FOR_EACH_OBSERVER(ManagedUserSyncServiceObserver, observers_, | 291 FOR_EACH_OBSERVER(ManagedUserSyncServiceObserver, observers_, |
| 283 OnManagedUserAcknowledged(managed_user_id)); | 292 OnManagedUserAcknowledged(managed_user_id)); |
| 284 } | 293 } |
| 285 | 294 |
| 286 void ManagedUserSyncService::NotifyManagedUsersSyncingStopped() { | 295 void ManagedUserSyncService::NotifyManagedUsersSyncingStopped() { |
| 287 FOR_EACH_OBSERVER(ManagedUserSyncServiceObserver, observers_, | 296 FOR_EACH_OBSERVER(ManagedUserSyncServiceObserver, observers_, |
| 288 OnManagedUsersSyncingStopped()); | 297 OnManagedUsersSyncingStopped()); |
| 289 } | 298 } |
| OLD | NEW |