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