OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/syncable_extension_settings_storage.h" | 5 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/stringprintf.h" | |
8 #include "chrome/browser/extensions/extension_settings_sync_util.h" | 9 #include "chrome/browser/extensions/extension_settings_sync_util.h" |
9 #include "chrome/browser/sync/api/sync_data.h" | 10 #include "chrome/browser/sync/api/sync_data.h" |
10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" | 11 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" |
11 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
12 | 13 |
13 SyncableExtensionSettingsStorage::SyncableExtensionSettingsStorage( | 14 SyncableExtensionSettingsStorage::SyncableExtensionSettingsStorage( |
14 std::string extension_id, ExtensionSettingsStorage* delegate) | 15 const scoped_refptr<ObserverListThreadSafe<ExtensionSettingsObserver> >& |
15 : extension_id_(extension_id), delegate_(delegate), sync_processor_(NULL) { | 16 observers, |
17 const std::string& extension_id, | |
18 ExtensionSettingsStorage* delegate) | |
19 : observers_(observers), | |
20 extension_id_(extension_id), | |
21 delegate_(delegate), | |
22 sync_processor_(NULL) { | |
16 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
17 } | 24 } |
18 | 25 |
19 SyncableExtensionSettingsStorage::~SyncableExtensionSettingsStorage() { | 26 SyncableExtensionSettingsStorage::~SyncableExtensionSettingsStorage() { |
20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
21 } | 28 } |
22 | 29 |
23 ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get( | 30 ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get( |
24 const std::string& key) { | 31 const std::string& key) { |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 } | 225 } |
219 | 226 |
220 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( | 227 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( |
221 const ExtensionSettingSyncDataList& sync_changes) { | 228 const ExtensionSettingSyncDataList& sync_changes) { |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
223 DCHECK(sync_processor_); | 230 DCHECK(sync_processor_); |
224 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; | 231 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; |
225 | 232 |
226 std::vector<SyncError> errors; | 233 std::vector<SyncError> errors; |
227 | 234 |
235 // Change event to build then send to onChanged listeners of the settings API. | |
236 ExtensionSettingsObserver::ChangeList change_event; | |
237 | |
228 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); | 238 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); |
229 it != sync_changes.end(); ++it) { | 239 it != sync_changes.end(); ++it) { |
230 DCHECK_EQ(extension_id_, it->extension_id()); | 240 DCHECK_EQ(extension_id_, it->extension_id()); |
241 | |
242 scoped_ptr<Value> current_value; | |
243 { | |
244 Result maybe_settings = Get(it->key()); | |
245 if (maybe_settings.HasError()) { | |
246 errors.push_back(SyncError( | |
247 FROM_HERE, | |
248 base::StringPrintf( | |
249 "Error getting current sync state for %s/%s: %s", | |
250 extension_id_.c_str(), | |
251 it->key().c_str(), | |
252 maybe_settings.GetError().c_str()), | |
253 syncable::EXTENSION_SETTINGS)); | |
254 continue; | |
255 } | |
256 if (maybe_settings.GetSettings() != NULL) { | |
257 Value* value_ownership = NULL; | |
258 maybe_settings.GetSettings()->RemoveWithoutPathExpansion( | |
259 it->key(), &value_ownership); | |
260 current_value.reset(value_ownership); | |
261 } | |
262 } | |
263 | |
231 switch (it->change_type()) { | 264 switch (it->change_type()) { |
232 case SyncChange::ACTION_ADD: | 265 case SyncChange::ACTION_ADD: { |
233 case SyncChange::ACTION_UPDATE: { | 266 if (current_value.get()) { |
267 errors.push_back(SyncError( | |
268 FROM_HERE, | |
269 base::StringPrintf( | |
270 "Got add from sync for existing setting %s/%s", | |
akalin
2011/10/12 22:26:30
unnecessary use of StringPrintf; just use strings
not at google - send to devlin
2011/10/13 06:40:43
...
So, reverting this back to the way it was. E
| |
271 extension_id_.c_str(), | |
272 it->key().c_str()), | |
273 syncable::EXTENSION_SETTINGS)); | |
274 break; | |
275 } | |
276 | |
234 synced_keys_.insert(it->key()); | 277 synced_keys_.insert(it->key()); |
235 Result result = delegate_->Set(it->key(), it->value()); | 278 Result result = delegate_->Set(it->key(), it->value()); |
236 if (result.HasError()) { | 279 if (result.HasError()) { |
237 errors.push_back(SyncError( | 280 errors.push_back(SyncError( |
238 FROM_HERE, | 281 FROM_HERE, |
239 std::string("Error pushing sync change to local settings: ") + | 282 base::StringPrintf( |
240 result.GetError(), | 283 "Error pushing sync add to local settings: %s", |
284 result.GetError().c_str()), | |
241 syncable::EXTENSION_SETTINGS)); | 285 syncable::EXTENSION_SETTINGS)); |
286 break; | |
242 } | 287 } |
288 | |
289 change_event.AppendChange(it->key(), NULL, it->value().DeepCopy()); | |
290 break; | |
291 } | |
292 | |
293 case SyncChange::ACTION_UPDATE: { | |
294 if (!current_value.get()) { | |
295 errors.push_back(SyncError( | |
296 FROM_HERE, | |
297 base::StringPrintf( | |
298 "Got update from sync for nonexistent setting %s/%s", | |
299 extension_id_.c_str(), | |
300 it->key().c_str()), | |
301 syncable::EXTENSION_SETTINGS)); | |
302 break; | |
303 } | |
304 | |
305 synced_keys_.insert(it->key()); | |
306 Result result = delegate_->Set(it->key(), it->value()); | |
307 if (result.HasError()) { | |
308 errors.push_back(SyncError( | |
309 FROM_HERE, | |
310 base::StringPrintf( | |
311 "Error pushing sync update to local settings: %s", | |
312 result.GetError().c_str()), | |
313 syncable::EXTENSION_SETTINGS)); | |
314 break; | |
315 } | |
316 | |
317 change_event.AppendChange( | |
318 it->key(), current_value.release(), it->value().DeepCopy()); | |
243 break; | 319 break; |
244 } | 320 } |
245 | 321 |
246 case SyncChange::ACTION_DELETE: { | 322 case SyncChange::ACTION_DELETE: { |
323 if (!current_value.get()) { | |
324 errors.push_back(SyncError( | |
325 FROM_HERE, | |
326 base::StringPrintf( | |
327 "Got delete from sync for nonexistent setting %s/%s", | |
328 extension_id_.c_str(), | |
329 it->key().c_str()), | |
330 syncable::EXTENSION_SETTINGS)); | |
331 break; | |
332 } | |
333 | |
247 synced_keys_.erase(it->key()); | 334 synced_keys_.erase(it->key()); |
248 Result result = delegate_->Remove(it->key()); | 335 Result result = delegate_->Remove(it->key()); |
249 if (result.HasError()) { | 336 if (result.HasError()) { |
250 errors.push_back(SyncError( | 337 errors.push_back(SyncError( |
251 FROM_HERE, | 338 FROM_HERE, |
252 std::string("Error removing sync change from local settings: ") + | 339 base::StringPrintf( |
253 result.GetError(), | 340 "Error pushing sync remove to local settings: %s", |
341 result.GetError().c_str()), | |
254 syncable::EXTENSION_SETTINGS)); | 342 syncable::EXTENSION_SETTINGS)); |
343 break; | |
255 } | 344 } |
345 | |
346 change_event.AppendChange(it->key(), current_value.release(), NULL); | |
256 break; | 347 break; |
257 } | 348 } |
258 | 349 |
259 default: | 350 default: |
260 NOTREACHED(); | 351 NOTREACHED(); |
261 } | 352 } |
262 } | 353 } |
263 | 354 |
355 observers_->Notify( | |
356 &ExtensionSettingsObserver::OnSettingsChanged, | |
357 static_cast<Profile*>(NULL), | |
358 extension_id_, | |
359 change_event.GetEventJson()); | |
360 | |
264 return errors; | 361 return errors; |
265 } | 362 } |
266 | 363 |
267 void SyncableExtensionSettingsStorage::SendAddsOrUpdatesToSync( | 364 void SyncableExtensionSettingsStorage::SendAddsOrUpdatesToSync( |
268 const std::set<std::string>& changed_keys, | 365 const std::set<std::string>& changed_keys, |
269 const DictionaryValue& settings) { | 366 const DictionaryValue& settings) { |
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
271 DCHECK(sync_processor_); | 368 DCHECK(sync_processor_); |
272 DCHECK(!changed_keys.empty()); | 369 DCHECK(!changed_keys.empty()); |
273 | 370 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 if (error.IsSet()) { | 413 if (error.IsSet()) { |
317 LOG(WARNING) << "Failed to send changes to sync: " << error.message(); | 414 LOG(WARNING) << "Failed to send changes to sync: " << error.message(); |
318 return; | 415 return; |
319 } | 416 } |
320 | 417 |
321 for (std::set<std::string>::const_iterator it = keys.begin(); | 418 for (std::set<std::string>::const_iterator it = keys.begin(); |
322 it != keys.end(); ++it) { | 419 it != keys.end(); ++it) { |
323 synced_keys_.erase(*it); | 420 synced_keys_.erase(*it); |
324 } | 421 } |
325 } | 422 } |
OLD | NEW |