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