OLD | NEW |
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/core/browser/bookmark_model.h" | 5 #include "components/bookmarks/core/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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/i18n/string_compare.h" | 12 #include "base/i18n/string_compare.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "components/bookmarks/core/browser/bookmark_expanded_state_tracker.h" | 16 #include "components/bookmarks/core/browser/bookmark_expanded_state_tracker.h" |
16 #include "components/bookmarks/core/browser/bookmark_index.h" | 17 #include "components/bookmarks/core/browser/bookmark_index.h" |
17 #include "components/bookmarks/core/browser/bookmark_match.h" | 18 #include "components/bookmarks/core/browser/bookmark_match.h" |
18 #include "components/bookmarks/core/browser/bookmark_model_observer.h" | 19 #include "components/bookmarks/core/browser/bookmark_model_observer.h" |
19 #include "components/bookmarks/core/browser/bookmark_node_data.h" | 20 #include "components/bookmarks/core/browser/bookmark_node_data.h" |
20 #include "components/bookmarks/core/browser/bookmark_storage.h" | 21 #include "components/bookmarks/core/browser/bookmark_storage.h" |
21 #include "components/bookmarks/core/browser/bookmark_utils.h" | 22 #include "components/bookmarks/core/browser/bookmark_utils.h" |
22 #include "components/favicon_base/favicon_types.h" | 23 #include "components/favicon_base/favicon_types.h" |
23 #include "grit/component_strings.h" | 24 #include "grit/component_strings.h" |
24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
25 #include "ui/gfx/favicon_size.h" | 26 #include "ui/gfx/favicon_size.h" |
26 | 27 |
27 using base::Time; | 28 using base::Time; |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 // Helper to get a mutable bookmark node. | 32 // Helper to get a mutable bookmark node. |
32 BookmarkNode* AsMutable(const BookmarkNode* node) { | 33 BookmarkNode* AsMutable(const BookmarkNode* node) { |
33 return const_cast<BookmarkNode*>(node); | 34 return const_cast<BookmarkNode*>(node); |
34 } | 35 } |
35 | 36 |
| 37 // Helper to get a mutable permanent bookmark node. |
| 38 BookmarkPermanentNode* AsMutable(const BookmarkPermanentNode* node) { |
| 39 return const_cast<BookmarkPermanentNode*>(node); |
| 40 } |
| 41 |
| 42 // Comparator used when sorting permanent nodes. Nodes that are initially |
| 43 // visible are sorted before nodes that are initially hidden. |
| 44 class VisibilityComparator |
| 45 : public std::binary_function<const BookmarkPermanentNode*, |
| 46 const BookmarkPermanentNode*, |
| 47 bool> { |
| 48 public: |
| 49 explicit VisibilityComparator(BookmarkClient* client) : client_(client) {} |
| 50 |
| 51 // Returns true if |n1| preceeds |n2|. |
| 52 bool operator()(const BookmarkPermanentNode* n1, |
| 53 const BookmarkPermanentNode* n2) { |
| 54 bool n1_visible = client_->IsPermanentNodeVisible(n1->type()); |
| 55 bool n2_visible = client_->IsPermanentNodeVisible(n2->type()); |
| 56 return n1_visible != n2_visible && n1_visible; |
| 57 } |
| 58 |
| 59 private: |
| 60 BookmarkClient* client_; |
| 61 }; |
| 62 |
36 // Comparator used when sorting bookmarks. Folders are sorted first, then | 63 // Comparator used when sorting bookmarks. Folders are sorted first, then |
37 // bookmarks. | 64 // bookmarks. |
38 class SortComparator : public std::binary_function<const BookmarkNode*, | 65 class SortComparator : public std::binary_function<const BookmarkNode*, |
39 const BookmarkNode*, | 66 const BookmarkNode*, |
40 bool> { | 67 bool> { |
41 public: | 68 public: |
42 explicit SortComparator(icu::Collator* collator) : collator_(collator) {} | 69 explicit SortComparator(icu::Collator* collator) : collator_(collator) {} |
43 | 70 |
44 // Returns true if |n1| preceeds |n2|. | 71 // Returns true if |n1| preceeds |n2|. |
45 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) { | 72 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) { |
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 | 678 |
652 index_->GetBookmarksMatching(text, max_count, matches); | 679 index_->GetBookmarksMatching(text, max_count, matches); |
653 } | 680 } |
654 | 681 |
655 void BookmarkModel::ClearStore() { | 682 void BookmarkModel::ClearStore() { |
656 store_ = NULL; | 683 store_ = NULL; |
657 } | 684 } |
658 | 685 |
659 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, | 686 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, |
660 bool value) { | 687 bool value) { |
| 688 AsMutable(PermanentNode(type))->set_visible( |
| 689 value || client_->IsPermanentNodeVisible(type)); |
| 690 } |
| 691 |
| 692 const BookmarkPermanentNode* BookmarkModel::PermanentNode( |
| 693 BookmarkNode::Type type) { |
661 DCHECK(loaded_); | 694 DCHECK(loaded_); |
662 switch (type) { | 695 switch (type) { |
663 case BookmarkNode::BOOKMARK_BAR: | 696 case BookmarkNode::BOOKMARK_BAR: |
664 bookmark_bar_node_->set_visible(value); | 697 return bookmark_bar_node_; |
665 break; | |
666 case BookmarkNode::OTHER_NODE: | 698 case BookmarkNode::OTHER_NODE: |
667 other_node_->set_visible(value); | 699 return other_node_; |
668 break; | |
669 case BookmarkNode::MOBILE: | 700 case BookmarkNode::MOBILE: |
670 mobile_node_->set_visible(value); | 701 return mobile_node_; |
671 break; | |
672 default: | 702 default: |
673 NOTREACHED(); | 703 NOTREACHED(); |
| 704 return NULL; |
674 } | 705 } |
675 } | 706 } |
676 | 707 |
677 bool BookmarkModel::IsBookmarkedNoLock(const GURL& url) { | 708 bool BookmarkModel::IsBookmarkedNoLock(const GURL& url) { |
678 BookmarkNode tmp_node(url); | 709 BookmarkNode tmp_node(url); |
679 return (nodes_ordered_by_url_set_.find(&tmp_node) != | 710 return (nodes_ordered_by_url_set_.find(&tmp_node) != |
680 nodes_ordered_by_url_set_.end()); | 711 nodes_ordered_by_url_set_.end()); |
681 } | 712 } |
682 | 713 |
683 void BookmarkModel::RemoveNode(BookmarkNode* node, | 714 void BookmarkModel::RemoveNode(BookmarkNode* node, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 // bookmarks file to persist new IDs. | 749 // bookmarks file to persist new IDs. |
719 if (store_.get()) | 750 if (store_.get()) |
720 store_->ScheduleSave(); | 751 store_->ScheduleSave(); |
721 } | 752 } |
722 bookmark_bar_node_ = details->release_bb_node(); | 753 bookmark_bar_node_ = details->release_bb_node(); |
723 other_node_ = details->release_other_folder_node(); | 754 other_node_ = details->release_other_folder_node(); |
724 mobile_node_ = details->release_mobile_folder_node(); | 755 mobile_node_ = details->release_mobile_folder_node(); |
725 index_.reset(details->release_index()); | 756 index_.reset(details->release_index()); |
726 | 757 |
727 // WARNING: order is important here, various places assume the order is | 758 // WARNING: order is important here, various places assume the order is |
728 // constant. | 759 // constant (but can vary between embedders with the initial visibility |
729 root_.Add(bookmark_bar_node_, 0); | 760 // of permanent nodes). |
730 root_.Add(other_node_, 1); | 761 BookmarkPermanentNode* root_children[] = { |
731 root_.Add(mobile_node_, 2); | 762 bookmark_bar_node_, other_node_, mobile_node_, |
| 763 }; |
| 764 std::stable_sort(root_children, |
| 765 root_children + arraysize(root_children), |
| 766 VisibilityComparator(client_)); |
| 767 for (size_t i = 0; i < arraysize(root_children); ++i) { |
| 768 root_.Add(root_children[i], static_cast<int>(i)); |
| 769 } |
732 | 770 |
733 root_.SetMetaInfoMap(details->model_meta_info_map()); | 771 root_.SetMetaInfoMap(details->model_meta_info_map()); |
734 root_.set_sync_transaction_version(details->model_sync_transaction_version()); | 772 root_.set_sync_transaction_version(details->model_sync_transaction_version()); |
735 | 773 |
736 { | 774 { |
737 base::AutoLock url_lock(url_lock_); | 775 base::AutoLock url_lock(url_lock_); |
738 // Update nodes_ordered_by_url_set_ from the nodes. | 776 // Update nodes_ordered_by_url_set_ from the nodes. |
739 PopulateNodesByURL(&root_); | 777 PopulateNodesByURL(&root_); |
740 } | 778 } |
741 | 779 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 (allow_end && index == parent->child_count())))); | 873 (allow_end && index == parent->child_count())))); |
836 } | 874 } |
837 | 875 |
838 BookmarkPermanentNode* BookmarkModel::CreatePermanentNode( | 876 BookmarkPermanentNode* BookmarkModel::CreatePermanentNode( |
839 BookmarkNode::Type type) { | 877 BookmarkNode::Type type) { |
840 DCHECK(type == BookmarkNode::BOOKMARK_BAR || | 878 DCHECK(type == BookmarkNode::BOOKMARK_BAR || |
841 type == BookmarkNode::OTHER_NODE || | 879 type == BookmarkNode::OTHER_NODE || |
842 type == BookmarkNode::MOBILE); | 880 type == BookmarkNode::MOBILE); |
843 BookmarkPermanentNode* node = | 881 BookmarkPermanentNode* node = |
844 new BookmarkPermanentNode(generate_next_node_id()); | 882 new BookmarkPermanentNode(generate_next_node_id()); |
845 if (type == BookmarkNode::MOBILE) | 883 node->set_visible(client_->IsPermanentNodeVisible(type)); |
846 node->set_visible(false); // Mobile node is initially hidden. | |
847 | 884 |
848 int title_id; | 885 int title_id; |
849 switch (type) { | 886 switch (type) { |
850 case BookmarkNode::BOOKMARK_BAR: | 887 case BookmarkNode::BOOKMARK_BAR: |
851 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; | 888 title_id = IDS_BOOKMARK_BAR_FOLDER_NAME; |
852 break; | 889 break; |
853 case BookmarkNode::OTHER_NODE: | 890 case BookmarkNode::OTHER_NODE: |
854 title_id = IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME; | 891 title_id = IDS_BOOKMARK_BAR_OTHER_FOLDER_NAME; |
855 break; | 892 break; |
856 case BookmarkNode::MOBILE: | 893 case BookmarkNode::MOBILE: |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 CreatePermanentNode(BookmarkNode::OTHER_NODE); | 977 CreatePermanentNode(BookmarkNode::OTHER_NODE); |
941 BookmarkPermanentNode* mobile_node = | 978 BookmarkPermanentNode* mobile_node = |
942 CreatePermanentNode(BookmarkNode::MOBILE); | 979 CreatePermanentNode(BookmarkNode::MOBILE); |
943 return scoped_ptr<BookmarkLoadDetails>(new BookmarkLoadDetails( | 980 return scoped_ptr<BookmarkLoadDetails>(new BookmarkLoadDetails( |
944 bb_node, | 981 bb_node, |
945 other_node, | 982 other_node, |
946 mobile_node, | 983 mobile_node, |
947 new BookmarkIndex(client_, index_urls_, accept_languages), | 984 new BookmarkIndex(client_, index_urls_, accept_languages), |
948 next_node_id_)); | 985 next_node_id_)); |
949 } | 986 } |
OLD | NEW |