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 COMPONENTS_READING_LIST_IOS_READING_LIST_STORE_H_ | |
6 #define COMPONENTS_READING_LIST_IOS_READING_LIST_STORE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/threading/non_thread_safe.h" | |
12 #include "components/reading_list/ios/reading_list_model_storage.h" | |
13 #include "components/reading_list/ios/reading_list_store_delegate.h" | |
14 #include "components/sync/model/model_error.h" | |
15 #include "components/sync/model/model_type_store.h" | |
16 | |
17 namespace syncer { | |
18 class MutableDataBatch; | |
19 } | |
20 | |
21 class ReadingListModel; | |
22 | |
23 // A ReadingListModelStorage storing and syncing data in protobufs. | |
24 class ReadingListStore : public ReadingListModelStorage, | |
25 public base::NonThreadSafe { | |
26 using StoreFactoryFunction = base::Callback<void( | |
27 const syncer::ModelTypeStore::InitCallback& callback)>; | |
28 | |
29 public: | |
30 ReadingListStore(StoreFactoryFunction create_store_callback, | |
31 const ChangeProcessorFactory& change_processor_factory); | |
32 ~ReadingListStore() override; | |
33 | |
34 std::unique_ptr<ScopedBatchUpdate> EnsureBatchCreated() override; | |
35 | |
36 // ReadingListModelStorage implementation | |
37 void SetReadingListModel(ReadingListModel* model, | |
38 ReadingListStoreDelegate* delegate, | |
39 base::Clock* clock) override; | |
40 | |
41 void SaveEntry(const ReadingListEntry& entry) override; | |
42 void RemoveEntry(const ReadingListEntry& entry) 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 base::Optional<syncer::ModelError> 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 base::Optional<syncer::ModelError> ApplySyncChanges( | |
72 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | |
73 syncer::EntityChangeList entity_changes) override; | |
74 | |
75 // Returns whether entries respect a strict order for sync and if |rhs| can be | |
76 // submitted to sync after |lhs| has been received. | |
77 // The order should ensure that there is no sync loop in sync and should be | |
78 // submitted to sync in strictly increasing order. | |
79 // Entries are in increasing order if all the fields respect increasing order. | |
80 // - URL must be the same. | |
81 // - update_title_time_us: | |
82 // rhs.update_title_time_us >= lhs.update_title_time_us | |
83 // - title: | |
84 // if rhs.update_title_time_us > lhs.update_title_time_us | |
85 // title can be anything | |
86 // if rhs.update_title_time_us == lhs.update_title_time_us | |
87 // title must verify rhs.title.compare(lhs.title) >= 0 | |
88 // - creation_time_us: | |
89 // rhs.creation_time_us >= lhs.creation_time_us | |
90 // - rhs.first_read_time_us: | |
91 // if rhs.creation_time_us > lhs.creation_time_us, | |
92 // rhs.first_read_time_us can be anything. | |
93 // if rhs.creation_time_us == lhs.creation_time_us | |
94 // and rhs.first_read_time_us == 0 | |
95 // rhs.first_read_time_us can be anything. | |
96 // if rhs.creation_time_us == lhs.creation_time_us, | |
97 // rhs.first_read_time_us <= lhs.first_read_time_us | |
98 // - update_time_us: | |
99 // rhs.update_time_us >= lhs.update_time_us | |
100 // - state: | |
101 // if rhs.update_time_us > lhs.update_time_us | |
102 // rhs.state can be anything. | |
103 // if rhs.update_time_us == lhs.update_time_us | |
104 // rhs.state >= lhs.state in the order UNSEEN, UNREAD, READ. | |
105 static bool CompareEntriesForSync(const sync_pb::ReadingListSpecifics& lhs, | |
106 const sync_pb::ReadingListSpecifics& rhs); | |
107 | |
108 // Asynchronously retrieve the corresponding sync data for |storage_keys|. | |
109 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | |
110 | |
111 // Asynchronously retrieve all of the local sync data. | |
112 void GetAllData(DataCallback callback) override; | |
113 | |
114 // Get or generate a client tag for |entity_data|. This must be the same tag | |
115 // that was/would have been generated in the SyncableService/Directory world | |
116 // for backward compatibility with pre-USS clients. The only time this | |
117 // theoretically needs to be called is on the creation of local data, however | |
118 // it is also used to verify the hash of remote data. If a data type was never | |
119 // launched pre-USS, then method does not need to be different from | |
120 // GetStorageKey(). | |
121 std::string GetClientTag(const syncer::EntityData& entity_data) override; | |
122 | |
123 // Get or generate a storage key for |entity_data|. This will only ever be | |
124 // called once when first encountering a remote entity. Local changes will | |
125 // provide their storage keys directly to Put instead of using this method. | |
126 // Theoretically this function doesn't need to be stable across multiple calls | |
127 // on the same or different clients, but to keep things simple, it probably | |
128 // should be. | |
129 std::string GetStorageKey(const syncer::EntityData& entity_data) override; | |
130 | |
131 // Methods used as callbacks given to DataTypeStore. | |
132 void OnStoreCreated(syncer::ModelTypeStore::Result result, | |
133 std::unique_ptr<syncer::ModelTypeStore> store); | |
134 | |
135 class ScopedBatchUpdate : public ReadingListModelStorage::ScopedBatchUpdate { | |
136 public: | |
137 explicit ScopedBatchUpdate(ReadingListStore* store); | |
138 | |
139 ~ScopedBatchUpdate() override; | |
140 | |
141 private: | |
142 ReadingListStore* store_; | |
143 | |
144 DISALLOW_COPY_AND_ASSIGN(ScopedBatchUpdate); | |
145 }; | |
146 | |
147 private: | |
148 void BeginTransaction(); | |
149 void CommitTransaction(); | |
150 // Callbacks needed for the database handling. | |
151 void OnDatabaseLoad( | |
152 syncer::ModelTypeStore::Result result, | |
153 std::unique_ptr<syncer::ModelTypeStore::RecordList> entries); | |
154 void OnDatabaseSave(syncer::ModelTypeStore::Result result); | |
155 void OnReadAllMetadata(base::Optional<syncer::ModelError> error, | |
156 std::unique_ptr<syncer::MetadataBatch> metadata_batch); | |
157 | |
158 void AddEntryToBatch(syncer::MutableDataBatch* batch, | |
159 const ReadingListEntry& entry); | |
160 | |
161 std::unique_ptr<syncer::ModelTypeStore> store_; | |
162 ReadingListModel* model_; | |
163 ReadingListStoreDelegate* delegate_; | |
164 StoreFactoryFunction create_store_callback_; | |
165 | |
166 int pending_transaction_count_; | |
167 std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch_; | |
168 | |
169 base::Clock* clock_; | |
170 | |
171 DISALLOW_COPY_AND_ASSIGN(ReadingListStore); | |
172 }; | |
173 | |
174 #endif // COMPONENTS_READING_LIST_IOS_READING_LIST_STORE_H_ | |
OLD | NEW |