Skip to content

Commit

Permalink
Bug 1637431 - Gives meaningful tabs.query error message. r=robwu,geck…
Browse files Browse the repository at this point in the history
…oview-reviewers,agi

***
Bug 1637431 - Gives meaningful tabs.query error message. r=robwu

Differential Revision: https://phabricator.services.mozilla.com/D78466
  • Loading branch information
DeepikaKaranji committed Jun 16, 2020
1 parent fa3acc4 commit 1d8e13f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
12 changes: 0 additions & 12 deletions browser/components/extensions/parent/ext-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,18 +933,6 @@ this.tabs = class extends ExtensionAPI {
});
}
}

queryInfo = Object.assign({}, queryInfo);

if (queryInfo.url !== null) {
queryInfo.url = new MatchPatternSet([].concat(queryInfo.url), {
restrictSchemes: false,
});
}
if (queryInfo.title !== null) {
queryInfo.title = new MatchGlob(queryInfo.title);
}

return Array.from(tabManager.query(queryInfo, context), tab =>
tab.convert()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,35 @@ add_task(async function testQueryWithoutURLOrTitlePermissions() {
await extension.unload();
});

add_task(async function testInvalidUrl() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs"],
},
async background() {
await browser.test.assertRejects(
browser.tabs.query({ url: "http://test1.net" }),
"Invalid url pattern: http://test1.net",
"Expected url to match pattern"
);
await browser.test.assertRejects(
browser.tabs.query({ url: ["test2"] }),
"Invalid url pattern: test2",
"Expected an array with an invalid match pattern"
);
await browser.test.assertRejects(
browser.tabs.query({ url: ["http://www.bbc.com/", "test3"] }),
"Invalid url pattern: test3",
"Expected an array with an invalid match pattern"
);
browser.test.notifyPass("testInvalidUrl");
},
});
await extension.startup();
await extension.awaitFinish("testInvalidUrl");
await extension.unload();
});

add_task(async function test_query_index() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
Expand Down
12 changes: 0 additions & 12 deletions mobile/android/components/extensions/ext-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,6 @@ this.tabs = class extends ExtensionAPI {
});
}
}

queryInfo = Object.assign({}, queryInfo);

if (queryInfo.url !== null) {
queryInfo.url = new MatchPatternSet([].concat(queryInfo.url), {
restrictSchemes: false,
});
}
if (queryInfo.title !== null) {
queryInfo.title = new MatchGlob(queryInfo.title);
}

return Array.from(tabManager.query(queryInfo, context), tab =>
tab.convert()
);
Expand Down
17 changes: 17 additions & 0 deletions toolkit/components/extensions/ExtensionUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ function getMessageManager(target) {
function flushJarCache(jarPath) {
Services.obs.notifyObservers(null, "flush-cache-entry", jarPath);
}
function parseMatchPatterns(patterns, options) {
try {
return new MatchPatternSet(patterns, options);
} catch (e) {
let pattern;
for (pattern of patterns) {
try {
new MatchPattern(pattern, options);
} catch (e) {
throw new ExtensionError(`Invalid url pattern: ${pattern}`);
}
}
// Unexpectedly MatchPatternSet threw, but MatchPattern did not.
throw e;
}
}

var ExtensionUtils = {
flushJarCache,
Expand All @@ -295,6 +311,7 @@ var ExtensionUtils = {
getUniqueId,
filterStack,
getWinUtils,
parseMatchPatterns,
promiseDocumentIdle,
promiseDocumentLoaded,
promiseDocumentReady,
Expand Down
16 changes: 16 additions & 0 deletions toolkit/components/extensions/parent/ext-tabs-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var {
DefaultWeakMap,
ExtensionError,
getWinUtils,
parseMatchPatterns,
} = ExtensionUtils;

var { defineLazyGetter } = ExtensionCommon;
Expand Down Expand Up @@ -1962,6 +1963,21 @@ class TabManagerBase {
* @returns {Iterator<TabBase>}
*/
*query(queryInfo = null, context = null) {
if (queryInfo) {
if (queryInfo.url !== null) {
queryInfo.url = parseMatchPatterns([].concat(queryInfo.url), {
restrictSchemes: false,
});
}

if (queryInfo.title !== null) {
try {
queryInfo.title = new MatchGlob(queryInfo.title);
} catch (e) {
throw new ExtensionError(`Invalid title: ${queryInfo.title}`);
}
}
}
function* candidates(windowWrapper) {
if (queryInfo) {
let { active, highlighted, index } = queryInfo;
Expand Down

0 comments on commit 1d8e13f

Please sign in to comment.