Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / bookmarks / bookmark_api_helpers.cc
blob0708c15f7690d872efc5b04333f9899121e468a6
1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h"
7 #include <math.h> // For floor()
8 #include <vector>
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h"
13 #include "chrome/common/extensions/api/bookmarks.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/browser/bookmark_utils.h"
16 #include "components/bookmarks/managed/managed_bookmark_service.h"
18 using bookmarks::BookmarkModel;
19 using bookmarks::BookmarkNode;
21 namespace extensions {
23 namespace keys = bookmark_api_constants;
24 using api::bookmarks::BookmarkTreeNode;
26 namespace bookmark_api_helpers {
28 namespace {
30 void AddNodeHelper(bookmarks::ManagedBookmarkService* managed,
31 const BookmarkNode* node,
32 std::vector<linked_ptr<BookmarkTreeNode>>* nodes,
33 bool recurse,
34 bool only_folders) {
35 if (node->IsVisible()) {
36 linked_ptr<BookmarkTreeNode> new_node(
37 GetBookmarkTreeNode(managed, node, recurse, only_folders));
38 nodes->push_back(new_node);
42 } // namespace
44 BookmarkTreeNode* GetBookmarkTreeNode(
45 bookmarks::ManagedBookmarkService* managed,
46 const BookmarkNode* node,
47 bool recurse,
48 bool only_folders) {
49 BookmarkTreeNode* bookmark_tree_node = new BookmarkTreeNode;
51 bookmark_tree_node->id = base::Int64ToString(node->id());
53 const BookmarkNode* parent = node->parent();
54 if (parent) {
55 bookmark_tree_node->parent_id.reset(new std::string(
56 base::Int64ToString(parent->id())));
57 bookmark_tree_node->index.reset(new int(parent->GetIndexOf(node)));
60 if (!node->is_folder()) {
61 bookmark_tree_node->url.reset(new std::string(node->url().spec()));
62 } else {
63 // Javascript Date wants milliseconds since the epoch, ToDoubleT is seconds.
64 base::Time t = node->date_folder_modified();
65 if (!t.is_null()) {
66 bookmark_tree_node->date_group_modified.reset(
67 new double(floor(t.ToDoubleT() * 1000)));
71 bookmark_tree_node->title = base::UTF16ToUTF8(node->GetTitle());
72 if (!node->date_added().is_null()) {
73 // Javascript Date wants milliseconds since the epoch, ToDoubleT is seconds.
74 bookmark_tree_node->date_added.reset(
75 new double(floor(node->date_added().ToDoubleT() * 1000)));
78 if (bookmarks::IsDescendantOf(node, managed->managed_node()) ||
79 bookmarks::IsDescendantOf(node, managed->supervised_node())) {
80 bookmark_tree_node->unmodifiable =
81 api::bookmarks::BOOKMARK_TREE_NODE_UNMODIFIABLE_MANAGED;
84 if (recurse && node->is_folder()) {
85 std::vector<linked_ptr<BookmarkTreeNode>> children;
86 for (int i = 0; i < node->child_count(); ++i) {
87 const BookmarkNode* child = node->GetChild(i);
88 if (child->IsVisible() && (!only_folders || child->is_folder())) {
89 linked_ptr<BookmarkTreeNode> child_node(
90 GetBookmarkTreeNode(managed, child, true, only_folders));
91 children.push_back(child_node);
94 bookmark_tree_node->children.reset(
95 new std::vector<linked_ptr<BookmarkTreeNode>>(children));
97 return bookmark_tree_node;
100 void AddNode(bookmarks::ManagedBookmarkService* managed,
101 const BookmarkNode* node,
102 std::vector<linked_ptr<BookmarkTreeNode>>* nodes,
103 bool recurse) {
104 return AddNodeHelper(managed, node, nodes, recurse, false);
107 void AddNodeFoldersOnly(bookmarks::ManagedBookmarkService* managed,
108 const BookmarkNode* node,
109 std::vector<linked_ptr<BookmarkTreeNode>>* nodes,
110 bool recurse) {
111 return AddNodeHelper(managed, node, nodes, recurse, true);
114 bool RemoveNode(BookmarkModel* model,
115 bookmarks::ManagedBookmarkService* managed,
116 int64 id,
117 bool recursive,
118 std::string* error) {
119 const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, id);
120 if (!node) {
121 *error = keys::kNoNodeError;
122 return false;
124 if (model->is_permanent_node(node)) {
125 *error = keys::kModifySpecialError;
126 return false;
128 if (bookmarks::IsDescendantOf(node, managed->managed_node()) ||
129 bookmarks::IsDescendantOf(node, managed->supervised_node())) {
130 *error = keys::kModifyManagedError;
131 return false;
133 if (node->is_folder() && !node->empty() && !recursive) {
134 *error = keys::kFolderNotEmptyError;
135 return false;
138 model->Remove(node);
139 return true;
142 void GetMetaInfo(const BookmarkNode& node,
143 base::DictionaryValue* id_to_meta_info_map) {
144 if (!node.IsVisible())
145 return;
147 const BookmarkNode::MetaInfoMap* meta_info = node.GetMetaInfoMap();
148 base::DictionaryValue* value = new base::DictionaryValue();
149 if (meta_info) {
150 BookmarkNode::MetaInfoMap::const_iterator itr;
151 for (itr = meta_info->begin(); itr != meta_info->end(); ++itr) {
152 value->SetStringWithoutPathExpansion(itr->first, itr->second);
155 id_to_meta_info_map->Set(base::Int64ToString(node.id()), value);
157 if (node.is_folder()) {
158 for (int i = 0; i < node.child_count(); ++i) {
159 GetMetaInfo(*(node.GetChild(i)), id_to_meta_info_map);
164 } // namespace bookmark_api_helpers
165 } // namespace extensions