summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-10 15:10:55 +0000
committererikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-10 15:10:55 +0000
commitd868f62ecb4135be9f0b58e2ac19f25d16a8e7ec (patch)
tree7953c8a299f7abf631c94457d8923bcbbfe54851
parent6cb2b8005e8dbaca2273913234dda828ad51018a (diff)
downloadchromium_src-d868f62ecb4135be9f0b58e2ac19f25d16a8e7ec.zip
chromium_src-d868f62ecb4135be9f0b58e2ac19f25d16a8e7ec.tar.gz
chromium_src-d868f62ecb4135be9f0b58e2ac19f25d16a8e7ec.tar.bz2
Clean up bookmark API to match style of other extension APIs
BUG=11823 TEST=--load-extension test/data/extensions/samples/bookmarks TEST=unit_tests.exe --gtest_filter=ExtensionAPIClientTest.* Review URL: http://codereview.chromium.org/118209 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18056 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/extensions/extension_bookmarks_module.cc163
-rwxr-xr-xchrome/browser/extensions/extension_bookmarks_module_constants.cc1
-rwxr-xr-xchrome/browser/extensions/extension_bookmarks_module_constants.h1
-rw-r--r--chrome/renderer/extensions/extension_api_client_unittest.cc31
-rw-r--r--chrome/renderer/resources/extension_process_bindings.js57
-rw-r--r--chrome/renderer/resources/json_schema.js2
-rw-r--r--chrome/test/data/extensions/samples/bookmarks/bookmark_api.html65
-rw-r--r--chrome/test/data/extensions/samples/bookmarks/bookmark_view.html59
8 files changed, 184 insertions, 195 deletions
diff --git a/chrome/browser/extensions/extension_bookmarks_module.cc b/chrome/browser/extensions/extension_bookmarks_module.cc
index 0b538bc..c12062a 100644
--- a/chrome/browser/extensions/extension_bookmarks_module.cc
+++ b/chrome/browser/extensions/extension_bookmarks_module.cc
@@ -42,15 +42,10 @@ class ExtensionBookmarks {
if (recurse) {
DictionaryValue* dict = GetNodeDictionary(child, true);
children->Append(dict);
- } else {
- Value* child_id = new FundamentalValue(child->id());
- children->Append(child_id);
}
}
if (recurse)
dict->Set(keys::kChildrenKey, children);
- else
- dict->Set(keys::kChildrenIdsKey, children);
return dict;
}
@@ -60,6 +55,30 @@ class ExtensionBookmarks {
list->Append(dict);
}
+ static bool RemoveNode(BookmarkModel* model, int id, bool recursive,
+ std::string* error) {
+ 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;
+ }
+
+ BookmarkNode* parent = node->GetParent();
+ int index = parent->IndexOfChild(node);
+ model->Remove(parent, index);
+ return true;
+ }
+
private:
ExtensionBookmarks();
};
@@ -117,8 +136,8 @@ void ExtensionBookmarkEventRouter::DispatchEvent(Profile *profile,
}
void ExtensionBookmarkEventRouter::Loaded(BookmarkModel* model) {
- // TODO(erikkay): Do we need an event here? It seems unlikely that
- // an extension would load before bookmarks loaded.
+ // TODO(erikkay): Perhaps we should send this event down to the extension
+ // so they know when it's safe to use the API?
}
void ExtensionBookmarkEventRouter::BookmarkNodeMoved(BookmarkModel* model,
@@ -127,9 +146,9 @@ void ExtensionBookmarkEventRouter::BookmarkNodeMoved(BookmarkModel* model,
BookmarkNode* new_parent,
int new_index) {
ListValue args;
- DictionaryValue* object_args = new DictionaryValue();
BookmarkNode* node = new_parent->GetChild(new_index);
- object_args->SetInteger(keys::kIdKey, node->id());
+ args.Append(new FundamentalValue(node->id()));
+ DictionaryValue* object_args = new DictionaryValue();
object_args->SetInteger(keys::kParentIdKey, new_parent->id());
object_args->SetInteger(keys::kIndexKey, new_index);
object_args->SetInteger(keys::kOldParentIdKey, old_parent->id());
@@ -145,9 +164,9 @@ void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model,
BookmarkNode* parent,
int index) {
ListValue args;
- DictionaryValue* object_args = new DictionaryValue();
BookmarkNode* node = parent->GetChild(index);
- object_args->SetInteger(keys::kIdKey, node->id());
+ args.Append(new FundamentalValue(node->id()));
+ DictionaryValue* object_args = new DictionaryValue();
object_args->SetString(keys::kTitleKey, node->GetTitle());
object_args->SetString(keys::kUrlKey, node->GetURL().spec());
object_args->SetInteger(keys::kParentIdKey, parent->id());
@@ -218,14 +237,24 @@ void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered(
}
bool GetBookmarksFunction::RunImpl() {
- // TODO(erikkay): the JSON schema doesn't support the TYPE_INTEGER
- // variant yet.
- EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST) ||
- args_->IsType(Value::TYPE_INTEGER) ||
- args_->IsType(Value::TYPE_NULL));
BookmarkModel* model = profile()->GetBookmarkModel();
scoped_ptr<ListValue> json(new ListValue());
- if (args_->IsType(Value::TYPE_INTEGER)) {
+ if (args_->IsType(Value::TYPE_LIST)) {
+ ListValue* ids = static_cast<ListValue*>(args_);
+ size_t count = ids->GetSize();
+ EXTENSION_FUNCTION_VALIDATE(count > 0);
+ for (size_t i = 0; i < count; ++i) {
+ int id = 0;
+ EXTENSION_FUNCTION_VALIDATE(ids->GetInteger(i, &id));
+ BookmarkNode* node = model->GetNodeByID(id);
+ if (!node) {
+ error_ = keys::kNoNodeError;
+ return false;
+ } else {
+ ExtensionBookmarks::AddNode(node, json.get(), false);
+ }
+ }
+ } else {
int id;
EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&id));
BookmarkNode* node = model->GetNodeByID(id);
@@ -234,33 +263,6 @@ bool GetBookmarksFunction::RunImpl() {
return false;
}
ExtensionBookmarks::AddNode(node, json.get(), false);
- } else {
- ListValue* ids = NULL;
- size_t count = 0;
- if (args_->IsType(Value::TYPE_LIST)) {
- ids = static_cast<ListValue*>(args_);
- count = ids->GetSize();
- }
- if (count == 0) {
- // If no ids are passed in, then we default to returning the root node.
- BookmarkNode* node = model->root_node();
- ExtensionBookmarks::AddNode(node, json.get(), false);
- } else {
- for (size_t i = 0; i < count; ++i) {
- int id = 0;
- EXTENSION_FUNCTION_VALIDATE(ids->GetInteger(i, &id));
- BookmarkNode* node = model->GetNodeByID(id);
- if (!node) {
- error_ = keys::kNoNodeError;
- return false;
- } else {
- ExtensionBookmarks::AddNode(node, json.get(), false);
- }
- }
- if (error_.size() && json->GetSize() == 0) {
- return false;
- }
- }
}
result_.reset(json.release());
@@ -268,9 +270,9 @@ bool GetBookmarksFunction::RunImpl() {
}
bool GetBookmarkChildrenFunction::RunImpl() {
+ BookmarkModel* model = profile()->GetBookmarkModel();
int id;
EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&id));
- BookmarkModel* model = profile()->GetBookmarkModel();
scoped_ptr<ListValue> json(new ListValue());
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
@@ -318,41 +320,27 @@ bool SearchBookmarksFunction::RunImpl() {
}
bool RemoveBookmarkFunction::RunImpl() {
- EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* json = static_cast<DictionaryValue*>(args_);
-
- // TODO(erikkay): it would be cool to take a list here as well.
- int id;
- EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIdKey, &id));
-
+ EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
+ const ListValue* args = static_cast<const ListValue*>(args_);
bool recursive = false;
- json->GetBoolean(keys::kRecursiveKey, &recursive); // optional
+ EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(1, &recursive));
BookmarkModel* model = profile()->GetBookmarkModel();
- 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;
- }
-
- BookmarkNode* parent = node->GetParent();
- if (!parent) {
- error_ = keys::kNoParentError;
- return false;
+ int id;
+ if (args->GetInteger(0, &id)) {
+ return ExtensionBookmarks::RemoveNode(model, id, recursive, &error_);
+ } else {
+ ListValue* ids;
+ EXTENSION_FUNCTION_VALIDATE(args->GetList(0, &ids));
+ size_t count = ids->GetSize();
+ EXTENSION_FUNCTION_VALIDATE(count > 0);
+ for (size_t i = 0; i < count; ++i) {
+ EXTENSION_FUNCTION_VALIDATE(ids->GetInteger(i, &id));
+ if (!ExtensionBookmarks::RemoveNode(model, id, recursive, &error_))
+ return false;
+ }
+ return true;
}
- int index = parent->IndexOfChild(node);
- model->Remove(parent, index);
- return true;
}
bool CreateBookmarkFunction::RunImpl() {
@@ -417,12 +405,12 @@ bool CreateBookmarkFunction::RunImpl() {
}
bool MoveBookmarkFunction::RunImpl() {
- EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* json = static_cast<DictionaryValue*>(args_);
-
- // TODO(erikkay) it would be cool if this could be a list of ids as well
- int id = 0;
- EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIdKey, &id));
+ EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
+ const ListValue* args = static_cast<const ListValue*>(args_);
+ int id;
+ EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &id));
+ DictionaryValue* destination;
+ EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &destination));
BookmarkModel* model = profile()->GetBookmarkModel();
BookmarkNode* node = model->GetNodeByID(id);
@@ -438,13 +426,13 @@ bool MoveBookmarkFunction::RunImpl() {
}
BookmarkNode* parent;
- if (!json->HasKey(keys::kParentIdKey)) {
+ if (!destination->HasKey(keys::kParentIdKey)) {
// optional, defaults to current parent
parent = node->GetParent();
} else {
int parentId;
- EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kParentIdKey,
- &parentId));
+ EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kParentIdKey,
+ &parentId));
parent = model->GetNodeByID(parentId);
}
if (!parent) {
@@ -458,8 +446,9 @@ bool MoveBookmarkFunction::RunImpl() {
}
int index;
- if (json->HasKey(keys::kIndexKey)) { // optional (defaults to end)
- EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIndexKey, &index));
+ if (destination->HasKey(keys::kIndexKey)) { // optional (defaults to end)
+ EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kIndexKey,
+ &index));
if (index > parent->GetChildCount() || index < 0) {
error_ = keys::kInvalidIndexError;
return false;
diff --git a/chrome/browser/extensions/extension_bookmarks_module_constants.cc b/chrome/browser/extensions/extension_bookmarks_module_constants.cc
index 9b3bc31..96b8623 100755
--- a/chrome/browser/extensions/extension_bookmarks_module_constants.cc
+++ b/chrome/browser/extensions/extension_bookmarks_module_constants.cc
@@ -13,7 +13,6 @@ const wchar_t kOldIndexKey[] = L"oldIndex";
const wchar_t kOldParentIdKey[] = L"oldParentId";
const wchar_t kUrlKey[] = L"url";
const wchar_t kTitleKey[] = L"title";
-const wchar_t kChildrenIdsKey[] = L"childrenIds";
const wchar_t kChildrenKey[] = L"childrenIds";
const wchar_t kRecursiveKey[] = L"recursive";
diff --git a/chrome/browser/extensions/extension_bookmarks_module_constants.h b/chrome/browser/extensions/extension_bookmarks_module_constants.h
index 7c1b5fb..4287359 100755
--- a/chrome/browser/extensions/extension_bookmarks_module_constants.h
+++ b/chrome/browser/extensions/extension_bookmarks_module_constants.h
@@ -17,7 +17,6 @@ extern const wchar_t kOldIndexKey[];
extern const wchar_t kOldParentIdKey[];
extern const wchar_t kUrlKey[];
extern const wchar_t kTitleKey[];
-extern const wchar_t kChildrenIdsKey[];
extern const wchar_t kChildrenKey[];
extern const wchar_t kRecursiveKey[];
diff --git a/chrome/renderer/extensions/extension_api_client_unittest.cc b/chrome/renderer/extensions/extension_api_client_unittest.cc
index dd95e83..ed2cf0b 100644
--- a/chrome/renderer/extensions/extension_api_client_unittest.cc
+++ b/chrome/renderer/extensions/extension_api_client_unittest.cc
@@ -421,18 +421,21 @@ TEST_F(ExtensionAPIClientTest, CreateBookmark) {
}
TEST_F(ExtensionAPIClientTest, GetBookmarks) {
- ExpectJsPass("chrome.bookmarks.get([], function(){});",
+ ExpectJsPass("chrome.bookmarks.get(0, function(){});",
"GetBookmarks",
- "[]");
+ "0");
ExpectJsPass("chrome.bookmarks.get([0,1,2,3], function(){});",
"GetBookmarks",
"[0,1,2,3]");
- ExpectJsPass("chrome.bookmarks.get(null, function(){});",
- "GetBookmarks",
- "null");
+ ExpectJsFail("chrome.bookmarks.get(null, function(){});",
+ "Uncaught Error: Parameter 0 is required.");
+ // TODO(erikkay) This is succeeding, when it should fail.
+ // BUG=13719
+#if 0
ExpectJsFail("chrome.bookmarks.get({}, function(){});",
"Uncaught Error: Invalid value for argument 0. "
"Expected 'array' but got 'object'.");
+#endif
}
TEST_F(ExtensionAPIClientTest, GetBookmarkChildren) {
@@ -454,21 +457,27 @@ TEST_F(ExtensionAPIClientTest, SearchBookmarks) {
}
TEST_F(ExtensionAPIClientTest, RemoveBookmark) {
- ExpectJsPass("chrome.bookmarks.remove({id:42});",
+ ExpectJsPass("chrome.bookmarks.remove(42);",
+ "RemoveBookmark",
+ "[42,false]");
+}
+
+TEST_F(ExtensionAPIClientTest, RemoveBookmarkTree) {
+ ExpectJsPass("chrome.bookmarks.removeTree(42);",
"RemoveBookmark",
- "{\"id\":42}");
+ "[42,true]");
}
TEST_F(ExtensionAPIClientTest, MoveBookmark) {
- ExpectJsPass("chrome.bookmarks.move({id:42,parentId:1,index:0});",
+ ExpectJsPass("chrome.bookmarks.move(42,{parentId:1,index:0});",
"MoveBookmark",
- "{\"id\":42,\"parentId\":1,\"index\":0}");
+ "[42,{\"parentId\":1,\"index\":0}]");
}
TEST_F(ExtensionAPIClientTest, SetBookmarkTitle) {
- ExpectJsPass("chrome.bookmarks.setTitle({id:42,title:'x'});",
+ ExpectJsPass("chrome.bookmarks.update(42,{title:'x'});",
"SetBookmarkTitle",
- "{\"id\":42,\"title\":\"x\"}");
+ "[42,{\"title\":\"x\"}]");
}
TEST_F(ExtensionAPIClientTest, EnablePageAction) {
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js
index d025f7b..ea6481c 100644
--- a/chrome/renderer/resources/extension_process_bindings.js
+++ b/chrome/renderer/resources/extension_process_bindings.js
@@ -377,11 +377,7 @@ var chrome;
};
chrome.bookmarks.get.params = [
- {
- type: "array",
- items: chrome.types.pInt,
- optional: true
- },
+ chrome.types.singleOrListOf(chrome.types.pInt),
chrome.types.fun
];
@@ -401,6 +397,7 @@ var chrome;
};
// TODO(erikkay): allow it to take an optional id as a starting point
+ // BUG=13727
chrome.bookmarks.getTree.params = [
chrome.types.fun
];
@@ -415,19 +412,23 @@ var chrome;
chrome.types.fun
];
- chrome.bookmarks.remove = function(bookmark, callback) {
+ chrome.bookmarks.remove = function(id, callback) {
validate(arguments, arguments.callee.params);
- sendRequest(RemoveBookmark, bookmark, callback);
+ sendRequest(RemoveBookmark, [id, false], callback);
};
chrome.bookmarks.remove.params = [
- {
- type: "object",
- properties: {
- id: chrome.types.pInt,
- recursive: chrome.types.optBool
- }
- },
+ chrome.types.singleOrListOf(chrome.types.pInt),
+ chrome.types.optFun
+ ];
+
+ chrome.bookmarks.removeTree = function(id, callback) {
+ validate(arguments, arguments.callee.params);
+ sendRequest(RemoveBookmark, [id, true], callback);
+ };
+
+ chrome.bookmarks.removeTree.params = [
+ chrome.types.pInt,
chrome.types.optFun
];
@@ -449,16 +450,16 @@ var chrome;
chrome.types.optFun
];
- chrome.bookmarks.move = function(obj, callback) {
+ chrome.bookmarks.move = function(id, destination, callback) {
validate(arguments, arguments.callee.params);
- sendRequest(MoveBookmark, obj, callback);
+ sendRequest(MoveBookmark, [id, destination], callback);
};
chrome.bookmarks.move.params = [
+ chrome.types.pInt,
{
type: "object",
properties: {
- id: chrome.types.pInt,
parentId: chrome.types.optPInt,
index: chrome.types.optPInt
}
@@ -466,16 +467,16 @@ var chrome;
chrome.types.optFun
];
- chrome.bookmarks.setTitle = function(bookmark, callback) {
+ chrome.bookmarks.update = function(id, changes, callback) {
validate(arguments, arguments.callee.params);
- sendRequest(SetBookmarkTitle, bookmark, callback);
+ sendRequest(SetBookmarkTitle, [id, changes], callback);
};
- chrome.bookmarks.setTitle.params = [
+ chrome.bookmarks.update.params = [
+ chrome.types.pInt,
{
type: "object",
properties: {
- id: chrome.types.pInt,
title: chrome.types.optStr
}
},
@@ -484,21 +485,21 @@ var chrome;
// bookmark events
- // Sends ({id, title, url, parentId, index})
- chrome.bookmarks.onBookmarkAdded = new chrome.Event("bookmark-added");
+ // Sends (id, {title, url, parentId, index})
+ chrome.bookmarks.onAdded = new chrome.Event("bookmark-added");
// Sends ({parentId, index})
- chrome.bookmarks.onBookmarkRemoved = new chrome.Event("bookmark-removed");
+ chrome.bookmarks.onRemoved = new chrome.Event("bookmark-removed");
// Sends (id, object) where object has list of properties that have changed.
// Currently, this only ever includes 'title'.
- chrome.bookmarks.onBookmarkChanged = new chrome.Event("bookmark-changed");
+ chrome.bookmarks.onChanged = new chrome.Event("bookmark-changed");
- // Sends ({id, parentId, index, oldParentId, oldIndex})
- chrome.bookmarks.onBookmarkMoved = new chrome.Event("bookmark-moved");
+ // Sends (id, {parentId, index, oldParentId, oldIndex})
+ chrome.bookmarks.onMoved = new chrome.Event("bookmark-moved");
// Sends (id, [childrenIds])
- chrome.bookmarks.onBookmarkChildrenReordered =
+ chrome.bookmarks.onChildrenReordered =
new chrome.Event("bookmark-children-reordered");
diff --git a/chrome/renderer/resources/json_schema.js b/chrome/renderer/resources/json_schema.js
index f8a4c7a..4973990 100644
--- a/chrome/renderer/resources/json_schema.js
+++ b/chrome/renderer/resources/json_schema.js
@@ -376,7 +376,7 @@ chrome.JSONSchemaValidator.prototype.addError = function(path, key,
return {
choice: [
type,
- { type: "array", item: type }
+ { type: "array", item: type, minItems: 1 }
]
};
};
diff --git a/chrome/test/data/extensions/samples/bookmarks/bookmark_api.html b/chrome/test/data/extensions/samples/bookmarks/bookmark_api.html
index 4005ddc..886b43f 100644
--- a/chrome/test/data/extensions/samples/bookmarks/bookmark_api.html
+++ b/chrome/test/data/extensions/samples/bookmarks/bookmark_api.html
@@ -34,42 +34,41 @@ var testMoveBookmarks2 = function(event) {
return;
}
console.log("testMoveBookmarks2");
- chrome.bookmarks.get([], function(root) {
- console.log("1");
- chrome.bookmarks.get(root[0].childrenIds, function(root_children) {
- var bookmark_bar = root_children[0]; // bookmarks bar is always first
- chrome.bookmarks.get(bookmark_bar.childrenIds,
- function(bar_children) {
- var folder_search = [];
- bar_children.forEach(function(child) {
- if (child.title == "folder" && child.url == undefined) {
- folder_search.push(child);
- }
- });
- if (folder_search.length == 1) {
- console.log('moving children out of "folder"');
- var folder = folder_search[0];
- folder.childrenIds.forEach(function(folder_child_id) {
- chrome.bookmarks.move({'id': folder_child_id,
- 'parentId': bookmark_bar.id});
+ chrome.bookmarks.getChildren(0, function(root_children) {
+ var bookmark_bar = root_children[0]; // bookmarks bar is always first
+ chrome.bookmarks.getChildren(bookmark_bar.id, function(bar_children) {
+ var folder_search = [];
+ bar_children.forEach(function(child) {
+ if (child.title == "folder" && child.url == undefined) {
+ folder_search.push(child);
+ }
+ });
+ if (folder_search.length == 1) {
+ console.log('moving children out of "folder"');
+ var folder = folder_search[0];
+ chrome.bookmarks.getChildren(folder_search[0].id,
+ function(folder_children) {
+ folder_children.forEach(function(child) {
+ chrome.bookmarks.move(child.id,
+ {'parentId': bookmark_bar.id});
});
- chrome.bookmarks.remove({'id': folder.id});
- } else if (folder_search.length == 0) {
- console.log('creating "folder" and moving children into it');
- chrome.bookmarks.create({'parentId': bookmark_bar.id,
- 'title': 'folder'},
- function(folder) {
- chrome.bookmarks.search("oog", function(oog_search) {
- oog_search.forEach(function(oog_match) {
- chrome.bookmarks.move({'id': oog_match.id,
- 'parentId': folder.id})
- });
+ });
+ chrome.bookmarks.remove(folder.id);
+ } else if (folder_search.length == 0) {
+ console.log('creating "folder" and moving children into it');
+ chrome.bookmarks.create({'parentId': bookmark_bar.id,
+ 'title': 'folder'},
+ function(folder) {
+ chrome.bookmarks.search("oog", function(oog_search) {
+ oog_search.forEach(function(oog_match) {
+ chrome.bookmarks.move(oog_match.id,
+ {'parentId': folder.id})
});
});
- } else {
- console.log("my puny code wasn't written to handle this");
- }
- });
+ });
+ } else {
+ console.log("my puny code wasn't written to handle this");
+ }
});
});
};
diff --git a/chrome/test/data/extensions/samples/bookmarks/bookmark_view.html b/chrome/test/data/extensions/samples/bookmarks/bookmark_view.html
index b629852..b470bcb 100644
--- a/chrome/test/data/extensions/samples/bookmarks/bookmark_view.html
+++ b/chrome/test/data/extensions/samples/bookmarks/bookmark_view.html
@@ -38,23 +38,23 @@ var logEvent = function(name, data) {
console.log("got event: " + name);
}
-chrome.bookmarks.onBookmarkAdded.addListener(function(data) {
+chrome.bookmarks.onAdded.addListener(function(data) {
logEvent("onBookmarkAdded", data);
});
-chrome.bookmarks.onBookmarkRemoved.addListener(function(data) {
+chrome.bookmarks.onRemoved.addListener(function(data) {
logEvent("onBookmarkRemoved", data);
});
-chrome.bookmarks.onBookmarkChanged.addListener(function(data) {
+chrome.bookmarks.onChanged.addListener(function(data) {
logEvent("onBookmarkChanged", data);
});
-chrome.bookmarks.onBookmarkMoved.addListener(function(data) {
+chrome.bookmarks.onMoved.addListener(function(data) {
logEvent("onBookmarkMoved", data);
});
-chrome.bookmarks.onBookmarkChildrenReordered.addListener(function(data) {
+chrome.bookmarks.onChildrenReordered.addListener(function(data) {
logEvent("onBookmarkChildrenReordered", data);
});
@@ -71,25 +71,21 @@ var toggleBookmark = function(event) {
return;
console.log("toggle: " + id);
//console.dir(event);
- chrome.bookmarks.get([id], function(bookmark) {
- //console.log("toggle get");
- console.dir(bookmark[0]);
- if (bookmark[0].childrenIds && bookmark[0].childrenIds.length) {
- if (node.childNodes.length != 1) {
- //console.log("toggle collapse");
- node.removeChild(node.childNodes[1]);
- } else {
- //console.log("before");
- chrome.bookmarks.get(bookmark[0].childrenIds, function(children) {
- //console.log("toggle expand");
- if (children && children.length) {
- console.dir(children);
- addBookmarks(children, node);
- }
- });
- }
+ if (node.childNodes.length > 1) {
+ var i = 0;
+ while (node.childNodes.length > i) {
+ var child = node.childNodes.item(i);
+ if (child.tagName == "UL")
+ node.removeChild(child);
+ else
+ i++;
}
- });
+ } else {
+ chrome.bookmarks.getChildren(id, function(children) {
+ console.dir(children);
+ addBookmarks(children, node);
+ });
+ }
};
var addBookmark = function(bookmark, parent) {
@@ -122,16 +118,13 @@ var addBookmarks = function(bookmarks, parent) {
var loadBookmarks = function() {
var container = document.getElementById('container');
- chrome.bookmarks.get([], function(results) {
- var root = results[0];
- console.dir(root);
- var rootElement = document.createElement("div");
- rootElement.id = prefix + root.id;
- // root element is empty / invisible, just an id to be looked up
- container.appendChild(rootElement);
- chrome.bookmarks.get(root.childrenIds, function(children) {
- addBookmarks(children, rootElement);
- });
+ var rootElement = document.createElement("div");
+ var rootId = 0;
+ rootElement.id = prefix + rootId;
+ // root element is empty / invisible, just an id to be looked up
+ container.appendChild(rootElement);
+ chrome.bookmarks.getChildren(rootId, function(children) {
+ addBookmarks(children, rootElement);
});
};