Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / components / enhanced_bookmarks / enhanced_bookmark_utils.cc
blob28c5bc8de1dd5e8fa16d2104f12e4dd3d956a8ea
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
3 // found in the LICENSE file.
5 #include "components/enhanced_bookmarks/enhanced_bookmark_utils.h"
7 #include "base/i18n/string_compare.h"
8 #include "components/bookmarks/browser/bookmark_model.h"
10 using bookmarks::BookmarkModel;
11 using bookmarks::BookmarkNode;
13 namespace enhanced_bookmarks {
15 // Comparator used to sort bookmarks. No folders are allowed.
16 class BookmarkNameComparator : public std::binary_function<const BookmarkNode*,
17 const BookmarkNode*,
18 bool> {
19 public:
20 explicit BookmarkNameComparator(icu::Collator* collator)
21 : collator_(collator) {}
23 // Returns true if |n1| preceeds |n2|.
24 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) {
25 DCHECK(!n1->is_folder());
26 DCHECK(!n2->is_folder());
27 if (!collator_)
28 return n1->GetTitle() < n2->GetTitle();
29 return base::i18n::CompareString16WithCollator(
30 collator_, n1->GetTitle(), n2->GetTitle()) == UCOL_LESS;
33 private:
34 icu::Collator* collator_;
37 void SortBookmarksByName(std::vector<const BookmarkNode*>& nodes) {
38 UErrorCode error = U_ZERO_ERROR;
39 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error));
40 if (U_FAILURE(error))
41 collator.reset(NULL);
42 std::sort(nodes.begin(), nodes.end(), BookmarkNameComparator(collator.get()));
45 std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model) {
46 DCHECK(model->loaded());
47 std::vector<const BookmarkNode*> nodes;
48 nodes.push_back(model->mobile_node());
49 nodes.push_back(model->bookmark_bar_node());
50 nodes.push_back(model->other_node());
51 return nodes;
54 std::vector<const BookmarkNode*> RootLevelFolders(BookmarkModel* model) {
55 std::vector<const BookmarkNode*> root_level_folders;
57 // Find the direct folder children of the primary permanent nodes.
58 std::vector<const BookmarkNode*> primary_permanent_nodes =
59 PrimaryPermanentNodes(model);
60 for (const BookmarkNode* parent : primary_permanent_nodes) {
61 int child_count = parent->child_count();
62 for (int i = 0; i < child_count; ++i) {
63 const BookmarkNode* node = parent->GetChild(i);
64 if (node->is_folder() && node->IsVisible())
65 root_level_folders.push_back(node);
68 return root_level_folders;
71 bool IsPrimaryPermanentNode(const BookmarkNode* node, BookmarkModel* model) {
72 std::vector<const BookmarkNode*> primary_nodes(PrimaryPermanentNodes(model));
73 if (std::find(primary_nodes.begin(), primary_nodes.end(), node) !=
74 primary_nodes.end()) {
75 return true;
77 return false;
80 const BookmarkNode* RootLevelFolderForNode(const BookmarkNode* node,
81 BookmarkModel* model) {
82 // This helper function doesn't work for managed bookmarks. This checks that
83 // |node| is editable by the user, which currently covers all the other
84 // bookmarks except the managed bookmarks.
85 DCHECK(model->client()->CanBeEditedByUser(node));
87 const std::vector<const BookmarkNode*> root_folders(RootLevelFolders(model));
88 const BookmarkNode* top = node;
89 while (top &&
90 std::find(root_folders.begin(), root_folders.end(), top) ==
91 root_folders.end()) {
92 top = top->parent();
94 return top;
97 } // namespace enhanced_bookmarks