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
*,
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());
28 return n1
->GetTitle() < n2
->GetTitle();
29 return base::i18n::CompareString16WithCollator(
30 collator_
, n1
->GetTitle(), n2
->GetTitle()) == UCOL_LESS
;
34 icu::Collator
* collator_
;
37 std::vector
<const BookmarkNode
*> PrimaryPermanentNodes(BookmarkModel
* model
) {
38 DCHECK(model
->loaded());
39 std::vector
<const BookmarkNode
*> nodes
;
40 nodes
.push_back(model
->mobile_node());
41 nodes
.push_back(model
->bookmark_bar_node());
42 nodes
.push_back(model
->other_node());
46 std::vector
<const BookmarkNode
*> RootLevelFolders(BookmarkModel
* model
) {
47 std::vector
<const BookmarkNode
*> root_level_folders
;
49 // Find the direct folder children of the primary permanent nodes.
50 std::vector
<const BookmarkNode
*> primary_permanent_nodes
=
51 PrimaryPermanentNodes(model
);
52 for (const BookmarkNode
* parent
: primary_permanent_nodes
) {
53 int child_count
= parent
->child_count();
54 for (int i
= 0; i
< child_count
; ++i
) {
55 const BookmarkNode
* node
= parent
->GetChild(i
);
56 if (node
->is_folder() && node
->IsVisible())
57 root_level_folders
.push_back(node
);
60 return root_level_folders
;
63 bool IsPrimaryPermanentNode(const BookmarkNode
* node
, BookmarkModel
* model
) {
64 std::vector
<const BookmarkNode
*> primary_nodes(PrimaryPermanentNodes(model
));
65 if (std::find(primary_nodes
.begin(), primary_nodes
.end(), node
) !=
66 primary_nodes
.end()) {
72 const BookmarkNode
* RootLevelFolderForNode(const BookmarkNode
* node
,
73 BookmarkModel
* model
) {
74 // This helper function doesn't work for managed bookmarks. This checks that
75 // |node| is editable by the user, which currently covers all the other
76 // bookmarks except the managed bookmarks.
77 DCHECK(model
->client()->CanBeEditedByUser(node
));
79 const std::vector
<const BookmarkNode
*> root_folders(RootLevelFolders(model
));
80 const BookmarkNode
* top
= node
;
82 std::find(root_folders
.begin(), root_folders
.end(), top
) ==
89 } // namespace enhanced_bookmarks