summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-14 20:05:54 +0000
committerjstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-14 20:05:54 +0000
commitce258c92a8cb8183105d05a6f8d8b8b48614eabf (patch)
treee346c6b52506877d2ee61114df9d4173b771c0d8
parent18b147138e014805955bfcb5dc93e2191d6e2447 (diff)
downloadchromium_src-ce258c92a8cb8183105d05a6f8d8b8b48614eabf.zip
chromium_src-ce258c92a8cb8183105d05a6f8d8b8b48614eabf.tar.gz
chromium_src-ce258c92a8cb8183105d05a6f8d8b8b48614eabf.tar.bz2
Tab array support for chrome.windows.create
With this change, a user can pass in an individual url or a list of urls to be opened with the new window. BUG=53517 TEST=ExtensionApiTest Review URL: http://codereview.chromium.org/3772004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62633 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/extensions/extension_tabs_module.cc49
-rw-r--r--chrome/common/extensions/api/extension_api.json10
-rw-r--r--chrome/common/extensions/docs/windows.html4
-rw-r--r--chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js23
-rw-r--r--chrome/test/data/extensions/api_test/tabs/basics/test.js23
-rw-r--r--chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js23
-rw-r--r--chrome/test/data/extensions/api_test/tabs/connect/tabs_util.js23
-rw-r--r--chrome/test/data/extensions/api_test/tabs/on_removed/tabs_util.js23
8 files changed, 97 insertions, 81 deletions
diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc
index 7cbaf29..7c5ac37 100644
--- a/chrome/browser/extensions/extension_tabs_module.cc
+++ b/chrome/browser/extensions/extension_tabs_module.cc
@@ -288,23 +288,43 @@ bool GetAllWindowsFunction::RunImpl() {
}
bool CreateWindowFunction::RunImpl() {
- GURL url;
DictionaryValue* args = NULL;
+ std::vector<GURL> urls;
if (HasOptionalArgument(0))
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
// Look for optional url.
if (args) {
- std::string url_string;
if (args->HasKey(keys::kUrlKey)) {
- EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey,
- &url_string));
- url = ResolvePossiblyRelativeURL(url_string, GetExtension());
- if (!url.is_valid()) {
- error_ = ExtensionErrorUtils::FormatErrorMessage(
- keys::kInvalidUrlError, url_string);
- return false;
+ Value* url_value;
+ std::vector<std::string> url_strings;
+ args->Get(keys::kUrlKey, &url_value);
+
+ // First, get all the URLs the client wants to open.
+ if (url_value->IsType(Value::TYPE_STRING)) {
+ std::string url_string;
+ url_value->GetAsString(&url_string);
+ url_strings.push_back(url_string);
+ } else if (url_value->IsType(Value::TYPE_LIST)) {
+ const ListValue* url_list = static_cast<const ListValue*>(url_value);
+ for (size_t i = 0; i < url_list->GetSize(); ++i) {
+ std::string url_string;
+ EXTENSION_FUNCTION_VALIDATE(url_list->GetString(i, &url_string));
+ url_strings.push_back(url_string);
+ }
+ }
+
+ // Second, resolve, validate and convert them to GURLs.
+ for (std::vector<std::string>::iterator i = url_strings.begin();
+ i != url_strings.end(); ++i) {
+ GURL url = ResolvePossiblyRelativeURL(*i, GetExtension());
+ if (!url.is_valid()) {
+ error_ = ExtensionErrorUtils::FormatErrorMessage(
+ keys::kInvalidUrlError, *i);
+ return false;
+ }
+ urls.push_back(url);
}
}
}
@@ -386,7 +406,14 @@ bool CreateWindowFunction::RunImpl() {
}
Browser* new_window = Browser::CreateForType(window_type, window_profile);
- new_window->AddSelectedTabWithURL(url, PageTransition::LINK);
+ for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) {
+ Browser::AddTabWithURLParams addTabParams =
+ Browser::AddTabWithURLParams(*i, PageTransition::LINK);
+ new_window->AddTabWithURL(&addTabParams);
+ }
+ if (urls.size() == 0)
+ new_window->NewTab();
+ new_window->SelectNumberedTab(0);
if (window_type & Browser::TYPE_POPUP)
new_window->window()->SetBounds(popup_bounds);
else
@@ -397,7 +424,7 @@ bool CreateWindowFunction::RunImpl() {
// Don't expose incognito windows if the extension isn't allowed.
result_.reset(Value::CreateNullValue());
} else {
- result_.reset(ExtensionTabUtil::CreateWindowValue(new_window, false));
+ result_.reset(ExtensionTabUtil::CreateWindowValue(new_window, true));
}
return true;
diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json
index 34ab416..216b652 100644
--- a/chrome/common/extensions/api/extension_api.json
+++ b/chrome/common/extensions/api/extension_api.json
@@ -687,7 +687,15 @@
"type": "object",
"name": "createData",
"properties": {
- "url": {"type": "string", "optional": true, "description": "The URL to navigate the first tab to. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page."},
+ "url": {
+ "type": "string",
+ "description": "A URL or list of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.",
+ "optional": true,
+ "choices": [
+ {"type": "string"},
+ {"type": "array", "items": {"type": "string"}}
+ ]
+ },
"left": {"type": "integer", "optional": true, "description": "The number of pixels to position the new window from the left edge of the screen. If not specified, the new window is offset naturally from the last focusd window."},
"top": {"type": "integer", "optional": true, "description": "The number of pixels to position the new window from the top edge of the screen. If not specified, the new window is offset naturally from the last focusd window."},
"width": {"type": "integer", "minimum": 0, "optional": true, "description": "The width in pixels of the new window. If not specified defaults to a natural width."},
diff --git a/chrome/common/extensions/docs/windows.html b/chrome/common/extensions/docs/windows.html
index 2da60a3..97518ea 100644
--- a/chrome/common/extensions/docs/windows.html
+++ b/chrome/common/extensions/docs/windows.html
@@ -561,7 +561,7 @@ For other examples and for help in viewing the source code, see
<span style="display: none; ">
array of <span><span></span></span>
</span>
- <span>string</span>
+ <span>string or array of string</span>
<span style="display: none; "></span>
</span>
</span>
@@ -573,7 +573,7 @@ For other examples and for help in viewing the source code, see
<dd class="todo" style="display: none; ">
Undocumented.
</dd>
- <dd>The URL to navigate the first tab to. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.</dd>
+ <dd>A URL or list of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.</dd>
<dd style="display: none; ">
This parameter was added in version
<b><span></span></b>.
diff --git a/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js
index cf7cf4f..9f13dd9 100644
--- a/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js
+++ b/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js
@@ -27,25 +27,16 @@ function setupWindow(tabUrls, callback) {
// At least one url must be specified.
// The |callback| should look like function(windowId, tabIds) {...}.
function createWindow(tabUrls, winOptions, callback) {
- winOptions["url"] = tabUrls[0];
+ winOptions["url"] = tabUrls;
chrome.windows.create(winOptions, function(win) {
- assertTrue(win.id > 0);
var newTabIds = [];
+ assertTrue(win.id > 0);
+ assertEq(tabUrls.length, win.tabs.length);
- // Create tabs and populate newTabIds array.
- chrome.tabs.getSelected(win.id, function (tab) {
- newTabIds.push(tab.id);
- for (var i = 1; i < tabUrls.length; i++) {
- chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]},
- function(tab){
- newTabIds.push(tab.id);
- if (newTabIds.length == tabUrls.length)
- callback(win.id, newTabIds);
- });
- }
- if (tabUrls.length == 1)
- callback(win.id, newTabIds);
- });
+ for (var i = 0; i < win.tabs.length; i++)
+ newTabIds.push(win.tabs[i].id);
+
+ callback(win.id, newTabIds);
});
}
diff --git a/chrome/test/data/extensions/api_test/tabs/basics/test.js b/chrome/test/data/extensions/api_test/tabs/basics/test.js
index 033e67e..3297bdf 100644
--- a/chrome/test/data/extensions/api_test/tabs/basics/test.js
+++ b/chrome/test/data/extensions/api_test/tabs/basics/test.js
@@ -71,6 +71,19 @@ chrome.test.runTests([
}));
},
+ function createWindowWithDefaultTab() {
+ var verify_default = function() {
+ return pass(function(win) {
+ assertEq(1, win.tabs.length);
+ assertEq("chrome://newtab/", win.tabs[0].url);
+ });
+ };
+
+ // Make sure the window always has the NTP when no URL is supplied.
+ chrome.windows.create({}, verify_default());
+ chrome.windows.create({url:[]}, verify_default());
+ },
+
function setupTwoWindows() {
setupWindow(["about:blank", "chrome://newtab/", pageUrl("a")],
pass(function(winId, tabIds) {
@@ -92,8 +105,8 @@ chrome.test.runTests([
assertEq(firstWindowId, tabs[i].windowId);
assertEq(i, tabs[i].index);
- // The most recent tab should be selected
- assertEq((i == 2), tabs[i].selected);
+ // The first tab should be selected
+ assertEq((i == 0), tabs[i].selected);
}
assertEq("about:blank", tabs[0].url);
assertEq("chrome://newtab/", tabs[1].url);
@@ -139,8 +152,9 @@ chrome.test.runTests([
function updateSelect() {
chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
+ assertEq(true, tabs[0].selected);
assertEq(false, tabs[1].selected);
- assertEq(true, tabs[2].selected);
+ assertEq(false, tabs[2].selected);
// Select tab[1].
chrome.tabs.update(tabs[1].id, {selected: true},
pass(function(tab1){
@@ -257,6 +271,9 @@ chrome.test.runTests([
function tabsOnCreated() {
chrome.test.listenOnce(chrome.tabs.onCreated, function(tab) {
assertEq(pageUrl("f"), tab.url);
+ // TODO(jstritar): http://crbug.com/59194 tab.selected is always false
+ // when passed into chrome.tabs.onCreated listener
+ //assertEq(true, tab.selected);
});
chrome.tabs.create({"windowId": firstWindowId, "url": pageUrl("f"),
diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js
index dc106a8..5bfd177 100644
--- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js
+++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js
@@ -4,25 +4,16 @@
// At least one url must be specified.
// The |callback| should look like function(windowId, tabIds) {...}.
function createWindow(tabUrls, winOptions, callback) {
- winOptions["url"] = tabUrls[0];
+ winOptions["url"] = tabUrls;
chrome.windows.create(winOptions, function(win) {
- assertTrue(win.id > 0);
var newTabIds = [];
+ assertTrue(win.id > 0);
+ assertEq(tabUrls.length, win.tabs.length);
- // Create tabs and populate newTabIds array.
- chrome.tabs.getSelected(win.id, function (tab) {
- newTabIds.push(tab.id);
- for (var i = 1; i < tabUrls.length; i++) {
- chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]},
- function(tab){
- newTabIds.push(tab.id);
- if (newTabIds.length == tabUrls.length)
- callback(win.id, newTabIds);
- });
- }
- if (tabUrls.length == 1)
- callback(win.id, newTabIds);
- });
+ for (var i = 0; i < win.tabs.length; i++)
+ newTabIds.push(win.tabs[i].id);
+
+ callback(win.id, newTabIds);
});
}
diff --git a/chrome/test/data/extensions/api_test/tabs/connect/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/connect/tabs_util.js
index cf7cf4f..9f13dd9 100644
--- a/chrome/test/data/extensions/api_test/tabs/connect/tabs_util.js
+++ b/chrome/test/data/extensions/api_test/tabs/connect/tabs_util.js
@@ -27,25 +27,16 @@ function setupWindow(tabUrls, callback) {
// At least one url must be specified.
// The |callback| should look like function(windowId, tabIds) {...}.
function createWindow(tabUrls, winOptions, callback) {
- winOptions["url"] = tabUrls[0];
+ winOptions["url"] = tabUrls;
chrome.windows.create(winOptions, function(win) {
- assertTrue(win.id > 0);
var newTabIds = [];
+ assertTrue(win.id > 0);
+ assertEq(tabUrls.length, win.tabs.length);
- // Create tabs and populate newTabIds array.
- chrome.tabs.getSelected(win.id, function (tab) {
- newTabIds.push(tab.id);
- for (var i = 1; i < tabUrls.length; i++) {
- chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]},
- function(tab){
- newTabIds.push(tab.id);
- if (newTabIds.length == tabUrls.length)
- callback(win.id, newTabIds);
- });
- }
- if (tabUrls.length == 1)
- callback(win.id, newTabIds);
- });
+ for (var i = 0; i < win.tabs.length; i++)
+ newTabIds.push(win.tabs[i].id);
+
+ callback(win.id, newTabIds);
});
}
diff --git a/chrome/test/data/extensions/api_test/tabs/on_removed/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/on_removed/tabs_util.js
index cf7cf4f..9f13dd9 100644
--- a/chrome/test/data/extensions/api_test/tabs/on_removed/tabs_util.js
+++ b/chrome/test/data/extensions/api_test/tabs/on_removed/tabs_util.js
@@ -27,25 +27,16 @@ function setupWindow(tabUrls, callback) {
// At least one url must be specified.
// The |callback| should look like function(windowId, tabIds) {...}.
function createWindow(tabUrls, winOptions, callback) {
- winOptions["url"] = tabUrls[0];
+ winOptions["url"] = tabUrls;
chrome.windows.create(winOptions, function(win) {
- assertTrue(win.id > 0);
var newTabIds = [];
+ assertTrue(win.id > 0);
+ assertEq(tabUrls.length, win.tabs.length);
- // Create tabs and populate newTabIds array.
- chrome.tabs.getSelected(win.id, function (tab) {
- newTabIds.push(tab.id);
- for (var i = 1; i < tabUrls.length; i++) {
- chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]},
- function(tab){
- newTabIds.push(tab.id);
- if (newTabIds.length == tabUrls.length)
- callback(win.id, newTabIds);
- });
- }
- if (tabUrls.length == 1)
- callback(win.id, newTabIds);
- });
+ for (var i = 0; i < win.tabs.length; i++)
+ newTabIds.push(win.tabs[i].id);
+
+ callback(win.id, newTabIds);
});
}