Bug 1095322 - Child process should know current rf state of NFC HW. r=allstars.chh

This commit is contained in:
Dimi Lee 2014-11-25 15:10:34 +08:00
parent 733e0a5e58
commit 1a4a4bc5e0
5 changed files with 52 additions and 7 deletions

View File

@ -89,6 +89,7 @@ NfcContentHelper.prototype = {
_window: null, _window: null,
_requestMap: null, _requestMap: null,
_rfState: null,
eventListener: null, eventListener: null,
init: function init(aWindow) { init: function init(aWindow) {
@ -107,6 +108,13 @@ NfcContentHelper.prototype = {
updateDebug(); updateDebug();
}; };
} }
let info = cpmm.sendSyncMessage("NFC:QueryInfo")[0];
this._rfState = info.rfState;
},
queryRFState: function queryRFState() {
return this._rfState;
}, },
encodeNDEFRecords: function encodeNDEFRecords(records) { encodeNDEFRecords: function encodeNDEFRecords(records) {
@ -319,6 +327,10 @@ NfcContentHelper.prototype = {
case NFC.TAG_EVENT_LOST: case NFC.TAG_EVENT_LOST:
this.eventListener.notifyTagLost(result.sessionToken); this.eventListener.notifyTagLost(result.sessionToken);
break; break;
case NFC.RF_EVENT_STATE_CHANGE:
this._rfState = result.rfState;
this.eventListener.notifyRFStateChange(this._rfState);
break;
} }
break; break;
} }

View File

@ -59,7 +59,8 @@ const NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES = [
]; ];
const NFC_IPC_MSG_NAMES = [ const NFC_IPC_MSG_NAMES = [
"NFC:CheckSessionToken" "NFC:CheckSessionToken",
"NFC:QueryInfo"
]; ];
const NFC_IPC_READ_PERM_MSG_NAMES = [ 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. * nsIMessageListener interface methods.
*/ */
@ -557,11 +565,12 @@ Nfc.prototype = {
this.notifyHCIEventTransaction(message); this.notifyHCIEventTransaction(message);
break; break;
case "ChangeRFStateResponse": case "ChangeRFStateResponse":
this.sendNfcResponse(message);
if (!message.errorMsg) { if (!message.errorMsg) {
this.rfState = message.rfState; this.rfState = message.rfState;
gMessageManager.onRFStateChange(this.rfState);
} }
this.sendNfcResponse(message);
break; break;
case "ConnectResponse": // Fall through. case "ConnectResponse": // Fall through.
case "CloseResponse": case "CloseResponse":
@ -602,13 +611,15 @@ Nfc.prototype = {
receiveMessage: function receiveMessage(message) { receiveMessage: function receiveMessage(message) {
let isRFAPI = message.name == "NFC:ChangeRFState"; let isRFAPI = message.name == "NFC:ChangeRFState";
let isSendFile = message.name == "NFC:SendFile"; 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); debug("NFC is not enabled. current rfState:" + this.rfState);
this.sendNfcErrorResponse(message, NFC.NFC_GECKO_ERROR_NOT_ENABLED); this.sendNfcErrorResponse(message, NFC.NFC_GECKO_ERROR_NOT_ENABLED);
return null; return null;
} }
if (!isRFAPI && !isSendFile) { if (!isRFAPI && !isSendFile && !isInfoAPI) {
// Update the current sessionId before sending to the NFC service. // Update the current sessionId before sending to the NFC service.
message.data.sessionId = SessionHelper.getId(message.data.sessionToken); message.data.sessionId = SessionHelper.getId(message.data.sessionToken);
} }
@ -647,6 +658,8 @@ Nfc.prototype = {
gSystemMessenger.broadcastMessage("nfc-manager-send-file", gSystemMessenger.broadcastMessage("nfc-manager-send-file",
message.data); message.data);
break; break;
case "NFC:QueryInfo":
return {rfState: this.rfState};
default: default:
debug("UnSupported : Message Name " + message.name); debug("UnSupported : Message Name " + message.name);
return null; return null;

View File

@ -51,6 +51,7 @@ this.PEER_EVENT_LOST = 0x02;
this.TAG_EVENT_FOUND = 0x03; this.TAG_EVENT_FOUND = 0x03;
this.TAG_EVENT_LOST = 0x04; this.TAG_EVENT_LOST = 0x04;
this.PEER_EVENT_FOUND = 0x05; this.PEER_EVENT_FOUND = 0x05;
this.RF_EVENT_STATE_CHANGE = 0x06;
// Allow this file to be imported via Components.utils.import(). // Allow this file to be imported via Components.utils.import().
this.EXPORTED_SYMBOLS = Object.keys(this); this.EXPORTED_SYMBOLS = Object.keys(this);

View File

@ -22,7 +22,7 @@ interface nsINfcTagEvent : nsISupports
readonly attribute boolean isFormatable; readonly attribute boolean isFormatable;
}; };
[scriptable, uuid(42c7a85f-59ae-4bde-b961-e1f1436476c3)] [scriptable, uuid(fcbd98d6-3d04-4657-bd64-1164e311b399)]
interface nsINfcEventListener : nsISupports interface nsINfcEventListener : nsISupports
{ {
/** /**
@ -63,6 +63,14 @@ interface nsINfcEventListener : nsISupports
* SessionToken received from parent process * SessionToken received from parent process
*/ */
void notifyPeerLost(in DOMString sessionToken); 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)] [scriptable, uuid(a8ef3590-d853-4766-b54a-a4547da4dde4)]
@ -79,7 +87,7 @@ interface nsINfcRequestCallback : nsISupports
void notifyError(in DOMString errorMsg); void notifyError(in DOMString errorMsg);
}; };
[scriptable, uuid(9da02537-c4d0-4b2d-b294-d3250ff1720e)] [scriptable, uuid(bcf214de-885b-43e6-9e53-9b7d880e1633)]
interface nsINfcContentHelper : nsISupports interface nsINfcContentHelper : nsISupports
{ {
void init(in nsIDOMWindow window); void init(in nsIDOMWindow window);
@ -166,6 +174,11 @@ interface nsINfcContentHelper : nsISupports
void close(in DOMString sessionToken, void close(in DOMString sessionToken,
in nsINfcRequestCallback callback); in nsINfcRequestCallback callback);
/**
* Get current RF state.
*/
DOMString queryRFState();
/** /**
* Initiate send file operation. * Initiate send file operation.
* *

View File

@ -265,6 +265,7 @@ function MozNFCImpl() {
MozNFCImpl.prototype = { MozNFCImpl.prototype = {
_nfcContentHelper: null, _nfcContentHelper: null,
_window: null, _window: null,
_rfState: null,
nfcPeer: null, nfcPeer: null,
nfcTag: null, nfcTag: null,
eventService: null, eventService: null,
@ -287,6 +288,7 @@ MozNFCImpl.prototype = {
if (this._nfcContentHelper) { if (this._nfcContentHelper) {
this._nfcContentHelper.init(aWindow); 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) { checkPermissions: function checkPermissions(perms) {
let principal = this._window.document.nodePrincipal; let principal = this._window.document.nodePrincipal;
for (let perm of perms) { for (let perm of perms) {