| Index: chrome/browser/extensions/syncable_extension_settings_storage.cc
|
| diff --git a/chrome/browser/extensions/syncable_extension_settings_storage.cc b/chrome/browser/extensions/syncable_extension_settings_storage.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..970057d98535d34070f34e29aa13cc1a6324e862
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/syncable_extension_settings_storage.cc
|
| @@ -0,0 +1,311 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/syncable_extension_settings_storage.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/task.h"
|
| +#include "chrome/browser/extensions/extension_settings_sync_util.h"
|
| +#include "chrome/browser/sync/api/sync_data.h"
|
| +#include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h"
|
| +#include "content/browser/browser_thread.h"
|
| +
|
| +SyncableExtensionSettingsStorage::SyncableExtensionSettingsStorage(
|
| + std::string extension_id, ExtensionSettingsStorage* delegate)
|
| + : extension_id_(extension_id), delegate_(delegate), sync_processor_(NULL) {}
|
| +
|
| +SyncableExtensionSettingsStorage::~SyncableExtensionSettingsStorage() {}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get(
|
| + const std::string& key) {
|
| + return delegate_->Get(key);
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get(
|
| + const std::vector<std::string>& keys) {
|
| + return delegate_->Get(keys);
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get() {
|
| + return delegate_->Get();
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Set(
|
| + const std::string& key, const Value& value) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + Result result = delegate_->Set(key, value);
|
| + if (result.HasError()) {
|
| + return result;
|
| + }
|
| + if (sync_processor_ != NULL) {
|
| + SendAddsOrUpdatesToSync(*result.GetSettings());
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Set(
|
| + const DictionaryValue& values) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + Result result = delegate_->Set(values);
|
| + if (result.HasError()) {
|
| + return result;
|
| + }
|
| + if (sync_processor_ != NULL) {
|
| + SendAddsOrUpdatesToSync(*result.GetSettings());
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Remove(
|
| + const std::string& key) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + Result result = delegate_->Remove(key);
|
| + if (result.HasError()) {
|
| + return result;
|
| + }
|
| + if (sync_processor_ != NULL) {
|
| + std::vector<std::string> keys;
|
| + keys.push_back(key);
|
| + SendDeletesToSync(keys);
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Remove(
|
| + const std::vector<std::string>& keys) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + Result result = delegate_->Remove(keys);
|
| + if (result.HasError()) {
|
| + return result;
|
| + }
|
| + if (sync_processor_ != NULL) {
|
| + SendDeletesToSync(keys);
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Clear() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + Result result = delegate_->Clear();
|
| + if (result.HasError()) {
|
| + return result;
|
| + }
|
| + if (sync_processor_ != NULL) {
|
| + std::vector<std::string> keys;
|
| + keys.insert(keys.end(), synced_keys_.begin(), synced_keys_.end());
|
| + SendDeletesToSync(keys);
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +// Sync-related methods.
|
| +
|
| +void SyncableExtensionSettingsStorage::StartSyncing(
|
| + const DictionaryValue& sync_state, SyncChangeProcessor* sync_processor) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + DCHECK(sync_processor_ == NULL);
|
| + DCHECK(synced_keys_.empty());
|
| + sync_processor_ = sync_processor;
|
| + for (DictionaryValue::key_iterator it = sync_state.begin_keys();
|
| + it != sync_state.end_keys(); ++it) {
|
| + synced_keys_.insert(*it);
|
| + }
|
| +
|
| + Result maybe_settings = delegate_->Get();
|
| + if (maybe_settings.HasError()) {
|
| + LOG(WARNING) << "Failed to get local settings to send to sync: " <<
|
| + maybe_settings.GetError();
|
| + return;
|
| + }
|
| + DictionaryValue* settings = maybe_settings.GetSettings();
|
| +
|
| + if (sync_state.empty()) {
|
| + SendLocalSettingsToSync(*settings);
|
| + } else {
|
| + OverwriteLocalSettingsWithSync(sync_state, *settings);
|
| + }
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::SendLocalSettingsToSync(
|
| + const DictionaryValue& settings) {
|
| + SyncChangeList changes;
|
| + for (DictionaryValue::key_iterator it = settings.begin_keys();
|
| + it != settings.end_keys(); ++it) {
|
| + Value* value;
|
| + settings.GetWithoutPathExpansion(*it, &value);
|
| + changes.push_back(
|
| + extension_settings_sync_util::CreateAdd(extension_id_, *it, *value));
|
| + }
|
| +
|
| + if (changes.empty()) {
|
| + return;
|
| + }
|
| +
|
| + SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
|
| + if (error.IsSet()) {
|
| + LOG(WARNING) << "Failed to send changes to sync: " << error.message();
|
| + return;
|
| + }
|
| +
|
| + for (DictionaryValue::key_iterator it = settings.begin_keys();
|
| + it != settings.end_keys(); ++it) {
|
| + synced_keys_.insert(*it);
|
| + }
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::OverwriteLocalSettingsWithSync(
|
| + const DictionaryValue& sync_state, const DictionaryValue& settings) {
|
| + // Treat this as a list of changes to sync and use ProcessSyncChanges.
|
| + // This gives notifications etc for free.
|
| + scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy());
|
| +
|
| + ExtensionSettingSyncDataList changes;
|
| + for (DictionaryValue::key_iterator it = settings.begin_keys();
|
| + it != settings.end_keys(); ++it) {
|
| + Value* sync_value;
|
| + if (new_sync_state->RemoveWithoutPathExpansion(*it, &sync_value)) {
|
| + Value* local_value;
|
| + settings.GetWithoutPathExpansion(*it, &local_value);
|
| + if (!local_value->Equals(sync_value)) {
|
| + // Sync value is different, update local setting with new value.
|
| + changes.push_back(
|
| + ExtensionSettingSyncData(
|
| + SyncChange::ACTION_UPDATE, "", *it, sync_value));
|
| + }
|
| + } else {
|
| + // Not synced, delete local setting.
|
| + changes.push_back(
|
| + ExtensionSettingSyncData(
|
| + SyncChange::ACTION_DELETE, "", *it, new DictionaryValue()));
|
| + }
|
| + }
|
| +
|
| + // Add all new settings to local settings.
|
| + while (!new_sync_state->empty()) {
|
| + std::string key = *new_sync_state->begin_keys();
|
| + Value* value;
|
| + new_sync_state->RemoveWithoutPathExpansion(key, &value);
|
| + changes.push_back(
|
| + ExtensionSettingSyncData(SyncChange::ACTION_ADD, "", key, value));
|
| + }
|
| +
|
| + if (!changes.empty()) {
|
| + ProcessSyncChanges(changes);
|
| + }
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::StopSyncing() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + DCHECK(sync_processor_ != NULL);
|
| + sync_processor_ = NULL;
|
| + synced_keys_.clear();
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::ProcessSyncChanges(
|
| + const ExtensionSettingSyncDataList& sync_changes) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + DCHECK(sync_processor_ != NULL);
|
| + DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_;
|
| +
|
| + for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin();
|
| + it != sync_changes.end(); ++it) {
|
| + switch (it->change_type()) {
|
| + case SyncChange::ACTION_ADD:
|
| + case SyncChange::ACTION_UPDATE: {
|
| + synced_keys_.insert(it->key());
|
| + Result result = delegate_->Set(it->key(), *it->value());
|
| + if (result.HasError()) {
|
| + LOG(WARNING) << "Error pushing sync change to local settings: " <<
|
| + result.GetError();
|
| + }
|
| + break;
|
| + }
|
| +
|
| + case SyncChange::ACTION_DELETE: {
|
| + synced_keys_.erase(it->key());
|
| + Result result = delegate_->Remove(it->key());
|
| + if (result.HasError()) {
|
| + LOG(WARNING) << "Error removing sync change from local settings: " <<
|
| + result.GetError();
|
| + }
|
| + break;
|
| + }
|
| +
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| + }
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::SendAddsOrUpdatesToSync(
|
| + const DictionaryValue& settings) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + DCHECK(sync_processor_ != NULL);
|
| +
|
| + SyncChangeList changes;
|
| + for (DictionaryValue::key_iterator it = settings.begin_keys();
|
| + it != settings.end_keys(); ++it) {
|
| + Value* value = NULL;
|
| + settings.GetWithoutPathExpansion(*it, &value);
|
| + DCHECK(value != NULL);
|
| + if (synced_keys_.count(*it)) {
|
| + // Key is synced, send ACTION_UPDATE to sync.
|
| + changes.push_back(
|
| + extension_settings_sync_util::CreateUpdate(
|
| + extension_id_, *it, *value));
|
| + } else {
|
| + // Key is not synced, send ACTION_ADD to sync.
|
| + changes.push_back(
|
| + extension_settings_sync_util::CreateAdd(extension_id_, *it, *value));
|
| + }
|
| + }
|
| +
|
| + if (changes.empty()) {
|
| + return;
|
| + }
|
| +
|
| + SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
|
| + if (error.IsSet()) {
|
| + LOG(WARNING) << "Failed to send changes to sync: " << error.message();
|
| + return;
|
| + }
|
| +
|
| + for (DictionaryValue::key_iterator it = settings.begin_keys();
|
| + it != settings.end_keys(); ++it) {
|
| + synced_keys_.insert(*it);
|
| + }
|
| +}
|
| +
|
| +void SyncableExtensionSettingsStorage::SendDeletesToSync(
|
| + const std::vector<std::string>& keys) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + DCHECK(sync_processor_ != NULL);
|
| +
|
| + SyncChangeList changes;
|
| + for (std::vector<std::string>::const_iterator it = keys.begin();
|
| + it != keys.end(); ++it) {
|
| + // Only remove from sync if it's actually synced.
|
| + if (synced_keys_.count(*it)) {
|
| + changes.push_back(
|
| + extension_settings_sync_util::CreateDelete(extension_id_, *it));
|
| + }
|
| + }
|
| +
|
| + if (changes.empty()) {
|
| + return;
|
| + }
|
| +
|
| + SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
|
| + if (error.IsSet()) {
|
| + LOG(WARNING) << "Failed to send changes to sync: " << error.message();
|
| + return;
|
| + }
|
| +
|
| + for (std::vector<std::string>::const_iterator it = keys.begin();
|
| + it != keys.end(); ++it) {
|
| + synced_keys_.erase(*it);
|
| + }
|
| +}
|
|
|