Chromium Code Reviews| 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 "chrome/browser/sync/glue/bookmark_model_associator.h" | 5 #include "chrome/browser/sync/glue/bookmark_model_associator.h" |
| 6 | 6 |
| 7 #include <stack> | 7 #include <stack> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sync/glue/bookmark_change_processor.h" | 18 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "sync/api/sync_error.h" | 20 #include "sync/api/sync_error.h" |
| 21 #include "sync/internal_api/public/delete_journal.h" | |
| 21 #include "sync/internal_api/public/read_node.h" | 22 #include "sync/internal_api/public/read_node.h" |
| 22 #include "sync/internal_api/public/read_transaction.h" | 23 #include "sync/internal_api/public/read_transaction.h" |
| 23 #include "sync/internal_api/public/write_node.h" | 24 #include "sync/internal_api/public/write_node.h" |
| 24 #include "sync/internal_api/public/write_transaction.h" | 25 #include "sync/internal_api/public/write_transaction.h" |
| 25 #include "sync/syncable/syncable_write_transaction.h" | 26 #include "sync/syncable/syncable_write_transaction.h" |
| 26 #include "sync/util/cryptographer.h" | 27 #include "sync/util/cryptographer.h" |
| 27 #include "sync/util/data_type_histogram.h" | 28 #include "sync/util/data_type_histogram.h" |
| 28 | 29 |
| 29 using content::BrowserThread; | 30 using content::BrowserThread; |
| 30 | 31 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 } | 78 } |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 // Provides the following abstraction: given a parent bookmark node, find best | 81 // Provides the following abstraction: given a parent bookmark node, find best |
| 81 // matching child node for many sync nodes. | 82 // matching child node for many sync nodes. |
| 82 class BookmarkNodeFinder { | 83 class BookmarkNodeFinder { |
| 83 public: | 84 public: |
| 84 // Creates an instance with the given parent bookmark node. | 85 // Creates an instance with the given parent bookmark node. |
| 85 explicit BookmarkNodeFinder(const BookmarkNode* parent_node); | 86 explicit BookmarkNodeFinder(const BookmarkNode* parent_node); |
| 86 | 87 |
| 87 // Finds best matching node for the given sync node. | 88 // Finds best matching node for the given sync node. |
|
tim (not reviewing)
2013/01/14 23:32:48
Update comment.
haitaol1
2013/01/15 19:44:31
Done.
| |
| 88 // Returns the matching node if one exists; NULL otherwise. If a matching | 89 // Returns the matching node if one exists; NULL otherwise. If a matching |
| 89 // node is found, it's removed for further matches. | 90 // node is found, it's removed for further matches. |
| 90 const BookmarkNode* FindBookmarkNode(const syncer::BaseNode& sync_node); | 91 const BookmarkNode* FindBookmarkNode(const GURL& url, |
| 92 const std::string& title, | |
| 93 bool is_folder); | |
| 91 | 94 |
| 92 private: | 95 private: |
| 93 typedef std::multiset<const BookmarkNode*, BookmarkComparer> BookmarkNodesSet; | 96 typedef std::multiset<const BookmarkNode*, BookmarkComparer> BookmarkNodesSet; |
| 94 | 97 |
| 95 const BookmarkNode* parent_node_; | 98 const BookmarkNode* parent_node_; |
| 96 BookmarkNodesSet child_nodes_; | 99 BookmarkNodesSet child_nodes_; |
| 97 | 100 |
| 98 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeFinder); | 101 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeFinder); |
| 99 }; | 102 }; |
| 100 | 103 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 116 }; | 119 }; |
| 117 | 120 |
| 118 BookmarkNodeFinder::BookmarkNodeFinder(const BookmarkNode* parent_node) | 121 BookmarkNodeFinder::BookmarkNodeFinder(const BookmarkNode* parent_node) |
| 119 : parent_node_(parent_node) { | 122 : parent_node_(parent_node) { |
| 120 for (int i = 0; i < parent_node_->child_count(); ++i) { | 123 for (int i = 0; i < parent_node_->child_count(); ++i) { |
| 121 child_nodes_.insert(parent_node_->GetChild(i)); | 124 child_nodes_.insert(parent_node_->GetChild(i)); |
| 122 } | 125 } |
| 123 } | 126 } |
| 124 | 127 |
| 125 const BookmarkNode* BookmarkNodeFinder::FindBookmarkNode( | 128 const BookmarkNode* BookmarkNodeFinder::FindBookmarkNode( |
| 126 const syncer::BaseNode& sync_node) { | 129 const GURL& url, const std::string& title, bool is_folder) { |
| 127 // Create a bookmark node from the given sync node. | 130 // Create a bookmark node from the given bookmark attributes. |
| 128 BookmarkNode temp_node(GURL(sync_node.GetBookmarkSpecifics().url())); | 131 BookmarkNode temp_node(url); |
| 129 temp_node.SetTitle(UTF8ToUTF16(sync_node.GetTitle())); | 132 temp_node.SetTitle(UTF8ToUTF16(title)); |
| 130 if (sync_node.GetIsFolder()) | 133 if (is_folder) |
| 131 temp_node.set_type(BookmarkNode::FOLDER); | 134 temp_node.set_type(BookmarkNode::FOLDER); |
| 132 else | 135 else |
| 133 temp_node.set_type(BookmarkNode::URL); | 136 temp_node.set_type(BookmarkNode::URL); |
| 134 | 137 |
| 135 const BookmarkNode* result = NULL; | 138 const BookmarkNode* result = NULL; |
| 136 BookmarkNodesSet::iterator iter = child_nodes_.find(&temp_node); | 139 BookmarkNodesSet::iterator iter = child_nodes_.find(&temp_node); |
| 137 if (iter != child_nodes_.end()) { | 140 if (iter != child_nodes_.end()) { |
| 138 result = *iter; | 141 result = *iter; |
| 139 // Remove the matched node so we don't match with it again. | 142 // Remove the matched node so we don't match with it again. |
| 140 child_nodes_.erase(iter); | 143 child_nodes_.erase(iter); |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 syncer::WriteTransaction trans(FROM_HERE, user_share_); | 443 syncer::WriteTransaction trans(FROM_HERE, user_share_); |
| 441 syncer::ReadNode bm_root(&trans); | 444 syncer::ReadNode bm_root(&trans); |
| 442 if (bm_root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::BOOKMARKS)) == | 445 if (bm_root.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::BOOKMARKS)) == |
| 443 syncer::BaseNode::INIT_OK) { | 446 syncer::BaseNode::INIT_OK) { |
| 444 syncer_merge_result->set_num_items_before_association( | 447 syncer_merge_result->set_num_items_before_association( |
| 445 bm_root.GetTotalNodeCount()); | 448 bm_root.GetTotalNodeCount()); |
| 446 } | 449 } |
| 447 local_merge_result->set_num_items_before_association( | 450 local_merge_result->set_num_items_before_association( |
| 448 bookmark_model_->root_node()->GetTotalNodeCount()); | 451 bookmark_model_->root_node()->GetTotalNodeCount()); |
| 449 | 452 |
| 453 // Remove obsolete bookmarks according to sync delete journal. | |
| 454 local_merge_result->set_num_items_deleted( | |
| 455 ApplyDeletesFromSyncJournal(&trans)); | |
| 456 | |
| 450 while (!dfs_stack.empty()) { | 457 while (!dfs_stack.empty()) { |
| 451 int64 sync_parent_id = dfs_stack.top(); | 458 int64 sync_parent_id = dfs_stack.top(); |
| 452 dfs_stack.pop(); | 459 dfs_stack.pop(); |
| 453 | 460 |
| 454 syncer::ReadNode sync_parent(&trans); | 461 syncer::ReadNode sync_parent(&trans); |
| 455 if (sync_parent.InitByIdLookup(sync_parent_id) != | 462 if (sync_parent.InitByIdLookup(sync_parent_id) != |
| 456 syncer::BaseNode::INIT_OK) { | 463 syncer::BaseNode::INIT_OK) { |
| 457 return unrecoverable_error_handler_->CreateAndUploadError( | 464 return unrecoverable_error_handler_->CreateAndUploadError( |
| 458 FROM_HERE, | 465 FROM_HERE, |
| 459 "Failed to lookup node.", | 466 "Failed to lookup node.", |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 473 syncer::WriteNode sync_child_node(&trans); | 480 syncer::WriteNode sync_child_node(&trans); |
| 474 if (sync_child_node.InitByIdLookup(sync_child_id) != | 481 if (sync_child_node.InitByIdLookup(sync_child_id) != |
| 475 syncer::BaseNode::INIT_OK) { | 482 syncer::BaseNode::INIT_OK) { |
| 476 return unrecoverable_error_handler_->CreateAndUploadError( | 483 return unrecoverable_error_handler_->CreateAndUploadError( |
| 477 FROM_HERE, | 484 FROM_HERE, |
| 478 "Failed to lookup node.", | 485 "Failed to lookup node.", |
| 479 model_type()); | 486 model_type()); |
| 480 } | 487 } |
| 481 | 488 |
| 482 const BookmarkNode* child_node = NULL; | 489 const BookmarkNode* child_node = NULL; |
| 483 child_node = node_finder.FindBookmarkNode(sync_child_node); | 490 child_node = node_finder.FindBookmarkNode( |
| 491 GURL(sync_child_node.GetBookmarkSpecifics().url()), | |
| 492 sync_child_node.GetTitle(), | |
| 493 sync_child_node.GetIsFolder()); | |
| 484 if (child_node) | 494 if (child_node) |
| 485 Associate(child_node, sync_child_id); | 495 Associate(child_node, sync_child_id); |
| 486 // All bookmarks are currently modified at association time (even if | 496 // All bookmarks are currently modified at association time (even if |
| 487 // it doesn't change anything). | 497 // it doesn't change anything). |
| 488 // TODO(sync): introduce logic to only modify the bookmark model if | 498 // TODO(sync): introduce logic to only modify the bookmark model if |
| 489 // necessary. | 499 // necessary. |
| 490 const BookmarkNode* new_child_node = | 500 const BookmarkNode* new_child_node = |
| 491 BookmarkChangeProcessor::CreateOrUpdateBookmarkNode( | 501 BookmarkChangeProcessor::CreateOrUpdateBookmarkNode( |
| 492 &sync_child_node, | 502 &sync_child_node, |
| 493 bookmark_model_, | 503 bookmark_model_, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 } | 539 } |
| 530 | 540 |
| 531 local_merge_result->set_num_items_after_association( | 541 local_merge_result->set_num_items_after_association( |
| 532 bookmark_model_->root_node()->GetTotalNodeCount()); | 542 bookmark_model_->root_node()->GetTotalNodeCount()); |
| 533 syncer_merge_result->set_num_items_after_association( | 543 syncer_merge_result->set_num_items_after_association( |
| 534 bm_root.GetTotalNodeCount()); | 544 bm_root.GetTotalNodeCount()); |
| 535 | 545 |
| 536 return syncer::SyncError(); | 546 return syncer::SyncError(); |
| 537 } | 547 } |
| 538 | 548 |
| 549 struct FolderInfo { | |
| 550 FolderInfo(const BookmarkNode* f, const BookmarkNode* p, int64 id) | |
| 551 : folder(f), parent(p), sync_id(id) {} | |
| 552 const BookmarkNode* folder; | |
| 553 const BookmarkNode* parent; | |
| 554 int64 sync_id; | |
| 555 }; | |
| 556 typedef std::vector<FolderInfo> FolderInfoList; | |
| 557 | |
| 558 int64 BookmarkModelAssociator::ApplyDeletesFromSyncJournal( | |
| 559 syncer::BaseTransaction* trans) { | |
| 560 int64 num_bookmark_deleted = 0; | |
| 561 | |
| 562 syncer::BookmarkDeleteJournalList bk_delete_journals; | |
| 563 syncer::DeleteJournal::GetBookmarkDeleteJournals(trans, &bk_delete_journals); | |
| 564 if (bk_delete_journals.empty()) | |
| 565 return 0; | |
| 566 size_t num_journals_unmatched = bk_delete_journals.size(); | |
| 567 | |
| 568 // Check bookmark model from top to bottom. | |
| 569 std::stack<const BookmarkNode*> dfs_stack; | |
| 570 dfs_stack.push(bookmark_model_->bookmark_bar_node()); | |
| 571 dfs_stack.push(bookmark_model_->other_node()); | |
| 572 if (expect_mobile_bookmarks_folder_) | |
| 573 dfs_stack.push(bookmark_model_->mobile_node()); | |
| 574 | |
| 575 FolderInfoList folders_matched; | |
|
tim (not reviewing)
2013/01/14 23:32:48
Comment explaining why we keep this, which I think
haitaol1
2013/01/15 19:44:31
Added comments. Do it in one pass is difficult bec
| |
| 576 while (!dfs_stack.empty()) { | |
| 577 const BookmarkNode* parent = dfs_stack.top(); | |
| 578 dfs_stack.pop(); | |
| 579 | |
| 580 BookmarkNodeFinder finder(parent); | |
| 581 // Iterate through journals from back to front and keep unmatched journals | |
|
tim (not reviewing)
2013/01/14 23:32:48
Mention that the reason we do this shuffling is so
haitaol1
2013/01/15 19:44:31
Done.
| |
| 582 // at the beginning of journal list. | |
| 583 for (int i = num_journals_unmatched - 1; i >= 0; --i) { | |
| 584 const BookmarkNode* child = finder.FindBookmarkNode( | |
| 585 GURL(bk_delete_journals[i].specifics.bookmark().url()), | |
| 586 bk_delete_journals[i].specifics.bookmark().title(), | |
| 587 bk_delete_journals[i].is_folder); | |
| 588 if (child) { | |
| 589 if (child->is_folder()) { | |
| 590 // Remember matched folder without removing and delete only empty | |
| 591 // ones later. | |
| 592 folders_matched.push_back(FolderInfo(child, parent, | |
| 593 bk_delete_journals[i].id)); | |
| 594 } else { | |
| 595 bookmark_model_->Remove(parent, parent->GetIndexOf(child)); | |
| 596 ++num_bookmark_deleted; | |
| 597 } | |
| 598 // Move unmatched journal here and decrement counter. | |
| 599 bk_delete_journals[i] = bk_delete_journals[--num_journals_unmatched]; | |
| 600 } | |
| 601 } | |
| 602 if (num_journals_unmatched == 0) | |
| 603 break; | |
| 604 | |
| 605 for (int i = 0; i < parent->child_count(); ++i) { | |
| 606 if (parent->GetChild(i)->is_folder()) | |
| 607 dfs_stack.push(parent->GetChild(i)); | |
| 608 } | |
| 609 } | |
| 610 | |
| 611 // Ids of sync nodes not found in bookmark model, meaning the deletions are | |
| 612 // persisted and correponding delete journals can be dropped. | |
| 613 std::set<int64> journals_to_purge; | |
| 614 | |
| 615 // Remove empty folders from bottom to top. | |
| 616 for (FolderInfoList::const_reverse_iterator it = folders_matched.rbegin(); | |
| 617 it != folders_matched.rend(); ++it) { | |
| 618 if (it->folder->child_count() == 0) { | |
| 619 bookmark_model_->Remove(it->parent, it->parent->GetIndexOf(it->folder)); | |
| 620 ++num_bookmark_deleted; | |
| 621 } else { | |
| 622 // Keep non-empty folder and remove its journal so that it won't match | |
| 623 // again in the future. | |
| 624 journals_to_purge.insert(it->sync_id); | |
| 625 } | |
| 626 } | |
| 627 | |
| 628 // Purge unmatched journals. | |
| 629 for (size_t i = 0; i < num_journals_unmatched; ++i) | |
| 630 journals_to_purge.insert(bk_delete_journals[i].id); | |
| 631 syncer::DeleteJournal::PurgeDeleteJournals(trans, journals_to_purge); | |
| 632 | |
| 633 return num_bookmark_deleted; | |
| 634 } | |
| 635 | |
| 539 void BookmarkModelAssociator::PostPersistAssociationsTask() { | 636 void BookmarkModelAssociator::PostPersistAssociationsTask() { |
| 540 // No need to post a task if a task is already pending. | 637 // No need to post a task if a task is already pending. |
| 541 if (weak_factory_.HasWeakPtrs()) | 638 if (weak_factory_.HasWeakPtrs()) |
| 542 return; | 639 return; |
| 543 MessageLoop::current()->PostTask( | 640 MessageLoop::current()->PostTask( |
| 544 FROM_HERE, | 641 FROM_HERE, |
| 545 base::Bind( | 642 base::Bind( |
| 546 &BookmarkModelAssociator::PersistAssociations, | 643 &BookmarkModelAssociator::PersistAssociations, |
| 547 weak_factory_.GetWeakPtr())); | 644 weak_factory_.GetWeakPtr())); |
| 548 } | 645 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", | 703 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", |
| 607 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); | 704 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); |
| 608 // Clear version on bookmark model so that we only report error once. | 705 // Clear version on bookmark model so that we only report error once. |
| 609 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), | 706 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), |
| 610 kBookmarkTransactionVersionKey); | 707 kBookmarkTransactionVersionKey); |
| 611 } | 708 } |
| 612 } | 709 } |
| 613 } | 710 } |
| 614 | 711 |
| 615 } // namespace browser_sync | 712 } // namespace browser_sync |
| OLD | NEW |