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

Side by Side Diff: components/sync/driver/generic_change_processor.cc

Issue 2203673002: [Sync] Move //components/sync_driver to //components/sync/driver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sd-a
Patch Set: Full change rebased on static lib. Created 4 years, 4 months 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
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 #include "components/sync_driver/generic_change_processor.h" 5 #include "components/sync/driver/generic_change_processor.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "components/sync/api/sync_change.h" 17 #include "components/sync/api/sync_change.h"
18 #include "components/sync/api/sync_error.h" 18 #include "components/sync/api/sync_error.h"
19 #include "components/sync/api/syncable_service.h" 19 #include "components/sync/api/syncable_service.h"
20 #include "components/sync/base/unrecoverable_error_handler.h" 20 #include "components/sync/base/unrecoverable_error_handler.h"
21 #include "components/sync/core/base_node.h" 21 #include "components/sync/core/base_node.h"
22 #include "components/sync/core/change_record.h" 22 #include "components/sync/core/change_record.h"
23 #include "components/sync/core/data_type_error_handler.h" 23 #include "components/sync/core/data_type_error_handler.h"
24 #include "components/sync/core/read_node.h" 24 #include "components/sync/core/read_node.h"
25 #include "components/sync/core/read_transaction.h" 25 #include "components/sync/core/read_transaction.h"
26 #include "components/sync/core/write_node.h" 26 #include "components/sync/core/write_node.h"
27 #include "components/sync/core/write_transaction.h" 27 #include "components/sync/core/write_transaction.h"
28 #include "components/sync/driver/sync_api_component_factory.h"
29 #include "components/sync/driver/sync_client.h"
28 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674. 30 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674.
29 #include "components/sync_driver/sync_api_component_factory.h"
30 #include "components/sync_driver/sync_client.h"
31 31
32 namespace sync_driver { 32 namespace sync_driver {
33 33
34 namespace { 34 namespace {
35 35
36 const int kContextSizeLimit = 1024; // Datatype context size limit. 36 const int kContextSizeLimit = 1024; // Datatype context size limit.
37 37
38 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics, 38 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics,
39 syncer::WriteNode* write_node) { 39 syncer::WriteNode* write_node) {
40 if (syncer::GetModelTypeFromSpecifics(entity_specifics) == 40 if (syncer::GetModelTypeFromSpecifics(entity_specifics) ==
41 syncer::PASSWORDS) { 41 syncer::PASSWORDS) {
42 write_node->SetPasswordSpecifics( 42 write_node->SetPasswordSpecifics(
43 entity_specifics.password().client_only_encrypted_data()); 43 entity_specifics.password().client_only_encrypted_data());
44 } else { 44 } else {
45 write_node->SetEntitySpecifics(entity_specifics); 45 write_node->SetEntitySpecifics(entity_specifics);
46 } 46 }
47 } 47 }
48 48
49 // Helper function to convert AttachmentId to AttachmentMetadataRecord. 49 // Helper function to convert AttachmentId to AttachmentMetadataRecord.
50 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord( 50 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord(
51 const syncer::AttachmentId& attachment_id) { 51 const syncer::AttachmentId& attachment_id) {
52 sync_pb::AttachmentMetadataRecord record; 52 sync_pb::AttachmentMetadataRecord record;
53 *record.mutable_id() = attachment_id.GetProto(); 53 *record.mutable_id() = attachment_id.GetProto();
54 return record; 54 return record;
55 } 55 }
56 56
57 // Replace |write_nodes|'s attachment ids with |attachment_ids|. 57 // Replace |write_nodes|'s attachment ids with |attachment_ids|.
58 void SetAttachmentMetadata(const syncer::AttachmentIdList& attachment_ids, 58 void SetAttachmentMetadata(const syncer::AttachmentIdList& attachment_ids,
59 syncer::WriteNode* write_node) { 59 syncer::WriteNode* write_node) {
60 DCHECK(write_node); 60 DCHECK(write_node);
61 sync_pb::AttachmentMetadata attachment_metadata; 61 sync_pb::AttachmentMetadata attachment_metadata;
62 std::transform( 62 std::transform(
63 attachment_ids.begin(), 63 attachment_ids.begin(), attachment_ids.end(),
64 attachment_ids.end(),
65 RepeatedFieldBackInserter(attachment_metadata.mutable_record()), 64 RepeatedFieldBackInserter(attachment_metadata.mutable_record()),
66 AttachmentIdToRecord); 65 AttachmentIdToRecord);
67 write_node->SetAttachmentMetadata(attachment_metadata); 66 write_node->SetAttachmentMetadata(attachment_metadata);
68 } 67 }
69 68
70 syncer::SyncData BuildRemoteSyncData( 69 syncer::SyncData BuildRemoteSyncData(
71 int64_t sync_id, 70 int64_t sync_id,
72 const syncer::ReadNode& read_node, 71 const syncer::ReadNode& read_node,
73 const syncer::AttachmentServiceProxy& attachment_service_proxy) { 72 const syncer::AttachmentServiceProxy& attachment_service_proxy) {
74 const syncer::AttachmentIdList& attachment_ids = read_node.GetAttachmentIds(); 73 const syncer::AttachmentIdList& attachment_ids = read_node.GetAttachmentIds();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 GenericChangeProcessor::~GenericChangeProcessor() { 149 GenericChangeProcessor::~GenericChangeProcessor() {
151 DCHECK(CalledOnValidThread()); 150 DCHECK(CalledOnValidThread());
152 } 151 }
153 152
154 void GenericChangeProcessor::ApplyChangesFromSyncModel( 153 void GenericChangeProcessor::ApplyChangesFromSyncModel(
155 const syncer::BaseTransaction* trans, 154 const syncer::BaseTransaction* trans,
156 int64_t model_version, 155 int64_t model_version,
157 const syncer::ImmutableChangeRecordList& changes) { 156 const syncer::ImmutableChangeRecordList& changes) {
158 DCHECK(CalledOnValidThread()); 157 DCHECK(CalledOnValidThread());
159 DCHECK(syncer_changes_.empty()); 158 DCHECK(syncer_changes_.empty());
160 for (syncer::ChangeRecordList::const_iterator it = 159 for (syncer::ChangeRecordList::const_iterator it = changes.Get().begin();
161 changes.Get().begin(); it != changes.Get().end(); ++it) { 160 it != changes.Get().end(); ++it) {
162 if (it->action == syncer::ChangeRecord::ACTION_DELETE) { 161 if (it->action == syncer::ChangeRecord::ACTION_DELETE) {
163 std::unique_ptr<sync_pb::EntitySpecifics> specifics; 162 std::unique_ptr<sync_pb::EntitySpecifics> specifics;
164 if (it->specifics.has_password()) { 163 if (it->specifics.has_password()) {
165 DCHECK(it->extra.get()); 164 DCHECK(it->extra.get());
166 specifics.reset(new sync_pb::EntitySpecifics(it->specifics)); 165 specifics.reset(new sync_pb::EntitySpecifics(it->specifics));
167 specifics->mutable_password()->mutable_client_only_encrypted_data()-> 166 specifics->mutable_password()
168 CopyFrom(it->extra->unencrypted()); 167 ->mutable_client_only_encrypted_data()
168 ->CopyFrom(it->extra->unencrypted());
169 } 169 }
170 const syncer::AttachmentIdList empty_list_of_attachment_ids; 170 const syncer::AttachmentIdList empty_list_of_attachment_ids;
171 syncer_changes_.push_back(syncer::SyncChange( 171 syncer_changes_.push_back(syncer::SyncChange(
172 FROM_HERE, syncer::SyncChange::ACTION_DELETE, 172 FROM_HERE, syncer::SyncChange::ACTION_DELETE,
173 syncer::SyncData::CreateRemoteData( 173 syncer::SyncData::CreateRemoteData(
174 it->id, specifics ? *specifics : it->specifics, base::Time(), 174 it->id, specifics ? *specifics : it->specifics, base::Time(),
175 empty_list_of_attachment_ids, attachment_service_proxy_))); 175 empty_list_of_attachment_ids, attachment_service_proxy_)));
176 } else { 176 } else {
177 syncer::SyncChange::SyncChangeType action = 177 syncer::SyncChange::SyncChangeType action =
178 (it->action == syncer::ChangeRecord::ACTION_ADD) ? 178 (it->action == syncer::ChangeRecord::ACTION_ADD)
179 syncer::SyncChange::ACTION_ADD : syncer::SyncChange::ACTION_UPDATE; 179 ? syncer::SyncChange::ACTION_ADD
180 : syncer::SyncChange::ACTION_UPDATE;
180 // Need to load specifics from node. 181 // Need to load specifics from node.
181 syncer::ReadNode read_node(trans); 182 syncer::ReadNode read_node(trans);
182 if (read_node.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { 183 if (read_node.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) {
183 syncer::SyncError error( 184 syncer::SyncError error(
184 FROM_HERE, 185 FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
185 syncer::SyncError::DATATYPE_ERROR,
186 "Failed to look up data for received change with id " + 186 "Failed to look up data for received change with id " +
187 base::Int64ToString(it->id), 187 base::Int64ToString(it->id),
188 syncer::GetModelTypeFromSpecifics(it->specifics)); 188 syncer::GetModelTypeFromSpecifics(it->specifics));
189 error_handler()->OnSingleDataTypeUnrecoverableError(error); 189 error_handler()->OnSingleDataTypeUnrecoverableError(error);
190 return; 190 return;
191 } 191 }
192 syncer_changes_.push_back(syncer::SyncChange( 192 syncer_changes_.push_back(syncer::SyncChange(
193 FROM_HERE, action, 193 FROM_HERE, action,
194 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_))); 194 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_)));
195 } 195 }
196 } 196 }
197 } 197 }
198 198
199 void GenericChangeProcessor::CommitChangesFromSyncModel() { 199 void GenericChangeProcessor::CommitChangesFromSyncModel() {
200 DCHECK(CalledOnValidThread()); 200 DCHECK(CalledOnValidThread());
201 if (syncer_changes_.empty()) 201 if (syncer_changes_.empty())
202 return; 202 return;
203 if (!local_service_.get()) { 203 if (!local_service_.get()) {
204 syncer::ModelType type = syncer_changes_[0].sync_data().GetDataType(); 204 syncer::ModelType type = syncer_changes_[0].sync_data().GetDataType();
205 syncer::SyncError error(FROM_HERE, 205 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
206 syncer::SyncError::DATATYPE_ERROR, 206 "Local service destroyed.", type);
207 "Local service destroyed.",
208 type);
209 error_handler()->OnSingleDataTypeUnrecoverableError(error); 207 error_handler()->OnSingleDataTypeUnrecoverableError(error);
210 return; 208 return;
211 } 209 }
212 syncer::SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, 210 syncer::SyncError error =
213 syncer_changes_); 211 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_);
214 syncer_changes_.clear(); 212 syncer_changes_.clear();
215 if (error.IsSet()) 213 if (error.IsSet())
216 error_handler()->OnSingleDataTypeUnrecoverableError(error); 214 error_handler()->OnSingleDataTypeUnrecoverableError(error);
217 } 215 }
218 216
219 syncer::SyncDataList GenericChangeProcessor::GetAllSyncData( 217 syncer::SyncDataList GenericChangeProcessor::GetAllSyncData(
220 syncer::ModelType type) const { 218 syncer::ModelType type) const {
221 DCHECK_EQ(type_, type); 219 DCHECK_EQ(type_, type);
222 // This is slow / memory intensive. Should be used sparingly by datatypes. 220 // This is slow / memory intensive. Should be used sparingly by datatypes.
223 syncer::SyncDataList data; 221 syncer::SyncDataList data;
224 GetAllSyncDataReturnError(&data); 222 GetAllSyncDataReturnError(&data);
225 return data; 223 return data;
226 } 224 }
227 225
228 syncer::SyncError GenericChangeProcessor::UpdateDataTypeContext( 226 syncer::SyncError GenericChangeProcessor::UpdateDataTypeContext(
229 syncer::ModelType type, 227 syncer::ModelType type,
230 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, 228 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status,
231 const std::string& context) { 229 const std::string& context) {
232 DCHECK(syncer::ProtocolTypes().Has(type)); 230 DCHECK(syncer::ProtocolTypes().Has(type));
233 DCHECK_EQ(type_, type); 231 DCHECK_EQ(type_, type);
234 232
235 if (context.size() > static_cast<size_t>(kContextSizeLimit)) { 233 if (context.size() > static_cast<size_t>(kContextSizeLimit)) {
236 return syncer::SyncError(FROM_HERE, 234 return syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
237 syncer::SyncError::DATATYPE_ERROR, 235 "Context size limit exceeded.", type);
238 "Context size limit exceeded.",
239 type);
240 } 236 }
241 237
242 syncer::WriteTransaction trans(FROM_HERE, share_handle()); 238 syncer::WriteTransaction trans(FROM_HERE, share_handle());
243 trans.SetDataTypeContext(type, refresh_status, context); 239 trans.SetDataTypeContext(type, refresh_status, context);
244 240
245 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can 241 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can
246 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|. 242 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|.
247 243
248 return syncer::SyncError(); 244 return syncer::SyncError();
249 } 245 }
250 246
251 void GenericChangeProcessor::OnAttachmentUploaded( 247 void GenericChangeProcessor::OnAttachmentUploaded(
252 const syncer::AttachmentId& attachment_id) { 248 const syncer::AttachmentId& attachment_id) {
253 syncer::WriteTransaction trans(FROM_HERE, share_handle()); 249 syncer::WriteTransaction trans(FROM_HERE, share_handle());
254 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id); 250 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id);
255 } 251 }
256 252
257 syncer::SyncError GenericChangeProcessor::GetAllSyncDataReturnError( 253 syncer::SyncError GenericChangeProcessor::GetAllSyncDataReturnError(
258 syncer::SyncDataList* current_sync_data) const { 254 syncer::SyncDataList* current_sync_data) const {
259 DCHECK(CalledOnValidThread()); 255 DCHECK(CalledOnValidThread());
260 std::string type_name = syncer::ModelTypeToString(type_); 256 std::string type_name = syncer::ModelTypeToString(type_);
261 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 257 syncer::ReadTransaction trans(FROM_HERE, share_handle());
262 syncer::ReadNode root(&trans); 258 syncer::ReadNode root(&trans);
263 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { 259 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) {
264 syncer::SyncError error(FROM_HERE, 260 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
265 syncer::SyncError::DATATYPE_ERROR,
266 "Server did not create the top-level " + type_name + 261 "Server did not create the top-level " + type_name +
267 " node. We might be running against an out-of-" 262 " node. We might be running against an out-of-"
268 "date server.", 263 "date server.",
269 type_); 264 type_);
270 return error; 265 return error;
271 } 266 }
272 267
273 // TODO(akalin): We'll have to do a tree traversal for bookmarks. 268 // TODO(akalin): We'll have to do a tree traversal for bookmarks.
274 DCHECK_NE(type_, syncer::BOOKMARKS); 269 DCHECK_NE(type_, syncer::BOOKMARKS);
275 270
276 std::vector<int64_t> child_ids; 271 std::vector<int64_t> child_ids;
277 root.GetChildIds(&child_ids); 272 root.GetChildIds(&child_ids);
278 273
279 for (std::vector<int64_t>::iterator it = child_ids.begin(); 274 for (std::vector<int64_t>::iterator it = child_ids.begin();
280 it != child_ids.end(); ++it) { 275 it != child_ids.end(); ++it) {
281 syncer::ReadNode sync_child_node(&trans); 276 syncer::ReadNode sync_child_node(&trans);
282 if (sync_child_node.InitByIdLookup(*it) != 277 if (sync_child_node.InitByIdLookup(*it) != syncer::BaseNode::INIT_OK) {
283 syncer::BaseNode::INIT_OK) {
284 syncer::SyncError error( 278 syncer::SyncError error(
285 FROM_HERE, 279 FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
286 syncer::SyncError::DATATYPE_ERROR, 280 "Failed to fetch child node for type " + type_name + ".", type_);
287 "Failed to fetch child node for type " + type_name + ".",
288 type_);
289 return error; 281 return error;
290 } 282 }
291 current_sync_data->push_back(BuildRemoteSyncData( 283 current_sync_data->push_back(BuildRemoteSyncData(
292 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_)); 284 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_));
293 } 285 }
294 return syncer::SyncError(); 286 return syncer::SyncError();
295 } 287 }
296 288
297 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const { 289 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const {
298 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 290 syncer::ReadTransaction trans(FROM_HERE, share_handle());
299 sync_pb::DataTypeContext context_proto; 291 sync_pb::DataTypeContext context_proto;
300 trans.GetDataTypeContext(type_, &context_proto); 292 trans.GetDataTypeContext(type_, &context_proto);
301 if (!context_proto.has_context()) 293 if (!context_proto.has_context())
302 return false; 294 return false;
303 295
304 DCHECK_EQ(type_, 296 DCHECK_EQ(type_, syncer::GetModelTypeFromSpecificsFieldNumber(
305 syncer::GetModelTypeFromSpecificsFieldNumber( 297 context_proto.data_type_id()));
306 context_proto.data_type_id()));
307 *context = context_proto.context(); 298 *context = context_proto.context();
308 return true; 299 return true;
309 } 300 }
310 301
311 int GenericChangeProcessor::GetSyncCount() { 302 int GenericChangeProcessor::GetSyncCount() {
312 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 303 syncer::ReadTransaction trans(FROM_HERE, share_handle());
313 syncer::ReadNode root(&trans); 304 syncer::ReadNode root(&trans);
314 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) 305 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK)
315 return 0; 306 return 0;
316 307
317 // Subtract one to account for type's root node. 308 // Subtract one to account for type's root node.
318 return root.GetTotalNodeCount() - 1; 309 return root.GetTotalNodeCount() - 1;
319 } 310 }
320 311
321 namespace { 312 namespace {
322 313
323 // WARNING: this code is sensitive to compiler optimizations. Be careful 314 // WARNING: this code is sensitive to compiler optimizations. Be careful
324 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else 315 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else
325 // the compiler attempts to merge it with other calls, losing useful information 316 // the compiler attempts to merge it with other calls, losing useful information
326 // in breakpad uploads. 317 // in breakpad uploads.
327 syncer::SyncError LogLookupFailure( 318 syncer::SyncError LogLookupFailure(
328 syncer::BaseNode::InitByLookupResult lookup_result, 319 syncer::BaseNode::InitByLookupResult lookup_result,
329 const tracked_objects::Location& from_here, 320 const tracked_objects::Location& from_here,
330 const std::string& error_prefix, 321 const std::string& error_prefix,
331 syncer::ModelType type, 322 syncer::ModelType type,
332 syncer::DataTypeErrorHandler* error_handler) { 323 syncer::DataTypeErrorHandler* error_handler) {
333 switch (lookup_result) { 324 switch (lookup_result) {
334 case syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: { 325 case syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: {
335 syncer::SyncError error; 326 syncer::SyncError error;
336 error.Reset(from_here, 327 error.Reset(
337 error_prefix + 328 from_here,
338 "could not find entry matching the lookup criteria.", 329 error_prefix + "could not find entry matching the lookup criteria.",
339 type); 330 type);
340 error_handler->OnSingleDataTypeUnrecoverableError(error); 331 error_handler->OnSingleDataTypeUnrecoverableError(error);
341 LOG(ERROR) << "Delete: Bad entry."; 332 LOG(ERROR) << "Delete: Bad entry.";
342 return error; 333 return error;
343 } 334 }
344 case syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL: { 335 case syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL: {
345 syncer::SyncError error; 336 syncer::SyncError error;
346 error.Reset(from_here, error_prefix + "entry is already deleted.", type); 337 error.Reset(from_here, error_prefix + "entry is already deleted.", type);
347 error_handler->OnSingleDataTypeUnrecoverableError(error); 338 error_handler->OnSingleDataTypeUnrecoverableError(error);
348 LOG(ERROR) << "Delete: Deleted entry."; 339 LOG(ERROR) << "Delete: Deleted entry.";
349 return error; 340 return error;
(...skipping 27 matching lines...) Expand all
377 368
378 syncer::SyncError AttemptDelete(const syncer::SyncChange& change, 369 syncer::SyncError AttemptDelete(const syncer::SyncChange& change,
379 syncer::ModelType type, 370 syncer::ModelType type,
380 const std::string& type_str, 371 const std::string& type_str,
381 syncer::WriteNode* node, 372 syncer::WriteNode* node,
382 syncer::DataTypeErrorHandler* error_handler) { 373 syncer::DataTypeErrorHandler* error_handler) {
383 DCHECK_EQ(change.change_type(), syncer::SyncChange::ACTION_DELETE); 374 DCHECK_EQ(change.change_type(), syncer::SyncChange::ACTION_DELETE);
384 if (change.sync_data().IsLocal()) { 375 if (change.sync_data().IsLocal()) {
385 const std::string& tag = syncer::SyncDataLocal(change.sync_data()).GetTag(); 376 const std::string& tag = syncer::SyncDataLocal(change.sync_data()).GetTag();
386 if (tag.empty()) { 377 if (tag.empty()) {
387 syncer::SyncError error( 378 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
388 FROM_HERE, 379 "Failed to delete " + type_str +
389 syncer::SyncError::DATATYPE_ERROR, 380 " node. Local data, empty tag. " +
390 "Failed to delete " + type_str + " node. Local data, empty tag. " + 381 change.location().ToString(),
391 change.location().ToString(), 382 type);
392 type);
393 error_handler->OnSingleDataTypeUnrecoverableError(error); 383 error_handler->OnSingleDataTypeUnrecoverableError(error);
394 NOTREACHED(); 384 NOTREACHED();
395 return error; 385 return error;
396 } 386 }
397 387
398 syncer::BaseNode::InitByLookupResult result = 388 syncer::BaseNode::InitByLookupResult result =
399 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag); 389 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag);
400 if (result != syncer::BaseNode::INIT_OK) { 390 if (result != syncer::BaseNode::INIT_OK) {
401 return LogLookupFailure( 391 return LogLookupFailure(result, FROM_HERE,
402 result, FROM_HERE, 392 "Failed to delete " + type_str +
403 "Failed to delete " + type_str + " node. Local data. " + 393 " node. Local data. " +
404 change.location().ToString(), 394 change.location().ToString(),
405 type, error_handler); 395 type, error_handler);
406 } 396 }
407 } else { 397 } else {
408 syncer::BaseNode::InitByLookupResult result = node->InitByIdLookup( 398 syncer::BaseNode::InitByLookupResult result = node->InitByIdLookup(
409 syncer::SyncDataRemote(change.sync_data()).GetId()); 399 syncer::SyncDataRemote(change.sync_data()).GetId());
410 if (result != syncer::BaseNode::INIT_OK) { 400 if (result != syncer::BaseNode::INIT_OK) {
411 return LogLookupFailure( 401 return LogLookupFailure(result, FROM_HERE,
412 result, FROM_HERE, 402 "Failed to delete " + type_str +
413 "Failed to delete " + type_str + " node. Non-local data. " + 403 " node. Non-local data. " +
414 change.location().ToString(), 404 change.location().ToString(),
415 type, error_handler); 405 type, error_handler);
416 } 406 }
417 } 407 }
418 if (IsActOnceDataType(type)) 408 if (IsActOnceDataType(type))
419 node->Drop(); 409 node->Drop();
420 else 410 else
421 node->Tombstone(); 411 node->Tombstone();
422 return syncer::SyncError(); 412 return syncer::SyncError();
423 } 413 }
424 414
425 } // namespace 415 } // namespace
426 416
427 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges( 417 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges(
428 const tracked_objects::Location& from_here, 418 const tracked_objects::Location& from_here,
429 const syncer::SyncChangeList& list_of_changes) { 419 const syncer::SyncChangeList& list_of_changes) {
430 DCHECK(CalledOnValidThread()); 420 DCHECK(CalledOnValidThread());
431 421
432 if (list_of_changes.empty()) { 422 if (list_of_changes.empty()) {
433 // No work. Exit without entering WriteTransaction. 423 // No work. Exit without entering WriteTransaction.
434 return syncer::SyncError(); 424 return syncer::SyncError();
435 } 425 }
436 426
437 // Keep track of brand new attachments so we can persist them on this device 427 // Keep track of brand new attachments so we can persist them on this device
438 // and upload them to the server. 428 // and upload them to the server.
439 syncer::AttachmentIdSet new_attachments; 429 syncer::AttachmentIdSet new_attachments;
440 430
441 syncer::WriteTransaction trans(from_here, share_handle()); 431 syncer::WriteTransaction trans(from_here, share_handle());
442 432
443 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin(); 433 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin();
444 iter != list_of_changes.end(); 434 iter != list_of_changes.end(); ++iter) {
445 ++iter) {
446 const syncer::SyncChange& change = *iter; 435 const syncer::SyncChange& change = *iter;
447 DCHECK_EQ(change.sync_data().GetDataType(), type_); 436 DCHECK_EQ(change.sync_data().GetDataType(), type_);
448 std::string type_str = syncer::ModelTypeToString(type_); 437 std::string type_str = syncer::ModelTypeToString(type_);
449 syncer::WriteNode sync_node(&trans); 438 syncer::WriteNode sync_node(&trans);
450 if (change.change_type() == syncer::SyncChange::ACTION_DELETE) { 439 if (change.change_type() == syncer::SyncChange::ACTION_DELETE) {
451 syncer::SyncError error = 440 syncer::SyncError error =
452 AttemptDelete(change, type_, type_str, &sync_node, error_handler()); 441 AttemptDelete(change, type_, type_str, &sync_node, error_handler());
453 if (error.IsSet()) { 442 if (error.IsSet()) {
454 NOTREACHED(); 443 NOTREACHED();
455 return error; 444 return error;
456 } 445 }
457 if (merge_result_.get()) { 446 if (merge_result_.get()) {
458 merge_result_->set_num_items_deleted( 447 merge_result_->set_num_items_deleted(
459 merge_result_->num_items_deleted() + 1); 448 merge_result_->num_items_deleted() + 1);
460 } 449 }
461 } else if (change.change_type() == syncer::SyncChange::ACTION_ADD) { 450 } else if (change.change_type() == syncer::SyncChange::ACTION_ADD) {
462 syncer::SyncError error = HandleActionAdd( 451 syncer::SyncError error = HandleActionAdd(change, type_str, trans,
463 change, type_str, trans, &sync_node, &new_attachments); 452 &sync_node, &new_attachments);
464 if (error.IsSet()) { 453 if (error.IsSet()) {
465 return error; 454 return error;
466 } 455 }
467 } else if (change.change_type() == syncer::SyncChange::ACTION_UPDATE) { 456 } else if (change.change_type() == syncer::SyncChange::ACTION_UPDATE) {
468 syncer::SyncError error = HandleActionUpdate( 457 syncer::SyncError error = HandleActionUpdate(
469 change, type_str, trans, &sync_node, &new_attachments); 458 change, type_str, trans, &sync_node, &new_attachments);
470 if (error.IsSet()) { 459 if (error.IsSet()) {
471 return error; 460 return error;
472 } 461 }
473 } else { 462 } else {
474 syncer::SyncError error( 463 syncer::SyncError error(
475 FROM_HERE, 464 FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
476 syncer::SyncError::DATATYPE_ERROR,
477 "Received unset SyncChange in the change processor, " + 465 "Received unset SyncChange in the change processor, " +
478 change.location().ToString(), 466 change.location().ToString(),
479 type_); 467 type_);
480 error_handler()->OnSingleDataTypeUnrecoverableError(error); 468 error_handler()->OnSingleDataTypeUnrecoverableError(error);
481 NOTREACHED(); 469 NOTREACHED();
482 LOG(ERROR) << "Unset sync change."; 470 LOG(ERROR) << "Unset sync change.";
483 return error; 471 return error;
484 } 472 }
485 } 473 }
486 474
487 if (!new_attachments.empty()) { 475 if (!new_attachments.empty()) {
488 // If datatype uses attachments it should have supplied attachment store 476 // If datatype uses attachments it should have supplied attachment store
489 // which would initialize attachment_service_. Fail if it isn't so. 477 // which would initialize attachment_service_. Fail if it isn't so.
490 if (!attachment_service_.get()) { 478 if (!attachment_service_.get()) {
491 syncer::SyncError error( 479 syncer::SyncError error(
492 FROM_HERE, 480 FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
493 syncer::SyncError::DATATYPE_ERROR,
494 "Datatype performs attachment operation without initializing " 481 "Datatype performs attachment operation without initializing "
495 "attachment store", 482 "attachment store",
496 type_); 483 type_);
497 error_handler()->OnSingleDataTypeUnrecoverableError(error); 484 error_handler()->OnSingleDataTypeUnrecoverableError(error);
498 NOTREACHED(); 485 NOTREACHED();
499 return error; 486 return error;
500 } 487 }
501 syncer::AttachmentIdList ids_to_upload; 488 syncer::AttachmentIdList ids_to_upload;
502 ids_to_upload.reserve(new_attachments.size()); 489 ids_to_upload.reserve(new_attachments.size());
503 std::copy(new_attachments.begin(), new_attachments.end(), 490 std::copy(new_attachments.begin(), new_attachments.end(),
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 } 524 }
538 case syncer::WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: { 525 case syncer::WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: {
539 syncer::SyncError error; 526 syncer::SyncError error;
540 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_); 527 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_);
541 error_handler()->OnSingleDataTypeUnrecoverableError(error); 528 error_handler()->OnSingleDataTypeUnrecoverableError(error);
542 LOG(ERROR) << "Create: Could not create entry."; 529 LOG(ERROR) << "Create: Could not create entry.";
543 return error; 530 return error;
544 } 531 }
545 case syncer::WriteNode::INIT_FAILED_SET_PREDECESSOR: { 532 case syncer::WriteNode::INIT_FAILED_SET_PREDECESSOR: {
546 syncer::SyncError error; 533 syncer::SyncError error;
547 error.Reset( 534 error.Reset(FROM_HERE, error_prefix + "failed to set predecessor",
548 FROM_HERE, error_prefix + "failed to set predecessor", type_); 535 type_);
549 error_handler()->OnSingleDataTypeUnrecoverableError(error); 536 error_handler()->OnSingleDataTypeUnrecoverableError(error);
550 LOG(ERROR) << "Create: Bad predecessor."; 537 LOG(ERROR) << "Create: Bad predecessor.";
551 return error; 538 return error;
552 } 539 }
553 case syncer::WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: { 540 case syncer::WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: {
554 syncer::SyncError error; 541 syncer::SyncError error;
555 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); 542 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_);
556 error_handler()->OnSingleDataTypeUnrecoverableError(error); 543 error_handler()->OnSingleDataTypeUnrecoverableError(error);
557 LOG(ERROR) << "Create: Failed to decrypt."; 544 LOG(ERROR) << "Create: Failed to decrypt.";
558 return error; 545 return error;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 } 631 }
645 // TODO(sync): Support updating other parts of the sync node (title, 632 // TODO(sync): Support updating other parts of the sync node (title,
646 // successor, parent, etc.). 633 // successor, parent, etc.).
647 return syncer::SyncError(); 634 return syncer::SyncError();
648 } 635 }
649 636
650 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) { 637 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) {
651 DCHECK(CalledOnValidThread()); 638 DCHECK(CalledOnValidThread());
652 DCHECK(has_nodes); 639 DCHECK(has_nodes);
653 std::string type_name = syncer::ModelTypeToString(type_); 640 std::string type_name = syncer::ModelTypeToString(type_);
654 std::string err_str = "Server did not create the top-level " + type_name + 641 std::string err_str =
642 "Server did not create the top-level " + type_name +
655 " node. We might be running against an out-of-date server."; 643 " node. We might be running against an out-of-date server.";
656 *has_nodes = false; 644 *has_nodes = false;
657 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 645 syncer::ReadTransaction trans(FROM_HERE, share_handle());
658 syncer::ReadNode type_root_node(&trans); 646 syncer::ReadNode type_root_node(&trans);
659 if (type_root_node.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { 647 if (type_root_node.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) {
660 LOG(ERROR) << err_str; 648 LOG(ERROR) << err_str;
661 return false; 649 return false;
662 } 650 }
663 651
664 // The sync model has user created nodes if the type's root node has any 652 // The sync model has user created nodes if the type's root node has any
665 // children. 653 // children.
666 *has_nodes = type_root_node.HasChildren(); 654 *has_nodes = type_root_node.HasChildren();
667 return true; 655 return true;
668 } 656 }
669 657
670 bool GenericChangeProcessor::CryptoReadyIfNecessary() { 658 bool GenericChangeProcessor::CryptoReadyIfNecessary() {
671 DCHECK(CalledOnValidThread()); 659 DCHECK(CalledOnValidThread());
672 // We only access the cryptographer while holding a transaction. 660 // We only access the cryptographer while holding a transaction.
673 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 661 syncer::ReadTransaction trans(FROM_HERE, share_handle());
674 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); 662 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes();
675 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready(); 663 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready();
676 } 664 }
677 665
678 void GenericChangeProcessor::StartImpl() { 666 void GenericChangeProcessor::StartImpl() {}
679 }
680 667
681 syncer::UserShare* GenericChangeProcessor::share_handle() const { 668 syncer::UserShare* GenericChangeProcessor::share_handle() const {
682 DCHECK(CalledOnValidThread()); 669 DCHECK(CalledOnValidThread());
683 return share_handle_; 670 return share_handle_;
684 } 671 }
685 672
686 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { 673 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() {
687 DCHECK(CalledOnValidThread()); 674 DCHECK(CalledOnValidThread());
688 DCHECK(attachment_service_.get()); 675 DCHECK(attachment_service_.get());
689 syncer::AttachmentIdList ids; 676 syncer::AttachmentIdList ids;
690 { 677 {
691 syncer::ReadTransaction trans(FROM_HERE, share_handle()); 678 syncer::ReadTransaction trans(FROM_HERE, share_handle());
692 trans.GetAttachmentIdsToUpload(type_, &ids); 679 trans.GetAttachmentIdsToUpload(type_, &ids);
693 } 680 }
694 if (!ids.empty()) { 681 if (!ids.empty()) {
695 attachment_service_->UploadAttachments(ids); 682 attachment_service_->UploadAttachments(ids);
696 } 683 }
697 } 684 }
698 685
699 std::unique_ptr<syncer::AttachmentService> 686 std::unique_ptr<syncer::AttachmentService>
700 GenericChangeProcessor::GetAttachmentService() const { 687 GenericChangeProcessor::GetAttachmentService() const {
701 return std::unique_ptr<syncer::AttachmentService>( 688 return std::unique_ptr<syncer::AttachmentService>(
702 new syncer::AttachmentServiceProxy(attachment_service_proxy_)); 689 new syncer::AttachmentServiceProxy(attachment_service_proxy_));
703 } 690 }
704 691
705 } // namespace sync_driver 692 } // namespace sync_driver
OLDNEW
« no previous file with comments | « components/sync/driver/generic_change_processor.h ('k') | components/sync/driver/generic_change_processor_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698