OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_INTERNAL_API_PUBLIC_SHARED_MODEL_TYPE_PROCESSOR_H_ | |
6 #define SYNC_INTERNAL_API_PUBLIC_SHARED_MODEL_TYPE_PROCESSOR_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <unordered_set> | |
12 | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 #include "sync/api/data_batch.h" | |
16 #include "sync/api/metadata_batch.h" | |
17 #include "sync/api/metadata_change_list.h" | |
18 #include "sync/api/model_type_change_processor.h" | |
19 #include "sync/api/model_type_service.h" | |
20 #include "sync/api/sync_error.h" | |
21 #include "sync/base/sync_export.h" | |
22 #include "sync/internal_api/public/base/model_type.h" | |
23 #include "sync/internal_api/public/data_type_error_handler.h" | |
24 #include "sync/internal_api/public/model_type_processor.h" | |
25 #include "sync/internal_api/public/non_blocking_sync_common.h" | |
26 #include "sync/protocol/data_type_state.pb.h" | |
27 #include "sync/protocol/sync.pb.h" | |
28 | |
29 namespace syncer_v2 { | |
30 struct ActivationContext; | |
31 class CommitQueue; | |
32 class ProcessorEntityTracker; | |
33 | |
34 // A sync component embedded on the synced type's thread that helps to handle | |
35 // communication between sync and model type threads. | |
36 class SYNC_EXPORT SharedModelTypeProcessor : public ModelTypeProcessor, | |
37 public ModelTypeChangeProcessor, | |
38 base::NonThreadSafe { | |
39 public: | |
40 SharedModelTypeProcessor(syncer::ModelType type, ModelTypeService* service); | |
41 ~SharedModelTypeProcessor() override; | |
42 | |
43 // An easily bound function that constructs a SharedModelTypeProcessor. | |
44 static std::unique_ptr<ModelTypeChangeProcessor> CreateAsChangeProcessor( | |
45 syncer::ModelType type, | |
46 ModelTypeService* service); | |
47 | |
48 // Whether the processor is allowing changes to its model type. If this is | |
49 // false, the service should not allow any changes to its data. | |
50 bool IsAllowingChanges() const; | |
51 | |
52 // Returns true if the handshake with sync thread is complete. | |
53 bool IsConnected() const; | |
54 | |
55 // ModelTypeChangeProcessor implementation. | |
56 void Put(const std::string& client_tag, | |
57 std::unique_ptr<EntityData> entity_data, | |
58 MetadataChangeList* metadata_change_list) override; | |
59 void Delete(const std::string& client_tag, | |
60 MetadataChangeList* metadata_change_list) override; | |
61 void OnMetadataLoaded(syncer::SyncError error, | |
62 std::unique_ptr<MetadataBatch> batch) override; | |
63 void OnSyncStarting(syncer::DataTypeErrorHandler* error_handler, | |
64 const StartCallback& callback) override; | |
65 void DisableSync() override; | |
66 syncer::SyncError CreateAndUploadError( | |
67 const tracked_objects::Location& location, | |
68 const std::string& message) override; | |
69 | |
70 // ModelTypeProcessor implementation. | |
71 void ConnectSync(std::unique_ptr<CommitQueue> worker) override; | |
72 void DisconnectSync() override; | |
73 void OnCommitCompleted(const sync_pb::DataTypeState& type_state, | |
74 const CommitResponseDataList& response_list) override; | |
75 void OnUpdateReceived(const sync_pb::DataTypeState& type_state, | |
76 const UpdateResponseDataList& updates) override; | |
77 | |
78 private: | |
79 friend class SharedModelTypeProcessorTest; | |
80 | |
81 using EntityMap = | |
82 std::map<std::string, std::unique_ptr<ProcessorEntityTracker>>; | |
83 using UpdateMap = std::map<std::string, std::unique_ptr<UpdateResponseData>>; | |
84 | |
85 // Check conditions, and if met inform sync that we are ready to connect. | |
86 void ConnectIfReady(); | |
87 | |
88 // Helper function to process the update for a single entity. If a local data | |
89 // change is required, it will be added to |entity_changes|. The return value | |
90 // is the tracker for this entity, or nullptr if the update should be ignored. | |
91 ProcessorEntityTracker* ProcessUpdate(const UpdateResponseData& update, | |
92 EntityChangeList* entity_changes); | |
93 | |
94 // Resolve a conflict between |update| and the pending commit in |entity|. | |
95 ConflictResolution::Type ResolveConflict(const UpdateResponseData& update, | |
96 ProcessorEntityTracker* entity, | |
97 EntityChangeList* changes); | |
98 | |
99 // Recommit all entities for encryption except those in |already_updated|. | |
100 void RecommitAllForEncryption(std::unordered_set<std::string> already_updated, | |
101 MetadataChangeList* metadata_changes); | |
102 | |
103 // Handle the first update received from the server after being enabled. | |
104 void OnInitialUpdateReceived(const sync_pb::DataTypeState& type_state, | |
105 const UpdateResponseDataList& updates); | |
106 | |
107 // ModelTypeService::GetData() callback for initial pending commit data. | |
108 void OnInitialPendingDataLoaded(syncer::SyncError error, | |
109 std::unique_ptr<DataBatch> data_batch); | |
110 | |
111 // ModelTypeService::GetData() callback for re-encryption commit data. | |
112 void OnDataLoadedForReEncryption(syncer::SyncError error, | |
113 std::unique_ptr<DataBatch> data_batch); | |
114 | |
115 // Caches EntityData from the |data_batch| in the entity trackers. | |
116 void ConsumeDataBatch(std::unique_ptr<DataBatch> data_batch); | |
117 | |
118 // Sends all commit requests that are due to be sent to the sync thread. | |
119 void FlushPendingCommitRequests(); | |
120 | |
121 // Computes the client tag hash for the given client |tag|. | |
122 std::string GetHashForTag(const std::string& tag); | |
123 | |
124 // Gets the entity for the given tag, or null if there isn't one. | |
125 ProcessorEntityTracker* GetEntityForTag(const std::string& tag); | |
126 | |
127 // Gets the entity for the given tag hash, or null if there isn't one. | |
128 ProcessorEntityTracker* GetEntityForTagHash(const std::string& tag_hash); | |
129 | |
130 // Create an entity in the entity map for |tag| and return a pointer to it. | |
131 // Requires that no entity for |tag| already exists in the map. | |
132 ProcessorEntityTracker* CreateEntity(const std::string& tag, | |
133 const EntityData& data); | |
134 | |
135 // Version of the above that generates a tag for |data|. | |
136 ProcessorEntityTracker* CreateEntity(const EntityData& data); | |
137 | |
138 const syncer::ModelType type_; | |
139 sync_pb::DataTypeState data_type_state_; | |
140 | |
141 // Stores the start callback in between OnSyncStarting() and ReadyToConnect(). | |
142 StartCallback start_callback_; | |
143 | |
144 // A cache for any error that may occur during startup and should be passed | |
145 // into the |start_callback_|. | |
146 syncer::SyncError start_error_; | |
147 | |
148 // Indicates whether the metadata has finished loading. | |
149 bool is_metadata_loaded_; | |
150 | |
151 // Indicates whether data for any initial pending commits has been loaded. | |
152 bool is_initial_pending_data_loaded_; | |
153 | |
154 // Reference to the CommitQueue. | |
155 // | |
156 // The interface hides the posting of tasks across threads as well as the | |
157 // CommitQueue's implementation. Both of these features are | |
158 // useful in tests. | |
159 std::unique_ptr<CommitQueue> worker_; | |
160 | |
161 // The set of sync entities known to this object. | |
162 EntityMap entities_; | |
163 | |
164 // ModelTypeService linked to this processor. | |
165 // The service owns this processor instance so the pointer should never | |
166 // become invalid. | |
167 ModelTypeService* const service_; | |
168 | |
169 // The object used for informing sync of errors; will be non-null after | |
170 // OnSyncStarting has been called. This pointer is not owned. | |
171 syncer::DataTypeErrorHandler* error_handler_; | |
172 | |
173 // WeakPtrFactory for this processor which will be sent to sync thread. | |
174 base::WeakPtrFactory<SharedModelTypeProcessor> weak_ptr_factory_; | |
175 }; | |
176 | |
177 } // namespace syncer_v2 | |
178 | |
179 #endif // SYNC_INTERNAL_API_PUBLIC_SHARED_MODEL_TYPE_PROCESSOR_H_ | |
OLD | NEW |