OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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_bookmarks/bookmark_change_processor.h" | 5 #include "components/sync_bookmarks/bookmark_change_processor.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <stack> | 10 #include <stack> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <utility> |
13 | 13 |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "components/bookmarks/browser/bookmark_client.h" | 19 #include "components/bookmarks/browser/bookmark_client.h" |
20 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
21 #include "components/bookmarks/browser/bookmark_utils.h" | 21 #include "components/bookmarks/browser/bookmark_utils.h" |
22 #include "components/favicon/core/favicon_service.h" | 22 #include "components/favicon/core/favicon_service.h" |
(...skipping 15 matching lines...) Expand all Loading... |
38 using syncer::ChangeRecord; | 38 using syncer::ChangeRecord; |
39 using syncer::ChangeRecordList; | 39 using syncer::ChangeRecordList; |
40 | 40 |
41 namespace browser_sync { | 41 namespace browser_sync { |
42 | 42 |
43 static const char kMobileBookmarksTag[] = "synced_bookmarks"; | 43 static const char kMobileBookmarksTag[] = "synced_bookmarks"; |
44 | 44 |
45 BookmarkChangeProcessor::BookmarkChangeProcessor( | 45 BookmarkChangeProcessor::BookmarkChangeProcessor( |
46 sync_driver::SyncClient* sync_client, | 46 sync_driver::SyncClient* sync_client, |
47 BookmarkModelAssociator* model_associator, | 47 BookmarkModelAssociator* model_associator, |
48 syncer::DataTypeErrorHandler* error_handler) | 48 std::unique_ptr<syncer::DataTypeErrorHandler> err_handler) |
49 : sync_driver::ChangeProcessor(error_handler), | 49 : sync_driver::ChangeProcessor(std::move(err_handler)), |
50 bookmark_model_(NULL), | 50 bookmark_model_(NULL), |
51 sync_client_(sync_client), | 51 sync_client_(sync_client), |
52 model_associator_(model_associator) { | 52 model_associator_(model_associator) { |
53 DCHECK(model_associator); | 53 DCHECK(model_associator); |
54 DCHECK(sync_client); | 54 DCHECK(sync_client); |
55 DCHECK(error_handler); | 55 DCHECK(error_handler()); |
56 } | 56 } |
57 | 57 |
58 BookmarkChangeProcessor::~BookmarkChangeProcessor() { | 58 BookmarkChangeProcessor::~BookmarkChangeProcessor() { |
59 if (bookmark_model_) | 59 if (bookmark_model_) |
60 bookmark_model_->RemoveObserver(this); | 60 bookmark_model_->RemoveObserver(this); |
61 } | 61 } |
62 | 62 |
63 void BookmarkChangeProcessor::StartImpl() { | 63 void BookmarkChangeProcessor::StartImpl() { |
64 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
65 DCHECK(!bookmark_model_); | 65 DCHECK(!bookmark_model_); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 int64_t new_version = syncer::syncable::kInvalidTransactionVersion; | 129 int64_t new_version = syncer::syncable::kInvalidTransactionVersion; |
130 { | 130 { |
131 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); | 131 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); |
132 syncer::WriteNode topmost_sync_node(&trans); | 132 syncer::WriteNode topmost_sync_node(&trans); |
133 if (!model_associator_->InitSyncNodeFromChromeId(topmost->id(), | 133 if (!model_associator_->InitSyncNodeFromChromeId(topmost->id(), |
134 &topmost_sync_node)) { | 134 &topmost_sync_node)) { |
135 syncer::SyncError error(FROM_HERE, | 135 syncer::SyncError error(FROM_HERE, |
136 syncer::SyncError::DATATYPE_ERROR, | 136 syncer::SyncError::DATATYPE_ERROR, |
137 "Failed to init sync node from chrome node", | 137 "Failed to init sync node from chrome node", |
138 syncer::BOOKMARKS); | 138 syncer::BOOKMARKS); |
139 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 139 error_handler()->OnUnrecoverableError(error); |
140 return; | 140 return; |
141 } | 141 } |
142 RemoveSyncNodeHierarchy(&trans, &topmost_sync_node, model_associator_); | 142 RemoveSyncNodeHierarchy(&trans, &topmost_sync_node, model_associator_); |
143 } | 143 } |
144 | 144 |
145 // Don't need to update versions of deleted nodes. | 145 // Don't need to update versions of deleted nodes. |
146 UpdateTransactionVersion(new_version, bookmark_model_, | 146 UpdateTransactionVersion(new_version, bookmark_model_, |
147 std::vector<const BookmarkNode*>()); | 147 std::vector<const BookmarkNode*>()); |
148 } | 148 } |
149 | 149 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 const BookmarkNode* child = parent->GetChild(index); | 303 const BookmarkNode* child = parent->GetChild(index); |
304 DCHECK(child); | 304 DCHECK(child); |
305 | 305 |
306 // Create a WriteNode container to hold the new node. | 306 // Create a WriteNode container to hold the new node. |
307 syncer::WriteNode sync_child(trans); | 307 syncer::WriteNode sync_child(trans); |
308 | 308 |
309 // Actually create the node with the appropriate initial position. | 309 // Actually create the node with the appropriate initial position. |
310 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator)) { | 310 if (!PlaceSyncNode(CREATE, parent, index, trans, &sync_child, associator)) { |
311 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 311 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
312 "Failed to create sync node.", syncer::BOOKMARKS); | 312 "Failed to create sync node.", syncer::BOOKMARKS); |
313 error_handler->OnSingleDataTypeUnrecoverableError(error); | 313 error_handler->OnUnrecoverableError(error); |
314 return syncer::kInvalidId; | 314 return syncer::kInvalidId; |
315 } | 315 } |
316 | 316 |
317 UpdateSyncNodeProperties(child, model, &sync_child, error_handler); | 317 UpdateSyncNodeProperties(child, model, &sync_child, error_handler); |
318 | 318 |
319 // Associate the ID from the sync domain with the bookmark node, so that we | 319 // Associate the ID from the sync domain with the bookmark node, so that we |
320 // can refer back to this item later. | 320 // can refer back to this item later. |
321 associator->Associate(child, sync_child); | 321 associator->Associate(child, sync_child); |
322 | 322 |
323 return sync_child.GetId(); | 323 return sync_child.GetId(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 syncer::WriteTransaction* trans, | 367 syncer::WriteTransaction* trans, |
368 BookmarkModelAssociator* associator, | 368 BookmarkModelAssociator* associator, |
369 syncer::DataTypeErrorHandler* error_handler) { | 369 syncer::DataTypeErrorHandler* error_handler) { |
370 // Lookup the sync node that's associated with |node|. | 370 // Lookup the sync node that's associated with |node|. |
371 syncer::WriteNode sync_node(trans); | 371 syncer::WriteNode sync_node(trans); |
372 if (!associator->InitSyncNodeFromChromeId(node->id(), &sync_node)) { | 372 if (!associator->InitSyncNodeFromChromeId(node->id(), &sync_node)) { |
373 syncer::SyncError error(FROM_HERE, | 373 syncer::SyncError error(FROM_HERE, |
374 syncer::SyncError::DATATYPE_ERROR, | 374 syncer::SyncError::DATATYPE_ERROR, |
375 "Failed to init sync node from chrome node", | 375 "Failed to init sync node from chrome node", |
376 syncer::BOOKMARKS); | 376 syncer::BOOKMARKS); |
377 error_handler->OnSingleDataTypeUnrecoverableError(error); | 377 error_handler->OnUnrecoverableError(error); |
378 return syncer::kInvalidId; | 378 return syncer::kInvalidId; |
379 } | 379 } |
380 UpdateSyncNodeProperties(node, model, &sync_node, error_handler); | 380 UpdateSyncNodeProperties(node, model, &sync_node, error_handler); |
381 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); | 381 DCHECK_EQ(sync_node.GetIsFolder(), node->is_folder()); |
382 DCHECK_EQ(associator->GetChromeNodeFromSyncId(sync_node.GetParentId()), | 382 DCHECK_EQ(associator->GetChromeNodeFromSyncId(sync_node.GetParentId()), |
383 node->parent()); | 383 node->parent()); |
384 DCHECK_EQ(node->parent()->GetIndexOf(node), sync_node.GetPositionIndex()); | 384 DCHECK_EQ(node->parent()->GetIndexOf(node), sync_node.GetPositionIndex()); |
385 return sync_node.GetId(); | 385 return sync_node.GetId(); |
386 } | 386 } |
387 | 387 |
(...skipping 21 matching lines...) Expand all Loading... |
409 // Acquire a scoped write lock via a transaction. | 409 // Acquire a scoped write lock via a transaction. |
410 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); | 410 syncer::WriteTransaction trans(FROM_HERE, share_handle(), &new_version); |
411 | 411 |
412 // Lookup the sync node that's associated with |child|. | 412 // Lookup the sync node that's associated with |child|. |
413 syncer::WriteNode sync_node(&trans); | 413 syncer::WriteNode sync_node(&trans); |
414 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { | 414 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), &sync_node)) { |
415 syncer::SyncError error(FROM_HERE, | 415 syncer::SyncError error(FROM_HERE, |
416 syncer::SyncError::DATATYPE_ERROR, | 416 syncer::SyncError::DATATYPE_ERROR, |
417 "Failed to init sync node from chrome node", | 417 "Failed to init sync node from chrome node", |
418 syncer::BOOKMARKS); | 418 syncer::BOOKMARKS); |
419 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 419 error_handler()->OnUnrecoverableError(error); |
420 return; | 420 return; |
421 } | 421 } |
422 | 422 |
423 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, | 423 if (!PlaceSyncNode(MOVE, new_parent, new_index, &trans, &sync_node, |
424 model_associator_)) { | 424 model_associator_)) { |
425 syncer::SyncError error(FROM_HERE, | 425 syncer::SyncError error(FROM_HERE, |
426 syncer::SyncError::DATATYPE_ERROR, | 426 syncer::SyncError::DATATYPE_ERROR, |
427 "Failed to place sync node", | 427 "Failed to place sync node", |
428 syncer::BOOKMARKS); | 428 syncer::BOOKMARKS); |
429 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 429 error_handler()->OnUnrecoverableError(error); |
430 return; | 430 return; |
431 } | 431 } |
432 } | 432 } |
433 | 433 |
434 UpdateTransactionVersion(new_version, model, | 434 UpdateTransactionVersion(new_version, model, |
435 std::vector<const BookmarkNode*>(1, child)); | 435 std::vector<const BookmarkNode*>(1, child)); |
436 } | 436 } |
437 | 437 |
438 void BookmarkChangeProcessor::BookmarkNodeFaviconChanged( | 438 void BookmarkChangeProcessor::BookmarkNodeFaviconChanged( |
439 BookmarkModel* model, | 439 BookmarkModel* model, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 const BookmarkNode* child = node->GetChild(i); | 481 const BookmarkNode* child = node->GetChild(i); |
482 children.push_back(child); | 482 children.push_back(child); |
483 | 483 |
484 syncer::WriteNode sync_child(&trans); | 484 syncer::WriteNode sync_child(&trans); |
485 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), | 485 if (!model_associator_->InitSyncNodeFromChromeId(child->id(), |
486 &sync_child)) { | 486 &sync_child)) { |
487 syncer::SyncError error(FROM_HERE, | 487 syncer::SyncError error(FROM_HERE, |
488 syncer::SyncError::DATATYPE_ERROR, | 488 syncer::SyncError::DATATYPE_ERROR, |
489 "Failed to init sync node from chrome node", | 489 "Failed to init sync node from chrome node", |
490 syncer::BOOKMARKS); | 490 syncer::BOOKMARKS); |
491 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 491 error_handler()->OnUnrecoverableError(error); |
492 return; | 492 return; |
493 } | 493 } |
494 DCHECK_EQ(sync_child.GetParentId(), | 494 DCHECK_EQ(sync_child.GetParentId(), |
495 model_associator_->GetSyncIdFromChromeId(node->id())); | 495 model_associator_->GetSyncIdFromChromeId(node->id())); |
496 | 496 |
497 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, | 497 if (!PlaceSyncNode(MOVE, node, i, &trans, &sync_child, |
498 model_associator_)) { | 498 model_associator_)) { |
499 syncer::SyncError error(FROM_HERE, | 499 syncer::SyncError error(FROM_HERE, |
500 syncer::SyncError::DATATYPE_ERROR, | 500 syncer::SyncError::DATATYPE_ERROR, |
501 "Failed to place sync node", | 501 "Failed to place sync node", |
502 syncer::BOOKMARKS); | 502 syncer::BOOKMARKS); |
503 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 503 error_handler()->OnUnrecoverableError(error); |
504 return; | 504 return; |
505 } | 505 } |
506 } | 506 } |
507 } | 507 } |
508 | 508 |
509 // TODO(haitaol): Filter out children that didn't actually change. | 509 // TODO(haitaol): Filter out children that didn't actually change. |
510 UpdateTransactionVersion(new_version, model, children); | 510 UpdateTransactionVersion(new_version, model, children); |
511 } | 511 } |
512 | 512 |
513 // static | 513 // static |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 if (!dst->empty()) { | 618 if (!dst->empty()) { |
619 if (!foster_parent) { | 619 if (!foster_parent) { |
620 foster_parent = model->AddFolder(model->other_node(), | 620 foster_parent = model->AddFolder(model->other_node(), |
621 model->other_node()->child_count(), | 621 model->other_node()->child_count(), |
622 base::string16()); | 622 base::string16()); |
623 if (!foster_parent) { | 623 if (!foster_parent) { |
624 syncer::SyncError error(FROM_HERE, | 624 syncer::SyncError error(FROM_HERE, |
625 syncer::SyncError::DATATYPE_ERROR, | 625 syncer::SyncError::DATATYPE_ERROR, |
626 "Failed to create foster parent", | 626 "Failed to create foster parent", |
627 syncer::BOOKMARKS); | 627 syncer::BOOKMARKS); |
628 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 628 error_handler()->OnUnrecoverableError(error); |
629 return; | 629 return; |
630 } | 630 } |
631 } | 631 } |
632 for (int i = dst->child_count() - 1; i >= 0; --i) { | 632 for (int i = dst->child_count() - 1; i >= 0; --i) { |
633 model->Move(dst->GetChild(i), foster_parent, | 633 model->Move(dst->GetChild(i), foster_parent, |
634 foster_parent->child_count()); | 634 foster_parent->child_count()); |
635 } | 635 } |
636 } | 636 } |
637 DCHECK_EQ(dst->child_count(), 0) << "Node being deleted has children"; | 637 DCHECK_EQ(dst->child_count(), 0) << "Node being deleted has children"; |
638 | 638 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 679 |
680 DCHECK_NE(it->action, ChangeRecord::ACTION_DELETE) | 680 DCHECK_NE(it->action, ChangeRecord::ACTION_DELETE) |
681 << "We should have passed all deletes by this point."; | 681 << "We should have passed all deletes by this point."; |
682 | 682 |
683 syncer::ReadNode src(trans); | 683 syncer::ReadNode src(trans); |
684 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { | 684 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { |
685 syncer::SyncError error(FROM_HERE, | 685 syncer::SyncError error(FROM_HERE, |
686 syncer::SyncError::DATATYPE_ERROR, | 686 syncer::SyncError::DATATYPE_ERROR, |
687 "Failed to load sync node", | 687 "Failed to load sync node", |
688 syncer::BOOKMARKS); | 688 syncer::BOOKMARKS); |
689 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 689 error_handler()->OnUnrecoverableError(error); |
690 return; | 690 return; |
691 } | 691 } |
692 | 692 |
693 const BookmarkNode* parent = | 693 const BookmarkNode* parent = |
694 model_associator_->GetChromeNodeFromSyncId(src.GetParentId()); | 694 model_associator_->GetChromeNodeFromSyncId(src.GetParentId()); |
695 if (!parent) { | 695 if (!parent) { |
696 LOG(ERROR) << "Could not find parent of node being added/updated." | 696 LOG(ERROR) << "Could not find parent of node being added/updated." |
697 << " Node title: " << src.GetTitle() | 697 << " Node title: " << src.GetTitle() |
698 << ", parent id = " << src.GetParentId(); | 698 << ", parent id = " << src.GetParentId(); |
699 continue; | 699 continue; |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); | 971 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); |
972 sync_node->SetBookmarkSpecifics(updated_specifics); | 972 sync_node->SetBookmarkSpecifics(updated_specifics); |
973 } | 973 } |
974 } | 974 } |
975 | 975 |
976 bool BookmarkChangeProcessor::CanSyncNode(const BookmarkNode* node) { | 976 bool BookmarkChangeProcessor::CanSyncNode(const BookmarkNode* node) { |
977 return bookmark_model_->client()->CanSyncNode(node); | 977 return bookmark_model_->client()->CanSyncNode(node); |
978 } | 978 } |
979 | 979 |
980 } // namespace browser_sync | 980 } // namespace browser_sync |
OLD | NEW |