Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_store.h

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

Powered by Google App Engine
This is Rietveld 408576698