1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright (c) 2012 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
#define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list_threadsafe.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/storage/setting_sync_data.h"
#include "extensions/browser/api/storage/settings_observer.h"
#include "extensions/browser/value_store/value_store.h"
#include "sync/api/sync_change.h"
#include "sync/api/syncable_service.h"
namespace extensions {
class SettingsSyncProcessor;
// Decorates a ValueStore with sync behaviour.
class SyncableSettingsStorage : public ValueStore {
public:
SyncableSettingsStorage(
const scoped_refptr<SettingsObserverList>& observers,
const std::string& extension_id,
// Ownership taken.
ValueStore* delegate,
syncer::ModelType sync_type,
const syncer::SyncableService::StartSyncFlare& flare);
~SyncableSettingsStorage() override;
// ValueStore implementation.
size_t GetBytesInUse(const std::string& key) override;
size_t GetBytesInUse(const std::vector<std::string>& keys) override;
size_t GetBytesInUse() override;
ReadResult Get(const std::string& key) override;
ReadResult Get(const std::vector<std::string>& keys) override;
ReadResult Get() override;
WriteResult Set(WriteOptions options,
const std::string& key,
const base::Value& value) override;
WriteResult Set(WriteOptions options,
const base::DictionaryValue& values) override;
WriteResult Remove(const std::string& key) override;
WriteResult Remove(const std::vector<std::string>& keys) override;
WriteResult Clear() override;
bool Restore() override;
bool RestoreKey(const std::string& key) override;
// Sync-related methods, analogous to those on SyncableService (handled by
// ExtensionSettings), but with looser guarantees about when the methods
// can be called.
// Must only be called if sync isn't already active.
syncer::SyncError StartSyncing(
const base::DictionaryValue& sync_state,
scoped_ptr<SettingsSyncProcessor> sync_processor);
// May be called at any time (idempotent).
void StopSyncing();
// May be called at any time; changes will be ignored if sync isn't active.
syncer::SyncError ProcessSyncChanges(const SettingSyncDataList& sync_changes);
private:
// Sends the changes from |result| to sync if it's enabled.
void SyncResultIfEnabled(const ValueStore::WriteResult& result);
// Sends all local settings to sync (synced settings assumed to be empty).
syncer::SyncError SendLocalSettingsToSync(
const base::DictionaryValue& settings);
// Overwrites local state with sync state.
syncer::SyncError OverwriteLocalSettingsWithSync(
const base::DictionaryValue& sync_state,
const base::DictionaryValue& settings);
// Called when an Add/Update/Remove comes from sync. Ownership of Value*s
// are taken.
syncer::SyncError OnSyncAdd(
const std::string& key,
base::Value* new_value,
ValueStoreChangeList* changes);
syncer::SyncError OnSyncUpdate(
const std::string& key,
base::Value* old_value,
base::Value* new_value,
ValueStoreChangeList* changes);
syncer::SyncError OnSyncDelete(
const std::string& key,
base::Value* old_value,
ValueStoreChangeList* changes);
// List of observers to settings changes.
const scoped_refptr<SettingsObserverList> observers_;
// Id of the extension these settings are for.
std::string const extension_id_;
// Storage area to sync.
const scoped_ptr<ValueStore> delegate_;
// Object which sends changes to sync.
scoped_ptr<SettingsSyncProcessor> sync_processor_;
const syncer::ModelType sync_type_;
const syncer::SyncableService::StartSyncFlare flare_;
DISALLOW_COPY_AND_ASSIGN(SyncableSettingsStorage);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
|