From 47ce7a8a57bbbe962bcbc5ba051d9857049a3561 Mon Sep 17 00:00:00 2001 From: Dimi Lee Date: Tue, 25 Nov 2014 15:10:34 +0800 Subject: [PATCH] Bug 1095322 - Child process should know current rf state of NFC HW. r=allstars.chh --- dom/nfc/NfcContentHelper.js | 12 ++++++++++++ dom/nfc/gonk/Nfc.js | 23 ++++++++++++++++++----- dom/nfc/gonk/nfc_consts.js | 1 + dom/nfc/nsINfcContentHelper.idl | 17 +++++++++++++++-- dom/nfc/nsNfc.js | 6 ++++++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/dom/nfc/NfcContentHelper.js b/dom/nfc/NfcContentHelper.js index 9a2a938f2713..362a7ae8311f 100644 --- a/dom/nfc/NfcContentHelper.js +++ b/dom/nfc/NfcContentHelper.js @@ -89,6 +89,7 @@ NfcContentHelper.prototype = { _window: null, _requestMap: null, + _rfState: null, eventListener: null, init: function init(aWindow) { @@ -107,6 +108,13 @@ NfcContentHelper.prototype = { updateDebug(); }; } + + let info = cpmm.sendSyncMessage("NFC:QueryInfo")[0]; + this._rfState = info.rfState; + }, + + queryRFState: function queryRFState() { + return this._rfState; }, encodeNDEFRecords: function encodeNDEFRecords(records) { @@ -319,6 +327,10 @@ NfcContentHelper.prototype = { case NFC.TAG_EVENT_LOST: this.eventListener.notifyTagLost(result.sessionToken); break; + case NFC.RF_EVENT_STATE_CHANGE: + this._rfState = result.rfState; + this.eventListener.notifyRFStateChange(this._rfState); + break; } break; } diff --git a/dom/nfc/gonk/Nfc.js b/dom/nfc/gonk/Nfc.js index 2e5f3d4b24bd..55059fb4fa8b 100644 --- a/dom/nfc/gonk/Nfc.js +++ b/dom/nfc/gonk/Nfc.js @@ -59,7 +59,8 @@ const NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES = [ ]; const NFC_IPC_MSG_NAMES = [ - "NFC:CheckSessionToken" + "NFC:CheckSessionToken", + "NFC:QueryInfo" ]; const NFC_IPC_READ_PERM_MSG_NAMES = [ @@ -268,6 +269,13 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () { } }, + onRFStateChange: function onRFStateChange(rfState) { + for (let target of this.eventListeners) { + this.notifyDOMEvent(target, { event: NFC.RF_EVENT_STATE_CHANGE, + rfState: rfState}); + } + }, + /** * nsIMessageListener interface methods. */ @@ -557,11 +565,12 @@ Nfc.prototype = { this.notifyHCIEventTransaction(message); break; case "ChangeRFStateResponse": + this.sendNfcResponse(message); + if (!message.errorMsg) { this.rfState = message.rfState; + gMessageManager.onRFStateChange(this.rfState); } - - this.sendNfcResponse(message); break; case "ConnectResponse": // Fall through. case "CloseResponse": @@ -602,13 +611,15 @@ Nfc.prototype = { receiveMessage: function receiveMessage(message) { let isRFAPI = message.name == "NFC:ChangeRFState"; let isSendFile = message.name == "NFC:SendFile"; - if (!isRFAPI && (this.rfState != NFC.NFC_RF_STATE_DISCOVERY)) { + let isInfoAPI = message.name == "NFC:QueryInfo"; + + if (!isRFAPI && !isInfoAPI && (this.rfState != NFC.NFC_RF_STATE_DISCOVERY)) { debug("NFC is not enabled. current rfState:" + this.rfState); this.sendNfcErrorResponse(message, NFC.NFC_GECKO_ERROR_NOT_ENABLED); return null; } - if (!isRFAPI && !isSendFile) { + if (!isRFAPI && !isSendFile && !isInfoAPI) { // Update the current sessionId before sending to the NFC service. message.data.sessionId = SessionHelper.getId(message.data.sessionToken); } @@ -647,6 +658,8 @@ Nfc.prototype = { gSystemMessenger.broadcastMessage("nfc-manager-send-file", message.data); break; + case "NFC:QueryInfo": + return {rfState: this.rfState}; default: debug("UnSupported : Message Name " + message.name); return null; diff --git a/dom/nfc/gonk/nfc_consts.js b/dom/nfc/gonk/nfc_consts.js index 097357ec1299..41f116e836a7 100644 --- a/dom/nfc/gonk/nfc_consts.js +++ b/dom/nfc/gonk/nfc_consts.js @@ -51,6 +51,7 @@ this.PEER_EVENT_LOST = 0x02; this.TAG_EVENT_FOUND = 0x03; this.TAG_EVENT_LOST = 0x04; this.PEER_EVENT_FOUND = 0x05; +this.RF_EVENT_STATE_CHANGE = 0x06; // 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 1e0c7f8d4712..a63edb8795cc 100644 --- a/dom/nfc/nsINfcContentHelper.idl +++ b/dom/nfc/nsINfcContentHelper.idl @@ -22,7 +22,7 @@ interface nsINfcTagEvent : nsISupports readonly attribute boolean isFormatable; }; -[scriptable, uuid(42c7a85f-59ae-4bde-b961-e1f1436476c3)] +[scriptable, uuid(fcbd98d6-3d04-4657-bd64-1164e311b399)] interface nsINfcEventListener : nsISupports { /** @@ -63,6 +63,14 @@ interface nsINfcEventListener : nsISupports * SessionToken received from parent process */ void notifyPeerLost(in DOMString sessionToken); + + /** + * Callback function used to notify RF state change. + * + * @param rfState + * RF state received from parent process + */ + void notifyRFStateChange(in DOMString rfState); }; [scriptable, uuid(a8ef3590-d853-4766-b54a-a4547da4dde4)] @@ -79,7 +87,7 @@ interface nsINfcRequestCallback : nsISupports void notifyError(in DOMString errorMsg); }; -[scriptable, uuid(9da02537-c4d0-4b2d-b294-d3250ff1720e)] +[scriptable, uuid(bcf214de-885b-43e6-9e53-9b7d880e1633)] interface nsINfcContentHelper : nsISupports { void init(in nsIDOMWindow window); @@ -166,6 +174,11 @@ interface nsINfcContentHelper : nsISupports void close(in DOMString sessionToken, in nsINfcRequestCallback callback); + /** + * Get current RF state. + */ + DOMString queryRFState(); + /** * Initiate send file operation. * diff --git a/dom/nfc/nsNfc.js b/dom/nfc/nsNfc.js index 921caa59857d..4bc21a7c843d 100644 --- a/dom/nfc/nsNfc.js +++ b/dom/nfc/nsNfc.js @@ -265,6 +265,7 @@ function MozNFCImpl() { MozNFCImpl.prototype = { _nfcContentHelper: null, _window: null, + _rfState: null, nfcPeer: null, nfcTag: null, eventService: null, @@ -287,6 +288,7 @@ MozNFCImpl.prototype = { if (this._nfcContentHelper) { this._nfcContentHelper.init(aWindow); + this._rfState = this._nfcContentHelper.queryRFState(); } }, @@ -520,6 +522,10 @@ MozNFCImpl.prototype = { } }, + notifyRFStateChange: function notifyRFStateChange(rfState) { + this._rfState = rfState; + }, + checkPermissions: function checkPermissions(perms) { let principal = this._window.document.nodePrincipal; for (let perm of perms) {