Bug 1165841 - Part 5: remove listener when window is destroyed. r=dimi

This commit is contained in:
Yoshi Huang 2015-05-20 15:01:16 +08:00
parent 4bf759585c
commit 82d3354929
4 changed files with 47 additions and 3 deletions

View File

@ -194,6 +194,12 @@ NfcContentHelper.prototype = {
cpmm.sendAsyncMessage("NFC:AddEventListener", { tabId: tabId });
},
removeEventListener: function removeEventListener(tabId) {
delete this.eventListeners[tabId];
cpmm.sendAsyncMessage("NFC:RemoveEventListener", { tabId: tabId });
},
registerTargetForPeerReady: function registerTargetForPeerReady(appId) {
cpmm.sendAsyncMessage("NFC:RegisterPeerReadyTarget", { appId: appId });
},

View File

@ -57,6 +57,7 @@ const NFC_CID =
const NFC_IPC_MSG_ENTRIES = [
{ permission: null,
messages: ["NFC:AddEventListener",
"NFC:RemoveEventListener",
"NFC:QueryInfo",
"NFC:CallDefaultFoundHandler",
"NFC:CallDefaultLostHandler"] },
@ -256,12 +257,16 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
removeEventListener: function removeEventListener(target) {
for (let id in this.eventListeners) {
if (target == this.eventListeners[id]) {
delete this.eventListeners[id];
this.removeEventListenerById(id);
break;
}
}
},
removeEventListenerById: function removeEventListenerById(id) {
delete this.eventListeners[id];
},
checkP2PRegistration: function checkP2PRegistration(message) {
let target = this.peerTargets[message.data.appId];
let sessionToken = SessionHelper.getCurrentP2PToken();
@ -385,6 +390,9 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
case "NFC:AddEventListener":
this.addEventListener(message.target, message.data.tabId);
return null;
case "NFC:RemoveEventListener":
this.removeEventListenerById(message.data.tabId);
return null;
case "NFC:RegisterPeerReadyTarget":
this.registerPeerReadyTarget(message.target, message.data.appId);
return null;

View File

@ -124,7 +124,7 @@ interface nsINfcBrowserAPI : nsISupports
in boolean isFocus);
};
[scriptable, uuid(f098c114-b21f-43f5-9f53-0c22b4f194eb)]
[scriptable, uuid(75f0c8c0-2e5a-491f-a75d-4f3849c4feec)]
interface nsINfcContentHelper : nsISupports
{
/**
@ -236,6 +236,14 @@ interface nsINfcContentHelper : nsISupports
*/
void addEventListener(in nsINfcEventListener listener, in uint64_t tabId);
/**
* Remove event listener.
*
* @param tabId
* The tabId provided in addEventListener.
*/
void removeEventListener(in uint64_t tabId);
/**
* Register the given application id with parent process
*

View File

@ -345,6 +345,7 @@ MozNFCImpl.prototype = {
_nfcContentHelper: null,
window: null,
_tabId: null,
_innerWindowId: null,
_rfState: null,
_contentObj: null,
nfcPeer: null,
@ -354,12 +355,19 @@ MozNFCImpl.prototype = {
init: function init(aWindow) {
debug("MozNFCImpl init called");
this.window = aWindow;
let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
this._innerWindowId = util.currentInnerWindowID;
this.defineEventHandlerGetterSetter("ontagfound");
this.defineEventHandlerGetterSetter("ontaglost");
this.defineEventHandlerGetterSetter("onpeerready");
this.defineEventHandlerGetterSetter("onpeerfound");
this.defineEventHandlerGetterSetter("onpeerlost");
Services.obs.addObserver(this, "inner-window-destroyed",
/* weak-ref */ false);
if (this._nfcContentHelper) {
this._tabId = this.getTabId(aWindow);
this._nfcContentHelper.addEventListener(this, this._tabId);
@ -441,6 +449,19 @@ MozNFCImpl.prototype = {
return this._rfState != RFState.IDLE;
},
observe: function observe(subject, topic, data) {
if (topic !== "inner-window-destroyed") {
return;
}
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
if (wId != this._innerWindowId) {
return;
}
this._nfcContentHelper.removeEventListener(this._tabId);
},
defineEventHandlerGetterSetter: function defineEventHandlerGetterSetter(name) {
Object.defineProperty(this, name, {
get: function get() {
@ -699,7 +720,8 @@ MozNFCImpl.prototype = {
contractID: "@mozilla.org/nfc/manager;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsINfcEventListener]),
Ci.nsINfcEventListener,
Ci.nsIObserver]),
};
function NFCSendFileWrapper() {