1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_bookmark_helpers.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
namespace keys = extension_bookmarks_module_constants;
// Helper functions.
namespace extension_bookmark_helpers {
DictionaryValue* GetNodeDictionary(const BookmarkNode* node,
bool recurse,
bool only_folders) {
DictionaryValue* dict = new DictionaryValue();
dict->SetString(keys::kIdKey, base::Int64ToString(node->id()));
const BookmarkNode* parent = node->GetParent();
if (parent) {
dict->SetString(keys::kParentIdKey, base::Int64ToString(parent->id()));
dict->SetInteger(keys::kIndexKey, parent->IndexOfChild(node));
}
if (!node->is_folder()) {
dict->SetString(keys::kUrlKey, node->GetURL().spec());
} else {
// Javascript Date wants milliseconds since the epoch, ToDoubleT is
// seconds.
base::Time t = node->date_group_modified();
if (!t.is_null())
dict->SetReal(keys::kDateGroupModifiedKey, floor(t.ToDoubleT() * 1000));
}
dict->SetString(keys::kTitleKey, node->GetTitle());
if (!node->date_added().is_null()) {
// Javascript Date wants milliseconds since the epoch, ToDoubleT is
// seconds.
dict->SetReal(keys::kDateAddedKey,
floor(node->date_added().ToDoubleT() * 1000));
}
if (recurse && node->is_folder()) {
int childCount = node->GetChildCount();
ListValue* children = new ListValue();
for (int i = 0; i < childCount; ++i) {
const BookmarkNode* child = node->GetChild(i);
if (!only_folders || child->is_folder()) {
DictionaryValue* dict = GetNodeDictionary(child, true, only_folders);
children->Append(dict);
}
}
dict->Set(keys::kChildrenKey, children);
}
return dict;
}
void AddNode(const BookmarkNode* node,
ListValue* list,
bool recurse,
bool only_folders) {
DictionaryValue* dict = GetNodeDictionary(node, recurse, only_folders);
list->Append(dict);
}
// Add a JSON representation of |node| to the JSON |list|.
void AddNode(const BookmarkNode* node,
ListValue* list,
bool recurse) {
return AddNode(node, list, recurse, false);
}
void AddNodeFoldersOnly(const BookmarkNode* node,
ListValue* list,
bool recurse) {
return AddNode(node, list, recurse, true);
}
bool RemoveNode(BookmarkModel* model,
int64 id,
bool recursive,
std::string* error) {
const BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
*error = keys::kNoNodeError;
return false;
}
if (node == model->root_node() ||
node == model->other_node() ||
node == model->GetBookmarkBarNode()) {
*error = keys::kModifySpecialError;
return false;
}
if (node->is_folder() && node->GetChildCount() > 0 && !recursive) {
*error = keys::kFolderNotEmptyError;
return false;
}
const BookmarkNode* parent = node->GetParent();
int index = parent->IndexOfChild(node);
model->Remove(parent, index);
return true;
}
}
|