Bug 1286526 - [webext] Fix shutdown issues on empty addon with a valid manifest. r=aswan

MozReview-Commit-ID: 30MryAPYBv0

--HG--
extra : rebase_source : 3cce09a704263c13eda837a7b4a0063deaa55268
This commit is contained in:
Luca Greco 2016-07-13 13:12:59 +02:00
Родитель 73fad24130
Коммит d7252a1c8d
5 изменённых файлов: 85 добавлений и 10 удалений

Просмотреть файл

@ -456,7 +456,7 @@ ParentAPIManager.init();
// For extensions that have called setUninstallURL(), send an event // For extensions that have called setUninstallURL(), send an event
// so the browser can display the URL. // so the browser can display the URL.
let UninstallObserver = { var UninstallObserver = {
initialized: false, initialized: false,
init: function() { init: function() {
@ -466,6 +466,13 @@ let UninstallObserver = {
} }
}, },
uninit: function() {
if (this.initialized) {
AddonManager.removeAddonListener(this);
this.initialized = false;
}
},
onUninstalling: function(addon) { onUninstalling: function(addon) {
let extension = GlobalManager.extensionMap.get(addon.id); let extension = GlobalManager.extensionMap.get(addon.id);
if (extension) { if (extension) {
@ -479,11 +486,13 @@ GlobalManager = {
// Map[extension ID -> Extension]. Determines which extension is // Map[extension ID -> Extension]. Determines which extension is
// responsible for content under a particular extension ID. // responsible for content under a particular extension ID.
extensionMap: new Map(), extensionMap: new Map(),
initialized: false,
init(extension) { init(extension) {
if (this.extensionMap.size == 0) { if (this.extensionMap.size == 0) {
Services.obs.addObserver(this, "content-document-global-created", false); Services.obs.addObserver(this, "content-document-global-created", false);
UninstallObserver.init(); UninstallObserver.init();
this.initialized = true;
} }
this.extensionMap.set(extension.id, extension); this.extensionMap.set(extension.id, extension);
@ -492,8 +501,10 @@ GlobalManager = {
uninit(extension) { uninit(extension) {
this.extensionMap.delete(extension.id); this.extensionMap.delete(extension.id);
if (this.extensionMap.size == 0) { if (this.extensionMap.size == 0 && this.initialized) {
Services.obs.removeObserver(this, "content-document-global-created"); Services.obs.removeObserver(this, "content-document-global-created");
UninstallObserver.uninit();
this.initialized = false;
} }
}, },

Просмотреть файл

@ -83,11 +83,13 @@ extensions.on("startup", (type, extension) => {
}); });
extensions.on("shutdown", (type, extension) => { extensions.on("shutdown", (type, extension) => {
for (let alarm of alarmsMap.get(extension).values()) { if (alarmsMap.has(extension)) {
alarm.clear(); for (let alarm of alarmsMap.get(extension).values()) {
alarm.clear();
}
alarmsMap.delete(extension);
alarmCallbacksMap.delete(extension);
} }
alarmsMap.delete(extension);
alarmCallbacksMap.delete(extension);
}); });
/* eslint-enable mozilla/balanced-listeners */ /* eslint-enable mozilla/balanced-listeners */

Просмотреть файл

@ -81,10 +81,12 @@ extensions.on("startup", (type, extension) => {
}); });
extensions.on("shutdown", (type, extension) => { extensions.on("shutdown", (type, extension) => {
for (let notification of notificationsMap.get(extension).values()) { if (notificationsMap.has(extension)) {
notification.clear(); for (let notification of notificationsMap.get(extension).values()) {
notification.clear();
}
notificationsMap.delete(extension);
} }
notificationsMap.delete(extension);
}); });
/* eslint-enable mozilla/balanced-listeners */ /* eslint-enable mozilla/balanced-listeners */

Просмотреть файл

@ -22,6 +22,7 @@ skip-if = (os == 'android') # browser.tabs is undefined. Bug 1258975 on android.
[test_chrome_ext_trustworthy_origin.html] [test_chrome_ext_trustworthy_origin.html]
[test_chrome_ext_webnavigation_resolved_urls.html] [test_chrome_ext_webnavigation_resolved_urls.html]
skip-if = (os == 'android') # browser.tabs is undefined. Bug 1258975 on android. skip-if = (os == 'android') # browser.tabs is undefined. Bug 1258975 on android.
[test_chrome_ext_shutdown_cleanup.html]
[test_chrome_native_messaging_paths.html] [test_chrome_native_messaging_paths.html]
skip-if = os != "mac" && os != "linux" skip-if = os != "mac" && os != "linux"
[test_ext_cookies_expiry.html] [test_ext_cookies_expiry.html]

Просмотреть файл

@ -0,0 +1,59 @@
<!DOCTYPE HTML>
<html>
<head>
<title>WebExtension test</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://testing-common/TestUtils.jsm");
const {
GlobalManager,
UninstallObserver,
} = Cu.import("resource://gre/modules/Extension.jsm");
/* eslint-disable mozilla/balanced-listeners */
add_task(function* testShutdownCleanup() {
is(GlobalManager.initialized, false,
"GlobalManager start as not initialized");
is(UninstallObserver.initialized, false,
"UninstallObserver start as not initialized");
let extension = ExtensionTestUtils.loadExtension({
background: "new " + function() {
browser.test.notifyPass("background page loaded");
},
});
yield extension.startup();
yield extension.awaitFinish("background page loaded");
is(GlobalManager.initialized, true,
"GlobalManager has been initialized once an extension is started");
is(UninstallObserver.initialized, true,
"UninstallObserver has been initialized once an extension is started");
yield extension.unload();
is(GlobalManager.initialized, false,
"GlobalManager has been uninitialized once all the webextensions have been stopped");
is(UninstallObserver.initialized, false,
"UninstallObserver has been uninitialized once all the webextensions have been stopped");
});
</script>
</body>
</html>