2013-11-05 14:18:56 -08:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Copyright © 2013, Deutsche Telekom, Inc. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2014-05-07 15:53:16 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "NFC", function () {
|
|
|
|
let obj = {};
|
|
|
|
Cu.import("resource://gre/modules/nfc_consts.js", obj);
|
|
|
|
return obj;
|
|
|
|
});
|
2013-11-05 14:18:56 -08:00
|
|
|
|
2013-12-10 22:04:27 -08:00
|
|
|
Cu.import("resource://gre/modules/systemlibs.js");
|
|
|
|
const NFC_ENABLED = libcutils.property_get("ro.moz.nfc.enabled", "false") === "true";
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
// set to true in nfc_consts.js to see debug messages
|
|
|
|
let DEBUG = NFC.DEBUG_NFC;
|
|
|
|
|
|
|
|
let debug;
|
|
|
|
if (DEBUG) {
|
|
|
|
debug = function (s) {
|
|
|
|
dump("-*- Nfc: " + s + "\n");
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debug = function (s) {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const NFC_CONTRACTID = "@mozilla.org/nfc;1";
|
|
|
|
const NFC_CID =
|
|
|
|
Components.ID("{2ff24790-5e74-11e1-b86c-0800200c9a66}");
|
|
|
|
|
|
|
|
const NFC_IPC_MSG_NAMES = [
|
2014-04-09 20:43:45 -07:00
|
|
|
"NFC:SetSessionToken"
|
|
|
|
];
|
|
|
|
|
|
|
|
const NFC_IPC_READ_PERM_MSG_NAMES = [
|
2013-11-05 14:18:56 -08:00
|
|
|
"NFC:ReadNDEF",
|
|
|
|
"NFC:GetDetailsNDEF",
|
|
|
|
"NFC:Connect",
|
2013-12-14 00:57:11 -08:00
|
|
|
"NFC:Close",
|
2013-11-05 14:18:56 -08:00
|
|
|
];
|
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
const NFC_IPC_WRITE_PERM_MSG_NAMES = [
|
|
|
|
"NFC:WriteNDEF",
|
|
|
|
"NFC:MakeReadOnlyNDEF",
|
|
|
|
"NFC:SendFile",
|
2014-07-24 03:11:42 -07:00
|
|
|
"NFC:RegisterPeerReadyTarget",
|
|
|
|
"NFC:UnregisterPeerReadyTarget"
|
2014-04-09 02:58:28 -07:00
|
|
|
];
|
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
const NFC_IPC_MANAGER_PERM_MSG_NAMES = [
|
2013-12-03 13:22:21 -08:00
|
|
|
"NFC:CheckP2PRegistration",
|
2014-01-15 15:42:49 -08:00
|
|
|
"NFC:NotifyUserAcceptedP2P",
|
2014-04-14 23:47:42 -07:00
|
|
|
"NFC:NotifySendFileStatus",
|
|
|
|
"NFC:StartPoll",
|
|
|
|
"NFC:StopPoll",
|
|
|
|
"NFC:PowerOff"
|
2013-12-03 13:22:21 -08:00
|
|
|
];
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
|
|
|
|
"@mozilla.org/system-message-internal;1",
|
|
|
|
"nsISystemMessagesInternal");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "UUIDGenerator",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
|
|
|
|
return {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
|
|
|
|
Ci.nsIObserver]),
|
|
|
|
|
|
|
|
nfc: null,
|
|
|
|
|
2013-12-03 13:22:21 -08:00
|
|
|
// Manage registered Peer Targets
|
|
|
|
peerTargetsMap: {},
|
|
|
|
currentPeerAppId: null,
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
init: function init(nfc) {
|
|
|
|
this.nfc = nfc;
|
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
Services.obs.addObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN, false);
|
2013-11-05 14:18:56 -08:00
|
|
|
this._registerMessageListeners();
|
|
|
|
},
|
|
|
|
|
|
|
|
_shutdown: function _shutdown() {
|
2014-02-18 23:14:22 -08:00
|
|
|
this.nfc.shutdown();
|
2013-11-05 14:18:56 -08:00
|
|
|
this.nfc = null;
|
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
Services.obs.removeObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN);
|
2013-11-05 14:18:56 -08:00
|
|
|
this._unregisterMessageListeners();
|
|
|
|
},
|
|
|
|
|
|
|
|
_registerMessageListeners: function _registerMessageListeners() {
|
|
|
|
ppmm.addMessageListener("child-process-shutdown", this);
|
2014-04-09 20:43:45 -07:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
for (let msgname of NFC_IPC_MSG_NAMES) {
|
|
|
|
ppmm.addMessageListener(msgname, this);
|
|
|
|
}
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
for (let msgname of NFC_IPC_READ_PERM_MSG_NAMES) {
|
2013-12-03 13:22:21 -08:00
|
|
|
ppmm.addMessageListener(msgname, this);
|
|
|
|
}
|
2014-04-09 02:58:28 -07:00
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
for (let msgname of NFC_IPC_WRITE_PERM_MSG_NAMES) {
|
|
|
|
ppmm.addMessageListener(msgname, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let msgname of NFC_IPC_MANAGER_PERM_MSG_NAMES) {
|
2014-04-09 02:58:28 -07:00
|
|
|
ppmm.addMessageListener(msgname, this);
|
|
|
|
}
|
2013-11-05 14:18:56 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_unregisterMessageListeners: function _unregisterMessageListeners() {
|
|
|
|
ppmm.removeMessageListener("child-process-shutdown", this);
|
2014-04-09 20:43:45 -07:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
for (let msgname of NFC_IPC_MSG_NAMES) {
|
|
|
|
ppmm.removeMessageListener(msgname, this);
|
|
|
|
}
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
for (let msgname of NFC_IPC_READ_PERM_MSG_NAMES) {
|
|
|
|
ppmm.removeMessageListener(msgname, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let msgname of NFC_IPC_WRITE_PERM_MSG_NAMES) {
|
2013-12-03 13:22:21 -08:00
|
|
|
ppmm.removeMessageListener(msgname, this);
|
|
|
|
}
|
2014-04-09 02:58:28 -07:00
|
|
|
|
2014-04-09 20:43:45 -07:00
|
|
|
for (let msgname of NFC_IPC_MANAGER_PERM_MSG_NAMES) {
|
2014-04-09 02:58:28 -07:00
|
|
|
ppmm.removeMessageListener(msgname, this);
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
ppmm = null;
|
|
|
|
},
|
|
|
|
|
2014-07-24 03:11:42 -07:00
|
|
|
registerPeerReadyTarget: function registerPeerReadyTarget(msg) {
|
2013-12-03 13:22:21 -08:00
|
|
|
let appInfo = msg.json;
|
|
|
|
let targets = this.peerTargetsMap;
|
|
|
|
let targetInfo = targets[appInfo.appId];
|
|
|
|
// If the application Id is already registered
|
|
|
|
if (targetInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-24 03:11:42 -07:00
|
|
|
// Target not registered yet! Add to the target map
|
2013-12-03 13:22:21 -08:00
|
|
|
let newTargetInfo = { target : msg.target,
|
2014-07-24 03:11:42 -07:00
|
|
|
isPeerReadyCalled: false };
|
2013-12-03 13:22:21 -08:00
|
|
|
targets[appInfo.appId] = newTargetInfo;
|
|
|
|
},
|
|
|
|
|
2014-07-24 03:11:42 -07:00
|
|
|
unregisterPeerReadyTarget: function unregisterPeerReadyTarget(msg) {
|
2013-12-03 13:22:21 -08:00
|
|
|
let appInfo = msg.json;
|
|
|
|
let targets = this.peerTargetsMap;
|
|
|
|
let targetInfo = targets[appInfo.appId];
|
|
|
|
if (targetInfo) {
|
2014-07-24 03:11:42 -07:00
|
|
|
// Remove the target from the list of registered targets
|
|
|
|
delete targets[appInfo.appId]
|
2013-12-03 13:22:21 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-14 17:43:21 -08:00
|
|
|
removePeerTarget: function removePeerTarget(target) {
|
|
|
|
let targets = this.peerTargetsMap;
|
|
|
|
Object.keys(targets).forEach((appId) => {
|
|
|
|
let targetInfo = targets[appId];
|
|
|
|
if (targetInfo && targetInfo.target === target) {
|
|
|
|
// Remove the target from the list of registered targets
|
|
|
|
delete targets[appId];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-24 03:11:42 -07:00
|
|
|
isPeerReadyTarget: function isPeerReadyTarget(appId) {
|
2013-12-03 13:22:21 -08:00
|
|
|
let targetInfo = this.peerTargetsMap[appId];
|
2014-07-24 03:11:42 -07:00
|
|
|
return (targetInfo != null);
|
2013-12-03 13:22:21 -08:00
|
|
|
},
|
|
|
|
|
2014-07-24 03:11:42 -07:00
|
|
|
isPeerReadyCalled: function isPeerReadyCalled(appId) {
|
2013-12-03 13:22:21 -08:00
|
|
|
let targetInfo = this.peerTargetsMap[appId];
|
2014-08-12 02:04:51 -07:00
|
|
|
return !!targetInfo.IsPeerReadyCalled;
|
2013-12-03 13:22:21 -08:00
|
|
|
},
|
|
|
|
|
2014-08-14 20:31:52 -07:00
|
|
|
notifyPeerEvent: function notifyPeerEvent(appId, event, sessionToken) {
|
2014-07-24 03:11:42 -07:00
|
|
|
let targetInfo = this.peerTargetsMap[appId];
|
|
|
|
targetInfo.target.sendAsyncMessage("NFC:PeerEvent", {
|
|
|
|
event: event,
|
2014-08-14 20:31:52 -07:00
|
|
|
sessionToken: sessionToken
|
2014-07-24 03:11:42 -07:00
|
|
|
});
|
2013-12-03 13:22:21 -08:00
|
|
|
},
|
|
|
|
|
2014-04-30 01:18:00 -07:00
|
|
|
checkP2PRegistration: function checkP2PRegistration(msg) {
|
|
|
|
// Check if the session and application id yeild a valid registered
|
|
|
|
// target. It should have registered for NFC_PEER_EVENT_READY
|
|
|
|
let isValid = !!this.nfc.sessionTokenMap[this.nfc._currentSessionId] &&
|
2014-07-24 03:11:42 -07:00
|
|
|
this.isPeerReadyTarget(msg.json.appId);
|
2014-04-30 01:18:00 -07:00
|
|
|
// Remember the current AppId if registered.
|
|
|
|
this.currentPeerAppId = (isValid) ? msg.json.appId : null;
|
|
|
|
|
2014-05-23 00:11:38 -07:00
|
|
|
let respMsg = { requestId: msg.json.requestId };
|
|
|
|
if(!isValid) {
|
|
|
|
respMsg.errorMsg = this.nfc.getErrorMessage(NFC.NFC_GECKO_ERROR_P2P_REG_INVALID);
|
|
|
|
}
|
2014-04-30 01:18:00 -07:00
|
|
|
// Notify the content process immediately of the status
|
2014-05-23 00:11:38 -07:00
|
|
|
msg.target.sendAsyncMessage(msg.name + "Response", respMsg);
|
2014-04-30 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
2014-08-14 20:31:52 -07:00
|
|
|
onPeerLost: function onPeerLost(sessionToken) {
|
|
|
|
let appId = this.currentPeerAppId;
|
|
|
|
// For peerlost, the message is delievered to the target which
|
|
|
|
// registered onpeerready and onpeerready has been called before.
|
|
|
|
if (this.isPeerReadyTarget(appId) && this.isPeerReadyCalled(appId)) {
|
|
|
|
this.notifyPeerEvent(appId, NFC.NFC_PEER_EVENT_LOST, sessionToken);
|
|
|
|
this.currentPeerAppId = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
/**
|
|
|
|
* nsIMessageListener interface methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
receiveMessage: function receiveMessage(msg) {
|
|
|
|
debug("Received '" + msg.name + "' message from content process");
|
|
|
|
if (msg.name == "child-process-shutdown") {
|
2014-01-14 17:43:21 -08:00
|
|
|
this.removePeerTarget(msg.target);
|
2013-11-05 14:18:56 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NFC_IPC_MSG_NAMES.indexOf(msg.name) != -1) {
|
2014-04-09 20:43:45 -07:00
|
|
|
// Do nothing.
|
|
|
|
} else if (NFC_IPC_READ_PERM_MSG_NAMES.indexOf(msg.name) != -1) {
|
2013-11-05 14:18:56 -08:00
|
|
|
if (!msg.target.assertPermission("nfc-read")) {
|
|
|
|
debug("Nfc message " + msg.name +
|
|
|
|
" from a content process with no 'nfc-read' privileges.");
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-09 20:43:45 -07:00
|
|
|
} else if (NFC_IPC_WRITE_PERM_MSG_NAMES.indexOf(msg.name) != -1) {
|
2013-12-03 13:22:21 -08:00
|
|
|
if (!msg.target.assertPermission("nfc-write")) {
|
|
|
|
debug("Nfc Peer message " + msg.name +
|
|
|
|
" from a content process with no 'nfc-write' privileges.");
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-09 20:43:45 -07:00
|
|
|
} else if (NFC_IPC_MANAGER_PERM_MSG_NAMES.indexOf(msg.name) != -1) {
|
2014-04-09 02:58:28 -07:00
|
|
|
if (!msg.target.assertPermission("nfc-manager")) {
|
|
|
|
debug("NFC message " + message.name +
|
|
|
|
" from a content process with no 'nfc-manager' privileges.");
|
|
|
|
return null;
|
2013-12-03 13:22:21 -08:00
|
|
|
}
|
2013-11-05 14:18:56 -08:00
|
|
|
} else {
|
|
|
|
debug("Ignoring unknown message type: " + msg.name);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg.name) {
|
|
|
|
case "NFC:SetSessionToken":
|
2014-05-27 01:19:00 -07:00
|
|
|
if (msg.json.sessionToken !== this.nfc.sessionTokenMap[this.nfc._currentSessionId]) {
|
|
|
|
debug("Received invalid Session Token: " + msg.json.sessionToken + " - Do not register this target");
|
|
|
|
return NFC.NFC_ERROR_BAD_SESSION_ID;
|
|
|
|
}
|
2014-08-15 01:29:55 -07:00
|
|
|
debug("Registering target for this SessionToken : " + msg.json.sessionToken);
|
2014-05-27 01:19:00 -07:00
|
|
|
return NFC.NFC_SUCCESS;
|
2014-07-24 03:11:42 -07:00
|
|
|
case "NFC:RegisterPeerReadyTarget":
|
|
|
|
this.registerPeerReadyTarget(msg);
|
2014-04-09 20:43:45 -07:00
|
|
|
return null;
|
2014-07-24 03:11:42 -07:00
|
|
|
case "NFC:UnregisterPeerReadyTarget":
|
|
|
|
this.unregisterPeerReadyTarget(msg);
|
2014-04-09 20:43:45 -07:00
|
|
|
return null;
|
2013-12-03 13:22:21 -08:00
|
|
|
case "NFC:CheckP2PRegistration":
|
2014-04-30 01:18:00 -07:00
|
|
|
this.checkP2PRegistration(msg);
|
2014-04-09 20:43:45 -07:00
|
|
|
return null;
|
2013-12-03 13:22:21 -08:00
|
|
|
case "NFC:NotifyUserAcceptedP2P":
|
|
|
|
// Notify the 'NFC_PEER_EVENT_READY' since user has acknowledged
|
2014-07-24 03:11:42 -07:00
|
|
|
if (!this.isPeerReadyTarget(msg.json.appId)) {
|
2014-08-11 00:45:35 -07:00
|
|
|
debug("Application ID : " + msg.json.appId + " is not a registered PeerReadytarget");
|
2014-07-24 03:11:42 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetInfo = this.peerTargetsMap[msg.json.appId];
|
|
|
|
targetInfo.IsPeerReadyCalled = true;
|
2014-08-14 20:31:52 -07:00
|
|
|
let sessionToken = this.nfc.sessionTokenMap[this.nfc._currentSessionId];
|
|
|
|
this.notifyPeerEvent(msg.json.appId, NFC.NFC_PEER_EVENT_READY, sessionToken);
|
2014-04-09 20:43:45 -07:00
|
|
|
return null;
|
2014-01-15 15:42:49 -08:00
|
|
|
case "NFC:NotifySendFileStatus":
|
|
|
|
// Upon receiving the status of sendFile operation, send the response
|
|
|
|
// to appropriate content process.
|
2014-08-15 01:29:55 -07:00
|
|
|
msg.json.type = "NotifySendFileStatus";
|
|
|
|
if (msg.json.status !== NFC.NFC_SUCCESS) {
|
|
|
|
msg.json.errorMsg =
|
|
|
|
this.nfc.getErrorMessage(NFC.NFC_GECKO_ERROR_SEND_FILE_FAILED);
|
|
|
|
}
|
|
|
|
this.nfc.sendNfcResponse(msg.json);
|
2014-04-09 20:43:45 -07:00
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return this.nfc.receiveMessage(msg);
|
2013-11-05 14:18:56 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsIObserver interface methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
|
|
|
switch (topic) {
|
2014-04-09 20:43:45 -07:00
|
|
|
case NFC.TOPIC_XPCOM_SHUTDOWN:
|
2013-11-05 14:18:56 -08:00
|
|
|
this._shutdown();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
function Nfc() {
|
2014-02-18 23:14:22 -08:00
|
|
|
debug("Starting Nfc Service");
|
|
|
|
|
|
|
|
let nfcService = Cc["@mozilla.org/nfc/service;1"].getService(Ci.nsINfcService);
|
|
|
|
if (!nfcService) {
|
|
|
|
debug("No nfc service component available!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nfcService.start(this);
|
|
|
|
this.nfcService = nfcService;
|
2013-11-05 14:18:56 -08:00
|
|
|
|
|
|
|
gMessageManager.init(this);
|
2014-04-24 02:05:26 -07:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
// Maps sessionId (that are generated from nfcd) with a unique guid : 'SessionToken'
|
|
|
|
this.sessionTokenMap = {};
|
2014-04-14 23:47:42 -07:00
|
|
|
this.targetsByRequestId = {};
|
2013-11-05 14:18:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Nfc.prototype = {
|
|
|
|
|
|
|
|
classID: NFC_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: NFC_CID,
|
|
|
|
classDescription: "Nfc",
|
2014-02-18 23:14:22 -08:00
|
|
|
interfaces: [Ci.nsINfcService]}),
|
2013-11-05 14:18:56 -08:00
|
|
|
|
2014-02-18 23:14:22 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsINfcEventListener]),
|
2013-11-05 14:18:56 -08:00
|
|
|
|
|
|
|
_currentSessionId: null,
|
2014-04-24 02:05:26 -07:00
|
|
|
|
|
|
|
powerLevel: NFC.NFC_POWER_LEVEL_UNKNOWN,
|
2013-11-05 14:18:56 -08:00
|
|
|
|
|
|
|
/**
|
2014-02-18 23:14:22 -08:00
|
|
|
* Send arbitrary message to Nfc service.
|
2013-11-05 14:18:56 -08:00
|
|
|
*
|
|
|
|
* @param nfcMessageType
|
|
|
|
* A text message type.
|
|
|
|
* @param message [optional]
|
|
|
|
* An optional message object to send.
|
|
|
|
*/
|
2014-02-18 23:14:22 -08:00
|
|
|
sendToNfcService: function sendToNfcService(nfcMessageType, message) {
|
2013-11-05 14:18:56 -08:00
|
|
|
message = message || {};
|
|
|
|
message.type = nfcMessageType;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.nfcService.sendCommand(message);
|
2013-11-05 14:18:56 -08:00
|
|
|
},
|
|
|
|
|
2014-08-15 01:29:55 -07:00
|
|
|
sendNfcResponse: function sendNfcResponse(message) {
|
|
|
|
let target = this.targetsByRequestId[message.requestId];
|
|
|
|
if (!target) {
|
|
|
|
debug("No target for requestId: " + message.requestId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete this.targetsByRequestId[message.requestId];
|
|
|
|
|
|
|
|
target.sendAsyncMessage("NFC:" + message.type, message);
|
|
|
|
},
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
/**
|
2014-05-23 00:11:38 -07:00
|
|
|
* Send Error response to content. This is used only
|
|
|
|
* in case of discovering an error in message received from
|
|
|
|
* content process.
|
2013-11-05 14:18:56 -08:00
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
* An nsIMessageListener's message parameter.
|
|
|
|
*/
|
2014-05-23 00:11:38 -07:00
|
|
|
sendNfcErrorResponse: function sendNfcErrorResponse(message, errorCode) {
|
2013-11-05 14:18:56 -08:00
|
|
|
if (!message.target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let nfcMsgType = message.name + "Response";
|
2014-05-23 00:11:38 -07:00
|
|
|
message.json.errorMsg = this.getErrorMessage(errorCode);
|
|
|
|
message.target.sendAsyncMessage(nfcMsgType, message.json);
|
|
|
|
},
|
|
|
|
|
|
|
|
getErrorMessage: function getErrorMessage(errorCode) {
|
|
|
|
return NFC.NFC_ERROR_MSG[errorCode] ||
|
|
|
|
NFC.NFC_ERROR_MSG[NFC.NFC_GECKO_ERROR_GENERIC_FAILURE];
|
2013-11-05 14:18:56 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-18 23:14:22 -08:00
|
|
|
* Process the incoming message from the NFC Service.
|
2013-11-05 14:18:56 -08:00
|
|
|
*/
|
2014-02-18 23:14:22 -08:00
|
|
|
onEvent: function onEvent(event) {
|
|
|
|
let message = Cu.cloneInto(event, this);
|
|
|
|
debug("Received message from NFC Service: " + JSON.stringify(message));
|
2013-11-05 14:18:56 -08:00
|
|
|
|
2014-05-23 00:11:38 -07:00
|
|
|
// mapping error code to error message
|
2014-06-16 04:54:00 -07:00
|
|
|
if (message.status !== undefined && message.status !== NFC.NFC_SUCCESS) {
|
2014-05-23 00:11:38 -07:00
|
|
|
message.errorMsg = this.getErrorMessage(message.status);
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
switch (message.type) {
|
2014-07-07 03:18:20 -07:00
|
|
|
case "InitializedNotification":
|
|
|
|
// Do nothing.
|
|
|
|
break;
|
|
|
|
case "TechDiscoveredNotification":
|
|
|
|
message.type = "techDiscovered";
|
2013-11-05 14:18:56 -08:00
|
|
|
this._currentSessionId = message.sessionId;
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
// Check if the session token already exists. If exists, continue to use the same one.
|
|
|
|
// If not, generate a new token.
|
|
|
|
if (!this.sessionTokenMap[this._currentSessionId]) {
|
|
|
|
this.sessionTokenMap[this._currentSessionId] = UUIDGenerator.generateUUID().toString();
|
|
|
|
}
|
|
|
|
// Update the upper layers with a session token (alias)
|
|
|
|
message.sessionToken = this.sessionTokenMap[this._currentSessionId];
|
|
|
|
// Do not expose the actual session to the content
|
|
|
|
delete message.sessionId;
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
gSystemMessenger.broadcastMessage("nfc-manager-tech-discovered", message);
|
|
|
|
break;
|
2014-07-07 03:18:20 -07:00
|
|
|
case "TechLostNotification":
|
|
|
|
message.type = "techLost";
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
// Update the upper layers with a session token (alias)
|
|
|
|
message.sessionToken = this.sessionTokenMap[this._currentSessionId];
|
|
|
|
// Do not expose the actual session to the content
|
|
|
|
delete message.sessionId;
|
2013-12-03 13:22:21 -08:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
gSystemMessenger.broadcastMessage("nfc-manager-tech-lost", message);
|
2014-08-14 20:31:52 -07:00
|
|
|
gMessageManager.onPeerLost(this.sessionTokenMap[this._currentSessionId]);
|
2014-07-24 03:11:42 -07:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
delete this.sessionTokenMap[this._currentSessionId];
|
|
|
|
this._currentSessionId = null;
|
2014-08-14 20:31:52 -07:00
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "ConfigResponse":
|
2014-05-23 00:11:38 -07:00
|
|
|
if (message.status === NFC.NFC_SUCCESS) {
|
2014-04-24 02:05:26 -07:00
|
|
|
this.powerLevel = message.powerLevel;
|
|
|
|
}
|
|
|
|
|
2014-08-15 01:29:55 -07:00
|
|
|
this.sendNfcResponse(message);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "ConnectResponse": // Fall through.
|
|
|
|
case "CloseResponse":
|
|
|
|
case "GetDetailsNDEFResponse":
|
|
|
|
case "ReadNDEFResponse":
|
|
|
|
case "MakeReadOnlyNDEFResponse":
|
|
|
|
case "WriteNDEFResponse":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.sendNfcResponse(message);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Don't know about this message type: " + message.type);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-02-18 23:14:22 -08:00
|
|
|
nfcService: null,
|
2013-11-05 14:18:56 -08:00
|
|
|
|
|
|
|
sessionTokenMap: null,
|
|
|
|
|
2014-04-14 23:47:42 -07:00
|
|
|
targetsByRequestId: null,
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
/**
|
|
|
|
* Process a message from the content process.
|
|
|
|
*/
|
|
|
|
receiveMessage: function receiveMessage(message) {
|
|
|
|
debug("Received '" + JSON.stringify(message) + "' message from content process");
|
|
|
|
|
2014-04-14 23:47:42 -07:00
|
|
|
// Handle messages without sessionToken.
|
|
|
|
if (message.name == "NFC:StartPoll") {
|
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
|
|
|
this.setConfig({powerLevel: NFC.NFC_POWER_LEVEL_ENABLED,
|
|
|
|
requestId: message.json.requestId});
|
|
|
|
return null;
|
|
|
|
} else if (message.name == "NFC:StopPoll") {
|
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
|
|
|
this.setConfig({powerLevel: NFC.NFC_POWER_LEVEL_LOW,
|
|
|
|
requestId: message.json.requestId});
|
|
|
|
return null;
|
|
|
|
} else if (message.name == "NFC:PowerOff") {
|
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
|
|
|
this.setConfig({powerLevel: NFC.NFC_POWER_LEVEL_DISABLED,
|
|
|
|
requestId: message.json.requestId});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-24 02:05:26 -07:00
|
|
|
if (this.powerLevel != NFC.NFC_POWER_LEVEL_ENABLED) {
|
|
|
|
debug("NFC is not enabled. current powerLevel:" + this.powerLevel);
|
2014-05-23 00:11:38 -07:00
|
|
|
this.sendNfcErrorResponse(message, NFC.NFC_GECKO_ERROR_NOT_ENABLED);
|
2013-11-05 14:18:56 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check on sessionId
|
|
|
|
if (message.json.sessionToken !== this.sessionTokenMap[this._currentSessionId]) {
|
|
|
|
debug("Invalid Session Token: " + message.json.sessionToken +
|
|
|
|
" Expected Session Token: " + this.sessionTokenMap[this._currentSessionId]);
|
2014-05-23 00:11:38 -07:00
|
|
|
this.sendNfcErrorResponse(message, NFC.NFC_ERROR_BAD_SESSION_ID);
|
2013-11-05 14:18:56 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current sessionId before sending to the worker
|
2014-03-11 07:23:30 -07:00
|
|
|
message.json.sessionId = this._currentSessionId;
|
2013-11-05 14:18:56 -08:00
|
|
|
|
|
|
|
switch (message.name) {
|
|
|
|
case "NFC:GetDetailsNDEF":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("getDetailsNDEF", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "NFC:ReadNDEF":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("readNDEF", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "NFC:WriteNDEF":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("writeNDEF", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "NFC:MakeReadOnlyNDEF":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("makeReadOnlyNDEF", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "NFC:Connect":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("connect", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
|
|
|
case "NFC:Close":
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("close", message.json);
|
2013-11-05 14:18:56 -08:00
|
|
|
break;
|
2013-12-14 00:57:11 -08:00
|
|
|
case "NFC:SendFile":
|
|
|
|
// Chrome process is the arbitrator / mediator between
|
|
|
|
// system app (content process) that issued nfc 'sendFile' operation
|
|
|
|
// and system app that handles the system message :
|
|
|
|
// 'nfc-manager-send-file'. System app subsequently handover's
|
|
|
|
// the data to alternate carrier's (BT / WiFi) 'sendFile' interface.
|
2014-08-15 01:29:55 -07:00
|
|
|
this.targetsByRequestId[message.json.requestId] = message.target;
|
2013-12-14 00:57:11 -08:00
|
|
|
|
|
|
|
// Notify system app to initiate BT send file operation
|
|
|
|
gSystemMessenger.broadcastMessage("nfc-manager-send-file",
|
|
|
|
message.json);
|
|
|
|
break;
|
2013-11-05 14:18:56 -08:00
|
|
|
default:
|
|
|
|
debug("UnSupported : Message Name " + message.name);
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-09 20:43:45 -07:00
|
|
|
|
|
|
|
return null;
|
2013-11-05 14:18:56 -08:00
|
|
|
},
|
|
|
|
|
2014-05-14 21:11:51 -07:00
|
|
|
/**
|
|
|
|
* nsIObserver interface methods.
|
|
|
|
*/
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
if (topic != "profile-after-change") {
|
|
|
|
debug("Should receive 'profile-after-change' only, received " + topic);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-05 14:18:56 -08:00
|
|
|
setConfig: function setConfig(prop) {
|
2014-02-18 23:14:22 -08:00
|
|
|
this.sendToNfcService("config", prop);
|
|
|
|
},
|
|
|
|
|
|
|
|
shutdown: function shutdown() {
|
|
|
|
this.nfcService.shutdown();
|
|
|
|
this.nfcService = null;
|
2013-11-05 14:18:56 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-10 22:04:27 -08:00
|
|
|
if (NFC_ENABLED) {
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Nfc]);
|
|
|
|
}
|