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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 switch (entry.type) { | 70 switch (entry.type) { |
69 case history::StarredEntry::URL: | 71 case history::StarredEntry::URL: |
70 type_ = BookmarkNode::URL; | 72 type_ = BookmarkNode::URL; |
71 break; | 73 break; |
72 case history::StarredEntry::USER_FOLDER: | 74 case history::StarredEntry::USER_FOLDER: |
73 type_ = BookmarkNode::FOLDER; | 75 type_ = BookmarkNode::FOLDER; |
74 break; | 76 break; |
75 case history::StarredEntry::BOOKMARK_BAR: | 77 case history::StarredEntry::BOOKMARK_BAR: |
76 type_ = BookmarkNode::BOOKMARK_BAR; | 78 type_ = BookmarkNode::BOOKMARK_BAR; |
77 break; | 79 break; |
80 case history::StarredEntry::SYNCED: | |
81 type_ = BookmarkNode::SYNCED; | |
82 break; | |
78 case history::StarredEntry::OTHER: | 83 case history::StarredEntry::OTHER: |
79 type_ = BookmarkNode::OTHER_NODE; | 84 type_ = BookmarkNode::OTHER_NODE; |
80 break; | 85 break; |
81 default: | 86 default: |
82 NOTREACHED(); | 87 NOTREACHED(); |
83 } | 88 } |
84 date_added_ = entry.date_added; | 89 date_added_ = entry.date_added; |
85 date_folder_modified_ = entry.date_folder_modified; | 90 date_folder_modified_ = entry.date_folder_modified; |
86 set_title(entry.title); | 91 set_title(entry.title); |
87 } | 92 } |
(...skipping 29 matching lines...) Expand all Loading... | |
117 | 122 |
118 } // namespace | 123 } // namespace |
119 | 124 |
120 BookmarkModel::BookmarkModel(Profile* profile) | 125 BookmarkModel::BookmarkModel(Profile* profile) |
121 : profile_(profile), | 126 : profile_(profile), |
122 loaded_(false), | 127 loaded_(false), |
123 file_changed_(false), | 128 file_changed_(false), |
124 root_(GURL()), | 129 root_(GURL()), |
125 bookmark_bar_node_(NULL), | 130 bookmark_bar_node_(NULL), |
126 other_node_(NULL), | 131 other_node_(NULL), |
132 synced_node_(NULL), | |
127 next_node_id_(1), | 133 next_node_id_(1), |
128 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), | 134 observers_(ObserverList<BookmarkModelObserver>::NOTIFY_EXISTING_ONLY), |
129 loaded_signal_(TRUE, FALSE) { | 135 loaded_signal_(TRUE, FALSE) { |
130 if (!profile_) { | 136 if (!profile_) { |
131 // Profile is null during testing. | 137 // Profile is null during testing. |
132 DoneLoading(CreateLoadDetails()); | 138 DoneLoading(CreateLoadDetails()); |
133 } | 139 } |
134 } | 140 } |
135 | 141 |
136 BookmarkModel::~BookmarkModel() { | 142 BookmarkModel::~BookmarkModel() { |
137 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, | 143 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
138 BookmarkModelBeingDeleted(this)); | 144 BookmarkModelBeingDeleted(this)); |
139 | 145 |
140 if (store_) { | 146 if (store_) { |
141 // The store maintains a reference back to us. We need to tell it we're gone | 147 // The store maintains a reference back to us. We need to tell it we're gone |
142 // so that it doesn't try and invoke a method back on us again. | 148 // so that it doesn't try and invoke a method back on us again. |
143 store_->BookmarkModelDeleted(); | 149 store_->BookmarkModelDeleted(); |
144 } | 150 } |
151 // If the switch wasn't set, then synced_node_ hasn't been added to the model | |
152 // and we must delete it. | |
153 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
154 switches::kEnableSyncedBookmarksFolder)) { | |
155 delete synced_node_; | |
156 } | |
145 } | 157 } |
146 | 158 |
147 void BookmarkModel::Load() { | 159 void BookmarkModel::Load() { |
148 if (store_.get()) { | 160 if (store_.get()) { |
149 // If the store is non-null, it means Load was already invoked. Load should | 161 // If the store is non-null, it means Load was already invoked. Load should |
150 // only be invoked once. | 162 // only be invoked once. |
151 NOTREACHED(); | 163 NOTREACHED(); |
152 return; | 164 return; |
153 } | 165 } |
154 | 166 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 } | 264 } |
253 | 265 |
254 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { | 266 void BookmarkModel::SetTitle(const BookmarkNode* node, const string16& title) { |
255 if (!node) { | 267 if (!node) { |
256 NOTREACHED(); | 268 NOTREACHED(); |
257 return; | 269 return; |
258 } | 270 } |
259 if (node->GetTitle() == title) | 271 if (node->GetTitle() == title) |
260 return; | 272 return; |
261 | 273 |
262 if (node == bookmark_bar_node_ || node == other_node_) { | 274 if (node == bookmark_bar_node_ || node == other_node_ || |
275 node == synced_node_) { | |
263 NOTREACHED(); | 276 NOTREACHED(); |
264 return; | 277 return; |
265 } | 278 } |
266 | 279 |
267 // The title index doesn't support changing the title, instead we remove then | 280 // The title index doesn't support changing the title, instead we remove then |
268 // add it back. | 281 // add it back. |
269 index_->Remove(node); | 282 index_->Remove(node); |
270 AsMutable(node)->set_title(title); | 283 AsMutable(node)->set_title(title); |
271 index_->Add(node); | 284 index_->Add(node); |
272 | 285 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
561 details->ids_reassigned()) { | 574 details->ids_reassigned()) { |
562 // If bookmarks file changed externally, the IDs may have changed | 575 // If bookmarks file changed externally, the IDs may have changed |
563 // externally. In that case, the decoder may have reassigned IDs to make | 576 // 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 | 577 // them unique. So when the file has changed externally, we should save the |
565 // bookmarks file to persist new IDs. | 578 // bookmarks file to persist new IDs. |
566 if (store_.get()) | 579 if (store_.get()) |
567 store_->ScheduleSave(); | 580 store_->ScheduleSave(); |
568 } | 581 } |
569 bookmark_bar_node_ = details->release_bb_node(); | 582 bookmark_bar_node_ = details->release_bb_node(); |
570 other_node_ = details->release_other_folder_node(); | 583 other_node_ = details->release_other_folder_node(); |
584 synced_node_ = details->release_synced_folder_node(); | |
571 index_.reset(details->release_index()); | 585 index_.reset(details->release_index()); |
572 | 586 |
573 // WARNING: order is important here, various places assume bookmark bar then | 587 // WARNING: order is important here, various places assume bookmark bar then |
574 // other node. | 588 // other node. |
575 root_.Add(bookmark_bar_node_, 0); | 589 root_.Add(bookmark_bar_node_, 0); |
576 root_.Add(other_node_, 1); | 590 root_.Add(other_node_, 1); |
591 // TODO(yfriedman): If this switch is removed, update the desctructor to avoid | |
592 // double-delete of synced_node_; | |
593 | |
594 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
595 switches::kEnableSyncedBookmarksFolder)) { | |
596 root_.Add(synced_node_, 2); | |
sky
2011/05/12 16:29:13
How come we don't always add the node to the model
Yaron
2011/05/12 18:19:23
Because that caused it to show in the UI. Perhaps
| |
597 } | |
577 | 598 |
578 { | 599 { |
579 base::AutoLock url_lock(url_lock_); | 600 base::AutoLock url_lock(url_lock_); |
580 // Update nodes_ordered_by_url_set_ from the nodes. | 601 // Update nodes_ordered_by_url_set_ from the nodes. |
581 PopulateNodesByURL(&root_); | 602 PopulateNodesByURL(&root_); |
582 } | 603 } |
583 | 604 |
584 loaded_ = true; | 605 loaded_ = true; |
585 | 606 |
586 loaded_signal_.Signal(); | 607 loaded_signal_.Signal(); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 entry.type = history::StarredEntry::BOOKMARK_BAR; | 733 entry.type = history::StarredEntry::BOOKMARK_BAR; |
713 return CreateRootNodeFromStarredEntry(entry); | 734 return CreateRootNodeFromStarredEntry(entry); |
714 } | 735 } |
715 | 736 |
716 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { | 737 BookmarkNode* BookmarkModel::CreateOtherBookmarksNode() { |
717 history::StarredEntry entry; | 738 history::StarredEntry entry; |
718 entry.type = history::StarredEntry::OTHER; | 739 entry.type = history::StarredEntry::OTHER; |
719 return CreateRootNodeFromStarredEntry(entry); | 740 return CreateRootNodeFromStarredEntry(entry); |
720 } | 741 } |
721 | 742 |
743 BookmarkNode* BookmarkModel::CreateSyncedBookmarksNode() { | |
744 history::StarredEntry entry; | |
745 entry.type = history::StarredEntry::SYNCED; | |
746 return CreateRootNodeFromStarredEntry(entry); | |
747 } | |
748 | |
722 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( | 749 BookmarkNode* BookmarkModel::CreateRootNodeFromStarredEntry( |
723 const history::StarredEntry& entry) { | 750 const history::StarredEntry& entry) { |
724 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || | 751 DCHECK(entry.type == history::StarredEntry::BOOKMARK_BAR || |
725 entry.type == history::StarredEntry::OTHER); | 752 entry.type == history::StarredEntry::OTHER || |
753 entry.type == history::StarredEntry::SYNCED); | |
726 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); | 754 BookmarkNode* node = new BookmarkNode(generate_next_node_id(), GURL()); |
727 node->Reset(entry); | 755 node->Reset(entry); |
728 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { | 756 if (entry.type == history::StarredEntry::BOOKMARK_BAR) { |
729 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); | 757 node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); |
758 } else if (entry.type == history::StarredEntry::SYNCED) { | |
759 node->set_title(l10n_util::GetStringUTF16( | |
760 IDS_BOOMARK_BAR_SYNCED_FOLDER_NAME)); | |
730 } else { | 761 } else { |
731 node->set_title( | 762 node->set_title( |
732 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); | 763 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); |
733 } | 764 } |
734 return node; | 765 return node; |
735 } | 766 } |
736 | 767 |
737 void BookmarkModel::OnFaviconDataAvailable( | 768 void BookmarkModel::OnFaviconDataAvailable( |
738 FaviconService::Handle handle, | 769 FaviconService::Handle handle, |
739 history::FaviconData favicon) { | 770 history::FaviconData favicon) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
819 return next_node_id_++; | 850 return next_node_id_++; |
820 } | 851 } |
821 | 852 |
822 void BookmarkModel::SetFileChanged() { | 853 void BookmarkModel::SetFileChanged() { |
823 file_changed_ = true; | 854 file_changed_ = true; |
824 } | 855 } |
825 | 856 |
826 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { | 857 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
827 BookmarkNode* bb_node = CreateBookmarkNode(); | 858 BookmarkNode* bb_node = CreateBookmarkNode(); |
828 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); | 859 BookmarkNode* other_folder_node = CreateOtherBookmarksNode(); |
860 BookmarkNode* synced_folder_node = CreateSyncedBookmarksNode(); | |
829 return new BookmarkLoadDetails( | 861 return new BookmarkLoadDetails( |
830 bb_node, other_folder_node, new BookmarkIndex(profile()), next_node_id_); | 862 bb_node, other_folder_node, synced_folder_node, |
863 new BookmarkIndex(profile()), next_node_id_); | |
831 } | 864 } |
OLD | NEW |