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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_SYNC_PROCESSOR_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_SYNC_PROCESSOR_H_
7 
8 #include <set>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "components/sync/model/sync_error.h"
13 #include "extensions/browser/value_store/value_store_change.h"
14 
15 namespace syncer {
16 class SyncChangeProcessor;
17 }  // namespace syncer
18 
19 namespace extensions {
20 
21 // A wrapper for a SyncChangeProcessor that deals specifically with the syncing
22 // of a single extension's settings. Handles:
23 //  - translating SettingChanges into calls into the Sync API.
24 //  - deciding whether to ADD/REMOVE/SET depending on the current state of
25 //    settings.
26 //  - rate limiting (inherently per-extension, which is what we want).
27 class SettingsSyncProcessor {
28  public:
29   SettingsSyncProcessor(const std::string& extension_id,
30                         syncer::ModelType type,
31                         syncer::SyncChangeProcessor* sync_processor);
32   ~SettingsSyncProcessor();
33 
34   // Initializes this with the initial state of sync.
35   void Init(const base::DictionaryValue& initial_state);
36 
37   // Sends |changes| to sync.
38   syncer::SyncError SendChanges(const ValueStoreChangeList& changes);
39 
40   // Informs this that |changes| have been receieved from sync. No action will
41   // be taken, but this must be notified for internal bookkeeping.
42   void NotifyChanges(const ValueStoreChangeList& changes);
43 
type()44   syncer::ModelType type() { return type_; }
45 
46  private:
47   // ID of the extension the changes are for.
48   const std::string extension_id_;
49 
50   // Sync model type. Either EXTENSION_SETTING or APP_SETTING.
51   const syncer::ModelType type_;
52 
53   // The sync processor used to send changes to sync.
54   syncer::SyncChangeProcessor* const sync_processor_;
55 
56   // Whether Init() has been called.
57   bool initialized_;
58 
59   // Keys of the settings that are currently being synced. Used to decide what
60   // kind of action (ADD, UPDATE, REMOVE) to send to sync.
61   std::set<std::string> synced_keys_;
62 
63   DISALLOW_COPY_AND_ASSIGN(SettingsSyncProcessor);
64 };
65 
66 }  // namespace extensions
67 
68 #endif  // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SETTINGS_SYNC_PROCESSOR_H_
69