| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bookmarks/bookmark_model.h" | 5 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/command_line.h" |
| 11 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_index.h" | 14 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_storage.h" | 15 #include "chrome/browser/bookmarks/bookmark_storage.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_utils.h" | 16 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/history/history_notifications.h" | 18 #include "chrome/browser/history/history_notifications.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/common/chrome_switches.h" |
| 19 #include "content/common/notification_service.h" | 21 #include "content/common/notification_service.h" |
| 20 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/l10n/l10n_util_collator.h" | 24 #include "ui/base/l10n/l10n_util_collator.h" |
| 23 #include "ui/gfx/codec/png_codec.h" | 25 #include "ui/gfx/codec/png_codec.h" |
| 24 | 26 |
| 25 using base::Time; | 27 using base::Time; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 54 favicon_load_handle_ = 0; | 56 favicon_load_handle_ = 0; |
| 55 type_ = !url_.is_empty() ? URL : BOOKMARK_BAR; | 57 type_ = !url_.is_empty() ? URL : BOOKMARK_BAR; |
| 56 date_added_ = Time::Now(); | 58 date_added_ = Time::Now(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 void BookmarkNode::InvalidateFavicon() { | 61 void BookmarkNode::InvalidateFavicon() { |
| 60 loaded_favicon_ = false; | 62 loaded_favicon_ = false; |
| 61 favicon_ = SkBitmap(); | 63 favicon_ = SkBitmap(); |
| 62 } | 64 } |
| 63 | 65 |
| 66 bool BookmarkNode::IsVisible() const { |
| 67 // The synced bookmark folder is invisible if the flag isn't set and there are |
| 68 // no bookmarks under it. |
| 69 if (type_ != BookmarkNode::SYNCED || |
| 70 CommandLine::ForCurrentProcess()->HasSwitch( |
| 71 switches::kEnableSyncedBookmarksFolder) || |
| 72 child_count() > 0) { |
| 73 return true; |
| 74 } |
| 75 return false; |
| 76 } |
| 77 |
| 64 void BookmarkNode::Reset(const history::StarredEntry& entry) { | 78 void BookmarkNode::Reset(const history::StarredEntry& entry) { |
| 65 DCHECK(entry.type != history::StarredEntry::URL || entry.url == url_); | 79 DCHECK(entry.type != history::StarredEntry::URL || entry.url == url_); |
| 66 | 80 |
| 67 favicon_ = SkBitmap(); | 81 favicon_ = SkBitmap(); |
| 68 switch (entry.type) { | 82 switch (entry.type) { |
| 69 case history::StarredEntry::URL: | 83 case history::StarredEntry::URL: |
| 70 type_ = BookmarkNode::URL; | 84 type_ = BookmarkNode::URL; |
| 71 break; | 85 break; |
| 72 case history::StarredEntry::USER_FOLDER: | 86 case history::StarredEntry::USER_FOLDER: |
| 73 type_ = BookmarkNode::FOLDER; | 87 type_ = BookmarkNode::FOLDER; |
| 74 break; | 88 break; |
| 75 case history::StarredEntry::BOOKMARK_BAR: | 89 case history::StarredEntry::BOOKMARK_BAR: |
| 76 type_ = BookmarkNode::BOOKMARK_BAR; | 90 type_ = BookmarkNode::BOOKMARK_BAR; |
| 77 break; | 91 break; |
| 92 case history::StarredEntry::SYNCED: |
| 93 type_ = BookmarkNode::SYNCED; |
| 94 break; |
| 78 case history::StarredEntry::OTHER: | 95 case history::StarredEntry::OTHER: |
| 79 type_ = BookmarkNode::OTHER_NODE; | 96 type_ = BookmarkNode::OTHER_NODE; |
| 80 break; | 97 break; |
| 81 default: | 98 default: |
| 82 NOTREACHED(); | 99 NOTREACHED(); |
| 83 } | 100 } |
| 84 date_added_ = entry.date_added; | 101 date_added_ = entry.date_added; |
| 85 date_folder_modified_ = entry.date_folder_modified; | 102 date_folder_modified_ = entry.date_folder_modified; |
| 86 set_title(entry.title); | 103 set_title(entry.title); |
| 87 } | 104 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 117 | 134 |
| 118 } // namespace | 135 } // namespace |
| 119 | 136 |
| 120 BookmarkModel::BookmarkModel(Profile* profile) | 137 BookmarkModel::BookmarkModel(Profile* profile) |
| 121 : profile_(profile), | 138 : profile_(profile), |
| 122 loaded_(false), | 139 loaded_(false), |
| 123 file_changed_(false), | 140 file_changed_(false), |
| 124 root_(GURL()), | 141 root_(GURL()), |
| 125 bookmark_bar_node_(NULL), | 142 bookmark_bar_node_(NULL), |
| 126 other_node_(NULL), | 143 other_node_(NULL), |
| 144 synced_node_(NULL), |
| 127 next_node_id_(1), | 145 next_node_id_(1), |
| 128 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), | 146 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), |
| 129 loaded_signal_(TRUE, FALSE) { | 147 loaded_signal_(TRUE, FALSE) { |
| 130 if (!profile_) { | 148 if (!profile_) { |
| 131 // Profile is null during testing. | 149 // Profile is null during testing. |
| 132 DoneLoading(CreateLoadDetails()); | 150 DoneLoading(CreateLoadDetails()); |
| 133 } | 151 } |
| 134 } | 152 } |
| 135 | 153 |
| 136 BookmarkModel::~BookmarkModel() { | 154 BookmarkModel::~BookmarkModel() { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 } | 270 } |
| 253 | 271 |
| 254 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { | 272 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { |
| 255 if (!node) { | 273 if (!node) { |
| 256 NOTREACHED(); | 274 NOTREACHED(); |
| 257 return; | 275 return; |
| 258 } | 276 } |
| 259 if (node->GetTitle() == title) | 277 if (node->GetTitle() == title) |
| 260 return; | 278 return; |
| 261 | 279 |
| 262 if (node == bookmark_bar_node_ || node == other_node_) { | 280 if (is_permanent_node(node)) { |
| 263 NOTREACHED(); | 281 NOTREACHED(); |
| 264 return; | 282 return; |
| 265 } | 283 } |
| 266 | 284 |
| 267 // The title index doesn't support changing the title, instead we remove then | 285 // The title index doesn't support changing the title, instead we remove then |
| 268 // add it back. | 286 // add it back. |
| 269 index_->Remove(node); | 287 index_->Remove(node); |
| 270 AsMutable(node)->set_title(title); | 288 AsMutable(node)->set_title(title); |
| 271 index_->Add(node); | 289 index_->Add(node); |
| 272 | 290 |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 details->ids_reassigned()) { | 579 details->ids_reassigned()) { |
| 562 // If bookmarks file changed externally, the IDs may have changed | 580 // If bookmarks file changed externally, the IDs may have changed |
| 563 // externally. In that case, the decoder may have reassigned IDs to make | 581 // externally. In that case, the decoder may have reassigned IDs to make |
| 564 // them unique. So when the file has changed externally, we should save the | 582 // them unique. So when the file has changed externally, we should save the |
| 565 // bookmarks file to persist new IDs. | 583 // bookmarks file to persist new IDs. |
| 566 if (store_.get()) | 584 if (store_.get()) |
| 567 store_->ScheduleSave(); | 585 store_->ScheduleSave(); |
| 568 } | 586 } |
| 569 bookmark_bar_node_ = details->release_bb_node(); | 587 bookmark_bar_node_ = details->release_bb_node(); |
| 570 other_node_ = details->release_other_folder_node(); | 588 other_node_ = details->release_other_folder_node(); |
| 589 synced_node_ = details->release_synced_folder_node(); |
| 571 index_.reset(details->release_index()); | 590 index_.reset(details->release_index()); |
| 572 | 591 |
| 573 // WARNING: order is important here, various places assume bookmark bar then | 592 // WARNING: order is important here, various places assume bookmark bar then |
| 574 // other node. | 593 // other node. |
| 575 root_.Add(bookmark_bar_node_, 0); | 594 root_.Add(bookmark_bar_node_, 0); |
| 576 root_.Add(other_node_, 1); | 595 root_.Add(other_node_, 1); |
| 596 root_.Add(synced_node_, 2); |
| 577 | 597 |
| 578 { | 598 { |
| 579 base::AutoLock url_lock(url_lock_); | 599 base::AutoLock url_lock(url_lock_); |
| 580 // Update nodes_ordered_by_url_set_ from the nodes. | 600 // Update nodes_ordered_by_url_set_ from the nodes. |
| 581 PopulateNodesByURL(&root_); | 601 PopulateNodesByURL(&root_); |
| 582 } | 602 } |
| 583 | 603 |
| 584 loaded_ = true; | 604 loaded_ = true; |
| 585 | 605 |
| 586 loaded_signal_.Signal(); | 606 loaded_signal_.Signal(); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 entry.type = history::StarredEntry::BOOKMARK_BAR; | 732 entry.type = history::StarredEntry::BOOKMARK_BAR; |
| 713 return CreateRootNodeFromStarredEntry(entry); | 733 return CreateRootNodeFromStarredEntry(entry); |
| 714 } | 734 } |
| 715 | 735 |
| 716 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { | 736 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { |
| 717 history::StarredEntry entry; | 737 history::StarredEntry entry; |
| 718 entry.type = history::StarredEntry::OTHER; | 738 entry.type = history::StarredEntry::OTHER; |
| 719 return CreateRootNodeFromStarredEntry(entry); | 739 return CreateRootNodeFromStarredEntry(entry); |
| 720 } | 740 } |
| 721 | 741 |
| 742 BookmarkNode* BookmarkModel::CreateSyncedBookmarksNode() { |
| 743 history::StarredEntry entry; |
| 744 entry.type = history::StarredEntry::SYNCED; |
| 745 return CreateRootNodeFromStarredEntry(entry); |
| 746 } |
| 747 |
| 722 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( | 748 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( |
| 723 const history::StarredEntry& entry) { | 749 const history::StarredEntry& entry) { |
| 724 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || | 750 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || |
| 725 entry.type == history::StarredEntry::OTHER); | 751 entry.type == history::StarredEntry::OTHER || |
| 752 entry.type == history::StarredEntry::SYNCED); |
| 726 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); | 753 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); |
| 727 node->Reset(entry); | 754 node->Reset(entry); |
| 728 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { | 755 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { |
| 729 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); | 756 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); |
| 757 } else if (entry.type == history::StarredEntry::SYNCED) { |
| 758 node->set_title(l10n_util::GetStringUTF16( |
| 759 IDS_BOOMARK_BAR_SYNCED_FOLDER_NAME)); |
| 730 } else { | 760 } else { |
| 731 node->set_title( | 761 node->set_title( |
| 732 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); | 762 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); |
| 733 } | 763 } |
| 734 return node; | 764 return node; |
| 735 } | 765 } |
| 736 | 766 |
| 737 void BookmarkModel::OnFaviconDataAvailable( | 767 void BookmarkModel::OnFaviconDataAvailable( |
| 738 FaviconService::Handle handle, | 768 FaviconService::Handle handle, |
| 739 history::FaviconData favicon) { | 769 history::FaviconData favicon) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 819 return next_node_id_++; | 849 return next_node_id_++; |
| 820 } | 850 } |
| 821 | 851 |
| 822 void BookmarkModel::SetFileChanged() { | 852 void BookmarkModel::SetFileChanged() { |
| 823 file_changed_ = true; | 853 file_changed_ = true; |
| 824 } | 854 } |
| 825 | 855 |
| 826 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { | 856 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
| 827 BookmarkNode* bb_node = CreateBookmarkNode(); | 857 BookmarkNode* bb_node = CreateBookmarkNode(); |
| 828 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); | 858 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); |
| 859 BookmarkNode* synced_folder_node = CreateSyncedBookmarksNode(); |
| 829 return new BookmarkLoadDetails( | 860 return new BookmarkLoadDetails( |
| 830 bb_node, other_folder_node, new BookmarkIndex(profile()), next_node_id_); | 861 bb_node, other_folder_node, synced_folder_node, |
| 862 new BookmarkIndex(profile()), next_node_id_); |
| 831 } | 863 } |
| OLD | NEW |