[Autofill] remove *MultiInfo* functions
[chromium-blink-merge.git] / components / enhanced_bookmarks / enhanced_bookmark_utils.cc
blob7108d98cf7e5874e6130a7d1c51f3c996b189421
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"
8 #include "components/variations/variations_associated_data.h"
10 using bookmarks::BookmarkModel;
11 using bookmarks::BookmarkNode;
13 namespace enhanced_bookmarks {
15 const char kFieldTrialName[] = "EnhancedBookmarks";
17 std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model) {
18 DCHECK(model->loaded());
19 std::vector<const BookmarkNode*> nodes;
20 nodes.push_back(model->mobile_node());
21 nodes.push_back(model->bookmark_bar_node());
22 nodes.push_back(model->other_node());
23 return nodes;
26 std::vector<const BookmarkNode*> RootLevelFolders(BookmarkModel* model) {
27 std::vector<const BookmarkNode*> root_level_folders;
29 // Find the direct folder children of the primary permanent nodes.
30 std::vector<const BookmarkNode*> primary_permanent_nodes =
31 PrimaryPermanentNodes(model);
32 for (const BookmarkNode* parent : primary_permanent_nodes) {
33 int child_count = parent->child_count();
34 for (int i = 0; i < child_count; ++i) {
35 const BookmarkNode* node = parent->GetChild(i);
36 if (node->is_folder() && node->IsVisible())
37 root_level_folders.push_back(node);
40 return root_level_folders;
43 bool IsPrimaryPermanentNode(const BookmarkNode* node, BookmarkModel* model) {
44 std::vector<const BookmarkNode*> primary_nodes(PrimaryPermanentNodes(model));
45 if (std::find(primary_nodes.begin(), primary_nodes.end(), node) !=
46 primary_nodes.end()) {
47 return true;
49 return false;
52 const BookmarkNode* RootLevelFolderForNode(const BookmarkNode* node,
53 BookmarkModel* model) {
54 // This helper function doesn't work for managed bookmarks. This checks that
55 // |node| is editable by the user, which currently covers all the other
56 // bookmarks except the managed bookmarks.
57 DCHECK(model->client()->CanBeEditedByUser(node));
59 const std::vector<const BookmarkNode*> root_folders(RootLevelFolders(model));
60 const BookmarkNode* top = node;
61 while (top &&
62 std::find(root_folders.begin(), root_folders.end(), top) ==
63 root_folders.end()) {
64 top = top->parent();
66 return top;
69 ViewMode GetDefaultViewMode() {
70 std::string default_view_mode = variations::GetVariationParamValue(
71 enhanced_bookmarks::kFieldTrialName, "DefaultViewMode");
73 if (default_view_mode == "List") {
74 return ViewMode::LIST;
75 } else if (default_view_mode == "Grid") {
76 return ViewMode::GRID;
79 // If finch data is not available or has an invalid value, we fall back to
80 // ViewMode::GRID.
81 return ViewMode::GRID;
84 } // namespace enhanced_bookmarks