summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authormitchellwrosen@chromium.org <mitchellwrosen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-31 23:01:13 +0000
committermitchellwrosen@chromium.org <mitchellwrosen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-31 23:01:13 +0000
commit7e2d34d9f816b125c24f1078190269ac765fedcd (patch)
tree897a004e59952ae87190dfbd0537919dbcb0784b /chrome
parenteb7ef82890488e0bcaabb940721fac337a2e0a93 (diff)
downloadchromium_src-7e2d34d9f816b125c24f1078190269ac765fedcd.zip
chromium_src-7e2d34d9f816b125c24f1078190269ac765fedcd.tar.gz
chromium_src-7e2d34d9f816b125c24f1078190269ac765fedcd.tar.bz2
Refactor chrome.bookmarks API to use JSON schema compiler.
BUG=121174 Review URL: https://chromiumcodereview.appspot.com/10694062 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149307 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/bookmarks/bookmark_extension_api.cc257
-rw-r--r--chrome/browser/bookmarks/bookmark_extension_helpers.cc80
-rw-r--r--chrome/browser/bookmarks/bookmark_extension_helpers.h37
-rw-r--r--chrome/browser/bookmarks/bookmark_extension_helpers_unittest.cc53
-rw-r--r--chrome/common/extensions/api/api.gyp1
-rw-r--r--chrome/common/extensions/api/bookmarks.json256
-rw-r--r--chrome/common/extensions/docs/apps/bookmarks.html1
-rw-r--r--chrome/common/extensions/docs/extensions/bookmarks.html1
8 files changed, 480 insertions, 206 deletions
diff --git a/chrome/browser/bookmarks/bookmark_extension_api.cc b/chrome/browser/bookmarks/bookmark_extension_api.cc
index 9476c99..1ec184e 100644
--- a/chrome/browser/bookmarks/bookmark_extension_api.cc
+++ b/chrome/browser/bookmarks/bookmark_extension_api.cc
@@ -34,14 +34,17 @@
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
+#include "chrome/common/extensions/api/bookmarks.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace keys = bookmark_extension_api_constants;
+namespace bookmarks = extensions::api::bookmarks;
using base::TimeDelta;
+using bookmarks::BookmarkTreeNode;
using content::BrowserThread;
using content::WebContents;
@@ -192,9 +195,9 @@ void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model,
ListValue args;
const BookmarkNode* node = parent->GetChild(index);
args.Append(new StringValue(base::Int64ToString(node->id())));
- DictionaryValue* obj =
- bookmark_extension_helpers::GetNodeDictionary(node, false, false);
- args.Append(obj);
+ BookmarkTreeNode* tree_node =
+ bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false);
+ args.Append(tree_node->ToValue().release());
std::string json_args;
base::JSONWriter::Write(&args, &json_args);
@@ -288,55 +291,56 @@ void BookmarkExtensionEventRouter::ExtensiveBookmarkChangesEnded(
}
bool GetBookmarksFunction::RunImpl() {
+ scoped_ptr<bookmarks::Get::Params> params(
+ bookmarks::Get::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ std::vector<linked_ptr<BookmarkTreeNode> > nodes;
BookmarkModel* model = profile()->GetBookmarkModel();
- scoped_ptr<ListValue> json(new ListValue());
- Value* arg0;
- EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0));
- if (arg0->IsType(Value::TYPE_LIST)) {
- const ListValue* ids = static_cast<const ListValue*>(arg0);
- size_t count = ids->GetSize();
+ if (params->id_or_id_list_type ==
+ bookmarks::Get::Params::ID_OR_ID_LIST_ARRAY) {
+ std::vector<std::string>* ids = params->id_or_id_list_array.get();
+ size_t count = ids->size();
EXTENSION_FUNCTION_VALIDATE(count > 0);
for (size_t i = 0; i < count; ++i) {
int64 id;
- std::string id_string;
- EXTENSION_FUNCTION_VALIDATE(ids->GetString(i, &id_string));
- if (!GetBookmarkIdAsInt64(id_string, &id))
+ if (!GetBookmarkIdAsInt64(ids->at(i), &id))
return false;
const BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
} else {
- bookmark_extension_helpers::AddNode(node, json.get(), false);
+ bookmark_extension_helpers::AddNode(node, &nodes, false);
}
}
} else {
int64 id;
- std::string id_string;
- EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string));
- if (!GetBookmarkIdAsInt64(id_string, &id))
+ if (!GetBookmarkIdAsInt64(*params->id_or_id_list_string, &id))
return false;
const BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
- bookmark_extension_helpers::AddNode(node, json.get(), false);
+ bookmark_extension_helpers::AddNode(node, &nodes, false);
}
- SetResult(json.release());
+ results_ = bookmarks::Get::Results::Create(nodes);
return true;
}
bool GetBookmarkChildrenFunction::RunImpl() {
- BookmarkModel* model = profile()->GetBookmarkModel();
+ scoped_ptr<bookmarks::GetChildren::Params> params(
+ bookmarks::GetChildren::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
int64 id;
- std::string id_string;
- EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string));
- if (!GetBookmarkIdAsInt64(id_string, &id))
+ if (!GetBookmarkIdAsInt64(params->id, &id))
return false;
- scoped_ptr<ListValue> json(new ListValue());
- const BookmarkNode* node = model->GetNodeByID(id);
+
+ std::vector<linked_ptr<BookmarkTreeNode> > nodes;
+ const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
@@ -344,79 +348,85 @@ bool GetBookmarkChildrenFunction::RunImpl() {
int child_count = node->child_count();
for (int i = 0; i < child_count; ++i) {
const BookmarkNode* child = node->GetChild(i);
- bookmark_extension_helpers::AddNode(child, json.get(), false);
+ bookmark_extension_helpers::AddNode(child, &nodes, false);
}
- SetResult(json.release());
+ results_ = bookmarks::GetChildren::Results::Create(nodes);
return true;
}
bool GetBookmarkRecentFunction::RunImpl() {
- int number_of_items;
- EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items));
- if (number_of_items < 1)
+ scoped_ptr<bookmarks::GetRecent::Params> params(
+ bookmarks::GetRecent::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+ if (params->number_of_items < 1)
return false;
- BookmarkModel* model = profile()->GetBookmarkModel();
- ListValue* json = new ListValue();
std::vector<const BookmarkNode*> nodes;
- bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes);
+ bookmark_utils::GetMostRecentlyAddedEntries(profile()->GetBookmarkModel(),
+ params->number_of_items,
+ &nodes);
+
+ std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes;
std::vector<const BookmarkNode*>::iterator i = nodes.begin();
for (; i != nodes.end(); ++i) {
const BookmarkNode* node = *i;
- bookmark_extension_helpers::AddNode(node, json, false);
+ bookmark_extension_helpers::AddNode(node, &tree_nodes, false);
}
- SetResult(json);
+
+ results_ = bookmarks::GetRecent::Results::Create(tree_nodes);
return true;
}
bool GetBookmarkTreeFunction::RunImpl() {
- BookmarkModel* model = profile()->GetBookmarkModel();
- scoped_ptr<ListValue> json(new ListValue());
- const BookmarkNode* node = model->root_node();
- bookmark_extension_helpers::AddNode(node, json.get(), true);
- SetResult(json.release());
+ std::vector<linked_ptr<BookmarkTreeNode> > nodes;
+ const BookmarkNode* node = profile()->GetBookmarkModel()->root_node();
+ bookmark_extension_helpers::AddNode(node, &nodes, true);
+ results_ = bookmarks::GetTree::Results::Create(nodes);
return true;
}
bool GetBookmarkSubTreeFunction::RunImpl() {
- BookmarkModel* model = profile()->GetBookmarkModel();
- scoped_ptr<ListValue> json(new ListValue());
- Value* arg0;
- EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0));
+ scoped_ptr<bookmarks::GetSubTree::Params> params(
+ bookmarks::GetSubTree::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
int64 id;
- std::string id_string;
- EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string));
- if (!GetBookmarkIdAsInt64(id_string, &id))
+ if (!GetBookmarkIdAsInt64(params->id, &id))
return false;
- const BookmarkNode* node = model->GetNodeByID(id);
+
+ const BookmarkNode* node = profile()->GetBookmarkModel()->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
- bookmark_extension_helpers::AddNode(node, json.get(), true);
- SetResult(json.release());
+
+ std::vector<linked_ptr<BookmarkTreeNode> > nodes;
+ bookmark_extension_helpers::AddNode(node, &nodes, true);
+ results_ = bookmarks::GetSubTree::Results::Create(nodes);
return true;
}
bool SearchBookmarksFunction::RunImpl() {
- string16 query;
- EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query));
+ scoped_ptr<bookmarks::Search::Params> params(
+ bookmarks::Search::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
- BookmarkModel* model = profile()->GetBookmarkModel();
- ListValue* json = new ListValue();
std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
std::vector<const BookmarkNode*> nodes;
- bookmark_utils::GetBookmarksContainingText(model, query,
+ bookmark_utils::GetBookmarksContainingText(profile()->GetBookmarkModel(),
+ UTF8ToUTF16(params->query),
std::numeric_limits<int>::max(),
- lang, &nodes);
- std::vector<const BookmarkNode*>::iterator i = nodes.begin();
- for (; i != nodes.end(); ++i) {
- const BookmarkNode* node = *i;
- bookmark_extension_helpers::AddNode(node, json, false);
+ lang,
+ &nodes);
+
+ std::vector<linked_ptr<BookmarkTreeNode> > tree_nodes;
+ for (std::vector<const BookmarkNode*>::iterator node_iter = nodes.begin();
+ node_iter != nodes.end(); ++node_iter) {
+ bookmark_extension_helpers::AddNode(*node_iter, &tree_nodes, false);
}
- SetResult(json);
+ results_ = bookmarks::Search::Results::Create(tree_nodes);
return true;
}
@@ -438,44 +448,44 @@ bool RemoveBookmarkFunction::ExtractIds(const ListValue* args,
bool RemoveBookmarkFunction::RunImpl() {
if (!EditBookmarksEnabled())
return false;
- std::list<int64> ids;
- bool invalid_id = false;
- EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id));
- if (invalid_id) {
+
+ scoped_ptr<bookmarks::Remove::Params> params(
+ bookmarks::Remove::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ int64 id;
+ if (!base::StringToInt64(params->id, &id)) {
error_ = keys::kInvalidIdError;
return false;
}
+
bool recursive = false;
if (name() == RemoveTreeBookmarkFunction::function_name())
recursive = true;
BookmarkModel* model = profile()->GetBookmarkModel();
- size_t count = ids.size();
- EXTENSION_FUNCTION_VALIDATE(count > 0);
- for (std::list<int64>::iterator it = ids.begin(); it != ids.end(); ++it) {
- if (!bookmark_extension_helpers::RemoveNode(model, *it, recursive, &error_))
- return false;
- }
+ if (!bookmark_extension_helpers::RemoveNode(model, id, recursive, &error_))
+ return false;
+
return true;
}
bool CreateBookmarkFunction::RunImpl() {
if (!EditBookmarksEnabled())
return false;
- DictionaryValue* json;
- EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json));
- EXTENSION_FUNCTION_VALIDATE(json != NULL);
+
+ scoped_ptr<bookmarks::Create::Params> params(
+ bookmarks::Create::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
BookmarkModel* model = profile()->GetBookmarkModel();
int64 parentId;
- if (!json->HasKey(keys::kParentIdKey)) {
+
+ if (!params->bookmark.parent_id.get()) {
// Optional, default to "other bookmarks".
parentId = model->other_node()->id();
} else {
- std::string parentId_string;
- EXTENSION_FUNCTION_VALIDATE(json->GetString(keys::kParentIdKey,
- &parentId_string));
- if (!GetBookmarkIdAsInt64(parentId_string, &parentId))
+ if (!GetBookmarkIdAsInt64(*params->bookmark.parent_id, &parentId))
return false;
}
const BookmarkNode* parent = model->GetNodeByID(parentId);
@@ -489,20 +499,24 @@ bool CreateBookmarkFunction::RunImpl() {
}
int index;
- if (!json->HasKey(keys::kIndexKey)) { // Optional (defaults to end).
+ if (!params->bookmark.index.get()) { // Optional (defaults to end).
index = parent->child_count();
} else {
- EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIndexKey, &index));
+ index = *params->bookmark.index;
if (index > parent->child_count() || index < 0) {
error_ = keys::kInvalidIndexError;
return false;
}
}
- string16 title;
- json->GetString(keys::kTitleKey, &title); // Optional.
- std::string url_string;
- json->GetString(keys::kUrlKey, &url_string); // Optional.
+ string16 title; // Optional.
+ if (params->bookmark.title.get())
+ title = UTF8ToUTF16(*params->bookmark.title.get());
+
+ std::string url_string; // Optional.
+ if (params->bookmark.url.get())
+ url_string = *params->bookmark.url.get();
+
GURL url(url_string);
if (!url_string.empty() && !url.is_valid()) {
error_ = keys::kInvalidUrlError;
@@ -520,9 +534,9 @@ bool CreateBookmarkFunction::RunImpl() {
return false;
}
- DictionaryValue* ret =
- bookmark_extension_helpers::GetNodeDictionary(node, false, false);
- SetResult(ret);
+ scoped_ptr<BookmarkTreeNode> ret(
+ bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false));
+ results_ = bookmarks::Create::Results::Create(*ret);
return true;
}
@@ -538,20 +552,19 @@ bool MoveBookmarkFunction::ExtractIds(const ListValue* args,
bool MoveBookmarkFunction::RunImpl() {
if (!EditBookmarksEnabled())
return false;
- std::list<int64> ids;
- bool invalid_id = false;
- EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id));
- if (invalid_id) {
+
+ scoped_ptr<bookmarks::Move::Params> params(
+ bookmarks::Move::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ int64 id;
+ if (!base::StringToInt64(params->id, &id)) {
error_ = keys::kInvalidIdError;
return false;
}
- EXTENSION_FUNCTION_VALIDATE(ids.size() == 1);
-
- DictionaryValue* destination;
- EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &destination));
BookmarkModel* model = profile()->GetBookmarkModel();
- const BookmarkNode* node = model->GetNodeByID(ids.front());
+ const BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
@@ -562,15 +575,12 @@ bool MoveBookmarkFunction::RunImpl() {
}
const BookmarkNode* parent = NULL;
- if (!destination->HasKey(keys::kParentIdKey)) {
+ if (!params->destination.parent_id.get()) {
// Optional, defaults to current parent.
parent = node->parent();
} else {
- std::string parentId_string;
- EXTENSION_FUNCTION_VALIDATE(destination->GetString(keys::kParentIdKey,
- &parentId_string));
int64 parentId;
- if (!GetBookmarkIdAsInt64(parentId_string, &parentId))
+ if (!GetBookmarkIdAsInt64(*params->destination.parent_id, &parentId))
return false;
parent = model->GetNodeByID(parentId);
@@ -586,9 +596,8 @@ bool MoveBookmarkFunction::RunImpl() {
}
int index;
- if (destination->HasKey(keys::kIndexKey)) { // Optional (defaults to end).
- EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kIndexKey,
- &index));
+ if (params->destination.index.get()) { // Optional (defaults to end).
+ index = *params->destination.index;
if (index > parent->child_count() || index < 0) {
error_ = keys::kInvalidIndexError;
return false;
@@ -599,9 +608,9 @@ bool MoveBookmarkFunction::RunImpl() {
model->Move(node, parent, index);
- DictionaryValue* ret =
- bookmark_extension_helpers::GetNodeDictionary(node, false, false);
- SetResult(ret);
+ scoped_ptr<BookmarkTreeNode> tree_node(
+ bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false));
+ results_ = bookmarks::Move::Results::Create(*tree_node);
return true;
}
@@ -617,33 +626,38 @@ bool UpdateBookmarkFunction::ExtractIds(const ListValue* args,
bool UpdateBookmarkFunction::RunImpl() {
if (!EditBookmarksEnabled())
return false;
- std::list<int64> ids;
- bool invalid_id = false;
- EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id));
- if (invalid_id) {
+
+ scoped_ptr<bookmarks::Update::Params> params(
+ bookmarks::Update::Params::Create(*args_));
+ EXTENSION_FUNCTION_VALIDATE(params.get());
+
+ int64 id;
+ if (!base::StringToInt64(params->id, &id)) {
error_ = keys::kInvalidIdError;
return false;
}
- EXTENSION_FUNCTION_VALIDATE(ids.size() == 1);
- DictionaryValue* updates;
- EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &updates));
+ BookmarkModel* model = profile()->GetBookmarkModel();
// Optional but we need to distinguish non present from an empty title.
string16 title;
- const bool has_title = updates->GetString(keys::kTitleKey, &title);
+ bool has_title = false;
+ if (params->changes.title.get()) {
+ title = UTF8ToUTF16(*params->changes.title);
+ has_title = true;
+ }
// Optional.
std::string url_string;
- updates->GetString(keys::kUrlKey, &url_string);
+ if (params->changes.url.get())
+ url_string = *params->changes.url;
GURL url(url_string);
if (!url_string.empty() && !url.is_valid()) {
error_ = keys::kInvalidUrlError;
return false;
}
- BookmarkModel* model = profile()->GetBookmarkModel();
- const BookmarkNode* node = model->GetNodeByID(ids.front());
+ const BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
@@ -657,10 +671,9 @@ bool UpdateBookmarkFunction::RunImpl() {
if (!url.is_empty())
model->SetURL(node, url);
- DictionaryValue* ret =
- bookmark_extension_helpers::GetNodeDictionary(node, false, false);
- SetResult(ret);
-
+ scoped_ptr<BookmarkTreeNode> tree_node(
+ bookmark_extension_helpers::GetBookmarkTreeNode(node, false, false));
+ results_ = bookmarks::Update::Results::Create(*tree_node);
return true;
}
diff --git a/chrome/browser/bookmarks/bookmark_extension_helpers.cc b/chrome/browser/bookmarks/bookmark_extension_helpers.cc
index fea02db..281ac02 100644
--- a/chrome/browser/bookmarks/bookmark_extension_helpers.cc
+++ b/chrome/browser/bookmarks/bookmark_extension_helpers.cc
@@ -5,17 +5,37 @@
#include "chrome/browser/bookmarks/bookmark_extension_helpers.h"
#include <math.h> // For floor()
+#include <vector>
#include "base/string_number_conversions.h"
+#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_extension_api_constants.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
+#include "chrome/common/extensions/api/bookmarks.h"
+
+using extensions::api::bookmarks::BookmarkTreeNode;
namespace keys = bookmark_extension_api_constants;
namespace {
void AddNode(const BookmarkNode* node,
+ std::vector<linked_ptr<BookmarkTreeNode> >* nodes,
+ bool recurse,
+ bool only_folders) {
+ if (node->IsVisible()) {
+ linked_ptr<BookmarkTreeNode> new_node(
+ bookmark_extension_helpers::GetBookmarkTreeNode(node,
+ recurse,
+ only_folders));
+ nodes->push_back(new_node);
+ }
+}
+
+// TODO(mwrosen): Remove this function once chrome.experimental.bookmarkManager
+// is refactored to use the JSON schema compiler.
+void AddNode(const BookmarkNode* node,
base::ListValue* list,
bool recurse,
bool only_folders) {
@@ -30,6 +50,54 @@ void AddNode(const BookmarkNode* node,
namespace bookmark_extension_helpers {
+BookmarkTreeNode* GetBookmarkTreeNode(const BookmarkNode* node,
+ bool recurse,
+ bool only_folders) {
+ BookmarkTreeNode* bookmark_tree_node = new BookmarkTreeNode;
+
+ bookmark_tree_node->id = base::Int64ToString(node->id());
+
+ const BookmarkNode* parent = node->parent();
+ if (parent) {
+ bookmark_tree_node->parent_id.reset(new std::string(
+ base::Int64ToString(parent->id())));
+ bookmark_tree_node->index.reset(new int(parent->GetIndexOf(node)));
+ }
+
+ if (!node->is_folder()) {
+ bookmark_tree_node->url.reset(new std::string(node->url().spec()));
+ } else {
+ // Javascript Date wants milliseconds since the epoch, ToDoubleT is seconds.
+ base::Time t = node->date_folder_modified();
+ if (!t.is_null()) {
+ bookmark_tree_node->date_group_modified.reset(
+ new double(floor(t.ToDoubleT() * 1000)));
+ }
+ }
+
+ bookmark_tree_node->title = UTF16ToUTF8(node->GetTitle());
+ if (!node->date_added().is_null()) {
+ // Javascript Date wants milliseconds since the epoch, ToDoubleT is seconds.
+ bookmark_tree_node->date_added.reset(
+ new double(floor(node->date_added().ToDoubleT() * 1000)));
+ }
+
+ if (recurse && node->is_folder()) {
+ std::vector<linked_ptr<BookmarkTreeNode> > children;
+ for (int i = 0; i < node->child_count(); ++i) {
+ const BookmarkNode* child = node->GetChild(i);
+ if (child->IsVisible() && (!only_folders || child->is_folder())) {
+ linked_ptr<BookmarkTreeNode> child_node(
+ GetBookmarkTreeNode(child, true, only_folders));
+ children.push_back(child_node);
+ }
+ }
+ bookmark_tree_node->children.reset(
+ new std::vector<linked_ptr<BookmarkTreeNode> >(children));
+ }
+ return bookmark_tree_node;
+}
+
base::DictionaryValue* GetNodeDictionary(const BookmarkNode* node,
bool recurse,
bool only_folders) {
@@ -73,6 +141,18 @@ base::DictionaryValue* GetNodeDictionary(const BookmarkNode* node,
return dict;
}
+void AddNode(const BookmarkNode* node,
+ std::vector<linked_ptr<BookmarkTreeNode> >* nodes,
+ bool recurse) {
+ return ::AddNode(node, nodes, recurse, false);
+}
+
+void AddNodeFoldersOnly(const BookmarkNode* node,
+ std::vector<linked_ptr<BookmarkTreeNode> >* nodes,
+ bool recurse) {
+ return ::AddNode(node, nodes, recurse, true);
+}
+
void AddNode(const BookmarkNode* node, base::ListValue* list, bool recurse) {
return ::AddNode(node, list, recurse, false);
}
diff --git a/chrome/browser/bookmarks/bookmark_extension_helpers.h b/chrome/browser/bookmarks/bookmark_extension_helpers.h
index f4e8e2e..5113b7a 100644
--- a/chrome/browser/bookmarks/bookmark_extension_helpers.h
+++ b/chrome/browser/bookmarks/bookmark_extension_helpers.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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.
@@ -6,28 +6,49 @@
#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_EXTENSION_HELPERS_H_
#include <string>
+#include <vector>
#include "base/basictypes.h"
+#include "chrome/common/extensions/api/bookmarks.h"
class BookmarkModel;
class BookmarkNode;
-namespace base {
-class DictionaryValue;
-class ListValue;
-}
-
// Helper functions.
namespace bookmark_extension_helpers {
// The returned value is owned by the caller.
+extensions::api::bookmarks::BookmarkTreeNode* GetBookmarkTreeNode(
+ const BookmarkNode* node,
+ bool recurse,
+ bool only_folders);
+
+// TODO(mwrosen): Remove this function once chrome.experimental.bookmarkManager
+// is refactored to use the JSON schema compiler.
base::DictionaryValue* GetNodeDictionary(const BookmarkNode* node,
bool recurse,
bool only_folders);
-// Add a JSON representation of |node| to the JSON |list|.
-void AddNode(const BookmarkNode* node, base::ListValue* list, bool recurse);
+// Add a JSON representation of |node| to the JSON |nodes|.
+void AddNode(const BookmarkNode* node,
+ std::vector<linked_ptr<
+ extensions::api::bookmarks::BookmarkTreeNode> >* nodes,
+ bool recurse);
+
+void AddNodeFoldersOnly(const BookmarkNode* node,
+ std::vector<linked_ptr<
+ extensions::api::bookmarks::BookmarkTreeNode> >*
+ nodes,
+ bool recurse);
+
+// TODO(mwrosen): Remove this function once chrome.experimental.bookmarkManager
+// is refactored to use the JSON schema compiler.
+void AddNode(const BookmarkNode* node,
+ base::ListValue* list,
+ bool recurse);
+// TODO(mwrosen): Remove this function once chrome.experimental.bookmarkManager
+// is refactored to use the JSON schema compiler.
void AddNodeFoldersOnly(const BookmarkNode* node,
base::ListValue* list,
bool recurse);
diff --git a/chrome/browser/bookmarks/bookmark_extension_helpers_unittest.cc b/chrome/browser/bookmarks/bookmark_extension_helpers_unittest.cc
index 50850f1..a2d54d3 100644
--- a/chrome/browser/bookmarks/bookmark_extension_helpers_unittest.cc
+++ b/chrome/browser/bookmarks/bookmark_extension_helpers_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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.
@@ -9,10 +9,13 @@
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_extension_api_constants.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
+#include "chrome/common/extensions/api/bookmarks.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace keys = bookmark_extension_api_constants;
+using extensions::api::bookmarks::BookmarkTreeNode;
+
class ExtensionBookmarksTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
@@ -33,55 +36,43 @@ class ExtensionBookmarksTest : public testing::Test {
const BookmarkNode* folder_;
};
TEST_F(ExtensionBookmarksTest, GetFullTreeFromRoot) {
- scoped_ptr<DictionaryValue> tree(
- bookmark_extension_helpers::GetNodeDictionary(
+ scoped_ptr<BookmarkTreeNode> tree(
+ bookmark_extension_helpers::GetBookmarkTreeNode(
model_->other_node(),
true, // Recurse.
false)); // Not only folders.
- ListValue* children;
- tree->GetList(keys::kChildrenKey, &children);
- ASSERT_EQ(3U, children->GetSize());
+ ASSERT_EQ(3U, tree->children->size());
}
TEST_F(ExtensionBookmarksTest, GetFoldersOnlyFromRoot) {
- scoped_ptr<DictionaryValue> tree(
- bookmark_extension_helpers::GetNodeDictionary(
+ scoped_ptr<BookmarkTreeNode> tree(
+ bookmark_extension_helpers::GetBookmarkTreeNode(
model_->other_node(),
true, // Recurse.
true)); // Only folders.
- ListValue* children;
- tree->GetList(keys::kChildrenKey, &children);
- ASSERT_EQ(1U, children->GetSize());
+ ASSERT_EQ(1U, tree->children->size());
}
TEST_F(ExtensionBookmarksTest, GetSubtree) {
- scoped_ptr<DictionaryValue> tree(
- bookmark_extension_helpers::GetNodeDictionary(
+ scoped_ptr<BookmarkTreeNode> tree(
+ bookmark_extension_helpers::GetBookmarkTreeNode(
folder_,
true, // Recurse.
false)); // Not only folders.
- ListValue* children;
- tree->GetList(keys::kChildrenKey, &children);
- ASSERT_EQ(4U, children->GetSize());
- DictionaryValue* digg;
- ASSERT_TRUE(children->GetDictionary(1, &digg));
- std::string title;
- digg->GetString(keys::kTitleKey, &title);
- ASSERT_EQ("Digg", title);
+ ASSERT_EQ(4U, tree->children->size());
+ linked_ptr<BookmarkTreeNode> digg = tree->children->at(1);
+ ASSERT_TRUE(digg.get());
+ ASSERT_EQ("Digg", digg->title);
}
TEST_F(ExtensionBookmarksTest, GetSubtreeFoldersOnly) {
- scoped_ptr<DictionaryValue> tree(
- bookmark_extension_helpers::GetNodeDictionary(
+ scoped_ptr<BookmarkTreeNode> tree(
+ bookmark_extension_helpers::GetBookmarkTreeNode(
folder_,
true, // Recurse.
true)); // Only folders.
- ListValue* children;
- tree->GetList(keys::kChildrenKey, &children);
- ASSERT_EQ(2U, children->GetSize());
- DictionaryValue* inner_folder;
- ASSERT_TRUE(children->GetDictionary(1, &inner_folder));
- std::string title;
- inner_folder->GetString(keys::kTitleKey, &title);
- ASSERT_EQ("inner folder 1", title);
+ ASSERT_EQ(2U, tree->children->size());
+ linked_ptr<BookmarkTreeNode> inner_folder = tree->children->at(1);
+ ASSERT_TRUE(inner_folder.get());
+ ASSERT_EQ("inner folder 1", inner_folder->title);
}
diff --git a/chrome/common/extensions/api/api.gyp b/chrome/common/extensions/api/api.gyp
index 1184700..edbc468 100644
--- a/chrome/common/extensions/api/api.gyp
+++ b/chrome/common/extensions/api/api.gyp
@@ -18,6 +18,7 @@
'variables': {
'chromium_code': 1,
'json_schema_files': [
+ 'bookmarks.json',
'content_settings.json',
'cookies.json',
'debugger.json',
diff --git a/chrome/common/extensions/api/bookmarks.json b/chrome/common/extensions/api/bookmarks.json
index eb19bd4..4b351c6 100644
--- a/chrome/common/extensions/api/bookmarks.json
+++ b/chrome/common/extensions/api/bookmarks.json
@@ -15,14 +15,47 @@
"type": "object",
"description": "A node (either a bookmark or a folder) in the bookmark tree. Child nodes are ordered within their parent folder.",
"properties": {
- "id": {"type": "string", "minimum": 0, "description": "The unique identifier for the node. IDs are unique within the current profile, and they remain valid even after the browser is restarted."},
- "parentId": {"type": "string", "minimum": 0, "optional": true, "description": "The <code>id</code> of the parent folder. Omitted for the root node."},
- "index": {"type": "integer", "optional": true, "description": "The 0-based position of this node within its parent folder."},
- "url": {"type": "string", "optional": true, "description": "The URL navigated to when a user clicks the bookmark. Omitted for folders."},
- "title": {"type": "string", "description": "The text displayed for the node."},
- "dateAdded": {"type": "number", "optional": true, "description": "When this node was created, in milliseconds since the epoch (<code>new Date(dateAdded)</code>)."},
- "dateGroupModified": {"type": "number", "optional": true, "description": "When the contents of this folder last changed, in milliseconds since the epoch."},
- "children": {"type": "array", "optional": true, "items": {"$ref": "BookmarkTreeNode"}, "description": "An ordered list of children of this node."}
+ "id": {
+ "type": "string",
+ "minimum": 0,
+ "description": "The unique identifier for the node. IDs are unique within the current profile, and they remain valid even after the browser is restarted."
+ },
+ "parentId": {
+ "type": "string",
+ "minimum": 0,
+ "optional": true,
+ "description": "The <code>id</code> of the parent folder. Omitted for the root node."
+ },
+ "index": {
+ "type": "integer",
+ "optional": true,
+ "description": "The 0-based position of this node within its parent folder."
+ },
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL navigated to when a user clicks the bookmark. Omitted for folders."
+ },
+ "title": {
+ "type": "string",
+ "description": "The text displayed for the node."
+ },
+ "dateAdded": {
+ "type": "number",
+ "optional": true,
+ "description": "When this node was created, in milliseconds since the epoch (<code>new Date(dateAdded)</code>)."
+ },
+ "dateGroupModified": {
+ "type": "number",
+ "optional": true,
+ "description": "When the contents of this folder last changed, in milliseconds since the epoch."
+ },
+ "children": {
+ "type": "array",
+ "optional": true,
+ "items": { "$ref": "BookmarkTreeNode" },
+ "description": "An ordered list of children of this node."
+ }
}
}
],
@@ -36,15 +69,29 @@
"name": "idOrIdList",
"description": "A single string-valued id, or an array of string-valued ids",
"choices": [
- {"type": "string"},
- {"type": "array", "items": {"type": "string"}, "minItems": 1}
+ {
+ "type": "string",
+ "serialized_type": "int64"
+ },
+ {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "serialized_type": "int64"
+ },
+ "minItems": 1
+ }
]
},
{
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
]
}
]
@@ -54,12 +101,20 @@
"type": "function",
"description": "Retrieves the children of the specified BookmarkTreeNode id.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "serialized_type": "int64",
+ "name": "id"
+ },
{
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode"}
+ }
]
}
]
@@ -79,7 +134,11 @@
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
]
}
]
@@ -93,7 +152,11 @@
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
]
}
]
@@ -105,6 +168,7 @@
"parameters": [
{
"type": "string",
+ "serialized_type": "int64",
"name": "id",
"description": "The ID of the root of the subtree to retrieve."
},
@@ -112,7 +176,11 @@
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
]
}
]
@@ -122,12 +190,19 @@
"type": "function",
"description": "Searches for BookmarkTreeNodes matching the given query.",
"parameters": [
- {"type": "string", "name": "query"},
+ {
+ "type": "string",
+ "name": "query"
+ },
{
"type": "function",
"name": "callback",
"parameters": [
- {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} }
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
]
}
]
@@ -143,11 +218,23 @@
"properties": {
"parentId": {
"type": "string",
+ "serialized_type": "int64",
"optional": true,
- "description": "Defaults to the Other Bookmarks folder."},
- "index": {"type": "integer", "minimum": 0, "optional": true},
- "title": {"type": "string", "optional": true},
- "url": {"type": "string", "optional": true}
+ "description": "Defaults to the Other Bookmarks folder."
+ },
+ "index": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true
+ },
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
}
},
{
@@ -155,7 +242,10 @@
"name": "callback",
"optional": true,
"parameters": [
- {"name": "result", "$ref": "BookmarkTreeNode" }
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
]
}
]
@@ -165,13 +255,24 @@
"type": "function",
"description": "Moves the specified BookmarkTreeNode to the provided location.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "serialized_type": "int64",
+ "name": "id"
+ },
{
"type": "object",
"name": "destination",
"properties": {
- "parentId": {"type": "string"},
- "index": {"type": "integer", "minimum": 0, "optional": true}
+ "parentId": {
+ "type": "string",
+ "optional": true
+ },
+ "index": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true
+ }
}
},
{
@@ -179,7 +280,10 @@
"name": "callback",
"optional": true,
"parameters": [
- {"name": "result", "$ref": "BookmarkTreeNode" }
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
]
}
]
@@ -189,13 +293,23 @@
"type": "function",
"description": "Updates the properties of a bookmark or folder. Specify only the properties that you want to change; unspecified properties will be left unchanged. <b>Note:</b> Currently, only 'title' and 'url' are supported.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "serialized_type": "int64",
+ "name": "id"
+ },
{
"type": "object",
"name": "changes",
"properties": {
- "title": {"type": "string", "optional": true},
- "url": {"type": "string", "optional": true}
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
}
},
{
@@ -203,7 +317,10 @@
"name": "callback",
"optional": true,
"parameters": [
- {"name": "result", "$ref": "BookmarkTreeNode" }
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
]
}
]
@@ -213,8 +330,17 @@
"type": "function",
"description": "Removes a bookmark or an empty bookmark folder.",
"parameters": [
- {"type": "string", "name": "id"},
- {"type": "function", "name": "callback", "optional": true, "parameters": []}
+ {
+ "type": "string",
+ "serialized_type": "int64",
+ "name": "id"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
]
},
{
@@ -222,8 +348,17 @@
"type": "function",
"description": "Recursively removes a bookmark folder.",
"parameters": [
- {"type": "string", "name": "id"},
- {"type": "function", "name": "callback", "optional": true, "parameters": []}
+ {
+ "type": "string",
+ "serialized_type": "int64",
+ "name": "id"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
]
},
{
@@ -232,7 +367,12 @@
"description": "Imports bookmarks from a chrome html bookmark file",
"nodoc": "true",
"parameters": [
- {"type": "function", "name": "callback", "optional": true, "parameters": []}
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
]
},
{
@@ -241,7 +381,12 @@
"description": "Exports bookmarks to a chrome html bookmark file",
"nodoc": "true",
"parameters": [
- {"type": "function", "name": "callback", "optional": true, "parameters": []}
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
]
}
],
@@ -251,7 +396,10 @@
"type": "function",
"description": "Fired when a bookmark or folder is created.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "name": "id"
+ },
{
"$ref": "BookmarkTreeNode",
"name": "bookmark"
@@ -263,7 +411,10 @@
"type": "function",
"description": "Fired when a bookmark or folder is removed. When a folder is removed recursively, a single notification is fired for the folder, and none for its contents.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "name": "id"
+ },
{
"type": "object",
"name": "removeInfo",
@@ -279,13 +430,19 @@
"type": "function",
"description": "Fired when a bookmark or folder changes. <b>Note:</b> Currently, only title and url changes trigger this.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "name": "id"
+ },
{
"type": "object",
"name": "changeInfo",
"properties": {
- "title": {"type": "string"},
- "url": {"type": "string", "optional": true}
+ "title": { "type": "string" },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
}
}
]
@@ -295,7 +452,10 @@
"type": "function",
"description": "Fired when a bookmark or folder is moved to a different parent folder.",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "name": "id"
+ },
{
"type": "object",
"name": "moveInfo",
@@ -313,12 +473,18 @@
"type": "function",
"description": "Fired when the children of a folder have changed their order due to the order being sorted in the UI. This is not called as a result of a move().",
"parameters": [
- {"type": "string", "name": "id"},
+ {
+ "type": "string",
+ "name": "id"
+ },
{
"type": "object",
"name": "reorderInfo",
"properties": {
- "childIds": { "type": "array", "items": { "type": "string" } }
+ "childIds": {
+ "type": "array",
+ "items": { "type": "string" }
+ }
}
}
]
diff --git a/chrome/common/extensions/docs/apps/bookmarks.html b/chrome/common/extensions/docs/apps/bookmarks.html
index 216fc1b..93e6151 100644
--- a/chrome/common/extensions/docs/apps/bookmarks.html
+++ b/chrome/common/extensions/docs/apps/bookmarks.html
@@ -905,6 +905,7 @@ For other examples and for help in viewing the source code, see
<!-- TYPE -->
<div style="display:inline">
(
+ <span class="optional">optional</span>
<span id="typeTemplate">
<span>
<span>string</span>
diff --git a/chrome/common/extensions/docs/extensions/bookmarks.html b/chrome/common/extensions/docs/extensions/bookmarks.html
index 751d959..8374e4d 100644
--- a/chrome/common/extensions/docs/extensions/bookmarks.html
+++ b/chrome/common/extensions/docs/extensions/bookmarks.html
@@ -1198,6 +1198,7 @@ For other examples and for help in viewing the source code, see
<!-- TYPE -->
<div style="display:inline">
(
+ <span class="optional">optional</span>
<span id="typeTemplate">
<span>
<span>string</span>