Commit f01542da authored by rob's avatar rob Committed by Commit bot

Allow "url" to be an array of URL patterns

And also add error validation to the tabs.query method (while we're at it).

BUG=128924
TEST=ExtensionApiTest.TabQuery

Review URL: https://codereview.chromium.org/569803004

Cr-Commit-Position: refs/heads/master@{#295623}
parent 6a0a688e
...@@ -802,12 +802,21 @@ bool TabsQueryFunction::RunSync() { ...@@ -802,12 +802,21 @@ bool TabsQueryFunction::RunSync() {
bool loading = params->query_info.status == bool loading = params->query_info.status ==
tabs::Query::Params::QueryInfo::STATUS_LOADING; tabs::Query::Params::QueryInfo::STATUS_LOADING;
// It is o.k. to use URLPattern::SCHEME_ALL here because this function does URLPatternSet url_patterns;
// not grant access to the content of the tabs, only to seeing their URLs and if (params->query_info.url.get()) {
// meta data. std::vector<std::string> url_pattern_strings;
URLPattern url_pattern(URLPattern::SCHEME_ALL, "<all_urls>"); if (params->query_info.url->as_string)
if (params->query_info.url.get()) url_pattern_strings.push_back(*params->query_info.url->as_string);
url_pattern = URLPattern(URLPattern::SCHEME_ALL, *params->query_info.url); else if (params->query_info.url->as_strings)
url_pattern_strings.swap(*params->query_info.url->as_strings);
// It is o.k. to use URLPattern::SCHEME_ALL here because this function does
// not grant access to the content of the tabs, only to seeing their URLs
// and meta data.
if (!url_patterns.Populate(url_pattern_strings, URLPattern::SCHEME_ALL,
true, &error_)) {
return false;
}
}
std::string title; std::string title;
if (params->query_info.title.get()) if (params->query_info.title.get())
...@@ -893,7 +902,8 @@ bool TabsQueryFunction::RunSync() { ...@@ -893,7 +902,8 @@ bool TabsQueryFunction::RunSync() {
base::UTF8ToUTF16(title))) base::UTF8ToUTF16(title)))
continue; continue;
if (!url_pattern.MatchesURL(web_contents->GetURL())) if (!url_patterns.is_empty() &&
!url_patterns.MatchesURL(web_contents->GetURL()))
continue; continue;
if (loading_status_set && loading != web_contents->IsLoading()) if (loading_status_set && loading != web_contents->IsLoading())
......
...@@ -374,9 +374,12 @@ ...@@ -374,9 +374,12 @@
"description": "Match page titles against a pattern." "description": "Match page titles against a pattern."
}, },
"url": { "url": {
"type": "string", "choices": [
{"type": "string"},
{"type": "array", "items": {"type": "string"}}
],
"optional": true, "optional": true,
"description": "Match tabs against a <a href='match_patterns'>URL pattern</a>. Note that fragment identifiers are not matched." "description": "Match tabs against one or more <a href='match_patterns'>URL patterns</a>. Note that fragment identifiers are not matched."
}, },
"windowId": { "windowId": {
"type": "integer", "type": "integer",
......
...@@ -11,7 +11,7 @@ var active_and_window_tabs = []; ...@@ -11,7 +11,7 @@ var active_and_window_tabs = [];
chrome.test.runTests([ chrome.test.runTests([
function setup() { function setup() {
var tabs = ['http://example.org/a.html', 'http://google.com']; var tabs = ['http://example.org/a.html', 'http://www.google.com/favicon.ico'];
chrome.windows.create({url: tabs}, pass(function(window) { chrome.windows.create({url: tabs}, pass(function(window) {
assertEq(2, window.tabs.length); assertEq(2, window.tabs.length);
testWindowId = window.id; testWindowId = window.id;
...@@ -125,6 +125,21 @@ chrome.test.runTests([ ...@@ -125,6 +125,21 @@ chrome.test.runTests([
})); }));
}, },
function queryUrlAsArray() {
chrome.tabs.query({url: ["http://*.example.org/*"]}, pass(function(tabs) {
assertEq(1, tabs.length);
assertEq("http://example.org/a.html", tabs[0].url);
}));
},
function queryUrlAsArray2() {
chrome.tabs.query({url: ["http://*.example.org/*", "*://*.google.com/*"]}, pass(function(tabs) {
assertEq(2, tabs.length);
assertEq("http://example.org/a.html", tabs[0].url);
assertEq("http://www.google.com/favicon.ico", tabs[1].url);
}));
},
function queryStatus() { function queryStatus() {
chrome.tabs.query({status: "complete"}, pass(function(tabs) { chrome.tabs.query({status: "complete"}, pass(function(tabs) {
for (var x = 0; x < tabs.length; x++) for (var x = 0; x < tabs.length; x++)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment