diff options
author | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-18 19:23:55 +0000 |
---|---|---|
committer | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-18 19:23:55 +0000 |
commit | a3c94c713dcf12e7d95b7c4548137d7beb790e9c (patch) | |
tree | 3cc374f21b4eaca2b5a1ff27850f62a19c28f4f8 | |
parent | 761caa4b594ceb7ce534f0abc8e68243204b900d (diff) | |
download | chromium_src-a3c94c713dcf12e7d95b7c4548137d7beb790e9c.zip chromium_src-a3c94c713dcf12e7d95b7c4548137d7beb790e9c.tar.gz chromium_src-a3c94c713dcf12e7d95b7c4548137d7beb790e9c.tar.bz2 |
Adds bookmarks.getRecent that returns the recently bookmarked items.
BUG=None
TEST=browser_tests.exe --gtest_filter=ExtensionApiTest.FLAKY_Bookmarks
Review URL: http://codereview.chromium.org/503053
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34972 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 301 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_bookmarks_module.cc b/chrome/browser/extensions/extension_bookmarks_module.cc index 9ae6ff9..568391c 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.cc +++ b/chrome/browser/extensions/extension_bookmarks_module.cc @@ -341,6 +341,26 @@ bool GetBookmarkChildrenFunction::RunImpl() { return true; } +bool GetBookmarkRecentFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_INTEGER)); + int number_of_items; + EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&number_of_items)); + if (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); + std::vector<const BookmarkNode*>::iterator i = nodes.begin(); + for (; i != nodes.end(); ++i) { + const BookmarkNode* node = *i; + ExtensionBookmarks::AddNode(node, json, false); + } + result_.reset(json); + return true; +} + bool GetBookmarkTreeFunction::RunImpl() { BookmarkModel* model = profile()->GetBookmarkModel(); scoped_ptr<ListValue> json(new ListValue()); diff --git a/chrome/browser/extensions/extension_bookmarks_module.h b/chrome/browser/extensions/extension_bookmarks_module.h index 42b780c..4caece2 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.h +++ b/chrome/browser/extensions/extension_bookmarks_module.h @@ -96,6 +96,13 @@ class GetBookmarkChildrenFunction : public BookmarksFunction { DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getChildren") }; +class GetBookmarkRecentFunction : public BookmarksFunction { + public: + virtual bool RunImpl(); + private: + DECLARE_EXTENSION_FUNCTION_NAME("bookmarks.getRecent") +}; + class GetBookmarkTreeFunction : public BookmarksFunction { public: virtual bool RunImpl(); diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index c1cb333..82e9d63 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -122,6 +122,7 @@ void FactoryRegistry::ResetFunctions() { // Bookmarks. RegisterFunction<GetBookmarksFunction>(); RegisterFunction<GetBookmarkChildrenFunction>(); + RegisterFunction<GetBookmarkRecentFunction>(); RegisterFunction<GetBookmarkTreeFunction>(); RegisterFunction<SearchBookmarksFunction>(); RegisterFunction<RemoveBookmarkFunction>(); diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index 14a7967..846c798 100755 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -1199,6 +1199,26 @@ ] }, { + "name": "getRecent", + "type": "function", + "description": "Retrieves the recently added bookmarks.", + "parameters": [ + { + "type": "integer", + "minimum": 1, + "name": "numberOfItems", + "description": "The maximum number of items to return." + }, + { + "type": "function", + "name": "callback", + "parameters": [ + {"name": "results", "type": "array", "items": { "$ref": "BookmarkTreeNode"} } + ] + } + ] + }, + { "name": "getTree", "type": "function", "description": "Retrieves the entire Bookmarks hierarchy.", diff --git a/chrome/common/extensions/docs/bookmarks.html b/chrome/common/extensions/docs/bookmarks.html index e7a14c8..5622930 100644 --- a/chrome/common/extensions/docs/bookmarks.html +++ b/chrome/common/extensions/docs/bookmarks.html @@ -237,6 +237,8 @@ </li><li> <a href="#method-getChildren">getChildren</a> </li><li> + <a href="#method-getRecent">getRecent</a> + </li><li> <a href="#method-getTree">getTree</a> </li><li> <a href="#method-move">move</a> @@ -1131,6 +1133,195 @@ For a full example of using bookmarks, see the </div> <!-- /description --> </div><div class="apiItem"> + <a name="method-getRecent"></a> <!-- method-anchor --> + <h4>getRecent</h4> + + <div class="summary"><span style="display: none; ">void</span> + <!-- Note: intentionally longer 80 columns --> + <span>chrome.bookmarks.getRecent</span>(<span class="null"><span style="display: none; ">, </span><span>integer</span> + <var><span>numberOfItems</span></var></span><span class="null"><span>, </span><span>function</span> + <var><span>callback</span></var></span>)</div> + + <div class="description"> + <p class="todo" style="display: none; ">Undocumented.</p> + <p>Retrieves the recently added bookmarks.</p> + + <!-- PARAMETERS --> + <h4>Parameters</h4> + <dl> + <div> + <div> + <dt> + <var>numberOfItems</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>integer</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo" style="display: none; "> + Undocumented. + </dd> + <dd>The maximum number of items to return.</dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> + </div><div> + <div> + <dt> + <var>callback</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>function</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo"> + Undocumented. + </dd> + <dd style="display: none; "> + Description of this parameter from the json schema. + </dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> + </div> + </dl> + + <!-- RETURNS --> + <h4 style="display: none; ">Returns</h4> + <dl> + <div style="display: none; "> + <div> + </div> + </div> + </dl> + + <!-- CALLBACK --> + <div> + <div> + <h4>Callback function</h4> + <p> + The callback <em>parameter</em> should specify a function + that looks like this: + </p> + <p style="display: none; "> + If you specify the <em>callback</em> parameter, it should + specify a function that looks like this: + </p> + + <!-- Note: intentionally longer 80 columns --> + <pre>function(<span>array of BookmarkTreeNode results</span>) <span class="subdued">{...}</span>);</pre> + <dl> + <div> + <div> + <dt> + <var>results</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span> + array of <span><span> + <span> + <a href="bookmarks.html#type-BookmarkTreeNode">BookmarkTreeNode</a> + </span> + <span style="display: none; "> + <span> + array of <span><span></span></span> + </span> + <span>paramType</span> + </span> + </span></span> + </span> + <span style="display: none; ">paramType</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo"> + Undocumented. + </dd> + <dd style="display: none; "> + Description of this parameter from the json schema. + </dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> + </div> + </dl> + </div> + </div> + + </div> <!-- /description --> + + </div><div class="apiItem"> <a name="method-getTree"></a> <!-- method-anchor --> <h4>getTree</h4> diff --git a/chrome/renderer/extensions/extension_api_client_unittest.cc b/chrome/renderer/extensions/extension_api_client_unittest.cc index ac06153..abe3f5c 100644 --- a/chrome/renderer/extensions/extension_api_client_unittest.cc +++ b/chrome/renderer/extensions/extension_api_client_unittest.cc @@ -489,6 +489,12 @@ TEST_F(ExtensionAPIClientTest, GetBookmarkChildren) { "\"42\""); } +TEST_F(ExtensionAPIClientTest, GetBookmarkRecent) { + ExpectJsPass("chrome.bookmarks.getRecent(5, function(){});", + "bookmarks.getRecent", + "5"); +} + TEST_F(ExtensionAPIClientTest, GetBookmarkTree) { ExpectJsPass("chrome.bookmarks.getTree(function(){});", "bookmarks.getTree", diff --git a/chrome/test/data/extensions/api_test/bookmarks/test.js b/chrome/test/data/extensions/api_test/bookmarks/test.js index eb32dcc..d96f41f 100644 --- a/chrome/test/data/extensions/api_test/bookmarks/test.js +++ b/chrome/test/data/extensions/api_test/bookmarks/test.js @@ -348,4 +348,60 @@ chrome.test.runTests([ chrome.test.resetQuota(); }, + + function getRecent_setup() { + // Clean up tree + ["1", "2"].forEach(function(id) { + chrome.bookmarks.getChildren(id, pass(function(children) { + children.forEach(function(child, i) { + chrome.bookmarks.removeTree(child.id, pass(function() {})); + + // When we have removed the last child we can continue. + if (i == children.length - 1) + afterRemove(); + }); + })); + }); + + function afterRemove() { + // Once done add 3 nodes + chrome.bookmarks.getTree(pass(function(results) { + chrome.test.assertEq(0, results[0].children[0].children.length); + chrome.test.assertEq(0, results[0].children[1].children.length); + expected = results; + + // Reset the nodes + node1 = {parentId:"1", title:"Foo bar baz", + url:"http://www.example.com/hello"}; + node2 = {parentId:"1", title:"foo quux", + url:"http://www.example.com/bar"}; + node3 = {parentId:"1", title:"bar baz", + url:"http://www.google.com/hello/quux"}; + createThreeNodes(node1, node2, node3); + })); + } + }, + + function getRecent() { + var failed = false; + try { + chrome.bookmarks.getRecent(0, function() {}); + } catch (ex) { + failed = true; + } + chrome.test.assertTrue(failed, "Calling with 0 should fail"); + + chrome.bookmarks.getRecent(10000, pass(function(results) { + chrome.test.assertEq(3, results.length, + "Should have gotten all recent bookmarks"); + })); + + chrome.bookmarks.getRecent(2, pass(function(results) { + chrome.test.assertEq(2, results.length, + "Should only get the last 2 bookmarks"); + + chrome.test.assertTrue(compareNode(node3, results[0])); + chrome.test.assertTrue(compareNode(node2, results[1])); + })); + } ]); |