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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 // Gets the syncable service for this store or null if it is not synced. | 43 // Gets the syncable service for this store or null if it is not synced. |
44 virtual syncer::SyncableService* GetSyncableService() = 0; | 44 virtual syncer::SyncableService* GetSyncableService() = 0; |
45 | 45 |
46 virtual bool AddEntry(const ArticleEntry& entry) = 0; | 46 virtual bool AddEntry(const ArticleEntry& entry) = 0; |
47 // Returns false if |entry| is not present or |entry| was not updated. | 47 // Returns false if |entry| is not present or |entry| was not updated. |
48 virtual bool UpdateEntry(const ArticleEntry& entry) = 0; | 48 virtual bool UpdateEntry(const ArticleEntry& entry) = 0; |
49 virtual bool RemoveEntry(const ArticleEntry& entry) = 0; | 49 virtual bool RemoveEntry(const ArticleEntry& entry) = 0; |
50 | 50 |
51 typedef base::Callback<void(bool success)> UpdateAttachmentsCallback; | 51 typedef base::Callback<void(bool success)> UpdateAttachmentsCallback; |
52 typedef base::Callback<void(bool success, | 52 typedef base::Callback< |
53 scoped_ptr<ArticleAttachmentsData> attachments)> | 53 void(bool success, std::unique_ptr<ArticleAttachmentsData> attachments)> |
54 GetAttachmentsCallback; | 54 GetAttachmentsCallback; |
55 | 55 |
56 // Updates the attachments for an entry. The callback will be called with | 56 // Updates the attachments for an entry. The callback will be called with |
57 // success==true once the new attachments have been stored locally and the | 57 // success==true once the new attachments have been stored locally and the |
58 // entry has been updated. It will be called with success==false if that | 58 // entry has been updated. It will be called with success==false if that |
59 // failed (e.g. storing the attachment failed, the entry couldn't be found, | 59 // failed (e.g. storing the attachment failed, the entry couldn't be found, |
60 // etc.). | 60 // etc.). |
61 virtual void UpdateAttachments( | 61 virtual void UpdateAttachments( |
62 const std::string& entry_id, | 62 const std::string& entry_id, |
63 scoped_ptr<ArticleAttachmentsData> attachments, | 63 std::unique_ptr<ArticleAttachmentsData> attachments, |
64 const UpdateAttachmentsCallback& callback) = 0; | 64 const UpdateAttachmentsCallback& callback) = 0; |
65 | 65 |
66 // Gets the attachments for an entry. If the attachments are available (either | 66 // Gets the attachments for an entry. If the attachments are available (either |
67 // locally or from sync), the callback will be called with success==true and | 67 // locally or from sync), the callback will be called with success==true and |
68 // a pointer to the attachments. Otherwise it will be called with | 68 // a pointer to the attachments. Otherwise it will be called with |
69 // success==false. | 69 // success==false. |
70 virtual void GetAttachments(const std::string& entry_id, | 70 virtual void GetAttachments(const std::string& entry_id, |
71 const GetAttachmentsCallback& callback) = 0; | 71 const GetAttachmentsCallback& callback) = 0; |
72 | 72 |
73 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry | 73 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry |
(...skipping 26 matching lines...) Expand all Loading... |
100 // to the other (i.e. non-source) two models. | 100 // to the other (i.e. non-source) two models. |
101 // TODO(cjhopman): Support deleting entries. | 101 // TODO(cjhopman): Support deleting entries. |
102 class DomDistillerStore : public syncer::SyncableService, | 102 class DomDistillerStore : public syncer::SyncableService, |
103 public DomDistillerStoreInterface { | 103 public DomDistillerStoreInterface { |
104 public: | 104 public: |
105 typedef std::vector<ArticleEntry> EntryVector; | 105 typedef std::vector<ArticleEntry> EntryVector; |
106 | 106 |
107 // Creates storage using the given database for local storage. Initializes the | 107 // Creates storage using the given database for local storage. Initializes the |
108 // database with |database_dir|. | 108 // database with |database_dir|. |
109 DomDistillerStore( | 109 DomDistillerStore( |
110 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, | 110 std::unique_ptr<leveldb_proto::ProtoDatabase<ArticleEntry>> database, |
111 const base::FilePath& database_dir); | 111 const base::FilePath& database_dir); |
112 | 112 |
113 // Creates storage using the given database for local storage. Initializes the | 113 // Creates storage using the given database for local storage. Initializes the |
114 // database with |database_dir|. Also initializes the internal model to | 114 // database with |database_dir|. Also initializes the internal model to |
115 // |initial_model|. | 115 // |initial_model|. |
116 DomDistillerStore( | 116 DomDistillerStore( |
117 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, | 117 std::unique_ptr<leveldb_proto::ProtoDatabase<ArticleEntry>> database, |
118 const std::vector<ArticleEntry>& initial_data, | 118 const std::vector<ArticleEntry>& initial_data, |
119 const base::FilePath& database_dir); | 119 const base::FilePath& database_dir); |
120 | 120 |
121 ~DomDistillerStore() override; | 121 ~DomDistillerStore() override; |
122 | 122 |
123 // DomDistillerStoreInterface implementation. | 123 // DomDistillerStoreInterface implementation. |
124 syncer::SyncableService* GetSyncableService() override; | 124 syncer::SyncableService* GetSyncableService() override; |
125 | 125 |
126 bool AddEntry(const ArticleEntry& entry) override; | 126 bool AddEntry(const ArticleEntry& entry) override; |
127 bool UpdateEntry(const ArticleEntry& entry) override; | 127 bool UpdateEntry(const ArticleEntry& entry) override; |
128 bool RemoveEntry(const ArticleEntry& entry) override; | 128 bool RemoveEntry(const ArticleEntry& entry) override; |
129 | 129 |
130 void UpdateAttachments(const std::string& entry_id, | 130 void UpdateAttachments( |
131 scoped_ptr<ArticleAttachmentsData> attachments_data, | 131 const std::string& entry_id, |
132 const UpdateAttachmentsCallback& callback) override; | 132 std::unique_ptr<ArticleAttachmentsData> attachments_data, |
| 133 const UpdateAttachmentsCallback& callback) override; |
133 void GetAttachments(const std::string& entry_id, | 134 void GetAttachments(const std::string& entry_id, |
134 const GetAttachmentsCallback& callback) override; | 135 const GetAttachmentsCallback& callback) override; |
135 | 136 |
136 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override; | 137 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override; |
137 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override; | 138 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override; |
138 std::vector<ArticleEntry> GetEntries() const override; | 139 std::vector<ArticleEntry> GetEntries() const override; |
139 | 140 |
140 void AddObserver(DomDistillerObserver* observer) override; | 141 void AddObserver(DomDistillerObserver* observer) override; |
141 void RemoveObserver(DomDistillerObserver* observer) override; | 142 void RemoveObserver(DomDistillerObserver* observer) override; |
142 | 143 |
143 // syncer::SyncableService implementation. | 144 // syncer::SyncableService implementation. |
144 syncer::SyncMergeResult MergeDataAndStartSyncing( | 145 syncer::SyncMergeResult MergeDataAndStartSyncing( |
145 syncer::ModelType type, | 146 syncer::ModelType type, |
146 const syncer::SyncDataList& initial_sync_data, | 147 const syncer::SyncDataList& initial_sync_data, |
147 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 148 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, |
148 scoped_ptr<syncer::SyncErrorFactory> error_handler) override; | 149 std::unique_ptr<syncer::SyncErrorFactory> error_handler) override; |
149 void StopSyncing(syncer::ModelType type) override; | 150 void StopSyncing(syncer::ModelType type) override; |
150 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | 151 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; |
151 syncer::SyncError ProcessSyncChanges( | 152 syncer::SyncError ProcessSyncChanges( |
152 const tracked_objects::Location& from_here, | 153 const tracked_objects::Location& from_here, |
153 const syncer::SyncChangeList& change_list) override; | 154 const syncer::SyncChangeList& change_list) override; |
154 | 155 |
155 private: | 156 private: |
156 void OnDatabaseInit(bool success); | 157 void OnDatabaseInit(bool success); |
157 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries); | 158 void OnDatabaseLoad(bool success, std::unique_ptr<EntryVector> entries); |
158 void OnDatabaseSave(bool success); | 159 void OnDatabaseSave(bool success); |
159 | 160 |
160 // Returns true if the change is successfully applied. | 161 // Returns true if the change is successfully applied. |
161 bool ChangeEntry(const ArticleEntry& entry, | 162 bool ChangeEntry(const ArticleEntry& entry, |
162 syncer::SyncChange::SyncChangeType changeType); | 163 syncer::SyncChange::SyncChangeType changeType); |
163 | 164 |
164 void OnAttachmentsWrite( | 165 void OnAttachmentsWrite( |
165 const std::string& entry_id, | 166 const std::string& entry_id, |
166 scoped_ptr<sync_pb::ArticleAttachments> article_attachments, | 167 std::unique_ptr<sync_pb::ArticleAttachments> article_attachments, |
167 const UpdateAttachmentsCallback& callback, | 168 const UpdateAttachmentsCallback& callback, |
168 const syncer::AttachmentStore::Result& result); | 169 const syncer::AttachmentStore::Result& result); |
169 | 170 |
170 void OnAttachmentsRead(const sync_pb::ArticleAttachments& attachments_proto, | 171 void OnAttachmentsRead(const sync_pb::ArticleAttachments& attachments_proto, |
171 const GetAttachmentsCallback& callback, | 172 const GetAttachmentsCallback& callback, |
172 const syncer::AttachmentStore::Result& result, | 173 const syncer::AttachmentStore::Result& result, |
173 scoped_ptr<syncer::AttachmentMap> attachments, | 174 std::unique_ptr<syncer::AttachmentMap> attachments, |
174 scoped_ptr<syncer::AttachmentIdList> missing); | 175 std::unique_ptr<syncer::AttachmentIdList> missing); |
175 | 176 |
176 syncer::SyncMergeResult MergeDataWithModel( | 177 syncer::SyncMergeResult MergeDataWithModel( |
177 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, | 178 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, |
178 syncer::SyncChangeList* changes_missing); | 179 syncer::SyncChangeList* changes_missing); |
179 | 180 |
180 // Convert a SyncDataList to a SyncChangeList of add or update changes based | 181 // Convert a SyncDataList to a SyncChangeList of add or update changes based |
181 // on the state of the in-memory model. Also calculate the entries missing | 182 // on the state of the in-memory model. Also calculate the entries missing |
182 // from the SyncDataList. | 183 // from the SyncDataList. |
183 void CalculateChangesForMerge(const syncer::SyncDataList& data, | 184 void CalculateChangesForMerge(const syncer::SyncDataList& data, |
184 syncer::SyncChangeList* changes_to_apply, | 185 syncer::SyncChangeList* changes_to_apply, |
185 syncer::SyncChangeList* changes_missing); | 186 syncer::SyncChangeList* changes_missing); |
186 | 187 |
187 bool ApplyChangesToSync(const tracked_objects::Location& from_here, | 188 bool ApplyChangesToSync(const tracked_objects::Location& from_here, |
188 const syncer::SyncChangeList& change_list); | 189 const syncer::SyncChangeList& change_list); |
189 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); | 190 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); |
190 | 191 |
191 // Applies the changes to |model_|. If the model returns an error, disables | 192 // Applies the changes to |model_|. If the model returns an error, disables |
192 // syncing and database changes and returns false. | 193 // syncing and database changes and returns false. |
193 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, | 194 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, |
194 syncer::SyncChangeList* changes_applied, | 195 syncer::SyncChangeList* changes_applied, |
195 syncer::SyncChangeList* changes_missing); | 196 syncer::SyncChangeList* changes_missing); |
196 | 197 |
197 void NotifyObservers(const syncer::SyncChangeList& changes); | 198 void NotifyObservers(const syncer::SyncChangeList& changes); |
198 | 199 |
199 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 200 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_; |
200 scoped_ptr<syncer::SyncErrorFactory> error_factory_; | 201 std::unique_ptr<syncer::SyncErrorFactory> error_factory_; |
201 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; | 202 std::unique_ptr<leveldb_proto::ProtoDatabase<ArticleEntry>> database_; |
202 bool database_loaded_; | 203 bool database_loaded_; |
203 scoped_ptr<syncer::AttachmentStore> attachment_store_; | 204 std::unique_ptr<syncer::AttachmentStore> attachment_store_; |
204 base::ObserverList<DomDistillerObserver> observers_; | 205 base::ObserverList<DomDistillerObserver> observers_; |
205 | 206 |
206 DomDistillerModel model_; | 207 DomDistillerModel model_; |
207 | 208 |
208 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; | 209 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; |
209 | 210 |
210 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); | 211 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); |
211 }; | 212 }; |
212 | 213 |
213 } // namespace dom_distiller | 214 } // namespace dom_distiller |
214 | 215 |
215 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 216 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
OLD | NEW |