Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / dom_distiller / core / dom_distiller_store.h
blob2440ce5c473c0bedfb7980ce0f494bc628f6a1ec
1 // Copyright 2013 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.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_
8 #include <string>
9 #include <vector>
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "components/dom_distiller/core/article_attachments_data.h"
15 #include "components/dom_distiller/core/article_entry.h"
16 #include "components/dom_distiller/core/dom_distiller_model.h"
17 #include "components/dom_distiller/core/dom_distiller_observer.h"
18 #include "components/leveldb_proto/proto_database.h"
19 #include "sync/api/sync_change.h"
20 #include "sync/api/sync_data.h"
21 #include "sync/api/sync_error.h"
22 #include "sync/api/sync_error_factory.h"
23 #include "sync/api/sync_merge_result.h"
24 #include "sync/api/syncable_service.h"
25 #include "url/gurl.h"
27 namespace base {
28 class FilePath;
31 namespace syncer {
32 class AttachmentStore;
35 namespace dom_distiller {
37 // Interface for accessing the stored/synced DomDistiller entries.
38 class DomDistillerStoreInterface {
39 public:
40 virtual ~DomDistillerStoreInterface() {}
42 // Gets the syncable service for this store or null if it is not synced.
43 virtual syncer::SyncableService* GetSyncableService() = 0;
45 virtual bool AddEntry(const ArticleEntry& entry) = 0;
46 // Returns false if |entry| is not present or |entry| was not updated.
47 virtual bool UpdateEntry(const ArticleEntry& entry) = 0;
48 virtual bool RemoveEntry(const ArticleEntry& entry) = 0;
50 typedef base::Callback<void(bool success)> UpdateAttachmentsCallback;
51 typedef base::Callback<void(bool success,
52 scoped_ptr<ArticleAttachmentsData> attachments)>
53 GetAttachmentsCallback;
55 // Updates the attachments for an entry. The callback will be called with
56 // success==true once the new attachments have been stored locally and the
57 // entry has been updated. It will be called with success==false if that
58 // failed (e.g. storing the attachment failed, the entry couldn't be found,
59 // etc.).
60 virtual void UpdateAttachments(
61 const std::string& entry_id,
62 scoped_ptr<ArticleAttachmentsData> attachments,
63 const UpdateAttachmentsCallback& callback) = 0;
65 // Gets the attachments for an entry. If the attachments are available (either
66 // locally or from sync), the callback will be called with success==true and
67 // a pointer to the attachments. Otherwise it will be called with
68 // success==false.
69 virtual void GetAttachments(const std::string& entry_id,
70 const GetAttachmentsCallback& callback) = 0;
72 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry
73 // was found. On success, if |entry| is not null, it will contain the entry.
74 virtual bool GetEntryById(const std::string& entry_id,
75 ArticleEntry* entry) = 0;
76 virtual bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) = 0;
78 // Gets a copy of all the current entries.
79 virtual std::vector<ArticleEntry> GetEntries() const = 0;
81 virtual void AddObserver(DomDistillerObserver* observer) = 0;
83 virtual void RemoveObserver(DomDistillerObserver* observer) = 0;
86 // Implements syncing/storing of DomDistiller entries. This keeps three
87 // models of the DOM distiller data in sync: the local database, sync, and the
88 // user (i.e. of DomDistillerStore). No changes are accepted while the local
89 // database is loading. Once the local database has loaded, changes from any of
90 // the three sources (technically just two, since changes don't come from the
91 // database) are handled similarly:
92 // 1. convert the change to a SyncChangeList.
93 // 2. apply that change to the in-memory model, calculating what changed
94 // (changes_applied) and what is missing--i.e. entries missing for a full merge,
95 // conflict resolution for normal changes-- (changes_missing).
96 // 3. send a message (possibly handled asynchronously) containing
97 // changes_missing to the source of the change.
98 // 4. send messages (possibly handled asynchronously) containing changes_applied
99 // to the other (i.e. non-source) two models.
100 // TODO(cjhopman): Support deleting entries.
101 class DomDistillerStore : public syncer::SyncableService,
102 public DomDistillerStoreInterface {
103 public:
104 typedef std::vector<ArticleEntry> EntryVector;
106 // Creates storage using the given database for local storage. Initializes the
107 // database with |database_dir|.
108 DomDistillerStore(
109 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database,
110 const base::FilePath& database_dir);
112 // Creates storage using the given database for local storage. Initializes the
113 // database with |database_dir|. Also initializes the internal model to
114 // |initial_model|.
115 DomDistillerStore(
116 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database,
117 const std::vector<ArticleEntry>& initial_data,
118 const base::FilePath& database_dir);
120 ~DomDistillerStore() override;
122 // DomDistillerStoreInterface implementation.
123 syncer::SyncableService* GetSyncableService() override;
125 bool AddEntry(const ArticleEntry& entry) override;
126 bool UpdateEntry(const ArticleEntry& entry) override;
127 bool RemoveEntry(const ArticleEntry& entry) override;
129 void UpdateAttachments(const std::string& entry_id,
130 scoped_ptr<ArticleAttachmentsData> attachments_data,
131 const UpdateAttachmentsCallback& callback) override;
132 void GetAttachments(const std::string& entry_id,
133 const GetAttachmentsCallback& callback) override;
135 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override;
136 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override;
137 std::vector<ArticleEntry> GetEntries() const override;
139 void AddObserver(DomDistillerObserver* observer) override;
140 void RemoveObserver(DomDistillerObserver* observer) override;
142 // syncer::SyncableService implementation.
143 syncer::SyncMergeResult MergeDataAndStartSyncing(
144 syncer::ModelType type,
145 const syncer::SyncDataList& initial_sync_data,
146 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
147 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
148 void StopSyncing(syncer::ModelType type) override;
149 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
150 syncer::SyncError ProcessSyncChanges(
151 const tracked_objects::Location& from_here,
152 const syncer::SyncChangeList& change_list) override;
154 private:
155 void OnDatabaseInit(bool success);
156 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries);
157 void OnDatabaseSave(bool success);
159 // Returns true if the change is successfully applied.
160 bool ChangeEntry(const ArticleEntry& entry,
161 syncer::SyncChange::SyncChangeType changeType);
163 void OnAttachmentsWrite(
164 const std::string& entry_id,
165 scoped_ptr<sync_pb::ArticleAttachments> article_attachments,
166 const UpdateAttachmentsCallback& callback,
167 const syncer::AttachmentStore::Result& result);
169 void OnAttachmentsRead(const sync_pb::ArticleAttachments& attachments_proto,
170 const GetAttachmentsCallback& callback,
171 const syncer::AttachmentStore::Result& result,
172 scoped_ptr<syncer::AttachmentMap> attachments,
173 scoped_ptr<syncer::AttachmentIdList> missing);
175 syncer::SyncMergeResult MergeDataWithModel(
176 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied,
177 syncer::SyncChangeList* changes_missing);
179 // Convert a SyncDataList to a SyncChangeList of add or update changes based
180 // on the state of the in-memory model. Also calculate the entries missing
181 // from the SyncDataList.
182 void CalculateChangesForMerge(const syncer::SyncDataList& data,
183 syncer::SyncChangeList* changes_to_apply,
184 syncer::SyncChangeList* changes_missing);
186 bool ApplyChangesToSync(const tracked_objects::Location& from_here,
187 const syncer::SyncChangeList& change_list);
188 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list);
190 // Applies the changes to |model_|. If the model returns an error, disables
191 // syncing and database changes and returns false.
192 void ApplyChangesToModel(const syncer::SyncChangeList& change_list,
193 syncer::SyncChangeList* changes_applied,
194 syncer::SyncChangeList* changes_missing);
196 void NotifyObservers(const syncer::SyncChangeList& changes);
198 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
199 scoped_ptr<syncer::SyncErrorFactory> error_factory_;
200 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_;
201 bool database_loaded_;
202 scoped_ptr<syncer::AttachmentStore> attachment_store_;
203 ObserverList<DomDistillerObserver> observers_;
205 DomDistillerModel model_;
207 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_;
209 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore);
212 } // namespace dom_distiller
214 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_