| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 if (synced_bookmarks.InitByTagLookup(kMobileBookmarksTag) == | 564 if (synced_bookmarks.InitByTagLookup(kMobileBookmarksTag) == |
| 565 syncer::BaseNode::INIT_OK && | 565 syncer::BaseNode::INIT_OK && |
| 566 synced_bookmarks.GetId() == it->id) { | 566 synced_bookmarks.GetId() == it->id) { |
| 567 // This is a newly created Synced Bookmarks node. Associate it. | 567 // This is a newly created Synced Bookmarks node. Associate it. |
| 568 model_associator_->Associate(model->mobile_node(), it->id); | 568 model_associator_->Associate(model->mobile_node(), it->id); |
| 569 } else { | 569 } else { |
| 570 // We ignore bookmarks we can't add. Chances are this is caused by | 570 // We ignore bookmarks we can't add. Chances are this is caused by |
| 571 // a bookmark that was not fully associated. | 571 // a bookmark that was not fully associated. |
| 572 LOG(ERROR) << "Failed to create bookmark node with title " | 572 LOG(ERROR) << "Failed to create bookmark node with title " |
| 573 << src.GetTitle() + " and url " | 573 << src.GetTitle() + " and url " |
| 574 << src.GetURL().possibly_invalid_spec(); | 574 << src.GetBookmarkSpecifics().url(); |
| 575 } | 575 } |
| 576 } | 576 } |
| 577 } | 577 } |
| 578 } | 578 } |
| 579 // Clean up the temporary node. | 579 // Clean up the temporary node. |
| 580 if (foster_parent) { | 580 if (foster_parent) { |
| 581 // There should be no nodes left under the foster parent. | 581 // There should be no nodes left under the foster parent. |
| 582 DCHECK_EQ(foster_parent->child_count(), 0); | 582 DCHECK_EQ(foster_parent->child_count(), 0); |
| 583 model->Remove(foster_parent->parent(), | 583 model->Remove(foster_parent->parent(), |
| 584 foster_parent->parent()->GetIndexOf(foster_parent)); | 584 foster_parent->parent()->GetIndexOf(foster_parent)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 if (dst) | 623 if (dst) |
| 624 model_associator->Associate(dst, src->GetId()); | 624 model_associator->Associate(dst, src->GetId()); |
| 625 } else { | 625 } else { |
| 626 // URL and is_folder are not expected to change. | 626 // URL and is_folder are not expected to change. |
| 627 // TODO(ncarter): Determine if such changes should be legal or not. | 627 // TODO(ncarter): Determine if such changes should be legal or not. |
| 628 DCHECK_EQ(src->GetIsFolder(), dst->is_folder()); | 628 DCHECK_EQ(src->GetIsFolder(), dst->is_folder()); |
| 629 | 629 |
| 630 // Handle reparenting and/or repositioning. | 630 // Handle reparenting and/or repositioning. |
| 631 model->Move(dst, parent, index); | 631 model->Move(dst, parent, index); |
| 632 | 632 |
| 633 const sync_pb::BookmarkSpecifics& specifics = src->GetBookmarkSpecifics(); |
| 633 if (!src->GetIsFolder()) | 634 if (!src->GetIsFolder()) |
| 634 model->SetURL(dst, src->GetURL()); | 635 model->SetURL(dst, GURL(specifics.url())); |
| 635 model->SetTitle(dst, UTF8ToUTF16(src->GetTitle())); | 636 model->SetTitle(dst, UTF8ToUTF16(src->GetTitle())); |
| 636 if (src->GetBookmarkSpecifics().has_creation_time_us()) { | 637 if (specifics.has_creation_time_us()) { |
| 637 model->SetDateAdded(dst, | 638 model->SetDateAdded(dst, |
| 638 base::Time::FromInternalValue( | 639 base::Time::FromInternalValue( |
| 639 src->GetBookmarkSpecifics().creation_time_us())); | 640 specifics.creation_time_us())); |
| 640 } | 641 } |
| 641 | 642 |
| 642 SetBookmarkFavicon(src, dst, model); | 643 SetBookmarkFavicon(src, dst, model); |
| 643 } | 644 } |
| 644 | 645 |
| 645 return dst; | 646 return dst; |
| 646 } | 647 } |
| 647 | 648 |
| 648 // static | 649 // static |
| 649 void BookmarkChangeProcessor::UpdateTransactionVersion( | 650 void BookmarkChangeProcessor::UpdateTransactionVersion( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 670 int index) { | 671 int index) { |
| 671 DCHECK(parent); | 672 DCHECK(parent); |
| 672 DCHECK(index >= 0 && index <= parent->child_count()); | 673 DCHECK(index >= 0 && index <= parent->child_count()); |
| 673 | 674 |
| 674 const BookmarkNode* node; | 675 const BookmarkNode* node; |
| 675 if (sync_node->GetIsFolder()) { | 676 if (sync_node->GetIsFolder()) { |
| 676 node = model->AddFolder(parent, index, | 677 node = model->AddFolder(parent, index, |
| 677 UTF8ToUTF16(sync_node->GetTitle())); | 678 UTF8ToUTF16(sync_node->GetTitle())); |
| 678 } else { | 679 } else { |
| 679 // 'creation_time_us' was added in m24. Assume a time of 0 means now. | 680 // 'creation_time_us' was added in m24. Assume a time of 0 means now. |
| 680 const int64 create_time_internal = | 681 const sync_pb::BookmarkSpecifics& specifics = |
| 681 sync_node->GetBookmarkSpecifics().creation_time_us(); | 682 sync_node->GetBookmarkSpecifics(); |
| 683 const int64 create_time_internal = specifics.creation_time_us(); |
| 682 base::Time create_time = (create_time_internal == 0) ? | 684 base::Time create_time = (create_time_internal == 0) ? |
| 683 base::Time::Now() : base::Time::FromInternalValue(create_time_internal); | 685 base::Time::Now() : base::Time::FromInternalValue(create_time_internal); |
| 684 node = model->AddURLWithCreationTime(parent, index, | 686 node = model->AddURLWithCreationTime(parent, index, |
| 685 UTF8ToUTF16(sync_node->GetTitle()), | 687 UTF8ToUTF16(sync_node->GetTitle()), |
| 686 sync_node->GetURL(), create_time); | 688 GURL(specifics.url()), create_time); |
| 687 if (node) | 689 if (node) |
| 688 SetBookmarkFavicon(sync_node, node, model); | 690 SetBookmarkFavicon(sync_node, node, model); |
| 689 } | 691 } |
| 690 return node; | 692 return node; |
| 691 } | 693 } |
| 692 | 694 |
| 693 // static | 695 // static |
| 694 // Sets the favicon of the given bookmark node from the given sync node. | 696 // Sets the favicon of the given bookmark node from the given sync node. |
| 695 bool BookmarkChangeProcessor::SetBookmarkFavicon( | 697 bool BookmarkChangeProcessor::SetBookmarkFavicon( |
| 696 syncer::BaseNode* sync_node, | 698 syncer::BaseNode* sync_node, |
| 697 const BookmarkNode* bookmark_node, | 699 const BookmarkNode* bookmark_node, |
| 698 BookmarkModel* bookmark_model) { | 700 BookmarkModel* bookmark_model) { |
| 699 std::vector<unsigned char> icon_bytes_vector; | 701 const sync_pb::BookmarkSpecifics& specifics = |
| 700 sync_node->GetFaviconBytes(&icon_bytes_vector); | 702 sync_node->GetBookmarkSpecifics(); |
| 701 if (icon_bytes_vector.empty()) | 703 const std::string& icon_str = specifics.favicon(); |
| 704 if (icon_str.empty()) |
| 702 return false; | 705 return false; |
| 703 | 706 |
| 707 scoped_refptr<base::RefCountedString> icon_bytes( |
| 708 new base::RefCountedString()); |
| 709 icon_bytes->data().assign(icon_str); |
| 710 |
| 704 ApplyBookmarkFavicon(bookmark_node, bookmark_model->profile(), | 711 ApplyBookmarkFavicon(bookmark_node, bookmark_model->profile(), |
| 705 icon_bytes_vector); | 712 icon_bytes); |
| 706 | 713 |
| 707 return true; | 714 return true; |
| 708 } | 715 } |
| 709 | 716 |
| 710 // static | 717 // static |
| 711 // Applies the given favicon bytes vector to the given bookmark node. | |
| 712 void BookmarkChangeProcessor::ApplyBookmarkFavicon( | 718 void BookmarkChangeProcessor::ApplyBookmarkFavicon( |
| 713 const BookmarkNode* bookmark_node, | 719 const BookmarkNode* bookmark_node, |
| 714 Profile* profile, | 720 Profile* profile, |
| 715 const std::vector<unsigned char>& icon_bytes_vector) { | 721 const scoped_refptr<base::RefCountedMemory>& bitmap_data) { |
| 716 HistoryService* history = | 722 HistoryService* history = |
| 717 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 723 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
| 718 FaviconService* favicon_service = | 724 FaviconService* favicon_service = |
| 719 FaviconServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 725 FaviconServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
| 720 | 726 |
| 721 history->AddPageNoVisitForBookmark(bookmark_node->url(), | 727 history->AddPageNoVisitForBookmark(bookmark_node->url(), |
| 722 bookmark_node->GetTitle()); | 728 bookmark_node->GetTitle()); |
| 723 // The client may have cached the favicon at 2x. Use MergeFavicon() as not to | 729 // The client may have cached the favicon at 2x. Use MergeFavicon() as not to |
| 724 // overwrite the cached 2x favicon bitmap. Use the page URL as a fake icon URL | 730 // overwrite the cached 2x favicon bitmap. Use the page URL as a fake icon URL |
| 725 // as it is guaranteed to be unique. Sync favicons are always | 731 // as it is guaranteed to be unique. Sync favicons are always |
| 726 // gfx::kFaviconSize in width and height. Store the favicon into history as | 732 // gfx::kFaviconSize in width and height. Store the favicon into history as |
| 727 // such. | 733 // such. |
| 728 scoped_refptr<base::RefCountedMemory> bitmap_data( | |
| 729 new base::RefCountedBytes(icon_bytes_vector)); | |
| 730 gfx::Size pixel_size(gfx::kFaviconSize, gfx::kFaviconSize); | 734 gfx::Size pixel_size(gfx::kFaviconSize, gfx::kFaviconSize); |
| 731 favicon_service->MergeFavicon(bookmark_node->url(), | 735 favicon_service->MergeFavicon(bookmark_node->url(), |
| 732 bookmark_node->url(), | 736 bookmark_node->url(), |
| 733 history::FAVICON, | 737 history::FAVICON, |
| 734 bitmap_data, | 738 bitmap_data, |
| 735 pixel_size); | 739 pixel_size); |
| 736 } | 740 } |
| 737 | 741 |
| 738 // static | 742 // static |
| 739 void BookmarkChangeProcessor::SetSyncNodeFavicon( | 743 void BookmarkChangeProcessor::SetSyncNodeFavicon( |
| 740 const BookmarkNode* bookmark_node, | 744 const BookmarkNode* bookmark_node, |
| 741 BookmarkModel* model, | 745 BookmarkModel* model, |
| 742 syncer::WriteNode* sync_node) { | 746 syncer::WriteNode* sync_node) { |
| 743 std::vector<unsigned char> favicon_bytes; | 747 std::vector<unsigned char> favicon_bytes; |
| 744 EncodeFavicon(bookmark_node, model, &favicon_bytes); | 748 EncodeFavicon(bookmark_node, model, &favicon_bytes); |
| 745 if (!favicon_bytes.empty()) | 749 if (!favicon_bytes.empty()) { |
| 746 sync_node->SetFaviconBytes(favicon_bytes); | 750 sync_pb::BookmarkSpecifics updated_specifics( |
| 751 sync_node->GetBookmarkSpecifics()); |
| 752 updated_specifics.set_favicon(&favicon_bytes[0], favicon_bytes.size()); |
| 753 sync_node->SetBookmarkSpecifics(updated_specifics); |
| 754 } |
| 747 } | 755 } |
| 748 | 756 |
| 749 } // namespace browser_sync | 757 } // namespace browser_sync |
| OLD | NEW |