| 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 "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.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 10 matching lines...) Expand all Loading... |
| 21 observers, | 21 observers, |
| 22 const std::string& extension_id, | 22 const std::string& extension_id, |
| 23 ValueStore* delegate, | 23 ValueStore* delegate, |
| 24 syncer::ModelType sync_type, | 24 syncer::ModelType sync_type, |
| 25 const syncer::SyncableService::StartSyncFlare& flare) | 25 const syncer::SyncableService::StartSyncFlare& flare) |
| 26 : observers_(observers), | 26 : observers_(observers), |
| 27 extension_id_(extension_id), | 27 extension_id_(extension_id), |
| 28 delegate_(delegate), | 28 delegate_(delegate), |
| 29 sync_type_(sync_type), | 29 sync_type_(sync_type), |
| 30 flare_(flare) { | 30 flare_(flare) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 31 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 32 } | 32 } |
| 33 | 33 |
| 34 SyncableSettingsStorage::~SyncableSettingsStorage() { | 34 SyncableSettingsStorage::~SyncableSettingsStorage() { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 35 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 36 } | 36 } |
| 37 | 37 |
| 38 size_t SyncableSettingsStorage::GetBytesInUse(const std::string& key) { | 38 size_t SyncableSettingsStorage::GetBytesInUse(const std::string& key) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 39 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 40 return delegate_->GetBytesInUse(key); | 40 return delegate_->GetBytesInUse(key); |
| 41 } | 41 } |
| 42 | 42 |
| 43 size_t SyncableSettingsStorage::GetBytesInUse( | 43 size_t SyncableSettingsStorage::GetBytesInUse( |
| 44 const std::vector<std::string>& keys) { | 44 const std::vector<std::string>& keys) { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 45 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 46 return delegate_->GetBytesInUse(keys); | 46 return delegate_->GetBytesInUse(keys); |
| 47 } | 47 } |
| 48 | 48 |
| 49 size_t SyncableSettingsStorage::GetBytesInUse() { | 49 size_t SyncableSettingsStorage::GetBytesInUse() { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 50 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 51 return delegate_->GetBytesInUse(); | 51 return delegate_->GetBytesInUse(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 ValueStore::ReadResult SyncableSettingsStorage::Get( | 54 ValueStore::ReadResult SyncableSettingsStorage::Get( |
| 55 const std::string& key) { | 55 const std::string& key) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 56 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 57 return delegate_->Get(key); | 57 return delegate_->Get(key); |
| 58 } | 58 } |
| 59 | 59 |
| 60 ValueStore::ReadResult SyncableSettingsStorage::Get( | 60 ValueStore::ReadResult SyncableSettingsStorage::Get( |
| 61 const std::vector<std::string>& keys) { | 61 const std::vector<std::string>& keys) { |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 62 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 63 return delegate_->Get(keys); | 63 return delegate_->Get(keys); |
| 64 } | 64 } |
| 65 | 65 |
| 66 ValueStore::ReadResult SyncableSettingsStorage::Get() { | 66 ValueStore::ReadResult SyncableSettingsStorage::Get() { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 67 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 68 return delegate_->Get(); | 68 return delegate_->Get(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 ValueStore::WriteResult SyncableSettingsStorage::Set( | 71 ValueStore::WriteResult SyncableSettingsStorage::Set( |
| 72 WriteOptions options, const std::string& key, const base::Value& value) { | 72 WriteOptions options, const std::string& key, const base::Value& value) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 73 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 74 WriteResult result = delegate_->Set(options, key, value); | 74 WriteResult result = delegate_->Set(options, key, value); |
| 75 if (result->HasError()) { | 75 if (result->HasError()) { |
| 76 return result.Pass(); | 76 return result.Pass(); |
| 77 } | 77 } |
| 78 SyncResultIfEnabled(result); | 78 SyncResultIfEnabled(result); |
| 79 return result.Pass(); | 79 return result.Pass(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 ValueStore::WriteResult SyncableSettingsStorage::Set( | 82 ValueStore::WriteResult SyncableSettingsStorage::Set( |
| 83 WriteOptions options, const base::DictionaryValue& values) { | 83 WriteOptions options, const base::DictionaryValue& values) { |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 84 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 85 WriteResult result = delegate_->Set(options, values); | 85 WriteResult result = delegate_->Set(options, values); |
| 86 if (result->HasError()) { | 86 if (result->HasError()) { |
| 87 return result.Pass(); | 87 return result.Pass(); |
| 88 } | 88 } |
| 89 SyncResultIfEnabled(result); | 89 SyncResultIfEnabled(result); |
| 90 return result.Pass(); | 90 return result.Pass(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 ValueStore::WriteResult SyncableSettingsStorage::Remove( | 93 ValueStore::WriteResult SyncableSettingsStorage::Remove( |
| 94 const std::string& key) { | 94 const std::string& key) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 95 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 96 WriteResult result = delegate_->Remove(key); | 96 WriteResult result = delegate_->Remove(key); |
| 97 if (result->HasError()) { | 97 if (result->HasError()) { |
| 98 return result.Pass(); | 98 return result.Pass(); |
| 99 } | 99 } |
| 100 SyncResultIfEnabled(result); | 100 SyncResultIfEnabled(result); |
| 101 return result.Pass(); | 101 return result.Pass(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 ValueStore::WriteResult SyncableSettingsStorage::Remove( | 104 ValueStore::WriteResult SyncableSettingsStorage::Remove( |
| 105 const std::vector<std::string>& keys) { | 105 const std::vector<std::string>& keys) { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 106 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 107 WriteResult result = delegate_->Remove(keys); | 107 WriteResult result = delegate_->Remove(keys); |
| 108 if (result->HasError()) { | 108 if (result->HasError()) { |
| 109 return result.Pass(); | 109 return result.Pass(); |
| 110 } | 110 } |
| 111 SyncResultIfEnabled(result); | 111 SyncResultIfEnabled(result); |
| 112 return result.Pass(); | 112 return result.Pass(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 ValueStore::WriteResult SyncableSettingsStorage::Clear() { | 115 ValueStore::WriteResult SyncableSettingsStorage::Clear() { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 116 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 117 WriteResult result = delegate_->Clear(); | 117 WriteResult result = delegate_->Clear(); |
| 118 if (result->HasError()) { | 118 if (result->HasError()) { |
| 119 return result.Pass(); | 119 return result.Pass(); |
| 120 } | 120 } |
| 121 SyncResultIfEnabled(result); | 121 SyncResultIfEnabled(result); |
| 122 return result.Pass(); | 122 return result.Pass(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 bool SyncableSettingsStorage::Restore() { | 125 bool SyncableSettingsStorage::Restore() { |
| 126 // If we're syncing, stop - we don't want to push the deletion of any data. | 126 // If we're syncing, stop - we don't want to push the deletion of any data. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 // asynchronously via StartSyncing(...) as soon as possible. | 159 // asynchronously via StartSyncing(...) as soon as possible. |
| 160 flare_.Run(sync_type_); | 160 flare_.Run(sync_type_); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Sync-related methods. | 164 // Sync-related methods. |
| 165 | 165 |
| 166 syncer::SyncError SyncableSettingsStorage::StartSyncing( | 166 syncer::SyncError SyncableSettingsStorage::StartSyncing( |
| 167 const base::DictionaryValue& sync_state, | 167 const base::DictionaryValue& sync_state, |
| 168 scoped_ptr<SettingsSyncProcessor> sync_processor) { | 168 scoped_ptr<SettingsSyncProcessor> sync_processor) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 169 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 170 DCHECK(!sync_processor_.get()); | 170 DCHECK(!sync_processor_.get()); |
| 171 | 171 |
| 172 sync_processor_ = sync_processor.Pass(); | 172 sync_processor_ = sync_processor.Pass(); |
| 173 sync_processor_->Init(sync_state); | 173 sync_processor_->Init(sync_state); |
| 174 | 174 |
| 175 ReadResult maybe_settings = delegate_->Get(); | 175 ReadResult maybe_settings = delegate_->Get(); |
| 176 if (maybe_settings->HasError()) { | 176 if (maybe_settings->HasError()) { |
| 177 return syncer::SyncError( | 177 return syncer::SyncError( |
| 178 FROM_HERE, | 178 FROM_HERE, |
| 179 syncer::SyncError::DATATYPE_ERROR, | 179 syncer::SyncError::DATATYPE_ERROR, |
| 180 base::StringPrintf("Failed to get settings: %s", | 180 base::StringPrintf("Failed to get settings: %s", |
| 181 maybe_settings->error().message.c_str()), | 181 maybe_settings->error().message.c_str()), |
| 182 sync_processor_->type()); | 182 sync_processor_->type()); |
| 183 } | 183 } |
| 184 | 184 |
| 185 const base::DictionaryValue& settings = maybe_settings->settings(); | 185 const base::DictionaryValue& settings = maybe_settings->settings(); |
| 186 return sync_state.empty() ? | 186 return sync_state.empty() ? |
| 187 SendLocalSettingsToSync(settings) : | 187 SendLocalSettingsToSync(settings) : |
| 188 OverwriteLocalSettingsWithSync(sync_state, settings); | 188 OverwriteLocalSettingsWithSync(sync_state, settings); |
| 189 } | 189 } |
| 190 | 190 |
| 191 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 191 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
| 192 const base::DictionaryValue& settings) { | 192 const base::DictionaryValue& settings) { |
| 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 193 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 194 | 194 |
| 195 ValueStoreChangeList changes; | 195 ValueStoreChangeList changes; |
| 196 for (base::DictionaryValue::Iterator i(settings); !i.IsAtEnd(); i.Advance()) { | 196 for (base::DictionaryValue::Iterator i(settings); !i.IsAtEnd(); i.Advance()) { |
| 197 changes.push_back(ValueStoreChange(i.key(), NULL, i.value().DeepCopy())); | 197 changes.push_back(ValueStoreChange(i.key(), NULL, i.value().DeepCopy())); |
| 198 } | 198 } |
| 199 | 199 |
| 200 if (changes.empty()) | 200 if (changes.empty()) |
| 201 return syncer::SyncError(); | 201 return syncer::SyncError(); |
| 202 | 202 |
| 203 syncer::SyncError error = sync_processor_->SendChanges(changes); | 203 syncer::SyncError error = sync_processor_->SendChanges(changes); |
| 204 if (error.IsSet()) | 204 if (error.IsSet()) |
| 205 StopSyncing(); | 205 StopSyncing(); |
| 206 | 206 |
| 207 return error; | 207 return error; |
| 208 } | 208 } |
| 209 | 209 |
| 210 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( | 210 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( |
| 211 const base::DictionaryValue& sync_state, | 211 const base::DictionaryValue& sync_state, |
| 212 const base::DictionaryValue& settings) { | 212 const base::DictionaryValue& settings) { |
| 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 213 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 214 // Treat this as a list of changes to sync and use ProcessSyncChanges. | 214 // Treat this as a list of changes to sync and use ProcessSyncChanges. |
| 215 // This gives notifications etc for free. | 215 // This gives notifications etc for free. |
| 216 scoped_ptr<base::DictionaryValue> new_sync_state(sync_state.DeepCopy()); | 216 scoped_ptr<base::DictionaryValue> new_sync_state(sync_state.DeepCopy()); |
| 217 | 217 |
| 218 SettingSyncDataList changes; | 218 SettingSyncDataList changes; |
| 219 for (base::DictionaryValue::Iterator it(settings); | 219 for (base::DictionaryValue::Iterator it(settings); |
| 220 !it.IsAtEnd(); it.Advance()) { | 220 !it.IsAtEnd(); it.Advance()) { |
| 221 scoped_ptr<base::Value> sync_value; | 221 scoped_ptr<base::Value> sync_value; |
| 222 if (new_sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { | 222 if (new_sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { |
| 223 if (sync_value->Equals(&it.value())) { | 223 if (sync_value->Equals(&it.value())) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 value.Pass())); | 256 value.Pass())); |
| 257 } | 257 } |
| 258 | 258 |
| 259 if (changes.empty()) | 259 if (changes.empty()) |
| 260 return syncer::SyncError(); | 260 return syncer::SyncError(); |
| 261 | 261 |
| 262 return ProcessSyncChanges(changes); | 262 return ProcessSyncChanges(changes); |
| 263 } | 263 } |
| 264 | 264 |
| 265 void SyncableSettingsStorage::StopSyncing() { | 265 void SyncableSettingsStorage::StopSyncing() { |
| 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 266 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 267 sync_processor_.reset(); | 267 sync_processor_.reset(); |
| 268 } | 268 } |
| 269 | 269 |
| 270 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( | 270 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( |
| 271 const SettingSyncDataList& sync_changes) { | 271 const SettingSyncDataList& sync_changes) { |
| 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 272 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 273 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; | 273 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; |
| 274 | 274 |
| 275 if (!sync_processor_.get()) { | 275 if (!sync_processor_.get()) { |
| 276 return syncer::SyncError( | 276 return syncer::SyncError( |
| 277 FROM_HERE, | 277 FROM_HERE, |
| 278 syncer::SyncError::DATATYPE_ERROR, | 278 syncer::SyncError::DATATYPE_ERROR, |
| 279 std::string("Sync is inactive for ") + extension_id_, | 279 std::string("Sync is inactive for ") + extension_id_, |
| 280 syncer::UNSPECIFIED); | 280 syncer::UNSPECIFIED); |
| 281 } | 281 } |
| 282 | 282 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 syncer::SyncError::DATATYPE_ERROR, | 416 syncer::SyncError::DATATYPE_ERROR, |
| 417 base::StringPrintf("Error pushing sync remove to local settings: %s", | 417 base::StringPrintf("Error pushing sync remove to local settings: %s", |
| 418 result->error().message.c_str()), | 418 result->error().message.c_str()), |
| 419 sync_processor_->type()); | 419 sync_processor_->type()); |
| 420 } | 420 } |
| 421 changes->push_back(ValueStoreChange(key, old_value, NULL)); | 421 changes->push_back(ValueStoreChange(key, old_value, NULL)); |
| 422 return syncer::SyncError(); | 422 return syncer::SyncError(); |
| 423 } | 423 } |
| 424 | 424 |
| 425 } // namespace extensions | 425 } // namespace extensions |
| OLD | NEW |