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