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 "components/bookmarks/browser/bookmark_model.h"
9 using bookmarks::BookmarkModel
;
10 using bookmarks::BookmarkNode
;
12 namespace enhanced_bookmarks
{
14 std::vector
<const BookmarkNode
*> PrimaryPermanentNodes(BookmarkModel
* model
) {
15 DCHECK(model
->loaded());
16 std::vector
<const BookmarkNode
*> nodes
;
17 nodes
.push_back(model
->mobile_node());
18 nodes
.push_back(model
->bookmark_bar_node());
19 nodes
.push_back(model
->other_node());
23 std::vector
<const BookmarkNode
*> RootLevelFolders(BookmarkModel
* model
) {
24 std::vector
<const BookmarkNode
*> root_level_folders
;
26 // Find the direct folder children of the primary permanent nodes.
27 std::vector
<const BookmarkNode
*> primary_permanent_nodes
=
28 PrimaryPermanentNodes(model
);
29 for (const BookmarkNode
* parent
: primary_permanent_nodes
) {
30 int child_count
= parent
->child_count();
31 for (int i
= 0; i
< child_count
; ++i
) {
32 const BookmarkNode
* node
= parent
->GetChild(i
);
33 if (node
->is_folder() && node
->IsVisible())
34 root_level_folders
.push_back(node
);
37 return root_level_folders
;
40 bool IsPrimaryPermanentNode(const BookmarkNode
* node
, BookmarkModel
* model
) {
41 std::vector
<const BookmarkNode
*> primary_nodes(PrimaryPermanentNodes(model
));
42 if (std::find(primary_nodes
.begin(), primary_nodes
.end(), node
) !=
43 primary_nodes
.end()) {
49 const BookmarkNode
* RootLevelFolderForNode(const BookmarkNode
* node
,
50 BookmarkModel
* model
) {
51 // This helper function doesn't work for managed bookmarks. This checks that
52 // |node| is editable by the user, which currently covers all the other
53 // bookmarks except the managed bookmarks.
54 DCHECK(model
->client()->CanBeEditedByUser(node
));
56 const std::vector
<const BookmarkNode
*> root_folders(RootLevelFolders(model
));
57 const BookmarkNode
* top
= node
;
59 std::find(root_folders
.begin(), root_folders
.end(), top
) ==
66 } // namespace enhanced_bookmarks