OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_READING_LIST_STORE_H_ |
| 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_STORE_H_ |
| 7 |
| 8 #include "components/leveldb_proto/proto_database.h" |
| 9 #include "components/sync/model/model_type_service.h" |
| 10 #include "components/sync/model/model_type_store.h" |
| 11 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" |
| 12 |
| 13 using ReadingListDB = |
| 14 leveldb_proto::ProtoDatabase<reading_list::ReadingListLocal>; |
| 15 |
| 16 namespace syncer { |
| 17 class ModelTypeService; |
| 18 class MutableDataBatch; |
| 19 } |
| 20 |
| 21 typedef base::Callback<void( |
| 22 const syncer::ModelTypeStore::InitCallback& callback)> |
| 23 StoreFactoryFunction; |
| 24 |
| 25 // A ReadingListModelStorage storing and syncing data in protobufs. |
| 26 class ReadingListStore : public ReadingListModelStorage, |
| 27 public syncer::ModelTypeService { |
| 28 using EntryVector = std::vector<reading_list::ReadingListLocal>; |
| 29 |
| 30 public: |
| 31 ReadingListStore(std::unique_ptr<ReadingListDB> database, |
| 32 const base::FilePath& database_dir, |
| 33 StoreFactoryFunction create_store_callback); |
| 34 ~ReadingListStore() override; |
| 35 |
| 36 // ReadingListModelStorage implementation |
| 37 void SetReadingListModel(ReadingListModelImpl* model) override; |
| 38 void BeginTransaction() override; |
| 39 void CommitTransaction() override; |
| 40 void SaveEntry(const ReadingListEntry& entry, |
| 41 bool read, |
| 42 bool from_sync) override; |
| 43 void RemoveEntry(const ReadingListEntry& entry, bool from_sync) override; |
| 44 |
| 45 // ReadingListModelStorage implementation. |
| 46 syncer::ModelTypeService* GetModelTypeService() override; |
| 47 |
| 48 // Creates an object used to communicate changes in the sync metadata to the |
| 49 // model type store. |
| 50 std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList() |
| 51 override; |
| 52 |
| 53 // Perform the initial merge between local and sync data. This should only be |
| 54 // called when a data type is first enabled to start syncing, and there is no |
| 55 // sync metadata. Best effort should be made to match local and sync data. The |
| 56 // keys in the |entity_data_map| will have been created via GetClientTag(...), |
| 57 // and if a local and sync data should match/merge but disagree on tags, the |
| 58 // service should use the sync data's tag. Any local pieces of data that are |
| 59 // not present in sync should immediately be Put(...) to the processor before |
| 60 // returning. The same MetadataChangeList that was passed into this function |
| 61 // can be passed to Put(...) calls. Delete(...) can also be called but should |
| 62 // not be needed for most model types. Durable storage writes, if not able to |
| 63 // combine all change atomically, should save the metadata after the data |
| 64 // changes, so that this merge will be re-driven by sync if is not completely |
| 65 // saved during the current run. |
| 66 syncer::SyncError MergeSyncData( |
| 67 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 68 syncer::EntityDataMap entity_data_map) override; |
| 69 |
| 70 // Apply changes from the sync server locally. |
| 71 // Please note that |entity_changes| might have fewer entries than |
| 72 // |metadata_change_list| in case when some of the data changes are filtered |
| 73 // out, or even be empty in case when a commit confirmation is processed and |
| 74 // only the metadata needs to persisted. |
| 75 syncer::SyncError ApplySyncChanges( |
| 76 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 77 syncer::EntityChangeList entity_changes) override; |
| 78 |
| 79 // Asynchronously retrieve the corresponding sync data for |storage_keys|. |
| 80 void GetData(StorageKeyList storage_keys, DataCallback callback) override; |
| 81 |
| 82 // Asynchronously retrieve all of the local sync data. |
| 83 void GetAllData(DataCallback callback) override; |
| 84 |
| 85 // Get or generate a client tag for |entity_data|. This must be the same tag |
| 86 // that was/would have been generated in the SyncableService/Directory world |
| 87 // for backward compatibility with pre-USS clients. The only time this |
| 88 // theoretically needs to be called is on the creation of local data, however |
| 89 // it is also used to verify the hash of remote data. If a data type was never |
| 90 // launched pre-USS, then method does not need to be different from |
| 91 // GetStorageKey(). |
| 92 std::string GetClientTag(const syncer::EntityData& entity_data) override; |
| 93 |
| 94 // Get or generate a storage key for |entity_data|. This will only ever be |
| 95 // called once when first encountering a remote entity. Local changes will |
| 96 // provide their storage keys directly to Put instead of using this method. |
| 97 // Theoretically this function doesn't need to be stable across multiple calls |
| 98 // on the same or different clients, but to keep things simple, it probably |
| 99 // should be. |
| 100 std::string GetStorageKey(const syncer::EntityData& entity_data) override; |
| 101 |
| 102 // Methods used as callbacks given to DataTypeStore. |
| 103 void OnStoreCreated(syncer::ModelTypeStore::Result result, |
| 104 std::unique_ptr<syncer::ModelTypeStore> store); |
| 105 |
| 106 private: |
| 107 // Callbacks needed for the database handling. |
| 108 void OnDatabaseInit(bool success); |
| 109 void OnDatabaseLoad( |
| 110 syncer::ModelTypeStore::Result result, |
| 111 std::unique_ptr<syncer::ModelTypeStore::RecordList> entries); |
| 112 void OnDatabaseSave(syncer::ModelTypeStore::Result result); |
| 113 void OnReadAllMetadata( |
| 114 syncer::ModelTypeStore::Result result, |
| 115 std::unique_ptr<syncer::ModelTypeStore::RecordList> metadata_records, |
| 116 const std::string& global_metadata); |
| 117 |
| 118 void AddEntryToBatch(syncer::MutableDataBatch* batch, |
| 119 const ReadingListEntry& entry, |
| 120 bool read); |
| 121 |
| 122 std::unique_ptr<syncer::ModelTypeStore> store_; |
| 123 bool database_loaded_; |
| 124 ReadingListModelImpl* model_; |
| 125 StoreFactoryFunction create_store_callback_; |
| 126 bool has_metadata_loaded_ = false; |
| 127 |
| 128 int pending_transaction_; |
| 129 std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch_; |
| 130 }; |
| 131 |
| 132 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_STORE_H_ |
OLD | NEW |