Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: components/bookmarks/browser/bookmark_model.cc

Issue 305973004: BookmarkClient can add extra nodes to BookmarkModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: renamed methods Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/bookmarks/browser/bookmark_model.h" 5 #include "components/bookmarks/browser/bookmark_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 class VisibilityComparator 47 class VisibilityComparator
48 : public std::binary_function<const BookmarkPermanentNode*, 48 : public std::binary_function<const BookmarkPermanentNode*,
49 const BookmarkPermanentNode*, 49 const BookmarkPermanentNode*,
50 bool> { 50 bool> {
51 public: 51 public:
52 explicit VisibilityComparator(BookmarkClient* client) : client_(client) {} 52 explicit VisibilityComparator(BookmarkClient* client) : client_(client) {}
53 53
54 // Returns true if |n1| preceeds |n2|. 54 // Returns true if |n1| preceeds |n2|.
55 bool operator()(const BookmarkPermanentNode* n1, 55 bool operator()(const BookmarkPermanentNode* n1,
56 const BookmarkPermanentNode* n2) { 56 const BookmarkPermanentNode* n2) {
57 bool n1_visible = client_->IsPermanentNodeVisible(n1->type()); 57 bool n1_visible = client_->IsPermanentNodeVisible(n1);
58 bool n2_visible = client_->IsPermanentNodeVisible(n2->type()); 58 bool n2_visible = client_->IsPermanentNodeVisible(n2);
59 return n1_visible != n2_visible && n1_visible; 59 return n1_visible != n2_visible && n1_visible;
60 } 60 }
61 61
62 private: 62 private:
63 BookmarkClient* client_; 63 BookmarkClient* client_;
64 }; 64 };
65 65
66 // Comparator used when sorting bookmarks. Folders are sorted first, then 66 // Comparator used when sorting bookmarks. Folders are sorted first, then
67 // bookmarks. 67 // bookmarks.
68 class SortComparator : public std::binary_function<const BookmarkNode*, 68 class SortComparator : public std::binary_function<const BookmarkNode*,
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 OnWillRemoveAllBookmarks(this)); 205 OnWillRemoveAllBookmarks(this));
206 206
207 BeginExtensiveChanges(); 207 BeginExtensiveChanges();
208 // Skip deleting permanent nodes. Permanent bookmark nodes are the root and 208 // Skip deleting permanent nodes. Permanent bookmark nodes are the root and
209 // its immediate children. For removing all non permanent nodes just remove 209 // its immediate children. For removing all non permanent nodes just remove
210 // all children of non-root permanent nodes. 210 // all children of non-root permanent nodes.
211 { 211 {
212 base::AutoLock url_lock(url_lock_); 212 base::AutoLock url_lock(url_lock_);
213 for (int i = 0; i < root_.child_count(); ++i) { 213 for (int i = 0; i < root_.child_count(); ++i) {
214 BookmarkNode* permanent_node = root_.GetChild(i); 214 BookmarkNode* permanent_node = root_.GetChild(i);
215
216 if (!client_->CanRemovePermanentNodeChildren(permanent_node))
217 continue;
218
215 for (int j = permanent_node->child_count() - 1; j >= 0; --j) { 219 for (int j = permanent_node->child_count() - 1; j >= 0; --j) {
216 BookmarkNode* child_node = permanent_node->GetChild(j); 220 BookmarkNode* child_node = permanent_node->GetChild(j);
217 removed_nodes.push_back(child_node); 221 removed_nodes.push_back(child_node);
218 RemoveNodeAndGetRemovedUrls(child_node, &removed_urls); 222 RemoveNodeAndGetRemovedUrls(child_node, &removed_urls);
219 } 223 }
220 } 224 }
221 } 225 }
222 EndExtensiveChanges(); 226 EndExtensiveChanges();
223 if (store_.get()) 227 if (store_.get())
224 store_->ScheduleSave(); 228 store_->ScheduleSave();
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 316
313 void BookmarkModel::SetTitle(const BookmarkNode* node, 317 void BookmarkModel::SetTitle(const BookmarkNode* node,
314 const base::string16& title) { 318 const base::string16& title) {
315 if (!node) { 319 if (!node) {
316 NOTREACHED(); 320 NOTREACHED();
317 return; 321 return;
318 } 322 }
319 if (node->GetTitle() == title) 323 if (node->GetTitle() == title)
320 return; 324 return;
321 325
322 if (is_permanent_node(node)) { 326 if (is_permanent_node(node) && !client_->CanSetPermanentNodeTitle(node)) {
323 NOTREACHED(); 327 NOTREACHED();
324 return; 328 return;
325 } 329 }
326 330
327 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, 331 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
328 OnWillChangeBookmarkNode(this, node)); 332 OnWillChangeBookmarkNode(this, node));
329 333
330 // The title index doesn't support changing the title, instead we remove then 334 // The title index doesn't support changing the title, instead we remove then
331 // add it back. 335 // add it back.
332 index_->Remove(node); 336 index_->Remove(node);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 if (AsMutable(node)->DeleteMetaInfo(key) && store_.get()) 428 if (AsMutable(node)->DeleteMetaInfo(key) && store_.get())
425 store_->ScheduleSave(); 429 store_->ScheduleSave();
426 430
427 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, 431 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
428 BookmarkMetaInfoChanged(this, node)); 432 BookmarkMetaInfoChanged(this, node));
429 } 433 }
430 434
431 void BookmarkModel::SetNodeSyncTransactionVersion( 435 void BookmarkModel::SetNodeSyncTransactionVersion(
432 const BookmarkNode* node, 436 const BookmarkNode* node,
433 int64 sync_transaction_version) { 437 int64 sync_transaction_version) {
438 DCHECK(client_->CanSyncNode(node));
439
434 if (sync_transaction_version == node->sync_transaction_version()) 440 if (sync_transaction_version == node->sync_transaction_version())
435 return; 441 return;
436 442
437 AsMutable(node)->set_sync_transaction_version(sync_transaction_version); 443 AsMutable(node)->set_sync_transaction_version(sync_transaction_version);
438 if (store_.get()) 444 if (store_.get())
439 store_->ScheduleSave(); 445 store_->ScheduleSave();
440 } 446 }
441 447
442 void BookmarkModel::OnFaviconChanged(const std::set<GURL>& urls) { 448 void BookmarkModel::OnFaviconChanged(const std::set<GURL>& urls) {
443 // Ignore events if |Load| has not been called yet. 449 // Ignore events if |Load| has not been called yet.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 { 613 {
608 // Only hold the lock for the duration of the insert. 614 // Only hold the lock for the duration of the insert.
609 base::AutoLock url_lock(url_lock_); 615 base::AutoLock url_lock(url_lock_);
610 nodes_ordered_by_url_set_.insert(new_node); 616 nodes_ordered_by_url_set_.insert(new_node);
611 } 617 }
612 618
613 return AddNode(AsMutable(parent), index, new_node); 619 return AddNode(AsMutable(parent), index, new_node);
614 } 620 }
615 621
616 void BookmarkModel::SortChildren(const BookmarkNode* parent) { 622 void BookmarkModel::SortChildren(const BookmarkNode* parent) {
623 DCHECK(client_->CanReorderChildren(parent));
624
617 if (!parent || !parent->is_folder() || is_root_node(parent) || 625 if (!parent || !parent->is_folder() || is_root_node(parent) ||
618 parent->child_count() <= 1) { 626 parent->child_count() <= 1) {
619 return; 627 return;
620 } 628 }
621 629
622 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, 630 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
623 OnWillReorderBookmarkNode(this, parent)); 631 OnWillReorderBookmarkNode(this, parent));
624 632
625 UErrorCode error = U_ZERO_ERROR; 633 UErrorCode error = U_ZERO_ERROR;
626 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error)); 634 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error));
627 if (U_FAILURE(error)) 635 if (U_FAILURE(error))
628 collator.reset(NULL); 636 collator.reset(NULL);
629 BookmarkNode* mutable_parent = AsMutable(parent); 637 BookmarkNode* mutable_parent = AsMutable(parent);
630 std::sort(mutable_parent->children().begin(), 638 std::sort(mutable_parent->children().begin(),
631 mutable_parent->children().end(), 639 mutable_parent->children().end(),
632 SortComparator(collator.get())); 640 SortComparator(collator.get()));
633 641
634 if (store_.get()) 642 if (store_.get())
635 store_->ScheduleSave(); 643 store_->ScheduleSave();
636 644
637 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, 645 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
638 BookmarkNodeChildrenReordered(this, parent)); 646 BookmarkNodeChildrenReordered(this, parent));
639 } 647 }
640 648
641 void BookmarkModel::ReorderChildren( 649 void BookmarkModel::ReorderChildren(
642 const BookmarkNode* parent, 650 const BookmarkNode* parent,
643 const std::vector<const BookmarkNode*>& ordered_nodes) { 651 const std::vector<const BookmarkNode*>& ordered_nodes) {
652 DCHECK(client_->CanReorderChildren(parent));
653
644 // Ensure that all children in |parent| are in |ordered_nodes|. 654 // Ensure that all children in |parent| are in |ordered_nodes|.
645 DCHECK_EQ(static_cast<size_t>(parent->child_count()), ordered_nodes.size()); 655 DCHECK_EQ(static_cast<size_t>(parent->child_count()), ordered_nodes.size());
646 for (size_t i = 0; i < ordered_nodes.size(); ++i) 656 for (size_t i = 0; i < ordered_nodes.size(); ++i)
647 DCHECK_EQ(parent, ordered_nodes[i]->parent()); 657 DCHECK_EQ(parent, ordered_nodes[i]->parent());
648 658
649 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, 659 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_,
650 OnWillReorderBookmarkNode(this, parent)); 660 OnWillReorderBookmarkNode(this, parent));
651 661
652 AsMutable(parent)->SetChildren( 662 AsMutable(parent)->SetChildren(
653 *(reinterpret_cast<const std::vector<BookmarkNode*>*>(&ordered_nodes))); 663 *(reinterpret_cast<const std::vector<BookmarkNode*>*>(&ordered_nodes)));
(...skipping 27 matching lines...) Expand all
681 691
682 index_->GetBookmarksMatching(text, max_count, matches); 692 index_->GetBookmarksMatching(text, max_count, matches);
683 } 693 }
684 694
685 void BookmarkModel::ClearStore() { 695 void BookmarkModel::ClearStore() {
686 store_ = NULL; 696 store_ = NULL;
687 } 697 }
688 698
689 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, 699 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type,
690 bool value) { 700 bool value) {
691 AsMutable(PermanentNode(type))->set_visible( 701 BookmarkPermanentNode* node = AsMutable(PermanentNode(type));
692 value || client_->IsPermanentNodeVisible(type)); 702 node->set_visible(value || client_->IsPermanentNodeVisible(node));
693 } 703 }
694 704
695 const BookmarkPermanentNode* BookmarkModel::PermanentNode( 705 const BookmarkPermanentNode* BookmarkModel::PermanentNode(
696 BookmarkNode::Type type) { 706 BookmarkNode::Type type) {
697 DCHECK(loaded_); 707 DCHECK(loaded_);
698 switch (type) { 708 switch (type) {
699 case BookmarkNode::BOOKMARK_BAR: 709 case BookmarkNode::BOOKMARK_BAR:
700 return bookmark_bar_node_; 710 return bookmark_bar_node_;
701 case BookmarkNode::OTHER_NODE: 711 case BookmarkNode::OTHER_NODE:
702 return other_node_; 712 return other_node_;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 // them unique. So when the file has changed externally, we should save the 761 // them unique. So when the file has changed externally, we should save the
752 // bookmarks file to persist new IDs. 762 // bookmarks file to persist new IDs.
753 if (store_.get()) 763 if (store_.get())
754 store_->ScheduleSave(); 764 store_->ScheduleSave();
755 } 765 }
756 bookmark_bar_node_ = details->release_bb_node(); 766 bookmark_bar_node_ = details->release_bb_node();
757 other_node_ = details->release_other_folder_node(); 767 other_node_ = details->release_other_folder_node();
758 mobile_node_ = details->release_mobile_folder_node(); 768 mobile_node_ = details->release_mobile_folder_node();
759 index_.reset(details->release_index()); 769 index_.reset(details->release_index());
760 770
771 // Get any extra nodes and take ownership of them at the |root_|.
772 std::vector<BookmarkPermanentNode*> extra_nodes;
773 details->release_extra_nodes(&extra_nodes);
774
761 // WARNING: order is important here, various places assume the order is 775 // WARNING: order is important here, various places assume the order is
762 // constant (but can vary between embedders with the initial visibility 776 // constant (but can vary between embedders with the initial visibility
763 // of permanent nodes). 777 // of permanent nodes).
764 BookmarkPermanentNode* root_children[] = { 778 std::vector<BookmarkPermanentNode*> root_children;
765 bookmark_bar_node_, other_node_, mobile_node_, 779 root_children.push_back(bookmark_bar_node_);
766 }; 780 root_children.push_back(other_node_);
767 std::stable_sort(root_children, 781 root_children.push_back(mobile_node_);
768 root_children + arraysize(root_children), 782 for (size_t i = 0; i < extra_nodes.size(); ++i)
783 root_children.push_back(extra_nodes[i]);
784 std::stable_sort(root_children.begin(),
785 root_children.end(),
769 VisibilityComparator(client_)); 786 VisibilityComparator(client_));
770 for (size_t i = 0; i < arraysize(root_children); ++i) { 787 for (size_t i = 0; i < root_children.size(); ++i)
771 root_.Add(root_children[i], static_cast<int>(i)); 788 root_.Add(root_children[i], static_cast<int>(i));
772 }
773 789
774 root_.SetMetaInfoMap(details->model_meta_info_map()); 790 root_.SetMetaInfoMap(details->model_meta_info_map());
775 root_.set_sync_transaction_version(details->model_sync_transaction_version()); 791 root_.set_sync_transaction_version(details->model_sync_transaction_version());
776 792
777 { 793 {
778 base::AutoLock url_lock(url_lock_); 794 base::AutoLock url_lock(url_lock_);
779 // Update nodes_ordered_by_url_set_ from the nodes. 795 // Update nodes_ordered_by_url_set_ from the nodes.
780 PopulateNodesByURL(&root_); 796 PopulateNodesByURL(&root_);
781 } 797 }
782 798
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 (allow_end && index == parent->child_count())))); 892 (allow_end && index == parent->child_count()))));
877 } 893 }
878 894
879 BookmarkPermanentNode* BookmarkModel::CreatePermanentNode( 895 BookmarkPermanentNode* BookmarkModel::CreatePermanentNode(
880 BookmarkNode::Type type) { 896 BookmarkNode::Type type) {
881 DCHECK(type == BookmarkNode::BOOKMARK_BAR || 897 DCHECK(type == BookmarkNode::BOOKMARK_BAR ||
882 type == BookmarkNode::OTHER_NODE || 898 type == BookmarkNode::OTHER_NODE ||
883 type == BookmarkNode::MOBILE); 899 type == BookmarkNode::MOBILE);
884 BookmarkPermanentNode* node = 900 BookmarkPermanentNode* node =
885 new BookmarkPermanentNode(generate_next_node_id()); 901 new BookmarkPermanentNode(generate_next_node_id());
886 node->set_visible(client_->IsPermanentNodeVisible(type)); 902 node->set_type(type);
903 node->set_visible(client_->IsPermanentNodeVisible(node));
887 904
888 int title_id; 905 int title_id;
889 switch (type) { 906 switch (type) {
890 case BookmarkNode::BOOKMARK_BAR: 907 case BookmarkNode::BOOKMARK_BAR:
891 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; 908 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME;
892 break; 909 break;
893 case BookmarkNode::OTHER_NODE: 910 case BookmarkNode::OTHER_NODE:
894 title_id = IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME; 911 title_id = IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME;
895 break; 912 break;
896 case BookmarkNode::MOBILE: 913 case BookmarkNode::MOBILE:
897 title_id = IDS_BOOKMARK_BAR_MOBILE_FOLDER_NAME; 914 title_id = IDS_BOOKMARK_BAR_MOBILE_FOLDER_NAME;
898 break; 915 break;
899 default: 916 default:
900 NOTREACHED(); 917 NOTREACHED();
901 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; 918 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME;
902 break; 919 break;
903 } 920 }
904 node->SetTitle(l10n_util::GetStringUTF16(title_id)); 921 node->SetTitle(l10n_util::GetStringUTF16(title_id));
905 node->set_type(type);
906 return node; 922 return node;
907 } 923 }
908 924
909 void BookmarkModel::OnFaviconDataAvailable( 925 void BookmarkModel::OnFaviconDataAvailable(
910 BookmarkNode* node, 926 BookmarkNode* node,
911 favicon_base::IconType icon_type, 927 favicon_base::IconType icon_type,
912 const favicon_base::FaviconImageResult& image_result) { 928 const favicon_base::FaviconImageResult& image_result) {
913 DCHECK(node); 929 DCHECK(node);
914 node->set_favicon_load_task_id(base::CancelableTaskTracker::kBadTaskId); 930 node->set_favicon_load_task_id(base::CancelableTaskTracker::kBadTaskId);
915 node->set_favicon_state(BookmarkNode::LOADED_FAVICON); 931 node->set_favicon_state(BookmarkNode::LOADED_FAVICON);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 BookmarkPermanentNode* bb_node = 993 BookmarkPermanentNode* bb_node =
978 CreatePermanentNode(BookmarkNode::BOOKMARK_BAR); 994 CreatePermanentNode(BookmarkNode::BOOKMARK_BAR);
979 BookmarkPermanentNode* other_node = 995 BookmarkPermanentNode* other_node =
980 CreatePermanentNode(BookmarkNode::OTHER_NODE); 996 CreatePermanentNode(BookmarkNode::OTHER_NODE);
981 BookmarkPermanentNode* mobile_node = 997 BookmarkPermanentNode* mobile_node =
982 CreatePermanentNode(BookmarkNode::MOBILE); 998 CreatePermanentNode(BookmarkNode::MOBILE);
983 return scoped_ptr<BookmarkLoadDetails>(new BookmarkLoadDetails( 999 return scoped_ptr<BookmarkLoadDetails>(new BookmarkLoadDetails(
984 bb_node, 1000 bb_node,
985 other_node, 1001 other_node,
986 mobile_node, 1002 mobile_node,
1003 client_->GetLoadExtraNodesCallback(),
987 new BookmarkIndex(client_, index_urls_, accept_languages), 1004 new BookmarkIndex(client_, index_urls_, accept_languages),
988 next_node_id_)); 1005 next_node_id_));
989 } 1006 }
OLDNEW
« no previous file with comments | « components/bookmarks/browser/bookmark_model.h ('k') | components/bookmarks/browser/bookmark_storage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698