Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / storage / sync_storage_backend.h
1 // Copyright 2014 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_SYNC_STORAGE_BACKEND_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "extensions/browser/api/storage/settings_observer.h"
18 #include "extensions/browser/api/storage/settings_storage_factory.h"
19 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
20 #include "sync/api/syncable_service.h"
21
22 namespace syncer {
23 class SyncErrorFactory;
24 }
25
26 namespace extensions {
27
28 class SettingsSyncProcessor;
29 class SyncableSettingsStorage;
30
31 // Manages ValueStore objects for extensions, including routing
32 // changes from sync to them.
33 // Lives entirely on the FILE thread.
34 class SyncStorageBackend : public syncer::SyncableService {
35  public:
36   // |storage_factory| is use to create leveldb storage areas.
37   // |base_path| is the base of the extension settings directory, so the
38   // databases will be at base_path/extension_id.
39   // |observers| is the list of observers to settings changes.
40   SyncStorageBackend(
41       const scoped_refptr<SettingsStorageFactory>& storage_factory,
42       const base::FilePath& base_path,
43       const SettingsStorageQuotaEnforcer::Limits& quota,
44       const scoped_refptr<SettingsObserverList>& observers,
45       syncer::ModelType sync_type,
46       const syncer::SyncableService::StartSyncFlare& flare);
47
48   ~SyncStorageBackend() override;
49
50   virtual ValueStore* GetStorage(const std::string& extension_id);
51   virtual void DeleteStorage(const std::string& extension_id);
52
53   // syncer::SyncableService implementation.
54   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
55   syncer::SyncMergeResult MergeDataAndStartSyncing(
56       syncer::ModelType type,
57       const syncer::SyncDataList& initial_sync_data,
58       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
59       scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) override;
60   syncer::SyncError ProcessSyncChanges(
61       const tracked_objects::Location& from_here,
62       const syncer::SyncChangeList& change_list) override;
63   void StopSyncing(syncer::ModelType type) override;
64
65  private:
66   // Gets a weak reference to the storage area for a given extension,
67   // initializing sync with some initial data if sync enabled.
68   SyncableSettingsStorage* GetOrCreateStorageWithSyncData(
69       const std::string& extension_id,
70       const base::DictionaryValue& sync_data) const;
71
72   // Gets all extension IDs known to extension settings.  This may not be all
73   // installed extensions.
74   std::set<std::string> GetKnownExtensionIDs() const;
75
76   // Creates a new SettingsSyncProcessor for an extension.
77   scoped_ptr<SettingsSyncProcessor> CreateSettingsSyncProcessor(
78       const std::string& extension_id) const;
79
80   // The Factory to use for creating new ValueStores.
81   const scoped_refptr<SettingsStorageFactory> storage_factory_;
82
83   // The base file path to use when creating new ValueStores.
84   const base::FilePath base_path_;
85
86   // Quota limits (see SettingsStorageQuotaEnforcer).
87   const SettingsStorageQuotaEnforcer::Limits quota_;
88
89   // The list of observers to settings changes.
90   const scoped_refptr<SettingsObserverList> observers_;
91
92   // A cache of ValueStore objects that have already been created.
93   // Ensure that there is only ever one created per extension.
94   typedef std::map<std::string, linked_ptr<SyncableSettingsStorage> >
95       StorageObjMap;
96   mutable StorageObjMap storage_objs_;
97
98   // Current sync model type. Either EXTENSION_SETTINGS or APP_SETTINGS.
99   syncer::ModelType sync_type_;
100
101   // Current sync processor, if any.
102   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
103
104   // Current sync error handler if any.
105   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
106
107   syncer::SyncableService::StartSyncFlare flare_;
108
109   DISALLOW_COPY_AND_ASSIGN(SyncStorageBackend);
110 };
111
112 }  // namespace extensions
113
114 #endif  // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNC_STORAGE_BACKEND_H_