Bug 1069177 - B2G NFC: rename nsINFCPeerEventListener to nsINfcDOMEventTarget. r=dimi

This commit is contained in:
Yoshi Huang 2014-09-24 14:52:04 +08:00
parent 6d320d2bd9
commit 2ec59f6b64
4 changed files with 58 additions and 23 deletions

View File

@ -55,7 +55,7 @@ const NFC_IPC_MSG_NAMES = [
"NFC:ConnectResponse",
"NFC:CloseResponse",
"NFC:CheckP2PRegistrationResponse",
"NFC:PeerEvent",
"NFC:DOMEvent",
"NFC:NotifySendFileStatusResponse",
"NFC:ConfigResponse"
];
@ -96,7 +96,7 @@ NfcContentHelper.prototype = {
}),
_requestMap: null,
peerEventListener: null,
eventTarget: null,
encodeNDEFRecords: function encodeNDEFRecords(records) {
let encodedRecords = [];
@ -257,8 +257,9 @@ NfcContentHelper.prototype = {
});
},
registerPeerEventListener: function registerPeerEventListener(listener) {
this.peerEventListener = listener;
registerEventTarget: function registerEventTarget(target) {
this.eventTarget = target;
cpmm.sendAsyncMessage("NFC:AddEventTarget");
},
registerTargetForPeerReady: function registerTargetForPeerReady(window, appId) {
@ -412,13 +413,13 @@ NfcContentHelper.prototype = {
this.fireRequestSuccess(atob(result.requestId), result);
}
break;
case "NFC:PeerEvent":
case "NFC:DOMEvent":
switch (result.event) {
case NFC.NFC_PEER_EVENT_READY:
this.peerEventListener.notifyPeerReady(result.sessionToken);
this.eventTarget.notifyPeerReady(result.sessionToken);
break;
case NFC.NFC_PEER_EVENT_LOST:
this.peerEventListener.notifyPeerLost(result.sessionToken);
this.eventTarget.notifyPeerLost(result.sessionToken);
break;
}
break;

View File

@ -47,6 +47,10 @@ const NFC_CONTRACTID = "@mozilla.org/nfc;1";
const NFC_CID =
Components.ID("{2ff24790-5e74-11e1-b86c-0800200c9a66}");
const NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES = [
"NFC:AddEventTarget"
];
const NFC_IPC_MSG_NAMES = [
"NFC:CheckSessionToken"
];
@ -95,6 +99,8 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
peerTargets: {},
currentPeer: null,
eventTargets: [],
init: function init(nfc) {
this.nfc = nfc;
@ -113,6 +119,10 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
_registerMessageListeners: function _registerMessageListeners() {
ppmm.addMessageListener("child-process-shutdown", this);
for (let message of NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES) {
ppmm.addMessageListener(message, this);
}
for (let message of NFC_IPC_MSG_NAMES) {
ppmm.addMessageListener(message, this);
}
@ -133,6 +143,10 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
_unregisterMessageListeners: function _unregisterMessageListeners() {
ppmm.removeMessageListener("child-process-shutdown", this);
for (let message of NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES) {
ppmm.removeMessageListener(message, this);
}
for (let message of NFC_IPC_MSG_NAMES) {
ppmm.removeMessageListener(message, this);
}
@ -175,16 +189,28 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
});
},
notifyPeerEvent: function notifyPeerEvent(target, event, sessionToken) {
notifyDOMEvent: function notifyDOMEvent(target, options) {
if (!target) {
dump("invalid target");
return;
}
target.sendAsyncMessage("NFC:PeerEvent", {
event: event,
sessionToken: sessionToken
});
target.sendAsyncMessage("NFC:DOMEvent", options);
},
addEventTarget: function addEventTarget(target) {
if (this.eventTargets.indexOf(target) != -1) {
return;
}
this.eventTargets.push(target);
},
removeEventTarget: function removeEventTarget(target) {
let index = this.eventTargets.indexOf(target);
if (index != -1) {
delete this.eventTargets[index];
}
},
checkP2PRegistration: function checkP2PRegistration(message) {
@ -210,7 +236,8 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
// Remember the target that receives onpeerready.
this.currentPeer = target;
this.notifyPeerEvent(target, NFC.NFC_PEER_EVENT_READY, sessionToken);
this.notifyDOMEvent(target, {event: NFC.NFC_PEER_EVENT_READY,
sessionToken: sessionToken});
},
onPeerLost: function onPeerLost(sessionToken) {
@ -221,7 +248,8 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
// For peerlost, the message is delievered to the target which
// onpeerready has been called before.
this.notifyPeerEvent(this.currentPeer, NFC.NFC_PEER_EVENT_LOST, sessionToken);
this.notifyDOMEvent(this.currentPeer, {event: NFC.NFC_PEER_EVENT_LOST,
sessionToken: sessionToken});
this.currentPeer = null;
},
@ -235,10 +263,12 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
if (message.name == "child-process-shutdown") {
this.removePeerTarget(message.target);
this.nfc.removeTarget(message.target);
this.removeEventTarget(msg.target);
return null;
}
if (NFC_IPC_MSG_NAMES.indexOf(message.name) != -1) {
if (NFC_IPC_MSG_NAMES.indexOf(message.name) != -1 ||
NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES.indexOf(message.name) != -1 ) {
// Do nothing.
} else if (NFC_IPC_READ_PERM_MSG_NAMES.indexOf(message.name) != -1) {
if (!message.target.assertPermission("nfc-read")) {
@ -264,6 +294,9 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
}
switch (message.name) {
case "NFC:AddEventTarget":
this.addEventTarget(message.target);
return null;
case "NFC:CheckSessionToken":
if (!SessionHelper.isValidToken(message.data.sessionToken)) {
debug("Received invalid Session Token: " + message.data.sessionToken);
@ -472,6 +505,7 @@ Nfc.prototype = {
// Update the upper layers with a session token (alias)
message.sessionToken =
SessionHelper.registerSession(message.sessionId, message.techList);
// Do not expose the actual session to the content
delete message.sessionId;

View File

@ -7,8 +7,8 @@
interface nsIVariant;
[scriptable, uuid(57fc2998-1058-4fd5-8dd9-0e303218d5fd)]
interface nsINfcPeerEventListener : nsISupports
[scriptable, uuid(e81cc1ac-6f0b-4581-a9fb-7ee47ed0158e)]
interface nsINfcDOMEventTarget : nsISupports
{
/**
* Callback function used to notify peerready.
@ -27,7 +27,7 @@ interface nsINfcPeerEventListener : nsISupports
void notifyPeerLost(in DOMString sessionToken);
};
[scriptable, uuid(9a41d969-3375-4933-814e-2da781c8f691)]
[scriptable, uuid(7eaf4c31-e1d1-422e-aa55-181f4eb156b0)]
interface nsINfcContentHelper : nsISupports
{
const long NFC_EVENT_PEER_READY = 0x01;
@ -65,11 +65,11 @@ interface nsINfcContentHelper : nsISupports
in DOMString sessionToken);
/**
* Register the peer event listener.
* Register the event target.
*
* @param listener An instance of the nsINfcPeerEventListener.
* @param target An instance of the nsINfcDOMEventTarget.
*/
void registerPeerEventListener(in nsINfcPeerEventListener listener);
void registerEventTarget(in nsINfcDOMEventTarget target);
/**
* Register the given application id with Chrome process

View File

@ -138,7 +138,7 @@ function mozNfc() {
debug("No NFC support.")
}
this._nfcContentHelper.registerPeerEventListener(this);
this._nfcContentHelper.registerEventTarget(this);
}
mozNfc.prototype = {
_nfcContentHelper: null,
@ -289,7 +289,7 @@ mozNfc.prototype = {
contractID: "@mozilla.org/navigatorNfc;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsINfcPeerEventListener]),
Ci.nsINfcDOMEventTarget]),
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);