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 the bookmark node that matches the given url, title and folder |
88 // Returns the matching node if one exists; NULL otherwise. If a matching | 89 // attribute. Returns the matching node if one exists; NULL otherwise. If a |
89 // node is found, it's removed for further matches. | 90 // matching 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 // Remember folders that match delete journals in first pass but don't delete |
| 576 // them in case there are bookmarks left under them. After non-folder |
| 577 // bookmarks are removed in first pass, recheck the folders in reverse order |
| 578 // to remove empty ones. |
| 579 FolderInfoList folders_matched; |
| 580 while (!dfs_stack.empty()) { |
| 581 const BookmarkNode* parent = dfs_stack.top(); |
| 582 dfs_stack.pop(); |
| 583 |
| 584 BookmarkNodeFinder finder(parent); |
| 585 // Iterate through journals from back to front. Remove matched journal by |
| 586 // moving an unmatched journal at the tail to its position so that we can |
| 587 // read unmatched journals off the head in next loop. |
| 588 for (int i = num_journals_unmatched - 1; i >= 0; --i) { |
| 589 const BookmarkNode* child = finder.FindBookmarkNode( |
| 590 GURL(bk_delete_journals[i].specifics.bookmark().url()), |
| 591 bk_delete_journals[i].specifics.bookmark().title(), |
| 592 bk_delete_journals[i].is_folder); |
| 593 if (child) { |
| 594 if (child->is_folder()) { |
| 595 // Remember matched folder without removing and delete only empty |
| 596 // ones later. |
| 597 folders_matched.push_back(FolderInfo(child, parent, |
| 598 bk_delete_journals[i].id)); |
| 599 } else { |
| 600 bookmark_model_->Remove(parent, parent->GetIndexOf(child)); |
| 601 ++num_bookmark_deleted; |
| 602 } |
| 603 // Move unmatched journal here and decrement counter. |
| 604 bk_delete_journals[i] = bk_delete_journals[--num_journals_unmatched]; |
| 605 } |
| 606 } |
| 607 if (num_journals_unmatched == 0) |
| 608 break; |
| 609 |
| 610 for (int i = 0; i < parent->child_count(); ++i) { |
| 611 if (parent->GetChild(i)->is_folder()) |
| 612 dfs_stack.push(parent->GetChild(i)); |
| 613 } |
| 614 } |
| 615 |
| 616 // Ids of sync nodes not found in bookmark model, meaning the deletions are |
| 617 // persisted and correponding delete journals can be dropped. |
| 618 std::set<int64> journals_to_purge; |
| 619 |
| 620 // Remove empty folders from bottom to top. |
| 621 for (FolderInfoList::reverse_iterator it = folders_matched.rbegin(); |
| 622 it != folders_matched.rend(); ++it) { |
| 623 if (it->folder->child_count() == 0) { |
| 624 bookmark_model_->Remove(it->parent, it->parent->GetIndexOf(it->folder)); |
| 625 ++num_bookmark_deleted; |
| 626 } else { |
| 627 // Keep non-empty folder and remove its journal so that it won't match |
| 628 // again in the future. |
| 629 journals_to_purge.insert(it->sync_id); |
| 630 } |
| 631 } |
| 632 |
| 633 // Purge unmatched journals. |
| 634 for (size_t i = 0; i < num_journals_unmatched; ++i) |
| 635 journals_to_purge.insert(bk_delete_journals[i].id); |
| 636 syncer::DeleteJournal::PurgeDeleteJournals(trans, journals_to_purge); |
| 637 |
| 638 return num_bookmark_deleted; |
| 639 } |
| 640 |
539 void BookmarkModelAssociator::PostPersistAssociationsTask() { | 641 void BookmarkModelAssociator::PostPersistAssociationsTask() { |
540 // No need to post a task if a task is already pending. | 642 // No need to post a task if a task is already pending. |
541 if (weak_factory_.HasWeakPtrs()) | 643 if (weak_factory_.HasWeakPtrs()) |
542 return; | 644 return; |
543 MessageLoop::current()->PostTask( | 645 MessageLoop::current()->PostTask( |
544 FROM_HERE, | 646 FROM_HERE, |
545 base::Bind( | 647 base::Bind( |
546 &BookmarkModelAssociator::PersistAssociations, | 648 &BookmarkModelAssociator::PersistAssociations, |
547 weak_factory_.GetWeakPtr())); | 649 weak_factory_.GetWeakPtr())); |
548 } | 650 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", | 708 UMA_HISTOGRAM_ENUMERATION("Sync.LocalModelOutOfSync", |
607 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); | 709 syncer::BOOKMARKS, syncer::MODEL_TYPE_COUNT); |
608 // Clear version on bookmark model so that we only report error once. | 710 // Clear version on bookmark model so that we only report error once. |
609 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), | 711 bookmark_model_->DeleteNodeMetaInfo(bookmark_model_->root_node(), |
610 kBookmarkTransactionVersionKey); | 712 kBookmarkTransactionVersionKey); |
611 } | 713 } |
612 } | 714 } |
613 } | 715 } |
614 | 716 |
615 } // namespace browser_sync | 717 } // namespace browser_sync |
OLD | NEW |