gecko/dom/nfc/nsNfc.js
Yoshi Huang a2a25825b0 Bug 1087931 - Remove mozNfc.getNFCTag. r=smaug, dimi
From 59396995908ed8a7227dd2b62b07aad0e16e88dc Mon Sep 17 00:00:00 2001
---
 dom/nfc/nsNfc.js                          |  8 --------
 dom/nfc/tests/marionette/test_nfc_peer.js | 15 +--------------
 dom/webidl/MozNFC.webidl                  |  5 -----
 3 files changed, 1 insertion(+), 27 deletions(-)
2014-10-23 18:36:10 +08:00

327 lines
9.9 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Copyright © 2013, Deutsche Telekom, Inc. */
"use strict";
const DEBUG = false;
function debug(s) {
if (DEBUG) dump("-*- Nfc DOM: " + s + "\n");
}
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyServiceGetter(this,
"appsService",
"@mozilla.org/AppsService;1",
"nsIAppsService");
/**
* NFCTag
*/
function MozNFCTag(aWindow, aSessionToken) {
debug("In MozNFCTag Constructor");
this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
.getService(Ci.nsINfcContentHelper);
this._window = aWindow;
this.session = aSessionToken;
}
MozNFCTag.prototype = {
_nfcContentHelper: null,
_window: null,
// NFCTag interface:
readNDEF: function readNDEF() {
return this._nfcContentHelper.readNDEF(this._window, this.session);
},
writeNDEF: function writeNDEF(records) {
return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
},
makeReadOnlyNDEF: function makeReadOnlyNDEF() {
return this._nfcContentHelper.makeReadOnlyNDEF(this._window, this.session);
},
classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"),
contractID: "@mozilla.org/nfc/NFCTag;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer]),
};
/**
* NFCPeer
*/
function MozNFCPeer(aWindow, aSessionToken) {
debug("In MozNFCPeer Constructor");
this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
.getService(Ci.nsINfcContentHelper);
this._window = aWindow;
this.session = aSessionToken;
}
MozNFCPeer.prototype = {
_nfcContentHelper: null,
_window: null,
_isLost: false,
// NFCPeer interface:
sendNDEF: function sendNDEF(records) {
if (this._isLost) {
throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
}
// Just forward sendNDEF to writeNDEF
return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
},
sendFile: function sendFile(blob) {
if (this._isLost) {
throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
}
let data = {
"blob": blob
};
return this._nfcContentHelper.sendFile(this._window,
Cu.cloneInto(data, this._window),
this.session);
},
invalidate: function invalidate() {
this._isLost = true;
},
classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"),
contractID: "@mozilla.org/nfc/NFCPeer;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer]),
};
/**
* Navigator NFC object
*/
function mozNfc() {
debug("In mozNfc Constructor");
try {
this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
.getService(Ci.nsINfcContentHelper);
} catch(e) {
debug("No NFC support.")
}
this._nfcContentHelper.registerEventTarget(this);
}
mozNfc.prototype = {
_nfcContentHelper: null,
_window: null,
nfcObject: null,
init: function init(aWindow) {
debug("mozNfc init called");
this._window = aWindow;
this.defineEventHandlerGetterSetter("ontagfound");
this.defineEventHandlerGetterSetter("ontaglost");
this.defineEventHandlerGetterSetter("onpeerready");
this.defineEventHandlerGetterSetter("onpeerlost");
if (this._nfcContentHelper) {
this._nfcContentHelper.init(aWindow);
}
},
// Only apps which have nfc-manager permission can call the following interfaces
// 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
// 'startPoll', 'stopPoll', and 'powerOff'.
checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
// Get the AppID and pass it to ContentHelper
let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
return this._nfcContentHelper.checkP2PRegistration(this._window, appID);
},
notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) {
let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
// Notify chrome process of user's acknowledgement
this._nfcContentHelper.notifyUserAcceptedP2P(this._window, appID);
},
notifySendFileStatus: function notifySendFileStatus(status, requestId) {
this._nfcContentHelper.notifySendFileStatus(this._window,
status, requestId);
},
startPoll: function startPoll() {
return this._nfcContentHelper.startPoll(this._window);
},
stopPoll: function stopPoll() {
return this._nfcContentHelper.stopPoll(this._window);
},
powerOff: function powerOff() {
return this._nfcContentHelper.powerOff(this._window);
},
getNFCPeer: function getNFCPeer(sessionToken) {
if (!sessionToken || !this._nfcContentHelper.checkSessionToken(sessionToken)) {
return null;
}
if (!this.nfcObject || this.nfcObject.session != sessionToken) {
let obj = new MozNFCPeer(this._window, sessionToken);
this.nfcObject = obj;
this.nfcObject.contentObject = this._window.MozNFCPeer._create(this._window, obj);
}
return this.nfcObject.contentObject;
},
defineEventHandlerGetterSetter: function defineEventHandlerGetterSetter(name) {
Object.defineProperty(this, name, {
get: function get() {
return this.__DOM_IMPL__.getEventHandler(name);
},
set: function set(handler) {
this.__DOM_IMPL__.setEventHandler(name, handler);
}
});
},
eventListenerWasAdded: function(eventType) {
if (eventType !== "peerready") {
return;
}
let appId = this._window.document.nodePrincipal.appId;
this._nfcContentHelper.registerTargetForPeerReady(this._window, appId);
},
eventListenerWasRemoved: function(eventType) {
if (eventType !== "peerready") {
return;
}
let appId = this._window.document.nodePrincipal.appId;
this._nfcContentHelper.unregisterTargetForPeerReady(this._window, appId);
},
notifyTagFound: function notifyTagFound(sessionToken, event, records) {
if (this.hasDeadWrapper()) {
dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
return;
}
if (!this.checkPermissions(["nfc-read", "nfc-write"])) {
return;
}
let tag = new MozNFCTag(this._window, sessionToken);
let tagContentObj = this._window.MozNFCTag._create(this._window, tag);
let length = records ? records.length : 0;
let ndefRecords = records ? [] : null;
for (let i = 0; i < length; i++) {
let record = records[i];
ndefRecords.push(new this._window.MozNDEFRecord({tnf: record.tnf,
type: record.type,
id: record.id,
payload: record.payload}));
}
let eventData = {
"tag": tagContentObj,
"ndefRecords": ndefRecords
};
debug("fire ontagfound " + sessionToken);
let tagEvent = new this._window.MozNFCTagEvent("tagfound", eventData);
this.__DOM_IMPL__.dispatchEvent(tagEvent);
},
notifyTagLost: function notifyTagLost(sessionToken) {
if (this.hasDeadWrapper()) {
dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
return;
}
if (!this.checkPermissions(["nfc-read", "nfc-write"])) {
return;
}
debug("fire ontaglost " + sessionToken);
let event = new this._window.Event("taglost");
this.__DOM_IMPL__.dispatchEvent(event);
},
notifyPeerReady: function notifyPeerReady(sessionToken) {
if (this.hasDeadWrapper()) {
dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
return;
}
this.session = sessionToken;
debug("fire onpeerready sessionToken : " + sessionToken);
let eventData = {
"peer":this.getNFCPeer(sessionToken)
};
let event = new this._window.MozNFCPeerEvent("peerready", eventData);
this.__DOM_IMPL__.dispatchEvent(event);
},
notifyPeerLost: function notifyPeerLost(sessionToken) {
if (this.hasDeadWrapper()) {
dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
return;
}
if (sessionToken != this.session) {
dump("Unpaired session for notifyPeerLost." + sessionToken);
return;
}
if (this.nfcObject && (this.nfcObject.session == sessionToken)) {
this.nfcObject.invalidate();
this.nfcObject = null;
}
this.session = null;
debug("fire onpeerlost");
let event = new this._window.Event("peerlost");
this.__DOM_IMPL__.dispatchEvent(event);
},
checkPermissions: function checkPermissions(perms) {
let principal = this._window.document.nodePrincipal;
for (let perm of perms) {
let permValue =
Services.perms.testExactPermissionFromPrincipal(principal, perm);
if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) {
return true;
} else {
debug("doesn't have " + perm + " permission.");
}
}
return false;
},
hasDeadWrapper: function hasDeadWrapper() {
return Cu.isDeadWrapper(this._window) || Cu.isDeadWrapper(this.__DOM_IMPL__);
},
classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
contractID: "@mozilla.org/navigatorNfc;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsINfcDOMEventTarget]),
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);