OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "components/dom_distiller/core/article_attachments_data.h" | |
14 #include "components/dom_distiller/core/article_entry.h" | 15 #include "components/dom_distiller/core/article_entry.h" |
15 #include "components/dom_distiller/core/dom_distiller_model.h" | 16 #include "components/dom_distiller/core/dom_distiller_model.h" |
16 #include "components/dom_distiller/core/dom_distiller_observer.h" | 17 #include "components/dom_distiller/core/dom_distiller_observer.h" |
17 #include "components/leveldb_proto/proto_database.h" | 18 #include "components/leveldb_proto/proto_database.h" |
18 #include "sync/api/sync_change.h" | 19 #include "sync/api/sync_change.h" |
19 #include "sync/api/sync_data.h" | 20 #include "sync/api/sync_data.h" |
20 #include "sync/api/sync_error.h" | 21 #include "sync/api/sync_error.h" |
21 #include "sync/api/sync_error_factory.h" | 22 #include "sync/api/sync_error_factory.h" |
22 #include "sync/api/sync_merge_result.h" | 23 #include "sync/api/sync_merge_result.h" |
23 #include "sync/api/syncable_service.h" | 24 #include "sync/api/syncable_service.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 class FilePath; | 28 class FilePath; |
28 } | 29 } |
29 | 30 |
31 namespace syncer { | |
32 class AttachmentStore; | |
33 } | |
34 | |
30 namespace dom_distiller { | 35 namespace dom_distiller { |
31 | 36 |
32 // Interface for accessing the stored/synced DomDistiller entries. | 37 // Interface for accessing the stored/synced DomDistiller entries. |
33 class DomDistillerStoreInterface { | 38 class DomDistillerStoreInterface { |
34 public: | 39 public: |
35 virtual ~DomDistillerStoreInterface() {} | 40 virtual ~DomDistillerStoreInterface() {} |
36 | 41 |
37 // Gets the syncable service for this store or null if it is not synced. | 42 // Gets the syncable service for this store or null if it is not synced. |
38 virtual syncer::SyncableService* GetSyncableService() = 0; | 43 virtual syncer::SyncableService* GetSyncableService() = 0; |
39 | 44 |
40 virtual bool AddEntry(const ArticleEntry& entry) = 0; | 45 virtual bool AddEntry(const ArticleEntry& entry) = 0; |
41 // Returns false if |entry| is not present or |entry| was not updated. | 46 // Returns false if |entry| is not present or |entry| was not updated. |
42 virtual bool UpdateEntry(const ArticleEntry& entry) = 0; | 47 virtual bool UpdateEntry(const ArticleEntry& entry) = 0; |
43 virtual bool RemoveEntry(const ArticleEntry& entry) = 0; | 48 virtual bool RemoveEntry(const ArticleEntry& entry) = 0; |
44 | 49 |
50 typedef base::Callback<void(bool success)> UpdateAttachmentsCallback; | |
51 typedef base::Callback<void(bool success, | |
52 scoped_ptr<ArticleAttachmentsData> attachments)> | |
53 GetAttachmentsCallback; | |
54 | |
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 | |
nyquist
2014/11/19 02:26:37
Optional nit: |success| here and below? Given how
| |
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; | |
64 | |
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 // the a pointer to the attachments. Otherwise it will be called with | |
nyquist
2014/11/19 02:26:37
Nit: remove "the"?
cjhopman
2014/11/19 19:31:54
Done.
| |
68 // success==false. | |
69 virtual void GetAttachments(const std::string& entry_id, | |
70 const GetAttachmentsCallback& callback) = 0; | |
71 | |
45 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry | 72 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry |
46 // was found. On success, if |entry| is not null, it will contain the entry. | 73 // was found. On success, if |entry| is not null, it will contain the entry. |
47 virtual bool GetEntryById(const std::string& entry_id, | 74 virtual bool GetEntryById(const std::string& entry_id, |
48 ArticleEntry* entry) = 0; | 75 ArticleEntry* entry) = 0; |
49 virtual bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) = 0; | 76 virtual bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) = 0; |
50 | 77 |
51 // Gets a copy of all the current entries. | 78 // Gets a copy of all the current entries. |
52 virtual std::vector<ArticleEntry> GetEntries() const = 0; | 79 virtual std::vector<ArticleEntry> GetEntries() const = 0; |
53 | 80 |
54 virtual void AddObserver(DomDistillerObserver* observer) = 0; | 81 virtual void AddObserver(DomDistillerObserver* observer) = 0; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 // |initial_model|. | 114 // |initial_model|. |
88 DomDistillerStore( | 115 DomDistillerStore( |
89 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, | 116 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, |
90 const std::vector<ArticleEntry>& initial_data, | 117 const std::vector<ArticleEntry>& initial_data, |
91 const base::FilePath& database_dir); | 118 const base::FilePath& database_dir); |
92 | 119 |
93 ~DomDistillerStore() override; | 120 ~DomDistillerStore() override; |
94 | 121 |
95 // DomDistillerStoreInterface implementation. | 122 // DomDistillerStoreInterface implementation. |
96 syncer::SyncableService* GetSyncableService() override; | 123 syncer::SyncableService* GetSyncableService() override; |
124 | |
97 bool AddEntry(const ArticleEntry& entry) override; | 125 bool AddEntry(const ArticleEntry& entry) override; |
98 bool UpdateEntry(const ArticleEntry& entry) override; | 126 bool UpdateEntry(const ArticleEntry& entry) override; |
99 bool RemoveEntry(const ArticleEntry& entry) override; | 127 bool RemoveEntry(const ArticleEntry& entry) override; |
128 | |
129 virtual void UpdateAttachments( | |
130 const std::string& entry_id, | |
131 scoped_ptr<ArticleAttachmentsData> attachments_data, | |
132 const UpdateAttachmentsCallback& callback) override; | |
133 virtual void GetAttachments(const std::string& entry_id, | |
134 const GetAttachmentsCallback& callback) override; | |
135 | |
100 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override; | 136 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override; |
101 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override; | 137 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override; |
102 std::vector<ArticleEntry> GetEntries() const override; | 138 std::vector<ArticleEntry> GetEntries() const override; |
139 | |
103 void AddObserver(DomDistillerObserver* observer) override; | 140 void AddObserver(DomDistillerObserver* observer) override; |
104 void RemoveObserver(DomDistillerObserver* observer) override; | 141 void RemoveObserver(DomDistillerObserver* observer) override; |
105 | 142 |
106 // syncer::SyncableService implementation. | 143 // syncer::SyncableService implementation. |
107 syncer::SyncMergeResult MergeDataAndStartSyncing( | 144 syncer::SyncMergeResult MergeDataAndStartSyncing( |
108 syncer::ModelType type, | 145 syncer::ModelType type, |
109 const syncer::SyncDataList& initial_sync_data, | 146 const syncer::SyncDataList& initial_sync_data, |
110 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 147 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
111 scoped_ptr<syncer::SyncErrorFactory> error_handler) override; | 148 scoped_ptr<syncer::SyncErrorFactory> error_handler) override; |
112 void StopSyncing(syncer::ModelType type) override; | 149 void StopSyncing(syncer::ModelType type) override; |
113 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | 150 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; |
114 syncer::SyncError ProcessSyncChanges( | 151 syncer::SyncError ProcessSyncChanges( |
115 const tracked_objects::Location& from_here, | 152 const tracked_objects::Location& from_here, |
116 const syncer::SyncChangeList& change_list) override; | 153 const syncer::SyncChangeList& change_list) override; |
117 | 154 |
118 private: | 155 private: |
119 void OnDatabaseInit(bool success); | 156 void OnDatabaseInit(bool success); |
120 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries); | 157 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries); |
121 void OnDatabaseSave(bool success); | 158 void OnDatabaseSave(bool success); |
122 | 159 |
123 bool ChangeEntry(const ArticleEntry& entry, | 160 bool ChangeEntry(const ArticleEntry& entry, |
124 syncer::SyncChange::SyncChangeType changeType); | 161 syncer::SyncChange::SyncChangeType changeType); |
125 | 162 |
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); | |
168 | |
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); | |
174 | |
126 syncer::SyncMergeResult MergeDataWithModel( | 175 syncer::SyncMergeResult MergeDataWithModel( |
127 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, | 176 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, |
128 syncer::SyncChangeList* changes_missing); | 177 syncer::SyncChangeList* changes_missing); |
129 | 178 |
130 // Convert a SyncDataList to a SyncChangeList of add or update changes based | 179 // Convert a SyncDataList to a SyncChangeList of add or update changes based |
131 // on the state of the in-memory model. Also calculate the entries missing | 180 // on the state of the in-memory model. Also calculate the entries missing |
132 // from the SyncDataList. | 181 // from the SyncDataList. |
133 void CalculateChangesForMerge(const syncer::SyncDataList& data, | 182 void CalculateChangesForMerge(const syncer::SyncDataList& data, |
134 syncer::SyncChangeList* changes_to_apply, | 183 syncer::SyncChangeList* changes_to_apply, |
135 syncer::SyncChangeList* changes_missing); | 184 syncer::SyncChangeList* changes_missing); |
136 | 185 |
137 bool ApplyChangesToSync(const tracked_objects::Location& from_here, | 186 bool ApplyChangesToSync(const tracked_objects::Location& from_here, |
138 const syncer::SyncChangeList& change_list); | 187 const syncer::SyncChangeList& change_list); |
139 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); | 188 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); |
140 | 189 |
141 // Applies the changes to |model_|. If the model returns an error, disables | 190 // Applies the changes to |model_|. If the model returns an error, disables |
142 // syncing and database changes and returns false. | 191 // syncing and database changes and returns false. |
143 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, | 192 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, |
144 syncer::SyncChangeList* changes_applied, | 193 syncer::SyncChangeList* changes_applied, |
145 syncer::SyncChangeList* changes_missing); | 194 syncer::SyncChangeList* changes_missing); |
146 | 195 |
147 void NotifyObservers(const syncer::SyncChangeList& changes); | 196 void NotifyObservers(const syncer::SyncChangeList& changes); |
148 | 197 |
149 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 198 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
150 scoped_ptr<syncer::SyncErrorFactory> error_factory_; | 199 scoped_ptr<syncer::SyncErrorFactory> error_factory_; |
151 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; | 200 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; |
152 bool database_loaded_; | 201 bool database_loaded_; |
202 scoped_refptr<syncer::AttachmentStore> attachment_store_; | |
153 ObserverList<DomDistillerObserver> observers_; | 203 ObserverList<DomDistillerObserver> observers_; |
154 | 204 |
155 DomDistillerModel model_; | 205 DomDistillerModel model_; |
156 | 206 |
157 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; | 207 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; |
158 | 208 |
159 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); | 209 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); |
160 }; | 210 }; |
161 | 211 |
162 } // namespace dom_distiller | 212 } // namespace dom_distiller |
163 | 213 |
164 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 214 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
OLD | NEW |