Bug 1003268 - [NFC] Implement onpeerfound. r=allstars.chh, r=smaug

This commit is contained in:
Krzysztof Mioduszewski 2014-10-30 02:08:00 -04:00
parent 05cedd6149
commit 0bae9eb769
6 changed files with 46 additions and 32 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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.

View File

@ -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;

View File

@ -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;