| 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_change_processor.h" | 5 #include "chrome/browser/sync/glue/bookmark_change_processor.h" |
| 6 | 6 |
| 7 #include <stack> | 7 #include <stack> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 DCHECK(error_handler); | 51 DCHECK(error_handler); |
| 52 } | 52 } |
| 53 | 53 |
| 54 BookmarkChangeProcessor::~BookmarkChangeProcessor() { | 54 BookmarkChangeProcessor::~BookmarkChangeProcessor() { |
| 55 if (bookmark_model_) | 55 if (bookmark_model_) |
| 56 bookmark_model_->RemoveObserver(this); | 56 bookmark_model_->RemoveObserver(this); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void BookmarkChangeProcessor::StartImpl(Profile* profile) { | 59 void BookmarkChangeProcessor::StartImpl(Profile* profile) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 61 DCHECK(profile); |
| 62 profile_ = profile; |
| 61 DCHECK(!bookmark_model_); | 63 DCHECK(!bookmark_model_); |
| 62 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); | 64 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); |
| 63 DCHECK(bookmark_model_->IsLoaded()); | 65 DCHECK(bookmark_model_->IsLoaded()); |
| 64 bookmark_model_->AddObserver(this); | 66 bookmark_model_->AddObserver(this); |
| 65 } | 67 } |
| 66 | 68 |
| 67 void BookmarkChangeProcessor::UpdateSyncNodeProperties( | 69 void BookmarkChangeProcessor::UpdateSyncNodeProperties( |
| 68 const BookmarkNode* src, | 70 const BookmarkNode* src, |
| 69 BookmarkModel* model, | 71 BookmarkModel* model, |
| 70 syncer::WriteNode* dst) { | 72 syncer::WriteNode* dst) { |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 passed_deletes = true; | 542 passed_deletes = true; |
| 541 | 543 |
| 542 syncer::ReadNode src(trans); | 544 syncer::ReadNode src(trans); |
| 543 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { | 545 if (src.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { |
| 544 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, | 546 error_handler()->OnSingleDatatypeUnrecoverableError(FROM_HERE, |
| 545 "ApplyModelChanges was passed a bad ID"); | 547 "ApplyModelChanges was passed a bad ID"); |
| 546 return; | 548 return; |
| 547 } | 549 } |
| 548 | 550 |
| 549 const BookmarkNode* node = CreateOrUpdateBookmarkNode(&src, model, | 551 const BookmarkNode* node = CreateOrUpdateBookmarkNode(&src, model, |
| 552 profile_, |
| 550 model_associator_); | 553 model_associator_); |
| 551 if (node) { | 554 if (node) { |
| 552 bookmark_model_->SetNodeMetaInfo(node, kBookmarkTransactionVersionKey, | 555 bookmark_model_->SetNodeMetaInfo(node, kBookmarkTransactionVersionKey, |
| 553 base::Int64ToString(model_version)); | 556 base::Int64ToString(model_version)); |
| 554 } else { | 557 } else { |
| 555 // Because the Synced Bookmarks node can be created server side, it's | 558 // Because the Synced Bookmarks node can be created server side, it's |
| 556 // possible it'll arrive at the client as an update. In that case it | 559 // possible it'll arrive at the client as an update. In that case it |
| 557 // won't have been associated at startup, the GetChromeNodeFromSyncId | 560 // won't have been associated at startup, the GetChromeNodeFromSyncId |
| 558 // call above will return NULL, and we won't detect it as a permanent | 561 // call above will return NULL, and we won't detect it as a permanent |
| 559 // node, resulting in us trying to create it here (which will | 562 // node, resulting in us trying to create it here (which will |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 model->SetNodeMetaInfo(model->root_node(), kBookmarkTransactionVersionKey, | 598 model->SetNodeMetaInfo(model->root_node(), kBookmarkTransactionVersionKey, |
| 596 base::Int64ToString(model_version)); | 599 base::Int64ToString(model_version)); |
| 597 } | 600 } |
| 598 | 601 |
| 599 // Static. | 602 // Static. |
| 600 // Create a bookmark node corresponding to |src| if one is not already | 603 // Create a bookmark node corresponding to |src| if one is not already |
| 601 // associated with |src|. | 604 // associated with |src|. |
| 602 const BookmarkNode* BookmarkChangeProcessor::CreateOrUpdateBookmarkNode( | 605 const BookmarkNode* BookmarkChangeProcessor::CreateOrUpdateBookmarkNode( |
| 603 syncer::BaseNode* src, | 606 syncer::BaseNode* src, |
| 604 BookmarkModel* model, | 607 BookmarkModel* model, |
| 608 Profile* profile, |
| 605 BookmarkModelAssociator* model_associator) { | 609 BookmarkModelAssociator* model_associator) { |
| 606 const BookmarkNode* parent = | 610 const BookmarkNode* parent = |
| 607 model_associator->GetChromeNodeFromSyncId(src->GetParentId()); | 611 model_associator->GetChromeNodeFromSyncId(src->GetParentId()); |
| 608 if (!parent) { | 612 if (!parent) { |
| 609 DLOG(WARNING) << "Could not find parent of node being added/updated." | 613 DLOG(WARNING) << "Could not find parent of node being added/updated." |
| 610 << " Node title: " << src->GetTitle() | 614 << " Node title: " << src->GetTitle() |
| 611 << ", parent id = " << src->GetParentId(); | 615 << ", parent id = " << src->GetParentId(); |
| 612 | 616 |
| 613 return NULL; | 617 return NULL; |
| 614 } | 618 } |
| 615 int index = CalculateBookmarkModelInsertionIndex(parent, | 619 int index = CalculateBookmarkModelInsertionIndex(parent, |
| 616 src, | 620 src, |
| 617 model_associator); | 621 model_associator); |
| 618 const BookmarkNode* dst = model_associator->GetChromeNodeFromSyncId( | 622 const BookmarkNode* dst = model_associator->GetChromeNodeFromSyncId( |
| 619 src->GetId()); | 623 src->GetId()); |
| 620 if (!dst) { | 624 if (!dst) { |
| 621 dst = CreateBookmarkNode(src, parent, model, index); | 625 dst = CreateBookmarkNode(src, parent, model, profile, index); |
| 622 if (dst) | 626 if (dst) |
| 623 model_associator->Associate(dst, src->GetId()); | 627 model_associator->Associate(dst, src->GetId()); |
| 624 } else { | 628 } else { |
| 625 // URL and is_folder are not expected to change. | 629 // URL and is_folder are not expected to change. |
| 626 // TODO(ncarter): Determine if such changes should be legal or not. | 630 // TODO(ncarter): Determine if such changes should be legal or not. |
| 627 DCHECK_EQ(src->GetIsFolder(), dst->is_folder()); | 631 DCHECK_EQ(src->GetIsFolder(), dst->is_folder()); |
| 628 | 632 |
| 629 // Handle reparenting and/or repositioning. | 633 // Handle reparenting and/or repositioning. |
| 630 model->Move(dst, parent, index); | 634 model->Move(dst, parent, index); |
| 631 | 635 |
| 632 const sync_pb::BookmarkSpecifics& specifics = src->GetBookmarkSpecifics(); | 636 const sync_pb::BookmarkSpecifics& specifics = src->GetBookmarkSpecifics(); |
| 633 if (!src->GetIsFolder()) | 637 if (!src->GetIsFolder()) |
| 634 model->SetURL(dst, GURL(specifics.url())); | 638 model->SetURL(dst, GURL(specifics.url())); |
| 635 model->SetTitle(dst, UTF8ToUTF16(src->GetTitle())); | 639 model->SetTitle(dst, UTF8ToUTF16(src->GetTitle())); |
| 636 if (specifics.has_creation_time_us()) { | 640 if (specifics.has_creation_time_us()) { |
| 637 model->SetDateAdded(dst, | 641 model->SetDateAdded(dst, |
| 638 base::Time::FromInternalValue( | 642 base::Time::FromInternalValue( |
| 639 specifics.creation_time_us())); | 643 specifics.creation_time_us())); |
| 640 } | 644 } |
| 641 | 645 |
| 642 SetBookmarkFavicon(src, dst, model); | 646 SetBookmarkFavicon(src, dst, model, profile); |
| 643 } | 647 } |
| 644 | 648 |
| 645 return dst; | 649 return dst; |
| 646 } | 650 } |
| 647 | 651 |
| 648 // static | 652 // static |
| 649 void BookmarkChangeProcessor::UpdateTransactionVersion( | 653 void BookmarkChangeProcessor::UpdateTransactionVersion( |
| 650 int64 new_version, | 654 int64 new_version, |
| 651 BookmarkModel* model, | 655 BookmarkModel* model, |
| 652 const std::vector<const BookmarkNode*>& nodes) { | 656 const std::vector<const BookmarkNode*>& nodes) { |
| 653 if (new_version != syncer::syncable::kInvalidTransactionVersion) { | 657 if (new_version != syncer::syncable::kInvalidTransactionVersion) { |
| 654 model->SetNodeMetaInfo(model->root_node(), kBookmarkTransactionVersionKey, | 658 model->SetNodeMetaInfo(model->root_node(), kBookmarkTransactionVersionKey, |
| 655 base::Int64ToString(new_version)); | 659 base::Int64ToString(new_version)); |
| 656 for (size_t i = 0; i < nodes.size(); ++i) { | 660 for (size_t i = 0; i < nodes.size(); ++i) { |
| 657 model->SetNodeMetaInfo(nodes[i], kBookmarkTransactionVersionKey, | 661 model->SetNodeMetaInfo(nodes[i], kBookmarkTransactionVersionKey, |
| 658 base::Int64ToString(new_version)); | 662 base::Int64ToString(new_version)); |
| 659 } | 663 } |
| 660 } | 664 } |
| 661 } | 665 } |
| 662 | 666 |
| 663 // static | 667 // static |
| 664 // Creates a bookmark node under the given parent node from the given sync | 668 // Creates a bookmark node under the given parent node from the given sync |
| 665 // node. Returns the newly created node. | 669 // node. Returns the newly created node. |
| 666 const BookmarkNode* BookmarkChangeProcessor::CreateBookmarkNode( | 670 const BookmarkNode* BookmarkChangeProcessor::CreateBookmarkNode( |
| 667 syncer::BaseNode* sync_node, | 671 syncer::BaseNode* sync_node, |
| 668 const BookmarkNode* parent, | 672 const BookmarkNode* parent, |
| 669 BookmarkModel* model, | 673 BookmarkModel* model, |
| 674 Profile* profile, |
| 670 int index) { | 675 int index) { |
| 671 DCHECK(parent); | 676 DCHECK(parent); |
| 672 DCHECK(index >= 0 && index <= parent->child_count()); | 677 DCHECK(index >= 0 && index <= parent->child_count()); |
| 673 | 678 |
| 674 const BookmarkNode* node; | 679 const BookmarkNode* node; |
| 675 if (sync_node->GetIsFolder()) { | 680 if (sync_node->GetIsFolder()) { |
| 676 node = model->AddFolder(parent, index, | 681 node = model->AddFolder(parent, index, |
| 677 UTF8ToUTF16(sync_node->GetTitle())); | 682 UTF8ToUTF16(sync_node->GetTitle())); |
| 678 } else { | 683 } else { |
| 679 // 'creation_time_us' was added in m24. Assume a time of 0 means now. | 684 // 'creation_time_us' was added in m24. Assume a time of 0 means now. |
| 680 const sync_pb::BookmarkSpecifics& specifics = | 685 const sync_pb::BookmarkSpecifics& specifics = |
| 681 sync_node->GetBookmarkSpecifics(); | 686 sync_node->GetBookmarkSpecifics(); |
| 682 const int64 create_time_internal = specifics.creation_time_us(); | 687 const int64 create_time_internal = specifics.creation_time_us(); |
| 683 base::Time create_time = (create_time_internal == 0) ? | 688 base::Time create_time = (create_time_internal == 0) ? |
| 684 base::Time::Now() : base::Time::FromInternalValue(create_time_internal); | 689 base::Time::Now() : base::Time::FromInternalValue(create_time_internal); |
| 685 node = model->AddURLWithCreationTime(parent, index, | 690 node = model->AddURLWithCreationTime(parent, index, |
| 686 UTF8ToUTF16(sync_node->GetTitle()), | 691 UTF8ToUTF16(sync_node->GetTitle()), |
| 687 GURL(specifics.url()), create_time); | 692 GURL(specifics.url()), create_time); |
| 688 if (node) | 693 if (node) |
| 689 SetBookmarkFavicon(sync_node, node, model); | 694 SetBookmarkFavicon(sync_node, node, model, profile); |
| 690 } | 695 } |
| 691 return node; | 696 return node; |
| 692 } | 697 } |
| 693 | 698 |
| 694 // static | 699 // static |
| 695 // Sets the favicon of the given bookmark node from the given sync node. | 700 // Sets the favicon of the given bookmark node from the given sync node. |
| 696 bool BookmarkChangeProcessor::SetBookmarkFavicon( | 701 bool BookmarkChangeProcessor::SetBookmarkFavicon( |
| 697 syncer::BaseNode* sync_node, | 702 syncer::BaseNode* sync_node, |
| 698 const BookmarkNode* bookmark_node, | 703 const BookmarkNode* bookmark_node, |
| 699 BookmarkModel* bookmark_model) { | 704 BookmarkModel* bookmark_model, |
| 705 Profile* profile) { |
| 700 const sync_pb::BookmarkSpecifics& specifics = | 706 const sync_pb::BookmarkSpecifics& specifics = |
| 701 sync_node->GetBookmarkSpecifics(); | 707 sync_node->GetBookmarkSpecifics(); |
| 702 const std::string& icon_bytes_str = specifics.favicon(); | 708 const std::string& icon_bytes_str = specifics.favicon(); |
| 703 if (icon_bytes_str.empty()) | 709 if (icon_bytes_str.empty()) |
| 704 return false; | 710 return false; |
| 705 | 711 |
| 706 scoped_refptr<base::RefCountedString> icon_bytes( | 712 scoped_refptr<base::RefCountedString> icon_bytes( |
| 707 new base::RefCountedString()); | 713 new base::RefCountedString()); |
| 708 icon_bytes->data().assign(icon_bytes_str); | 714 icon_bytes->data().assign(icon_bytes_str); |
| 709 GURL icon_url(specifics.icon_url()); | 715 GURL icon_url(specifics.icon_url()); |
| 710 | 716 |
| 711 // Old clients may not be syncing the favicon URL. If the icon URL is not | 717 // Old clients may not be syncing the favicon URL. If the icon URL is not |
| 712 // synced, use the page URL as a fake icon URL as it is guaranteed to be | 718 // synced, use the page URL as a fake icon URL as it is guaranteed to be |
| 713 // unique. | 719 // unique. |
| 714 if (icon_url.is_empty()) | 720 if (icon_url.is_empty()) |
| 715 icon_url = bookmark_node->url(); | 721 icon_url = bookmark_node->url(); |
| 716 | 722 |
| 717 ApplyBookmarkFavicon(bookmark_node, bookmark_model->profile(), icon_url, | 723 ApplyBookmarkFavicon(bookmark_node, profile, icon_url, icon_bytes); |
| 718 icon_bytes); | |
| 719 | 724 |
| 720 return true; | 725 return true; |
| 721 } | 726 } |
| 722 | 727 |
| 723 // static | 728 // static |
| 724 void BookmarkChangeProcessor::ApplyBookmarkFavicon( | 729 void BookmarkChangeProcessor::ApplyBookmarkFavicon( |
| 725 const BookmarkNode* bookmark_node, | 730 const BookmarkNode* bookmark_node, |
| 726 Profile* profile, | 731 Profile* profile, |
| 727 const GURL& icon_url, | 732 const GURL& icon_url, |
| 728 const scoped_refptr<base::RefCountedMemory>& bitmap_data) { | 733 const scoped_refptr<base::RefCountedMemory>& bitmap_data) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 756 sync_pb::BookmarkSpecifics updated_specifics( | 761 sync_pb::BookmarkSpecifics updated_specifics( |
| 757 sync_node->GetBookmarkSpecifics()); | 762 sync_node->GetBookmarkSpecifics()); |
| 758 updated_specifics.set_favicon(favicon_bytes->front(), | 763 updated_specifics.set_favicon(favicon_bytes->front(), |
| 759 favicon_bytes->size()); | 764 favicon_bytes->size()); |
| 760 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); | 765 updated_specifics.set_icon_url(bookmark_node->icon_url().spec()); |
| 761 sync_node->SetBookmarkSpecifics(updated_specifics); | 766 sync_node->SetBookmarkSpecifics(updated_specifics); |
| 762 } | 767 } |
| 763 } | 768 } |
| 764 | 769 |
| 765 } // namespace browser_sync | 770 } // namespace browser_sync |
| OLD | NEW |