Bug 1082445: B2G NFC: notifiy peer/taglost when app goes to background. r=smaug, dimi

This commit is contained in:
Yoshi Huang 2014-11-19 12:32:47 +08:00
parent 087c7db00c
commit 0282ce8669

View File

@ -303,6 +303,9 @@ MozNFCImpl.prototype = {
return;
}
this.eventService.addSystemEventListener(this._window, "visibilitychange",
this, /* useCapture */false);
let tagImpl = new MozNFCTagImpl(this._window, sessionToken, event);
let tag = this._window.MozNFCTag._create(this._window, tagImpl);
this.nfcTag = tag;
@ -342,6 +345,12 @@ MozNFCImpl.prototype = {
return;
}
// Remove system event listener only when tag and peer are both lost.
if (!this.nfcPeer) {
this.eventService.removeSystemEventListener(this._window, "visibilitychange",
this, /* useCapture */false);
}
this.nfcTag.isLost = true;
this.nfcTag = null;
@ -366,6 +375,9 @@ MozNFCImpl.prototype = {
return;
}
this.eventService.addSystemEventListener(this._window, "visibilitychange",
this, /* useCapture */false);
this.nfcPeer = this._createNFCPeer(sessionToken);
let eventData = { "peer": this.nfcPeer };
let type = (isPeerReady) ? "peerready" : "peerfound";
@ -390,6 +402,12 @@ MozNFCImpl.prototype = {
return;
}
// Remove system event listener only when tag and peer are both lost.
if (!this.nfcTag) {
this.eventService.removeSystemEventListener(this._window, "visibilitychange",
this, /* useCapture */false);
}
this.nfcPeer.isLost = true;
this.nfcPeer = null;
@ -398,6 +416,22 @@ MozNFCImpl.prototype = {
this.__DOM_IMPL__.dispatchEvent(event);
},
handleEvent: function handleEvent (event) {
if (!this._window.document.hidden) {
return;
}
if (this.nfcTag) {
debug("handleEvent notifyTagLost");
this.notifyTagLost(this.nfcTag.session);
}
if (this.nfcPeer) {
debug("handleEvent notifyPeerLost");
this.notifyPeerLost(this.nfcPeer.session);
}
},
checkPermissions: function checkPermissions(perms) {
let principal = this._window.document.nodePrincipal;
for (let perm of perms) {
@ -421,7 +455,8 @@ MozNFCImpl.prototype = {
contractID: "@mozilla.org/navigatorNfc;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsINfcEventListener]),
Ci.nsINfcEventListener,
Ci.nsIDOMEventListener]),
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTagImpl,