| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/storage/syncable_settings_storage.h" | 5 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/storage/settings_namespace.h" | 7 #include "chrome/browser/extensions/api/storage/settings_namespace.h" |
| 8 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 8 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
| 9 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 9 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 69 WriteResult result = delegate_->Set(options, key, value); | 69 WriteResult result = delegate_->Set(options, key, value); |
| 70 if (result->HasError()) { | 70 if (result->HasError()) { |
| 71 return result.Pass(); | 71 return result.Pass(); |
| 72 } | 72 } |
| 73 SyncResultIfEnabled(result); | 73 SyncResultIfEnabled(result); |
| 74 return result.Pass(); | 74 return result.Pass(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 ValueStore::WriteResult SyncableSettingsStorage::Set( | 77 ValueStore::WriteResult SyncableSettingsStorage::Set( |
| 78 WriteOptions options, const DictionaryValue& values) { | 78 WriteOptions options, const base::DictionaryValue& values) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 80 WriteResult result = delegate_->Set(options, values); | 80 WriteResult result = delegate_->Set(options, values); |
| 81 if (result->HasError()) { | 81 if (result->HasError()) { |
| 82 return result.Pass(); | 82 return result.Pass(); |
| 83 } | 83 } |
| 84 SyncResultIfEnabled(result); | 84 SyncResultIfEnabled(result); |
| 85 return result.Pass(); | 85 return result.Pass(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 ValueStore::WriteResult SyncableSettingsStorage::Remove( | 88 ValueStore::WriteResult SyncableSettingsStorage::Remove( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 if (sync_processor_.get() && !result->changes().empty()) { | 122 if (sync_processor_.get() && !result->changes().empty()) { |
| 123 syncer::SyncError error = sync_processor_->SendChanges(result->changes()); | 123 syncer::SyncError error = sync_processor_->SendChanges(result->changes()); |
| 124 if (error.IsSet()) | 124 if (error.IsSet()) |
| 125 StopSyncing(); | 125 StopSyncing(); |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Sync-related methods. | 129 // Sync-related methods. |
| 130 | 130 |
| 131 syncer::SyncError SyncableSettingsStorage::StartSyncing( | 131 syncer::SyncError SyncableSettingsStorage::StartSyncing( |
| 132 const DictionaryValue& sync_state, | 132 const base::DictionaryValue& sync_state, |
| 133 scoped_ptr<SettingsSyncProcessor> sync_processor) { | 133 scoped_ptr<SettingsSyncProcessor> sync_processor) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 DCHECK(!sync_processor_.get()); | 135 DCHECK(!sync_processor_.get()); |
| 136 | 136 |
| 137 sync_processor_ = sync_processor.Pass(); | 137 sync_processor_ = sync_processor.Pass(); |
| 138 sync_processor_->Init(sync_state); | 138 sync_processor_->Init(sync_state); |
| 139 | 139 |
| 140 ReadResult maybe_settings = delegate_->Get(); | 140 ReadResult maybe_settings = delegate_->Get(); |
| 141 if (maybe_settings->HasError()) { | 141 if (maybe_settings->HasError()) { |
| 142 return syncer::SyncError( | 142 return syncer::SyncError( |
| 143 FROM_HERE, | 143 FROM_HERE, |
| 144 std::string("Failed to get settings: ") + maybe_settings->error(), | 144 std::string("Failed to get settings: ") + maybe_settings->error(), |
| 145 sync_processor_->type()); | 145 sync_processor_->type()); |
| 146 } | 146 } |
| 147 | 147 |
| 148 const DictionaryValue& settings = *maybe_settings->settings().get(); | 148 const base::DictionaryValue& settings = *maybe_settings->settings().get(); |
| 149 if (sync_state.empty()) | 149 if (sync_state.empty()) |
| 150 return SendLocalSettingsToSync(settings); | 150 return SendLocalSettingsToSync(settings); |
| 151 else | 151 else |
| 152 return OverwriteLocalSettingsWithSync(sync_state, settings); | 152 return OverwriteLocalSettingsWithSync(sync_state, settings); |
| 153 } | 153 } |
| 154 | 154 |
| 155 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 155 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
| 156 const DictionaryValue& settings) { | 156 const base::DictionaryValue& settings) { |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 158 | 158 |
| 159 ValueStoreChangeList changes; | 159 ValueStoreChangeList changes; |
| 160 for (DictionaryValue::Iterator i(settings); !i.IsAtEnd(); i.Advance()) { | 160 for (base::DictionaryValue::Iterator i(settings); !i.IsAtEnd(); i.Advance()) { |
| 161 changes.push_back(ValueStoreChange(i.key(), NULL, i.value().DeepCopy())); | 161 changes.push_back(ValueStoreChange(i.key(), NULL, i.value().DeepCopy())); |
| 162 } | 162 } |
| 163 | 163 |
| 164 if (changes.empty()) | 164 if (changes.empty()) |
| 165 return syncer::SyncError(); | 165 return syncer::SyncError(); |
| 166 | 166 |
| 167 syncer::SyncError error = sync_processor_->SendChanges(changes); | 167 syncer::SyncError error = sync_processor_->SendChanges(changes); |
| 168 if (error.IsSet()) | 168 if (error.IsSet()) |
| 169 StopSyncing(); | 169 StopSyncing(); |
| 170 | 170 |
| 171 return error; | 171 return error; |
| 172 } | 172 } |
| 173 | 173 |
| 174 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( | 174 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( |
| 175 const DictionaryValue& sync_state, const DictionaryValue& settings) { | 175 const base::DictionaryValue& sync_state, |
| 176 const base::DictionaryValue& settings) { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 177 // Treat this as a list of changes to sync and use ProcessSyncChanges. | 178 // Treat this as a list of changes to sync and use ProcessSyncChanges. |
| 178 // This gives notifications etc for free. | 179 // This gives notifications etc for free. |
| 179 scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy()); | 180 scoped_ptr<base::DictionaryValue> new_sync_state(sync_state.DeepCopy()); |
| 180 | 181 |
| 181 SettingSyncDataList changes; | 182 SettingSyncDataList changes; |
| 182 for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) { | 183 for (base::DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()
) { |
| 183 Value* orphaned_sync_value = NULL; | 184 Value* orphaned_sync_value = NULL; |
| 184 if (new_sync_state->RemoveWithoutPathExpansion( | 185 if (new_sync_state->RemoveWithoutPathExpansion( |
| 185 it.key(), &orphaned_sync_value)) { | 186 it.key(), &orphaned_sync_value)) { |
| 186 scoped_ptr<Value> sync_value(orphaned_sync_value); | 187 scoped_ptr<Value> sync_value(orphaned_sync_value); |
| 187 if (sync_value->Equals(&it.value())) { | 188 if (sync_value->Equals(&it.value())) { |
| 188 // Sync and local values are the same, no changes to send. | 189 // Sync and local values are the same, no changes to send. |
| 189 } else { | 190 } else { |
| 190 // Sync value is different, update local setting with new value. | 191 // Sync value is different, update local setting with new value. |
| 191 changes.push_back( | 192 changes.push_back( |
| 192 SettingSyncData( | 193 SettingSyncData( |
| 193 syncer::SyncChange::ACTION_UPDATE, | 194 syncer::SyncChange::ACTION_UPDATE, |
| 194 extension_id_, | 195 extension_id_, |
| 195 it.key(), | 196 it.key(), |
| 196 sync_value.Pass())); | 197 sync_value.Pass())); |
| 197 } | 198 } |
| 198 } else { | 199 } else { |
| 199 // Not synced, delete local setting. | 200 // Not synced, delete local setting. |
| 200 changes.push_back( | 201 changes.push_back( |
| 201 SettingSyncData( | 202 SettingSyncData( |
| 202 syncer::SyncChange::ACTION_DELETE, | 203 syncer::SyncChange::ACTION_DELETE, |
| 203 extension_id_, | 204 extension_id_, |
| 204 it.key(), | 205 it.key(), |
| 205 scoped_ptr<Value>(new DictionaryValue()))); | 206 scoped_ptr<Value>(new base::DictionaryValue()))); |
| 206 } | 207 } |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Add all new settings to local settings. | 210 // Add all new settings to local settings. |
| 210 while (!new_sync_state->empty()) { | 211 while (!new_sync_state->empty()) { |
| 211 DictionaryValue::Iterator first_entry(*new_sync_state); | 212 base::DictionaryValue::Iterator first_entry(*new_sync_state); |
| 212 std::string key = first_entry.key(); | 213 std::string key = first_entry.key(); |
| 213 Value* value = NULL; | 214 Value* value = NULL; |
| 214 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); | 215 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); |
| 215 changes.push_back( | 216 changes.push_back( |
| 216 SettingSyncData( | 217 SettingSyncData( |
| 217 syncer::SyncChange::ACTION_ADD, | 218 syncer::SyncChange::ACTION_ADD, |
| 218 extension_id_, | 219 extension_id_, |
| 219 key, | 220 key, |
| 220 make_scoped_ptr(value))); | 221 make_scoped_ptr(value))); |
| 221 } | 222 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 FROM_HERE, | 377 FROM_HERE, |
| 377 std::string("Error pushing sync remove to local settings: ") + | 378 std::string("Error pushing sync remove to local settings: ") + |
| 378 result->error(), | 379 result->error(), |
| 379 sync_processor_->type()); | 380 sync_processor_->type()); |
| 380 } | 381 } |
| 381 changes->push_back(ValueStoreChange(key, old_value, NULL)); | 382 changes->push_back(ValueStoreChange(key, old_value, NULL)); |
| 382 return syncer::SyncError(); | 383 return syncer::SyncError(); |
| 383 } | 384 } |
| 384 | 385 |
| 385 } // namespace extensions | 386 } // namespace extensions |
| OLD | NEW |