OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ | 5 #ifndef COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ |
6 #define COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ | 6 #define COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <unordered_set> | 11 #include <unordered_set> |
12 | 12 |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
15 #include "components/sync/api/data_batch.h" | 15 #include "components/sync/api/data_batch.h" |
16 #include "components/sync/api/data_type_error_handler.h" | 16 #include "components/sync/api/data_type_error_handler.h" |
17 #include "components/sync/api/metadata_batch.h" | 17 #include "components/sync/api/metadata_batch.h" |
18 #include "components/sync/api/metadata_change_list.h" | 18 #include "components/sync/api/metadata_change_list.h" |
19 #include "components/sync/api/model_type_change_processor.h" | 19 #include "components/sync/api/model_type_change_processor.h" |
20 #include "components/sync/api/model_type_service.h" | 20 #include "components/sync/api/model_type_service.h" |
21 #include "components/sync/api/sync_error.h" | 21 #include "components/sync/api/sync_error.h" |
22 #include "components/sync/base/model_type.h" | 22 #include "components/sync/base/model_type.h" |
23 #include "components/sync/core/model_type_processor.h" | 23 #include "components/sync/core/model_type_processor.h" |
24 #include "components/sync/core/non_blocking_sync_common.h" | 24 #include "components/sync/core/non_blocking_sync_common.h" |
25 #include "components/sync/protocol/data_type_state.pb.h" | 25 #include "components/sync/protocol/data_type_state.pb.h" |
26 #include "components/sync/protocol/sync.pb.h" | 26 #include "components/sync/protocol/sync.pb.h" |
27 | 27 |
28 namespace syncer { | 28 namespace syncer_v2 { |
29 struct ActivationContext; | 29 struct ActivationContext; |
30 class CommitQueue; | 30 class CommitQueue; |
31 class ProcessorEntityTracker; | 31 class ProcessorEntityTracker; |
32 | 32 |
33 // A sync component embedded on the synced type's thread that helps to handle | 33 // A sync component embedded on the synced type's thread that helps to handle |
34 // communication between sync and model type threads. | 34 // communication between sync and model type threads. |
35 class SharedModelTypeProcessor : public ModelTypeProcessor, | 35 class SharedModelTypeProcessor : public ModelTypeProcessor, |
36 public ModelTypeChangeProcessor, | 36 public ModelTypeChangeProcessor, |
37 base::NonThreadSafe { | 37 base::NonThreadSafe { |
38 public: | 38 public: |
39 SharedModelTypeProcessor(ModelType type, ModelTypeService* service); | 39 SharedModelTypeProcessor(syncer::ModelType type, ModelTypeService* service); |
40 ~SharedModelTypeProcessor() override; | 40 ~SharedModelTypeProcessor() override; |
41 | 41 |
42 // An easily bound function that constructs a SharedModelTypeProcessor. | 42 // An easily bound function that constructs a SharedModelTypeProcessor. |
43 static std::unique_ptr<ModelTypeChangeProcessor> CreateAsChangeProcessor( | 43 static std::unique_ptr<ModelTypeChangeProcessor> CreateAsChangeProcessor( |
44 ModelType type, | 44 syncer::ModelType type, |
45 ModelTypeService* service); | 45 ModelTypeService* service); |
46 | 46 |
47 // Whether the processor is allowing changes to its model type. If this is | 47 // Whether the processor is allowing changes to its model type. If this is |
48 // false, the service should not allow any changes to its data. | 48 // false, the service should not allow any changes to its data. |
49 bool IsAllowingChanges() const; | 49 bool IsAllowingChanges() const; |
50 | 50 |
51 // Returns true if the handshake with sync thread is complete. | 51 // Returns true if the handshake with sync thread is complete. |
52 bool IsConnected() const; | 52 bool IsConnected() const; |
53 | 53 |
54 // Returns a ListValue representing all nodes for data type |type| through | 54 // Returns a ListValue representing all nodes for data type |type| through |
55 // |callback| on this thread. | 55 // |callback| on this thread. |
56 // Used for populating nodes in Sync Node Browser of chrome://sync-internals. | 56 // Used for populating nodes in Sync Node Browser of chrome://sync-internals. |
57 // TODO(gangwu): GetAllNodes could be in a helper class. | 57 // TODO(gangwu): GetAllNodes could be in a helper class. |
58 void GetAllNodes( | 58 void GetAllNodes( |
59 const scoped_refptr<base::TaskRunner>& task_runner, | 59 const scoped_refptr<base::TaskRunner>& task_runner, |
60 const base::Callback<void(const ModelType type, | 60 const base::Callback<void(const syncer::ModelType type, |
61 std::unique_ptr<base::ListValue>)>& callback); | 61 std::unique_ptr<base::ListValue>)>& callback); |
62 | 62 |
63 // ModelTypeChangeProcessor implementation. | 63 // ModelTypeChangeProcessor implementation. |
64 void Put(const std::string& storage_key, | 64 void Put(const std::string& storage_key, |
65 std::unique_ptr<EntityData> entity_data, | 65 std::unique_ptr<EntityData> entity_data, |
66 MetadataChangeList* metadata_change_list) override; | 66 MetadataChangeList* metadata_change_list) override; |
67 void Delete(const std::string& storage_key, | 67 void Delete(const std::string& storage_key, |
68 MetadataChangeList* metadata_change_list) override; | 68 MetadataChangeList* metadata_change_list) override; |
69 void OnMetadataLoaded(SyncError error, | 69 void OnMetadataLoaded(syncer::SyncError error, |
70 std::unique_ptr<MetadataBatch> batch) override; | 70 std::unique_ptr<MetadataBatch> batch) override; |
71 void OnSyncStarting(std::unique_ptr<DataTypeErrorHandler> error_handler, | 71 void OnSyncStarting( |
72 const StartCallback& callback) override; | 72 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, |
| 73 const StartCallback& callback) override; |
73 void DisableSync() override; | 74 void DisableSync() override; |
74 SyncError CreateAndUploadError(const tracked_objects::Location& location, | 75 syncer::SyncError CreateAndUploadError( |
75 const std::string& message) override; | 76 const tracked_objects::Location& location, |
| 77 const std::string& message) override; |
76 | 78 |
77 // ModelTypeProcessor implementation. | 79 // ModelTypeProcessor implementation. |
78 void ConnectSync(std::unique_ptr<CommitQueue> worker) override; | 80 void ConnectSync(std::unique_ptr<CommitQueue> worker) override; |
79 void DisconnectSync() override; | 81 void DisconnectSync() override; |
80 void OnCommitCompleted(const sync_pb::DataTypeState& type_state, | 82 void OnCommitCompleted(const sync_pb::DataTypeState& type_state, |
81 const CommitResponseDataList& response_list) override; | 83 const CommitResponseDataList& response_list) override; |
82 void OnUpdateReceived(const sync_pb::DataTypeState& type_state, | 84 void OnUpdateReceived(const sync_pb::DataTypeState& type_state, |
83 const UpdateResponseDataList& updates) override; | 85 const UpdateResponseDataList& updates) override; |
84 | 86 |
85 private: | 87 private: |
(...skipping 19 matching lines...) Expand all Loading... |
105 | 107 |
106 // Recommit all entities for encryption except those in |already_updated|. | 108 // Recommit all entities for encryption except those in |already_updated|. |
107 void RecommitAllForEncryption(std::unordered_set<std::string> already_updated, | 109 void RecommitAllForEncryption(std::unordered_set<std::string> already_updated, |
108 MetadataChangeList* metadata_changes); | 110 MetadataChangeList* metadata_changes); |
109 | 111 |
110 // Handle the first update received from the server after being enabled. | 112 // Handle the first update received from the server after being enabled. |
111 void OnInitialUpdateReceived(const sync_pb::DataTypeState& type_state, | 113 void OnInitialUpdateReceived(const sync_pb::DataTypeState& type_state, |
112 const UpdateResponseDataList& updates); | 114 const UpdateResponseDataList& updates); |
113 | 115 |
114 // ModelTypeService::GetData() callback for initial pending commit data. | 116 // ModelTypeService::GetData() callback for initial pending commit data. |
115 void OnInitialPendingDataLoaded(SyncError error, | 117 void OnInitialPendingDataLoaded(syncer::SyncError error, |
116 std::unique_ptr<DataBatch> data_batch); | 118 std::unique_ptr<DataBatch> data_batch); |
117 | 119 |
118 // ModelTypeService::GetData() callback for re-encryption commit data. | 120 // ModelTypeService::GetData() callback for re-encryption commit data. |
119 void OnDataLoadedForReEncryption(SyncError error, | 121 void OnDataLoadedForReEncryption(syncer::SyncError error, |
120 std::unique_ptr<DataBatch> data_batch); | 122 std::unique_ptr<DataBatch> data_batch); |
121 | 123 |
122 // Caches EntityData from the |data_batch| in the entity trackers. | 124 // Caches EntityData from the |data_batch| in the entity trackers. |
123 void ConsumeDataBatch(std::unique_ptr<DataBatch> data_batch); | 125 void ConsumeDataBatch(std::unique_ptr<DataBatch> data_batch); |
124 | 126 |
125 // Sends all commit requests that are due to be sent to the sync thread. | 127 // Sends all commit requests that are due to be sent to the sync thread. |
126 void FlushPendingCommitRequests(); | 128 void FlushPendingCommitRequests(); |
127 | 129 |
128 // Computes the client tag hash for the given client |tag|. | 130 // Computes the client tag hash for the given client |tag|. |
129 std::string GetHashForTag(const std::string& tag); | 131 std::string GetHashForTag(const std::string& tag); |
(...skipping 17 matching lines...) Expand all Loading... |
147 ProcessorEntityTracker* CreateEntity(const std::string& storage_key, | 149 ProcessorEntityTracker* CreateEntity(const std::string& storage_key, |
148 const EntityData& data); | 150 const EntityData& data); |
149 | 151 |
150 // Version of the above that generates a tag for |data|. | 152 // Version of the above that generates a tag for |data|. |
151 ProcessorEntityTracker* CreateEntity(const EntityData& data); | 153 ProcessorEntityTracker* CreateEntity(const EntityData& data); |
152 | 154 |
153 // This is callback function for ModelTypeService::GetAllData. This function | 155 // This is callback function for ModelTypeService::GetAllData. This function |
154 // will merge real data |batch| with metadata, then pass to |callback|. | 156 // will merge real data |batch| with metadata, then pass to |callback|. |
155 void MergeDataWithMetadata( | 157 void MergeDataWithMetadata( |
156 const scoped_refptr<base::TaskRunner>& task_runner, | 158 const scoped_refptr<base::TaskRunner>& task_runner, |
157 const base::Callback<void(const ModelType, | 159 const base::Callback<void(const syncer::ModelType, |
158 std::unique_ptr<base::ListValue>)>& callback, | 160 std::unique_ptr<base::ListValue>)>& callback, |
159 SyncError error, | 161 syncer::SyncError error, |
160 std::unique_ptr<DataBatch> batch); | 162 std::unique_ptr<DataBatch> batch); |
161 | 163 |
162 const ModelType type_; | 164 const syncer::ModelType type_; |
163 sync_pb::DataTypeState data_type_state_; | 165 sync_pb::DataTypeState data_type_state_; |
164 | 166 |
165 // Stores the start callback in between OnSyncStarting() and ReadyToConnect(). | 167 // Stores the start callback in between OnSyncStarting() and ReadyToConnect(). |
166 StartCallback start_callback_; | 168 StartCallback start_callback_; |
167 | 169 |
168 // A cache for any error that may occur during startup and should be passed | 170 // A cache for any error that may occur during startup and should be passed |
169 // into the |start_callback_|. | 171 // into the |start_callback_|. |
170 SyncError start_error_; | 172 syncer::SyncError start_error_; |
171 | 173 |
172 // Indicates whether the metadata has finished loading. | 174 // Indicates whether the metadata has finished loading. |
173 bool is_metadata_loaded_; | 175 bool is_metadata_loaded_; |
174 | 176 |
175 // Indicates whether data for any initial pending commits has been loaded. | 177 // Indicates whether data for any initial pending commits has been loaded. |
176 bool is_initial_pending_data_loaded_; | 178 bool is_initial_pending_data_loaded_; |
177 | 179 |
178 // Reference to the CommitQueue. | 180 // Reference to the CommitQueue. |
179 // | 181 // |
180 // The interface hides the posting of tasks across threads as well as the | 182 // The interface hides the posting of tasks across threads as well as the |
(...skipping 12 matching lines...) Expand all Loading... |
193 // client tag hash. The other direction can use |entities_|. | 195 // client tag hash. The other direction can use |entities_|. |
194 std::map<std::string, std::string> storage_key_to_tag_hash_; | 196 std::map<std::string, std::string> storage_key_to_tag_hash_; |
195 | 197 |
196 // ModelTypeService linked to this processor. | 198 // ModelTypeService linked to this processor. |
197 // The service owns this processor instance so the pointer should never | 199 // The service owns this processor instance so the pointer should never |
198 // become invalid. | 200 // become invalid. |
199 ModelTypeService* const service_; | 201 ModelTypeService* const service_; |
200 | 202 |
201 // The object used for informing sync of errors; will be non-null after | 203 // The object used for informing sync of errors; will be non-null after |
202 // OnSyncStarting has been called. This pointer is not owned. | 204 // OnSyncStarting has been called. This pointer is not owned. |
203 std::unique_ptr<DataTypeErrorHandler> error_handler_; | 205 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler_; |
204 | 206 |
205 // WeakPtrFactory for this processor which will be sent to sync thread. | 207 // WeakPtrFactory for this processor which will be sent to sync thread. |
206 base::WeakPtrFactory<SharedModelTypeProcessor> weak_ptr_factory_; | 208 base::WeakPtrFactory<SharedModelTypeProcessor> weak_ptr_factory_; |
207 }; | 209 }; |
208 | 210 |
209 } // namespace syncer | 211 } // namespace syncer_v2 |
210 | 212 |
211 #endif // COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ | 213 #endif // COMPONENTS_SYNC_CORE_SHARED_MODEL_TYPE_PROCESSOR_H_ |
OLD | NEW |