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 if (type_ != BookmarkNode::SYNCED || |
| 68 CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 switches::kEnableSyncedBookmarksFolder)) { |
| 70 return true; |
| 71 } |
| 72 return false; |
| 73 } |
| 74 |
64 void BookmarkNode::Reset(const history::StarredEntry& entry) { | 75 void BookmarkNode::Reset(const history::StarredEntry& entry) { |
65 DCHECK(entry.type != history::StarredEntry::URL || entry.url == url_); | 76 DCHECK(entry.type != history::StarredEntry::URL || entry.url == url_); |
66 | 77 |
67 favicon_ = SkBitmap(); | 78 favicon_ = SkBitmap(); |
68 switch (entry.type) { | 79 switch (entry.type) { |
69 case history::StarredEntry::URL: | 80 case history::StarredEntry::URL: |
70 type_ = BookmarkNode::URL; | 81 type_ = BookmarkNode::URL; |
71 break; | 82 break; |
72 case history::StarredEntry::USER_FOLDER: | 83 case history::StarredEntry::USER_FOLDER: |
73 type_ = BookmarkNode::FOLDER; | 84 type_ = BookmarkNode::FOLDER; |
74 break; | 85 break; |
75 case history::StarredEntry::BOOKMARK_BAR: | 86 case history::StarredEntry::BOOKMARK_BAR: |
76 type_ = BookmarkNode::BOOKMARK_BAR; | 87 type_ = BookmarkNode::BOOKMARK_BAR; |
77 break; | 88 break; |
| 89 case history::StarredEntry::SYNCED: |
| 90 type_ = BookmarkNode::SYNCED; |
| 91 break; |
78 case history::StarredEntry::OTHER: | 92 case history::StarredEntry::OTHER: |
79 type_ = BookmarkNode::OTHER_NODE; | 93 type_ = BookmarkNode::OTHER_NODE; |
80 break; | 94 break; |
81 default: | 95 default: |
82 NOTREACHED(); | 96 NOTREACHED(); |
83 } | 97 } |
84 date_added_ = entry.date_added; | 98 date_added_ = entry.date_added; |
85 date_folder_modified_ = entry.date_folder_modified; | 99 date_folder_modified_ = entry.date_folder_modified; |
86 set_title(entry.title); | 100 set_title(entry.title); |
87 } | 101 } |
(...skipping 29 matching lines...) Expand all Loading... |
117 | 131 |
118 } // namespace | 132 } // namespace |
119 | 133 |
120 BookmarkModel::BookmarkModel(Profile* profile) | 134 BookmarkModel::BookmarkModel(Profile* profile) |
121 : profile_(profile), | 135 : profile_(profile), |
122 loaded_(false), | 136 loaded_(false), |
123 file_changed_(false), | 137 file_changed_(false), |
124 root_(GURL()), | 138 root_(GURL()), |
125 bookmark_bar_node_(NULL), | 139 bookmark_bar_node_(NULL), |
126 other_node_(NULL), | 140 other_node_(NULL), |
| 141 synced_node_(NULL), |
127 next_node_id_(1), | 142 next_node_id_(1), |
128 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), | 143 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), |
129 loaded_signal_(TRUE, FALSE) { | 144 loaded_signal_(TRUE, FALSE) { |
130 if (!profile_) { | 145 if (!profile_) { |
131 // Profile is null during testing. | 146 // Profile is null during testing. |
132 DoneLoading(CreateLoadDetails()); | 147 DoneLoading(CreateLoadDetails()); |
133 } | 148 } |
134 } | 149 } |
135 | 150 |
136 BookmarkModel::~BookmarkModel() { | 151 BookmarkModel::~BookmarkModel() { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 267 } |
253 | 268 |
254 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { | 269 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { |
255 if (!node) { | 270 if (!node) { |
256 NOTREACHED(); | 271 NOTREACHED(); |
257 return; | 272 return; |
258 } | 273 } |
259 if (node->GetTitle() == title) | 274 if (node->GetTitle() == title) |
260 return; | 275 return; |
261 | 276 |
262 if (node == bookmark_bar_node_ || node == other_node_) { | 277 if (is_permanent_node(node)) { |
263 NOTREACHED(); | 278 NOTREACHED(); |
264 return; | 279 return; |
265 } | 280 } |
266 | 281 |
267 // The title index doesn't support changing the title, instead we remove then | 282 // The title index doesn't support changing the title, instead we remove then |
268 // add it back. | 283 // add it back. |
269 index_->Remove(node); | 284 index_->Remove(node); |
270 AsMutable(node)->set_title(title); | 285 AsMutable(node)->set_title(title); |
271 index_->Add(node); | 286 index_->Add(node); |
272 | 287 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 details->ids_reassigned()) { | 576 details->ids_reassigned()) { |
562 // If bookmarks file changed externally, the IDs may have changed | 577 // If bookmarks file changed externally, the IDs may have changed |
563 // externally. In that case, the decoder may have reassigned IDs to make | 578 // 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 | 579 // them unique. So when the file has changed externally, we should save the |
565 // bookmarks file to persist new IDs. | 580 // bookmarks file to persist new IDs. |
566 if (store_.get()) | 581 if (store_.get()) |
567 store_->ScheduleSave(); | 582 store_->ScheduleSave(); |
568 } | 583 } |
569 bookmark_bar_node_ = details->release_bb_node(); | 584 bookmark_bar_node_ = details->release_bb_node(); |
570 other_node_ = details->release_other_folder_node(); | 585 other_node_ = details->release_other_folder_node(); |
| 586 synced_node_ = details->release_synced_folder_node(); |
571 index_.reset(details->release_index()); | 587 index_.reset(details->release_index()); |
572 | 588 |
573 // WARNING: order is important here, various places assume bookmark bar then | 589 // WARNING: order is important here, various places assume bookmark bar then |
574 // other node. | 590 // other node. |
575 root_.Add(bookmark_bar_node_, 0); | 591 root_.Add(bookmark_bar_node_, 0); |
576 root_.Add(other_node_, 1); | 592 root_.Add(other_node_, 1); |
| 593 root_.Add(synced_node_, 2); |
577 | 594 |
578 { | 595 { |
579 base::AutoLock url_lock(url_lock_); | 596 base::AutoLock url_lock(url_lock_); |
580 // Update nodes_ordered_by_url_set_ from the nodes. | 597 // Update nodes_ordered_by_url_set_ from the nodes. |
581 PopulateNodesByURL(&root_); | 598 PopulateNodesByURL(&root_); |
582 } | 599 } |
583 | 600 |
584 loaded_ = true; | 601 loaded_ = true; |
585 | 602 |
586 loaded_signal_.Signal(); | 603 loaded_signal_.Signal(); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 entry.type = history::StarredEntry::BOOKMARK_BAR; | 729 entry.type = history::StarredEntry::BOOKMARK_BAR; |
713 return CreateRootNodeFromStarredEntry(entry); | 730 return CreateRootNodeFromStarredEntry(entry); |
714 } | 731 } |
715 | 732 |
716 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { | 733 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { |
717 history::StarredEntry entry; | 734 history::StarredEntry entry; |
718 entry.type = history::StarredEntry::OTHER; | 735 entry.type = history::StarredEntry::OTHER; |
719 return CreateRootNodeFromStarredEntry(entry); | 736 return CreateRootNodeFromStarredEntry(entry); |
720 } | 737 } |
721 | 738 |
| 739 BookmarkNode* BookmarkModel::CreateSyncedBookmarksNode() { |
| 740 history::StarredEntry entry; |
| 741 entry.type = history::StarredEntry::SYNCED; |
| 742 return CreateRootNodeFromStarredEntry(entry); |
| 743 } |
| 744 |
722 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( | 745 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( |
723 const history::StarredEntry& entry) { | 746 const history::StarredEntry& entry) { |
724 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || | 747 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || |
725 entry.type == history::StarredEntry::OTHER); | 748 entry.type == history::StarredEntry::OTHER || |
| 749 entry.type == history::StarredEntry::SYNCED); |
726 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); | 750 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); |
727 node->Reset(entry); | 751 node->Reset(entry); |
728 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { | 752 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { |
729 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); | 753 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); |
| 754 } else if (entry.type == history::StarredEntry::SYNCED) { |
| 755 node->set_title(l10n_util::GetStringUTF16( |
| 756 IDS_BOOMARK_BAR_SYNCED_FOLDER_NAME)); |
730 } else { | 757 } else { |
731 node->set_title( | 758 node->set_title( |
732 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); | 759 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); |
733 } | 760 } |
734 return node; | 761 return node; |
735 } | 762 } |
736 | 763 |
737 void BookmarkModel::OnFaviconDataAvailable( | 764 void BookmarkModel::OnFaviconDataAvailable( |
738 FaviconService::Handle handle, | 765 FaviconService::Handle handle, |
739 history::FaviconData favicon) { | 766 history::FaviconData favicon) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 return next_node_id_++; | 846 return next_node_id_++; |
820 } | 847 } |
821 | 848 |
822 void BookmarkModel::SetFileChanged() { | 849 void BookmarkModel::SetFileChanged() { |
823 file_changed_ = true; | 850 file_changed_ = true; |
824 } | 851 } |
825 | 852 |
826 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { | 853 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
827 BookmarkNode* bb_node = CreateBookmarkNode(); | 854 BookmarkNode* bb_node = CreateBookmarkNode(); |
828 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); | 855 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); |
| 856 BookmarkNode* synced_folder_node = CreateSyncedBookmarksNode(); |
829 return new BookmarkLoadDetails( | 857 return new BookmarkLoadDetails( |
830 bb_node, other_folder_node, new BookmarkIndex(profile()), next_node_id_); | 858 bb_node, other_folder_node, synced_folder_node, |
| 859 new BookmarkIndex(profile()), next_node_id_); |
831 } | 860 } |
OLD | NEW |