OLD | NEW |
| (Empty) |
1 // Copyright 2015 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_SYNC_API_MODEL_TYPE_SERVICE_H_ | |
6 #define COMPONENTS_SYNC_API_MODEL_TYPE_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "components/sync/api/conflict_resolution.h" | |
15 #include "components/sync/api/data_type_error_handler.h" | |
16 #include "components/sync/api/entity_change.h" | |
17 #include "components/sync/api/entity_data.h" | |
18 #include "components/sync/api/model_type_change_processor.h" | |
19 #include "components/sync/api/sync_error.h" | |
20 #include "components/sync/core/activation_context.h" | |
21 | |
22 namespace syncer { | |
23 | |
24 class DataBatch; | |
25 class MetadataChangeList; | |
26 | |
27 // Interface implemented by model types to receive updates from sync via the | |
28 // SharedModelTypeProcessor. Provides a way for sync to update the data and | |
29 // metadata for entities, as well as the model type state. | |
30 class ModelTypeService : public base::SupportsWeakPtr<ModelTypeService> { | |
31 public: | |
32 typedef base::Callback<void(SyncError, std::unique_ptr<DataBatch>)> | |
33 DataCallback; | |
34 typedef std::vector<std::string> StorageKeyList; | |
35 typedef base::Callback<std::unique_ptr<ModelTypeChangeProcessor>( | |
36 ModelType type, | |
37 ModelTypeService* service)> | |
38 ChangeProcessorFactory; | |
39 | |
40 ModelTypeService(const ChangeProcessorFactory& change_processor_factory, | |
41 ModelType type); | |
42 | |
43 virtual ~ModelTypeService(); | |
44 | |
45 // Creates an object used to communicate changes in the sync metadata to the | |
46 // model type store. | |
47 virtual std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() = 0; | |
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 virtual SyncError MergeSyncData( | |
63 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
64 EntityDataMap entity_data_map) = 0; | |
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 virtual SyncError ApplySyncChanges( | |
72 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
73 EntityChangeList entity_changes) = 0; | |
74 | |
75 // Asynchronously retrieve the corresponding sync data for |storage_keys|. | |
76 virtual void GetData(StorageKeyList storage_keys, DataCallback callback) = 0; | |
77 | |
78 // Asynchronously retrieve all of the local sync data. | |
79 virtual void GetAllData(DataCallback callback) = 0; | |
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 virtual std::string GetClientTag(const EntityData& entity_data) = 0; | |
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 virtual std::string GetStorageKey(const EntityData& entity_data) = 0; | |
97 | |
98 // Overridable notification for when the processor is set. This is typically | |
99 // when the service should start loading metadata and then subsequently giving | |
100 // it to the processor. | |
101 virtual void OnChangeProcessorSet() = 0; | |
102 | |
103 // Resolve a conflict between the client and server versions of data. They are | |
104 // guaranteed not to match (both be deleted or have identical specifics). A | |
105 // default implementation chooses the server data unless it is a deletion. | |
106 virtual ConflictResolution ResolveConflict( | |
107 const EntityData& local_data, | |
108 const EntityData& remote_data) const; | |
109 | |
110 // Called by the DataTypeController to gather additional information needed | |
111 // before the processor can be connected to a sync worker. Once the | |
112 // metadata has been loaded, the info is collected and given to |callback|. | |
113 void OnSyncStarting(std::unique_ptr<DataTypeErrorHandler> error_handler, | |
114 const ModelTypeChangeProcessor::StartCallback& callback); | |
115 | |
116 // Indicates that we no longer want to do any sync-related things for this | |
117 // data type. Severs all ties to the sync thread, deletes all local sync | |
118 // metadata, and then destroys the change processor. | |
119 // TODO(crbug.com/584365): This needs to be called from DataTypeController. | |
120 void DisableSync(); | |
121 | |
122 ModelTypeChangeProcessor* change_processor() const; | |
123 | |
124 protected: | |
125 void CreateChangeProcessor(); | |
126 | |
127 void clear_change_processor(); | |
128 | |
129 private: | |
130 std::unique_ptr<ModelTypeChangeProcessor> change_processor_; | |
131 | |
132 ChangeProcessorFactory change_processor_factory_; | |
133 | |
134 const ModelType type_; | |
135 }; | |
136 | |
137 } // namespace syncer | |
138 | |
139 #endif // COMPONENTS_SYNC_API_MODEL_TYPE_SERVICE_H_ | |
OLD | NEW |