1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* Copyright © 2013, Deutsche Telekom, Inc. */
11 if (DEBUG) dump("-*- Nfc DOM: " + s + "\n");
14 const Cc = Components.classes;
15 const Ci = Components.interfaces;
16 const Cu = Components.utils;
18 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
19 Cu.import("resource://gre/modules/Services.jsm");
21 XPCOMUtils.defineLazyServiceGetter(this,
23 "@mozilla.org/AppsService;1",
25 const NFC_PEER_EVENT_READY = 0x01;
26 const NFC_PEER_EVENT_LOST = 0x02;
31 function MozNFCTag() {
32 debug("In MozNFCTag Constructor");
33 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
34 .getService(Ci.nsINfcContentHelper);
37 // Map WebIDL declared enum map names to integer
38 this._techTypesMap = [];
39 this._techTypesMap['NFC_A'] = 0;
40 this._techTypesMap['NFC_B'] = 1;
41 this._techTypesMap['NFC_ISO_DEP'] = 2;
42 this._techTypesMap['NFC_F'] = 3;
43 this._techTypesMap['NFC_V'] = 4;
44 this._techTypesMap['NDEF'] = 5;
45 this._techTypesMap['NDEF_FORMATABLE'] = 6;
46 this._techTypesMap['MIFARE_CLASSIC'] = 7;
47 this._techTypesMap['MIFARE_ULTRALIGHT'] = 8;
48 this._techTypesMap['NFC_BARCODE'] = 9;
49 this._techTypesMap['P2P'] = 10;
51 MozNFCTag.prototype = {
52 _nfcContentHelper: null,
55 initialize: function(aWindow, aSessionToken) {
56 this._window = aWindow;
57 this.session = aSessionToken;
63 readNDEF: function readNDEF() {
64 return this._nfcContentHelper.readNDEF(this._window, this.session);
66 writeNDEF: function writeNDEF(records) {
67 return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
69 makeReadOnlyNDEF: function makeReadOnlyNDEF() {
70 return this._nfcContentHelper.makeReadOnlyNDEF(this._window, this.session);
73 classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"),
74 contractID: "@mozilla.org/nfc/NFCTag;1",
75 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
76 Ci.nsIDOMGlobalPropertyInitializer]),
82 function MozNFCPeer() {
83 debug("In MozNFCPeer Constructor");
84 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
85 .getService(Ci.nsINfcContentHelper);
88 MozNFCPeer.prototype = {
89 _nfcContentHelper: null,
93 initialize: function(aWindow, aSessionToken) {
94 this._window = aWindow;
95 this.session = aSessionToken;
99 sendNDEF: function sendNDEF(records) {
101 throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
104 // Just forward sendNDEF to writeNDEF
105 return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
108 sendFile: function sendFile(blob) {
110 throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
116 return this._nfcContentHelper.sendFile(this._window,
117 Cu.cloneInto(data, this._window),
121 invalidate: function invalidate() {
125 classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"),
126 contractID: "@mozilla.org/nfc/NFCPeer;1",
127 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
128 Ci.nsIDOMGlobalPropertyInitializer]),
132 * Navigator NFC object
135 debug("In mozNfc Constructor");
137 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
138 .getService(Ci.nsINfcContentHelper);
140 debug("No NFC support.")
143 this._nfcContentHelper.registerPeerEventListener(this);
146 _nfcContentHelper: null,
150 init: function init(aWindow) {
151 debug("mozNfc init called");
152 this._window = aWindow;
155 // Only apps which have nfc-manager permission can call the following interfaces
156 // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
157 // 'startPoll', 'stopPoll', and 'powerOff'.
158 checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
159 // Get the AppID and pass it to ContentHelper
160 let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
161 return this._nfcContentHelper.checkP2PRegistration(this._window, appID);
164 notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) {
165 let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
166 // Notify chrome process of user's acknowledgement
167 this._nfcContentHelper.notifyUserAcceptedP2P(this._window, appID);
170 notifySendFileStatus: function notifySendFileStatus(status, requestId) {
171 this._nfcContentHelper.notifySendFileStatus(this._window,
175 startPoll: function startPoll() {
176 return this._nfcContentHelper.startPoll(this._window);
179 stopPoll: function stopPoll() {
180 return this._nfcContentHelper.stopPoll(this._window);
183 powerOff: function powerOff() {
184 return this._nfcContentHelper.powerOff(this._window);
187 getNFCTag: function getNFCTag(sessionToken) {
188 let obj = new MozNFCTag();
189 obj.initialize(this._window, sessionToken);
190 if (this._nfcContentHelper.checkSessionToken(sessionToken)) {
191 return this._window.MozNFCTag._create(this._window, obj);
196 getNFCPeer: function getNFCPeer(sessionToken) {
197 if (!sessionToken || !this._nfcContentHelper.checkSessionToken(sessionToken)) {
201 if (!this.nfcObject || this.nfcObject.session != sessionToken) {
202 let obj = new MozNFCPeer();
203 obj.initialize(this._window, sessionToken);
204 this.nfcObject = obj;
205 this.nfcObject.contentObject = this._window.MozNFCPeer._create(this._window, obj);
208 return this.nfcObject.contentObject;
211 // get/set onpeerready
213 return this.__DOM_IMPL__.getEventHandler("onpeerready");
216 set onpeerready(handler) {
217 this.__DOM_IMPL__.setEventHandler("onpeerready", handler);
220 // get/set onpeerlost
222 return this.__DOM_IMPL__.getEventHandler("onpeerlost");
225 set onpeerlost(handler) {
226 this.__DOM_IMPL__.setEventHandler("onpeerlost", handler);
229 eventListenerWasAdded: function(evt) {
230 let eventType = this.getEventType(evt);
231 if (eventType != NFC_PEER_EVENT_READY) {
235 let appId = this._window.document.nodePrincipal.appId;
236 this._nfcContentHelper.registerTargetForPeerReady(this._window, appId);
239 eventListenerWasRemoved: function(evt) {
240 let eventType = this.getEventType(evt);
241 if (eventType != NFC_PEER_EVENT_READY) {
245 let appId = this._window.document.nodePrincipal.appId;
246 this._nfcContentHelper.unregisterTargetForPeerReady(this._window, appId);
249 notifyPeerReady: function notifyPeerReady(sessionToken) {
250 if (this.hasDeadWrapper()) {
251 dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
255 this.session = sessionToken;
257 debug("fire onpeerready sessionToken : " + sessionToken);
259 "peer":this.getNFCPeer(sessionToken)
261 let event = new this._window.MozNFCPeerEvent("peerready", eventData);
262 this.__DOM_IMPL__.dispatchEvent(event);
265 notifyPeerLost: function notifyPeerLost(sessionToken) {
266 if (this.hasDeadWrapper()) {
267 dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
271 if (sessionToken != this.session) {
272 dump("Unpaired session for notifyPeerLost." + sessionToken);
276 if (this.nfcObject && (this.nfcObject.session == sessionToken)) {
277 this.nfcObject.invalidate();
278 this.nfcObject = null;
283 debug("fire onpeerlost");
284 let event = new this._window.Event("peerlost");
285 this.__DOM_IMPL__.dispatchEvent(event);
288 getEventType: function getEventType(evt) {
292 eventType = NFC_PEER_EVENT_READY;
295 eventType = NFC_PEER_EVENT_LOST;
303 hasDeadWrapper: function hasDeadWrapper() {
304 return Cu.isDeadWrapper(this._window) || Cu.isDeadWrapper(this.__DOM_IMPL__);
307 classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
308 contractID: "@mozilla.org/navigatorNfc;1",
309 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
310 Ci.nsIDOMGlobalPropertyInitializer,
311 Ci.nsINfcPeerEventListener]),
314 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);