From 0bae9eb7697fdc2ea75cc0f138c67d6314dda34c Mon Sep 17 00:00:00 2001 From: Krzysztof Mioduszewski Date: Thu, 30 Oct 2014 02:08:00 -0400 Subject: [PATCH] Bug 1003268 - [NFC] Implement onpeerfound. r=allstars.chh, r=smaug --- dom/nfc/NfcContentHelper.js | 5 ++++- dom/nfc/gonk/Nfc.js | 30 ++++++++++-------------------- dom/nfc/gonk/nfc_consts.js | 1 + dom/nfc/nsINfcContentHelper.idl | 9 +++++---- dom/nfc/nsNfc.js | 22 +++++++++++++++------- dom/webidl/MozNFC.webidl | 11 +++++++++++ 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/dom/nfc/NfcContentHelper.js b/dom/nfc/NfcContentHelper.js index c4e3e815680..c5b64ca4d55 100644 --- a/dom/nfc/NfcContentHelper.js +++ b/dom/nfc/NfcContentHelper.js @@ -350,7 +350,10 @@ NfcContentHelper.prototype = { case "NFC:DOMEvent": switch (result.event) { case NFC.PEER_EVENT_READY: - this.eventTarget.notifyPeerReady(result.sessionToken); + this.eventTarget.notifyPeerFound(result.sessionToken, /* isPeerReady */ true); + break; + case NFC.PEER_EVENT_FOUND: + this.eventTarget.notifyPeerFound(result.sessionToken); break; case NFC.PEER_EVENT_LOST: this.eventTarget.notifyPeerLost(result.sessionToken); diff --git a/dom/nfc/gonk/Nfc.js b/dom/nfc/gonk/Nfc.js index 27e29140bda..c9a93171fd9 100644 --- a/dom/nfc/gonk/Nfc.js +++ b/dom/nfc/gonk/Nfc.js @@ -103,7 +103,6 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { // Manage registered Peer Targets peerTargets: {}, - currentPeer: null, eventTargets: [], @@ -192,9 +191,6 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { removePeerTarget: function removePeerTarget(target) { Object.keys(this.peerTargets).forEach((appId) => { if (this.peerTargets[appId] === target) { - if (this.currentPeer === target) { - this.currentPeer = null; - } delete this.peerTargets[appId]; } }); @@ -219,8 +215,8 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { removeEventTarget: function removeEventTarget(target) { let index = this.eventTargets.indexOf(target); - if (index != -1) { - delete this.eventTargets[index]; + if (index !== -1) { + this.eventTargets.splice(index, 1); } }, @@ -245,8 +241,6 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { return; } - // Remember the target that receives onpeerready. - this.currentPeer = target; this.notifyDOMEvent(target, {event: NFC.PEER_EVENT_READY, sessionToken: sessionToken}); }, @@ -266,17 +260,11 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { } }, - onPeerLost: function onPeerLost(sessionToken) { - if (!this.currentPeer) { - // The target is already killed. - return; + onPeerEvent: function onPeerEvent(eventType, sessionToken) { + for (let target of this.eventTargets) { + this.notifyDOMEvent(target, { event: eventType, + sessionToken: sessionToken }); } - - // For peerlost, the message is delievered to the target which - // onpeerready has been called before. - this.notifyDOMEvent(this.currentPeer, {event: NFC.PEER_EVENT_LOST, - sessionToken: sessionToken}); - this.currentPeer = null; }, /** @@ -544,7 +532,9 @@ Nfc.prototype = { let sessionId = message.sessionId; delete message.sessionId; - if (!SessionHelper.isP2PSession(sessionId)) { + if (SessionHelper.isP2PSession(sessionId)) { + gMessageManager.onPeerEvent(NFC.PEER_EVENT_FOUND, message.sessionToken); + } else { gMessageManager.onTagFound(message); } @@ -556,7 +546,7 @@ Nfc.prototype = { // Update the upper layers with a session token (alias) message.sessionToken = SessionHelper.getToken(message.sessionId); if (SessionHelper.isP2PSession(message.sessionId)) { - gMessageManager.onPeerLost(message.sessionToken); + gMessageManager.onPeerEvent(NFC.PEER_EVENT_LOST, message.sessionToken); } else { gMessageManager.onTagLost(message.sessionToken); } diff --git a/dom/nfc/gonk/nfc_consts.js b/dom/nfc/gonk/nfc_consts.js index e246abef401..152781465a0 100644 --- a/dom/nfc/gonk/nfc_consts.js +++ b/dom/nfc/gonk/nfc_consts.js @@ -90,6 +90,7 @@ this.PEER_EVENT_READY = 0x01; this.PEER_EVENT_LOST = 0x02; this.TAG_EVENT_FOUND = 0x03; this.TAG_EVENT_LOST = 0x04; +this.PEER_EVENT_FOUND = 0x05; // Allow this file to be imported via Components.utils.import(). this.EXPORTED_SYMBOLS = Object.keys(this); diff --git a/dom/nfc/nsINfcContentHelper.idl b/dom/nfc/nsINfcContentHelper.idl index 9f0abdd152d..16ccabf613e 100644 --- a/dom/nfc/nsINfcContentHelper.idl +++ b/dom/nfc/nsINfcContentHelper.idl @@ -13,7 +13,7 @@ interface nsINfcTagEvent : nsISupports readonly attribute nsIVariant techList; }; -[scriptable, uuid(8d77d653-f76a-4a21-ae3f-c4cc6074d8ec)] +[scriptable, uuid(2074503e-f590-4017-a45e-85d3ca766f41)] interface nsINfcDOMEventTarget : nsISupports { /** @@ -39,12 +39,13 @@ interface nsINfcDOMEventTarget : nsISupports void notifyTagLost(in DOMString sessionToken); /** - * Callback function used to notify peerready. - * + * Callback function used to notify peerfound/peerready. * @param sessionToken * SessionToken received from parent process + * @param isPeerReady + * Set to true to dispatch peerready instead of peerfound */ - void notifyPeerReady(in DOMString sessionToken); + void notifyPeerFound(in DOMString sessionToken, [optional] in boolean isPeerReady); /** * Callback function used to notify peerlost. diff --git a/dom/nfc/nsNfc.js b/dom/nfc/nsNfc.js index 82c2172acb1..9bfbc346e50 100644 --- a/dom/nfc/nsNfc.js +++ b/dom/nfc/nsNfc.js @@ -127,6 +127,7 @@ mozNfc.prototype = { this.defineEventHandlerGetterSetter("ontagfound"); this.defineEventHandlerGetterSetter("ontaglost"); this.defineEventHandlerGetterSetter("onpeerready"); + this.defineEventHandlerGetterSetter("onpeerfound"); this.defineEventHandlerGetterSetter("onpeerlost"); if (this._nfcContentHelper) { @@ -256,19 +257,22 @@ mozNfc.prototype = { this.__DOM_IMPL__.dispatchEvent(event); }, - notifyPeerReady: function notifyPeerReady(sessionToken) { + notifyPeerFound: function notifyPeerFound(sessionToken, isPeerReady) { if (this.hasDeadWrapper()) { dump("this._window or this.__DOM_IMPL__ is a dead wrapper."); return; } - this.session = sessionToken; + if (!this.checkPermissions(["nfc-write"])) { + return; + } - debug("fire onpeerready sessionToken : " + sessionToken); - let eventData = { - "peer":this.getNFCPeer(sessionToken) - }; - let event = new this._window.MozNFCPeerEvent("peerready", eventData); + this.session = sessionToken; + let eventData = { "peer": this.getNFCPeer(sessionToken) }; + let type = (isPeerReady) ? "peerready" : "peerfound"; + + debug("fire on" + type + " " + sessionToken); + let event = new this._window.MozNFCPeerEvent(type, eventData); this.__DOM_IMPL__.dispatchEvent(event); }, @@ -278,6 +282,10 @@ mozNfc.prototype = { return; } + if (!this.checkPermissions(["nfc-write"])) { + return; + } + if (sessionToken != this.session) { dump("Unpaired session for notifyPeerLost." + sessionToken); return; diff --git a/dom/webidl/MozNFC.webidl b/dom/webidl/MozNFC.webidl index ace801ae4de..479518801e7 100644 --- a/dom/webidl/MozNFC.webidl +++ b/dom/webidl/MozNFC.webidl @@ -65,6 +65,17 @@ interface MozNFC : EventTarget { */ [CheckPermissions="nfc-write"] attribute EventHandler onpeerready; + + /** + * This event will be fired when a NFCPeer is detected. + */ + [CheckPermissions="nfc-write"] + attribute EventHandler onpeerfound; + + /** + * This event will be fired when NFCPeer, earlier detected in onpeerready + * or onpeerfound, moves out of range. + */ [CheckPermissions="nfc-write"] attribute EventHandler onpeerlost;