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