| 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_entry.h" | 14 #include "components/dom_distiller/core/article_entry.h" |
| 15 #include "components/dom_distiller/core/dom_distiller_database.h" | |
| 16 #include "components/dom_distiller/core/dom_distiller_model.h" | 15 #include "components/dom_distiller/core/dom_distiller_model.h" |
| 17 #include "components/dom_distiller/core/dom_distiller_observer.h" | 16 #include "components/dom_distiller/core/dom_distiller_observer.h" |
| 17 #include "components/leveldb_proto/proto_database.h" |
| 18 #include "sync/api/sync_change.h" | 18 #include "sync/api/sync_change.h" |
| 19 #include "sync/api/sync_data.h" | 19 #include "sync/api/sync_data.h" |
| 20 #include "sync/api/sync_error.h" | 20 #include "sync/api/sync_error.h" |
| 21 #include "sync/api/sync_error_factory.h" | 21 #include "sync/api/sync_error_factory.h" |
| 22 #include "sync/api/sync_merge_result.h" | 22 #include "sync/api/sync_merge_result.h" |
| 23 #include "sync/api/syncable_service.h" | 23 #include "sync/api/syncable_service.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 namespace base { | 26 namespace base { |
| 27 class FilePath; | 27 class FilePath; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // (changes_applied) and what is missing--i.e. entries missing for a full merge, | 69 // (changes_applied) and what is missing--i.e. entries missing for a full merge, |
| 70 // conflict resolution for normal changes-- (changes_missing). | 70 // conflict resolution for normal changes-- (changes_missing). |
| 71 // 3. send a message (possibly handled asynchronously) containing | 71 // 3. send a message (possibly handled asynchronously) containing |
| 72 // changes_missing to the source of the change. | 72 // changes_missing to the source of the change. |
| 73 // 4. send messages (possibly handled asynchronously) containing changes_applied | 73 // 4. send messages (possibly handled asynchronously) containing changes_applied |
| 74 // to the other (i.e. non-source) two models. | 74 // to the other (i.e. non-source) two models. |
| 75 // TODO(cjhopman): Support deleting entries. | 75 // TODO(cjhopman): Support deleting entries. |
| 76 class DomDistillerStore : public syncer::SyncableService, | 76 class DomDistillerStore : public syncer::SyncableService, |
| 77 public DomDistillerStoreInterface { | 77 public DomDistillerStoreInterface { |
| 78 public: | 78 public: |
| 79 typedef std::vector<ArticleEntry> EntryVector; |
| 80 |
| 79 // Creates storage using the given database for local storage. Initializes the | 81 // Creates storage using the given database for local storage. Initializes the |
| 80 // database with |database_dir|. | 82 // database with |database_dir|. |
| 81 DomDistillerStore(scoped_ptr<DomDistillerDatabaseInterface> database, | 83 DomDistillerStore( |
| 82 const base::FilePath& database_dir); | 84 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, |
| 85 const base::FilePath& database_dir); |
| 83 | 86 |
| 84 // Creates storage using the given database for local storage. Initializes the | 87 // Creates storage using the given database for local storage. Initializes the |
| 85 // database with |database_dir|. Also initializes the internal model to | 88 // database with |database_dir|. Also initializes the internal model to |
| 86 // |initial_model|. | 89 // |initial_model|. |
| 87 DomDistillerStore(scoped_ptr<DomDistillerDatabaseInterface> database, | 90 DomDistillerStore( |
| 88 const std::vector<ArticleEntry>& initial_data, | 91 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database, |
| 89 const base::FilePath& database_dir); | 92 const std::vector<ArticleEntry>& initial_data, |
| 93 const base::FilePath& database_dir); |
| 90 | 94 |
| 91 virtual ~DomDistillerStore(); | 95 virtual ~DomDistillerStore(); |
| 92 | 96 |
| 93 // DomDistillerStoreInterface implementation. | 97 // DomDistillerStoreInterface implementation. |
| 94 virtual syncer::SyncableService* GetSyncableService() OVERRIDE; | 98 virtual syncer::SyncableService* GetSyncableService() OVERRIDE; |
| 95 virtual bool AddEntry(const ArticleEntry& entry) OVERRIDE; | 99 virtual bool AddEntry(const ArticleEntry& entry) OVERRIDE; |
| 96 virtual bool UpdateEntry(const ArticleEntry& entry) OVERRIDE; | 100 virtual bool UpdateEntry(const ArticleEntry& entry) OVERRIDE; |
| 97 virtual bool RemoveEntry(const ArticleEntry& entry) OVERRIDE; | 101 virtual bool RemoveEntry(const ArticleEntry& entry) OVERRIDE; |
| 98 virtual bool GetEntryById(const std::string& entry_id, | 102 virtual bool GetEntryById(const std::string& entry_id, |
| 99 ArticleEntry* entry) OVERRIDE; | 103 ArticleEntry* entry) OVERRIDE; |
| 100 virtual bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) OVERRIDE; | 104 virtual bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) OVERRIDE; |
| 101 virtual std::vector<ArticleEntry> GetEntries() const OVERRIDE; | 105 virtual std::vector<ArticleEntry> GetEntries() const OVERRIDE; |
| 102 virtual void AddObserver(DomDistillerObserver* observer) OVERRIDE; | 106 virtual void AddObserver(DomDistillerObserver* observer) OVERRIDE; |
| 103 virtual void RemoveObserver(DomDistillerObserver* observer) OVERRIDE; | 107 virtual void RemoveObserver(DomDistillerObserver* observer) OVERRIDE; |
| 104 | 108 |
| 105 // syncer::SyncableService implementation. | 109 // syncer::SyncableService implementation. |
| 106 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( | 110 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( |
| 107 syncer::ModelType type, | 111 syncer::ModelType type, const syncer::SyncDataList& initial_sync_data, |
| 108 const syncer::SyncDataList& initial_sync_data, | |
| 109 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 112 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 110 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE; | 113 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE; |
| 111 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; | 114 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; |
| 112 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const | 115 virtual syncer::SyncDataList GetAllSyncData( |
| 113 OVERRIDE; | 116 syncer::ModelType type) const OVERRIDE; |
| 114 virtual syncer::SyncError ProcessSyncChanges( | 117 virtual syncer::SyncError ProcessSyncChanges( |
| 115 const tracked_objects::Location& from_here, | 118 const tracked_objects::Location& from_here, |
| 116 const syncer::SyncChangeList& change_list) OVERRIDE; | 119 const syncer::SyncChangeList& change_list) OVERRIDE; |
| 120 |
| 117 private: | 121 private: |
| 118 void OnDatabaseInit(bool success); | 122 void OnDatabaseInit(bool success); |
| 119 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries); | 123 void OnDatabaseLoad(bool success, scoped_ptr<EntryVector> entries); |
| 120 void OnDatabaseSave(bool success); | 124 void OnDatabaseSave(bool success); |
| 121 | 125 |
| 122 syncer::SyncMergeResult MergeDataWithModel( | 126 syncer::SyncMergeResult MergeDataWithModel( |
| 123 const syncer::SyncDataList& data, | 127 const syncer::SyncDataList& data, syncer::SyncChangeList* changes_applied, |
| 124 syncer::SyncChangeList* changes_applied, | |
| 125 syncer::SyncChangeList* changes_missing); | 128 syncer::SyncChangeList* changes_missing); |
| 126 | 129 |
| 127 // Convert a SyncDataList to a SyncChangeList of add or update changes based | 130 // Convert a SyncDataList to a SyncChangeList of add or update changes based |
| 128 // on the state of the in-memory model. Also calculate the entries missing | 131 // on the state of the in-memory model. Also calculate the entries missing |
| 129 // from the SyncDataList. | 132 // from the SyncDataList. |
| 130 void CalculateChangesForMerge(const syncer::SyncDataList& data, | 133 void CalculateChangesForMerge(const syncer::SyncDataList& data, |
| 131 syncer::SyncChangeList* changes_to_apply, | 134 syncer::SyncChangeList* changes_to_apply, |
| 132 syncer::SyncChangeList* changes_missing); | 135 syncer::SyncChangeList* changes_missing); |
| 133 | 136 |
| 134 bool ApplyChangesToSync(const tracked_objects::Location& from_here, | 137 bool ApplyChangesToSync(const tracked_objects::Location& from_here, |
| 135 const syncer::SyncChangeList& change_list); | 138 const syncer::SyncChangeList& change_list); |
| 136 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); | 139 bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list); |
| 137 | 140 |
| 138 // Applies the changes to |model_|. If the model returns an error, disables | 141 // Applies the changes to |model_|. If the model returns an error, disables |
| 139 // syncing and database changes and returns false. | 142 // syncing and database changes and returns false. |
| 140 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, | 143 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, |
| 141 syncer::SyncChangeList* changes_applied, | 144 syncer::SyncChangeList* changes_applied, |
| 142 syncer::SyncChangeList* changes_missing); | 145 syncer::SyncChangeList* changes_missing); |
| 143 | 146 |
| 144 void NotifyObservers(const syncer::SyncChangeList& changes); | 147 void NotifyObservers(const syncer::SyncChangeList& changes); |
| 145 | 148 |
| 146 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 149 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 147 scoped_ptr<syncer::SyncErrorFactory> error_factory_; | 150 scoped_ptr<syncer::SyncErrorFactory> error_factory_; |
| 148 scoped_ptr<DomDistillerDatabaseInterface> database_; | 151 scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> > database_; |
| 149 bool database_loaded_; | 152 bool database_loaded_; |
| 150 ObserverList<DomDistillerObserver> observers_; | 153 ObserverList<DomDistillerObserver> observers_; |
| 151 | 154 |
| 152 DomDistillerModel model_; | 155 DomDistillerModel model_; |
| 153 | 156 |
| 154 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; | 157 base::WeakPtrFactory<DomDistillerStore> weak_ptr_factory_; |
| 155 | 158 |
| 156 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); | 159 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore); |
| 157 }; | 160 }; |
| 158 | 161 |
| 159 } // namespace dom_distiller | 162 } // namespace dom_distiller |
| 160 | 163 |
| 161 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ | 164 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_ |
| OLD | NEW |