зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1650897 - Return the top window for extension popups. r=robwu,owlish
When using the `tabs.query` API in a popup (e.g. a browserAction popup) extensions expects to refer to the window where the popup is. On mobile we don't really have a concept of window in the way that extensions expect us to have, we also treat all tabs as belonging to separate tabs. To make the behavior of the extension API more consistent with desktop, we pretend that the popup belongs to whatever the top tab is at the time. Differential Revision: https://phabricator.services.mozilla.com/D82769
This commit is contained in:
Родитель
d8589ad28e
Коммит
51a1c98a33
|
@ -134,6 +134,16 @@ class WindowTracker extends WindowTrackerBase {
|
|||
this.progressListeners = new DefaultWeakMap(() => new WeakMap());
|
||||
}
|
||||
|
||||
getCurrentWindow(context) {
|
||||
// In GeckoView the popup is on a separate window so getCurrentWindow for
|
||||
// the popup should return whatever is the topWindow.
|
||||
// TODO: Bug 1651506 use context?.viewType === "popup" instead
|
||||
if (context?.currentWindow?.moduleManager.settings.isPopup) {
|
||||
return this.topWindow;
|
||||
}
|
||||
return super.getCurrentWindow(context);
|
||||
}
|
||||
|
||||
get topWindow() {
|
||||
return mobileWindowTracker.topWindow;
|
||||
}
|
||||
|
@ -461,6 +471,16 @@ class Window extends WindowBase {
|
|||
return this.window.document.hasFocus();
|
||||
}
|
||||
|
||||
isCurrentFor(context) {
|
||||
// In GeckoView the popup is on a separate window so the current window for
|
||||
// the popup is whatever is the topWindow.
|
||||
// TODO: Bug 1651506 use context?.viewType === "popup" instead
|
||||
if (context?.currentWindow?.moduleManager.settings.isPopup) {
|
||||
return mobileWindowTracker.topWindow == this.window;
|
||||
}
|
||||
return super.isCurrentFor(context);
|
||||
}
|
||||
|
||||
get top() {
|
||||
return this.window.screenY;
|
||||
}
|
||||
|
|
|
@ -139,6 +139,7 @@ skip-if = true #Bug 1628642
|
|||
[test_ext_streamfilter_processswitch.html]
|
||||
[test_ext_subframes_privileges.html]
|
||||
skip-if = os == 'android' || verify # bug 1489771
|
||||
[test_ext_tabs_query_popup.html]
|
||||
[test_ext_tabs_sendMessage.html]
|
||||
[test_ext_test.html]
|
||||
[test_ext_unlimitedStorage.html]
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Tabs create Test</title>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
|
||||
<script type="text/javascript" src="head.js"></script>
|
||||
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
"use strict";
|
||||
|
||||
async function test_query(query) {
|
||||
let extension = ExtensionTestUtils.loadExtension({
|
||||
manifest: {
|
||||
applications: {
|
||||
gecko: {
|
||||
id: "current-window@tests.mozilla.org",
|
||||
}
|
||||
},
|
||||
permissions: ["tabs"],
|
||||
browser_action: {
|
||||
default_popup: "popup.html",
|
||||
},
|
||||
},
|
||||
|
||||
useAddonManager: "permanent",
|
||||
|
||||
background: async function() {
|
||||
let query = await new Promise(resolve => {
|
||||
browser.test.onMessage.addListener(message => {
|
||||
resolve(message);
|
||||
});
|
||||
});
|
||||
let tab = await browser.tabs.create({ url: "http://www.example.com", active: true });
|
||||
browser.runtime.onMessage.addListener(message => {
|
||||
if (message === "popup-loaded") {
|
||||
browser.runtime.sendMessage({ tab, query });
|
||||
}
|
||||
});
|
||||
browser.test.withHandlingUserInput(() =>
|
||||
browser.browserAction.openPopup()
|
||||
);
|
||||
},
|
||||
|
||||
files: {
|
||||
"popup.html": `<!DOCTYPE html><meta charset="utf-8"><script src="popup.js"><\/script>`,
|
||||
"popup.js"() {
|
||||
browser.runtime.onMessage.addListener(async function({ tab, query }) {
|
||||
let tabs = await browser.tabs.query(query);
|
||||
browser.test.assertEq(tabs.length, 1, `Got one tab`);
|
||||
browser.test.assertEq(tabs[0].id, tab.id, "The tab is the right one");
|
||||
|
||||
// Create a new tab and verify that we still see the right result
|
||||
let newTab = await browser.tabs.create({ url: "http://www.example.com", active: true });
|
||||
tabs = await browser.tabs.query(query);
|
||||
browser.test.assertEq(tabs.length, 1, `Got one tab`);
|
||||
browser.test.assertEq(tabs[0].id, newTab.id, "Got the newly-created tab");
|
||||
|
||||
await browser.tabs.remove(newTab.id);
|
||||
|
||||
// Remove the tab and verify that we see the old tab
|
||||
tabs = await browser.tabs.query(query);
|
||||
browser.test.assertEq(tabs.length, 1, `Got one tab`);
|
||||
browser.test.assertEq(tabs[0].id, tab.id, "Got the tab that was active before");
|
||||
|
||||
// Cleanup
|
||||
await browser.tabs.remove(tab.id);
|
||||
|
||||
browser.test.notifyPass("tabs.query");
|
||||
});
|
||||
browser.runtime.sendMessage("popup-loaded");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await extension.startup();
|
||||
extension.sendMessage(query);
|
||||
await extension.awaitFinish("tabs.query");
|
||||
await extension.unload();
|
||||
}
|
||||
|
||||
add_task(function test_query_currentWindow_from_popup() {
|
||||
return test_query({ currentWindow: true, active: true });
|
||||
});
|
||||
|
||||
add_task(function test_query_lastActiveWindow_from_popup() {
|
||||
return test_query({ lastFocusedWindow: true, active: true });
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче