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 <utility> |
| 8 |
7 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 10 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
9 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 11 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
10 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
11 #include "extensions/browser/api/storage/settings_namespace.h" | 13 #include "extensions/browser/api/storage/settings_namespace.h" |
12 #include "sync/api/sync_data.h" | 14 #include "sync/api/sync_data.h" |
13 #include "sync/protocol/extension_setting_specifics.pb.h" | 15 #include "sync/protocol/extension_setting_specifics.pb.h" |
14 | 16 |
15 using content::BrowserThread; | 17 using content::BrowserThread; |
16 | 18 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 ValueStore::ReadResult SyncableSettingsStorage::Get() { | 82 ValueStore::ReadResult SyncableSettingsStorage::Get() { |
81 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 83 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
82 return HandleResult(delegate_->Get()); | 84 return HandleResult(delegate_->Get()); |
83 } | 85 } |
84 | 86 |
85 ValueStore::WriteResult SyncableSettingsStorage::Set( | 87 ValueStore::WriteResult SyncableSettingsStorage::Set( |
86 WriteOptions options, const std::string& key, const base::Value& value) { | 88 WriteOptions options, const std::string& key, const base::Value& value) { |
87 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 89 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
88 WriteResult result = HandleResult(delegate_->Set(options, key, value)); | 90 WriteResult result = HandleResult(delegate_->Set(options, key, value)); |
89 if (!result->status().ok()) | 91 if (!result->status().ok()) |
90 return result.Pass(); | 92 return result; |
91 SyncResultIfEnabled(result); | 93 SyncResultIfEnabled(result); |
92 return result.Pass(); | 94 return result; |
93 } | 95 } |
94 | 96 |
95 ValueStore::WriteResult SyncableSettingsStorage::Set( | 97 ValueStore::WriteResult SyncableSettingsStorage::Set( |
96 WriteOptions options, const base::DictionaryValue& values) { | 98 WriteOptions options, const base::DictionaryValue& values) { |
97 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 99 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
98 WriteResult result = HandleResult(delegate_->Set(options, values)); | 100 WriteResult result = HandleResult(delegate_->Set(options, values)); |
99 if (!result->status().ok()) | 101 if (!result->status().ok()) |
100 return result.Pass(); | 102 return result; |
101 SyncResultIfEnabled(result); | 103 SyncResultIfEnabled(result); |
102 return result.Pass(); | 104 return result; |
103 } | 105 } |
104 | 106 |
105 ValueStore::WriteResult SyncableSettingsStorage::Remove( | 107 ValueStore::WriteResult SyncableSettingsStorage::Remove( |
106 const std::string& key) { | 108 const std::string& key) { |
107 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 109 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
108 WriteResult result = HandleResult(delegate_->Remove(key)); | 110 WriteResult result = HandleResult(delegate_->Remove(key)); |
109 if (!result->status().ok()) | 111 if (!result->status().ok()) |
110 return result.Pass(); | 112 return result; |
111 SyncResultIfEnabled(result); | 113 SyncResultIfEnabled(result); |
112 return result.Pass(); | 114 return result; |
113 } | 115 } |
114 | 116 |
115 ValueStore::WriteResult SyncableSettingsStorage::Remove( | 117 ValueStore::WriteResult SyncableSettingsStorage::Remove( |
116 const std::vector<std::string>& keys) { | 118 const std::vector<std::string>& keys) { |
117 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 119 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
118 WriteResult result = HandleResult(delegate_->Remove(keys)); | 120 WriteResult result = HandleResult(delegate_->Remove(keys)); |
119 if (!result->status().ok()) | 121 if (!result->status().ok()) |
120 return result.Pass(); | 122 return result; |
121 SyncResultIfEnabled(result); | 123 SyncResultIfEnabled(result); |
122 return result.Pass(); | 124 return result; |
123 } | 125 } |
124 | 126 |
125 ValueStore::WriteResult SyncableSettingsStorage::Clear() { | 127 ValueStore::WriteResult SyncableSettingsStorage::Clear() { |
126 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 128 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
127 WriteResult result = HandleResult(delegate_->Clear()); | 129 WriteResult result = HandleResult(delegate_->Clear()); |
128 if (!result->status().ok()) | 130 if (!result->status().ok()) |
129 return result.Pass(); | 131 return result; |
130 SyncResultIfEnabled(result); | 132 SyncResultIfEnabled(result); |
131 return result.Pass(); | 133 return result; |
132 } | 134 } |
133 | 135 |
134 void SyncableSettingsStorage::SyncResultIfEnabled( | 136 void SyncableSettingsStorage::SyncResultIfEnabled( |
135 const ValueStore::WriteResult& result) { | 137 const ValueStore::WriteResult& result) { |
136 if (result->changes().empty()) | 138 if (result->changes().empty()) |
137 return; | 139 return; |
138 | 140 |
139 if (sync_processor_.get()) { | 141 if (sync_processor_.get()) { |
140 syncer::SyncError error = sync_processor_->SendChanges(result->changes()); | 142 syncer::SyncError error = sync_processor_->SendChanges(result->changes()); |
141 if (error.IsSet()) | 143 if (error.IsSet()) |
142 StopSyncing(); | 144 StopSyncing(); |
143 } else { | 145 } else { |
144 // Tell sync to try and start soon, because syncable changes to sync_type_ | 146 // Tell sync to try and start soon, because syncable changes to sync_type_ |
145 // have started happening. This will cause sync to call us back | 147 // have started happening. This will cause sync to call us back |
146 // asynchronously via StartSyncing(...) as soon as possible. | 148 // asynchronously via StartSyncing(...) as soon as possible. |
147 flare_.Run(sync_type_); | 149 flare_.Run(sync_type_); |
148 } | 150 } |
149 } | 151 } |
150 | 152 |
151 // Sync-related methods. | 153 // Sync-related methods. |
152 | 154 |
153 syncer::SyncError SyncableSettingsStorage::StartSyncing( | 155 syncer::SyncError SyncableSettingsStorage::StartSyncing( |
154 scoped_ptr<base::DictionaryValue> sync_state, | 156 scoped_ptr<base::DictionaryValue> sync_state, |
155 scoped_ptr<SettingsSyncProcessor> sync_processor) { | 157 scoped_ptr<SettingsSyncProcessor> sync_processor) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 158 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
157 DCHECK(sync_state); | 159 DCHECK(sync_state); |
158 DCHECK(!sync_processor_.get()); | 160 DCHECK(!sync_processor_.get()); |
159 | 161 |
160 sync_processor_ = sync_processor.Pass(); | 162 sync_processor_ = std::move(sync_processor); |
161 sync_processor_->Init(*sync_state); | 163 sync_processor_->Init(*sync_state); |
162 | 164 |
163 ReadResult maybe_settings = delegate_->Get(); | 165 ReadResult maybe_settings = delegate_->Get(); |
164 if (!maybe_settings->status().ok()) { | 166 if (!maybe_settings->status().ok()) { |
165 return syncer::SyncError( | 167 return syncer::SyncError( |
166 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 168 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
167 base::StringPrintf("Failed to get settings: %s", | 169 base::StringPrintf("Failed to get settings: %s", |
168 maybe_settings->status().message.c_str()), | 170 maybe_settings->status().message.c_str()), |
169 sync_processor_->type()); | 171 sync_processor_->type()); |
170 } | 172 } |
171 | 173 |
172 scoped_ptr<base::DictionaryValue> current_settings = | 174 scoped_ptr<base::DictionaryValue> current_settings = |
173 maybe_settings->PassSettings(); | 175 maybe_settings->PassSettings(); |
174 return sync_state->empty() ? SendLocalSettingsToSync(current_settings.Pass()) | 176 return sync_state->empty() |
175 : OverwriteLocalSettingsWithSync( | 177 ? SendLocalSettingsToSync(std::move(current_settings)) |
176 sync_state.Pass(), current_settings.Pass()); | 178 : OverwriteLocalSettingsWithSync(std::move(sync_state), |
| 179 std::move(current_settings)); |
177 } | 180 } |
178 | 181 |
179 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 182 syncer::SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
180 scoped_ptr<base::DictionaryValue> local_state) { | 183 scoped_ptr<base::DictionaryValue> local_state) { |
181 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 184 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
182 | 185 |
183 if (local_state->empty()) | 186 if (local_state->empty()) |
184 return syncer::SyncError(); | 187 return syncer::SyncError(); |
185 | 188 |
186 // Transform the current settings into a list of sync changes. | 189 // Transform the current settings into a list of sync changes. |
(...skipping 22 matching lines...) Expand all Loading... |
209 scoped_ptr<SettingSyncDataList> changes(new SettingSyncDataList()); | 212 scoped_ptr<SettingSyncDataList> changes(new SettingSyncDataList()); |
210 | 213 |
211 for (base::DictionaryValue::Iterator it(*local_state); !it.IsAtEnd(); | 214 for (base::DictionaryValue::Iterator it(*local_state); !it.IsAtEnd(); |
212 it.Advance()) { | 215 it.Advance()) { |
213 scoped_ptr<base::Value> sync_value; | 216 scoped_ptr<base::Value> sync_value; |
214 if (sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { | 217 if (sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { |
215 if (sync_value->Equals(&it.value())) { | 218 if (sync_value->Equals(&it.value())) { |
216 // Sync and local values are the same, no changes to send. | 219 // Sync and local values are the same, no changes to send. |
217 } else { | 220 } else { |
218 // Sync value is different, update local setting with new value. | 221 // Sync value is different, update local setting with new value. |
219 changes->push_back( | 222 changes->push_back(new SettingSyncData( |
220 new SettingSyncData(syncer::SyncChange::ACTION_UPDATE, | 223 syncer::SyncChange::ACTION_UPDATE, extension_id_, it.key(), |
221 extension_id_, it.key(), sync_value.Pass())); | 224 std::move(sync_value))); |
222 } | 225 } |
223 } else { | 226 } else { |
224 // Not synced, delete local setting. | 227 // Not synced, delete local setting. |
225 changes->push_back(new SettingSyncData( | 228 changes->push_back(new SettingSyncData( |
226 syncer::SyncChange::ACTION_DELETE, extension_id_, it.key(), | 229 syncer::SyncChange::ACTION_DELETE, extension_id_, it.key(), |
227 scoped_ptr<base::Value>(new base::DictionaryValue()))); | 230 scoped_ptr<base::Value>(new base::DictionaryValue()))); |
228 } | 231 } |
229 } | 232 } |
230 | 233 |
231 // Add all new settings to local settings. | 234 // Add all new settings to local settings. |
232 while (!sync_state->empty()) { | 235 while (!sync_state->empty()) { |
233 // It's not possible to iterate over a DictionaryValue and modify it at the | 236 // It's not possible to iterate over a DictionaryValue and modify it at the |
234 // same time, so hack around that restriction. | 237 // same time, so hack around that restriction. |
235 std::string key = base::DictionaryValue::Iterator(*sync_state).key(); | 238 std::string key = base::DictionaryValue::Iterator(*sync_state).key(); |
236 scoped_ptr<base::Value> value; | 239 scoped_ptr<base::Value> value; |
237 CHECK(sync_state->RemoveWithoutPathExpansion(key, &value)); | 240 CHECK(sync_state->RemoveWithoutPathExpansion(key, &value)); |
238 changes->push_back(new SettingSyncData(syncer::SyncChange::ACTION_ADD, | 241 changes->push_back(new SettingSyncData( |
239 extension_id_, key, value.Pass())); | 242 syncer::SyncChange::ACTION_ADD, extension_id_, key, std::move(value))); |
240 } | 243 } |
241 | 244 |
242 if (changes->empty()) | 245 if (changes->empty()) |
243 return syncer::SyncError(); | 246 return syncer::SyncError(); |
244 return ProcessSyncChanges(changes.Pass()); | 247 return ProcessSyncChanges(std::move(changes)); |
245 } | 248 } |
246 | 249 |
247 void SyncableSettingsStorage::StopSyncing() { | 250 void SyncableSettingsStorage::StopSyncing() { |
248 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 251 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
249 sync_processor_.reset(); | 252 sync_processor_.reset(); |
250 } | 253 } |
251 | 254 |
252 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( | 255 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( |
253 scoped_ptr<SettingSyncDataList> sync_changes) { | 256 scoped_ptr<SettingSyncDataList> sync_changes) { |
254 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 257 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 396 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
394 base::StringPrintf("Error pushing sync remove to local settings: %s", | 397 base::StringPrintf("Error pushing sync remove to local settings: %s", |
395 result->status().message.c_str()), | 398 result->status().message.c_str()), |
396 sync_processor_->type()); | 399 sync_processor_->type()); |
397 } | 400 } |
398 changes->push_back(ValueStoreChange(key, old_value, NULL)); | 401 changes->push_back(ValueStoreChange(key, old_value, NULL)); |
399 return syncer::SyncError(); | 402 return syncer::SyncError(); |
400 } | 403 } |
401 | 404 |
402 } // namespace extensions | 405 } // namespace extensions |
OLD | NEW |