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 |
| 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 // 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; |
| 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 // Returns true if the change is successfully applied. | 160 // Returns true if the change is successfully applied. |
124 bool ChangeEntry(const ArticleEntry& entry, | 161 bool ChangeEntry(const ArticleEntry& entry, |
125 syncer::SyncChange::SyncChangeType changeType); | 162 syncer::SyncChange::SyncChangeType changeType); |
126 | 163 |
| 164 void OnAttachmentsWrite( |
| 165 const std::string& entry_id, |
| 166 scoped_ptr<sync_pb::ArticleAttachments> article_attachments, |
| 167 const UpdateAttachmentsCallback& callback, |
| 168 const syncer::AttachmentStore::Result& result); |
| 169 |
| 170 void OnAttachmentsRead(const sync_pb::ArticleAttachments& attachments_proto, |
| 171 const GetAttachmentsCallback& callback, |
| 172 const syncer::AttachmentStore::Result& result, |
| 173 scoped_ptr<syncer::AttachmentMap> attachments, |
| 174 scoped_ptr<syncer::AttachmentIdList> missing); |
| 175 |
127 syncer::SyncMergeResult MergeDataWithModel( | 176 syncer::SyncMergeResult MergeDataWithModel( |
128 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, | 177 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, |
129 syncer::SyncChangeList* changes_missing); | 178 syncer::SyncChangeList* changes_missing); |
130 | 179 |
131 // Convert a SyncDataList to a SyncChangeList of add or update changes based | 180 // Convert a SyncDataList to a SyncChangeList of add or update changes based |
132 // on the state of the in-memory model. Also calculate the entries missing | 181 // on the state of the in-memory model. Also calculate the entries missing |
133 // from the SyncDataList. | 182 // from the SyncDataList. |
134 void CalculateChangesForMerge(const syncer::SyncDataList& data, | 183 void CalculateChangesForMerge(const syncer::SyncDataList& data, |
135 syncer::SyncChangeList* changes_to_apply, | 184 syncer::SyncChangeList* changes_to_apply, |
136 syncer::SyncChangeList* changes_missing); | 185 syncer::SyncChangeList* changes_missing); |
137 | 186 |
138 bool ApplyChangesToSync(const tracked_objects::Location& from_here, | 187 bool ApplyChangesToSync(const tracked_objects::Location& from_here, |
139 const syncer::SyncChangeList& change_list); | 188 const syncer::SyncChangeList& change_list); |
140 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); | 189 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); |
141 | 190 |
142 // Applies the changes to |model_|. If the model returns an error, disables | 191 // Applies the changes to |model_|. If the model returns an error, disables |
143 // syncing and database changes and returns false. | 192 // syncing and database changes and returns false. |
144 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, | 193 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, |
145 syncer::SyncChangeList* changes_applied, | 194 syncer::SyncChangeList* changes_applied, |
146 syncer::SyncChangeList* changes_missing); | 195 syncer::SyncChangeList* changes_missing); |
147 | 196 |
148 void NotifyObservers(const syncer::SyncChangeList& changes); | 197 void NotifyObservers(const syncer::SyncChangeList& changes); |
149 | 198 |
150 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 199 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
151 scoped_ptr<syncer::SyncErrorFactory> error_factory_; | 200 scoped_ptr<syncer::SyncErrorFactory> error_factory_; |
152 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; | 201 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; |
153 bool database_loaded_; | 202 bool database_loaded_; |
| 203 scoped_refptr<syncer::AttachmentStore> attachment_store_; |
154 ObserverList<DomDistillerObserver> observers_; | 204 ObserverList<DomDistillerObserver> observers_; |
155 | 205 |
156 DomDistillerModel model_; | 206 DomDistillerModel model_; |
157 | 207 |
158 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; | 208 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; |
159 | 209 |
160 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); | 210 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); |
161 }; | 211 }; |
162 | 212 |
163 } // namespace dom_distiller | 213 } // namespace dom_distiller |
164 | 214 |
165 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 215 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
OLD | NEW |