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 SYNC_API_MODEL_TYPE_SERVICE_H_ | |
6 #define SYNC_API_MODEL_TYPE_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "sync/api/conflict_resolution.h" | |
14 #include "sync/api/entity_change.h" | |
15 #include "sync/api/entity_data.h" | |
16 #include "sync/api/model_type_change_processor.h" | |
17 #include "sync/api/sync_error.h" | |
18 #include "sync/base/sync_export.h" | |
19 #include "sync/internal_api/public/activation_context.h" | |
20 | |
21 namespace syncer { | |
22 class DataTypeErrorHandler; | |
23 } // namespace syncer | |
24 | |
25 namespace syncer_v2 { | |
26 | |
27 class DataBatch; | |
28 class MetadataChangeList; | |
29 | |
30 // Interface implemented by model types to receive updates from sync via the | |
31 // SharedModelTypeProcessor. Provides a way for sync to update the data and | |
32 // metadata for entities, as well as the model type state. | |
33 class SYNC_EXPORT ModelTypeService { | |
34 public: | |
35 typedef base::Callback<void(syncer::SyncError, std::unique_ptr<DataBatch>)> | |
36 DataCallback; | |
37 typedef std::vector<std::string> ClientTagList; | |
38 typedef base::Callback<std::unique_ptr<ModelTypeChangeProcessor>( | |
39 syncer::ModelType type, | |
40 ModelTypeService* service)> | |
41 ChangeProcessorFactory; | |
42 | |
43 ModelTypeService(const ChangeProcessorFactory& change_processor_factory, | |
44 syncer::ModelType type); | |
45 | |
46 virtual ~ModelTypeService(); | |
47 | |
48 // Creates an object used to communicate changes in the sync metadata to the | |
49 // model type store. | |
50 virtual std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() = 0; | |
51 | |
52 // Perform the initial merge between local and sync data. This should only be | |
53 // called when a data type is first enabled to start syncing, and there is no | |
54 // sync metadata. Best effort should be made to match local and sync data. The | |
55 // keys in the |entity_data_map| will have been created via GetClientTag(...), | |
56 // and if a local and sync data should match/merge but disagree on tags, the | |
57 // service should use the sync data's tag. Any local pieces of data that are | |
58 // not present in sync should immediately be Put(...) to the processor before | |
59 // returning. The same MetadataChangeList that was passed into this function | |
60 // can be passed to Put(...) calls. Delete(...) can also be called but should | |
61 // not be needed for most model types. Durable storage writes, if not able to | |
62 // combine all change atomically, should save the metadata after the data | |
63 // changes, so that this merge will be re-driven by sync if is not completely | |
64 // saved during the current run. | |
65 virtual syncer::SyncError MergeSyncData( | |
66 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
67 EntityDataMap entity_data_map) = 0; | |
68 | |
69 // Apply changes from the sync server locally. | |
70 // Please note that |entity_changes| might have fewer entries than | |
71 // |metadata_change_list| in case when some of the data changes are filtered | |
72 // out, or even be empty in case when a commit confirmation is processed and | |
73 // only the metadata needs to persisted. | |
74 virtual syncer::SyncError ApplySyncChanges( | |
75 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
76 EntityChangeList entity_changes) = 0; | |
77 | |
78 // Asynchronously retrieve the corresponding sync data for |client_tags|. | |
79 virtual void GetData(ClientTagList client_tags, DataCallback callback) = 0; | |
80 | |
81 // Asynchronously retrieve all of the local sync data. | |
82 virtual void GetAllData(DataCallback callback) = 0; | |
83 | |
84 // Get or generate a client tag for |entity_data|. | |
85 virtual std::string GetClientTag(const EntityData& entity_data) = 0; | |
86 | |
87 // Overridable notification for when the processor is set. This is typically | |
88 // when the service should start loading metadata and then subsequently giving | |
89 // it to the processor. | |
90 virtual void OnChangeProcessorSet() = 0; | |
91 | |
92 // Resolve a conflict between the client and server versions of data. They are | |
93 // guaranteed not to match (both be deleted or have identical specifics). A | |
94 // default implementation chooses the server data unless it is a deletion. | |
95 virtual ConflictResolution ResolveConflict( | |
96 const EntityData& local_data, | |
97 const EntityData& remote_data) const; | |
98 | |
99 // Called by the DataTypeController to gather additional information needed | |
100 // before the processor can be connected to a sync worker. Once the | |
101 // metadata has been loaded, the info is collected and given to |callback|. | |
102 void OnSyncStarting(syncer::DataTypeErrorHandler* error_handler, | |
103 const ModelTypeChangeProcessor::StartCallback& callback); | |
104 | |
105 // Indicates that we no longer want to do any sync-related things for this | |
106 // data type. Severs all ties to the sync thread, deletes all local sync | |
107 // metadata, and then destroys the change processor. | |
108 // TODO(crbug.com/584365): This needs to be called from DataTypeController. | |
109 void DisableSync(); | |
110 | |
111 protected: | |
112 void CreateChangeProcessor(); | |
113 | |
114 ModelTypeChangeProcessor* change_processor() const; | |
115 | |
116 void clear_change_processor(); | |
117 | |
118 private: | |
119 std::unique_ptr<ModelTypeChangeProcessor> change_processor_; | |
120 | |
121 ChangeProcessorFactory change_processor_factory_; | |
122 | |
123 const syncer::ModelType type_; | |
124 }; | |
125 | |
126 } // namespace syncer_v2 | |
127 | |
128 #endif // SYNC_API_MODEL_TYPE_SERVICE_H_ | |
OLD | NEW |