|
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/. */ |
|
4 |
|
5 /* Copyright © 2013, Deutsche Telekom, Inc. */ |
|
6 |
|
7 "use strict"; |
|
8 |
|
9 const DEBUG = false; |
|
10 function debug(s) { |
|
11 if (DEBUG) dump("-*- Nfc DOM: " + s + "\n"); |
|
12 } |
|
13 |
|
14 const Cc = Components.classes; |
|
15 const Ci = Components.interfaces; |
|
16 const Cu = Components.utils; |
|
17 |
|
18 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
19 Cu.import("resource://gre/modules/Services.jsm"); |
|
20 Cu.import("resource://gre/modules/ObjectWrapper.jsm"); |
|
21 |
|
22 XPCOMUtils.defineLazyServiceGetter(this, |
|
23 "appsService", |
|
24 "@mozilla.org/AppsService;1", |
|
25 "nsIAppsService"); |
|
26 const NFC_PEER_EVENT_READY = 0x01; |
|
27 const NFC_PEER_EVENT_LOST = 0x02; |
|
28 |
|
29 /** |
|
30 * NFCTag |
|
31 */ |
|
32 function MozNFCTag() { |
|
33 debug("In MozNFCTag Constructor"); |
|
34 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
|
35 .getService(Ci.nsINfcContentHelper); |
|
36 this.session = null; |
|
37 |
|
38 // Map WebIDL declared enum map names to integer |
|
39 this._techTypesMap = []; |
|
40 this._techTypesMap['NFC_A'] = 0; |
|
41 this._techTypesMap['NFC_B'] = 1; |
|
42 this._techTypesMap['NFC_ISO_DEP'] = 2; |
|
43 this._techTypesMap['NFC_F'] = 3; |
|
44 this._techTypesMap['NFC_V'] = 4; |
|
45 this._techTypesMap['NDEF'] = 5; |
|
46 this._techTypesMap['NDEF_FORMATABLE'] = 6; |
|
47 this._techTypesMap['MIFARE_CLASSIC'] = 7; |
|
48 this._techTypesMap['MIFARE_ULTRALIGHT'] = 8; |
|
49 this._techTypesMap['NFC_BARCODE'] = 9; |
|
50 this._techTypesMap['P2P'] = 10; |
|
51 } |
|
52 MozNFCTag.prototype = { |
|
53 _nfcContentHelper: null, |
|
54 _window: null, |
|
55 |
|
56 initialize: function(aWindow, aSessionToken) { |
|
57 this._window = aWindow; |
|
58 this.setSessionToken(aSessionToken); |
|
59 }, |
|
60 |
|
61 // ChromeOnly interface |
|
62 setSessionToken: function setSessionToken(aSessionToken) { |
|
63 debug("Setting session token."); |
|
64 this.session = aSessionToken; |
|
65 // report to NFC worker: |
|
66 this._nfcContentHelper.setSessionToken(aSessionToken); |
|
67 }, |
|
68 |
|
69 _techTypesMap: null, |
|
70 |
|
71 // NFCTag interface: |
|
72 getDetailsNDEF: function getDetailsNDEF() { |
|
73 return this._nfcContentHelper.getDetailsNDEF(this._window, this.session); |
|
74 }, |
|
75 readNDEF: function readNDEF() { |
|
76 return this._nfcContentHelper.readNDEF(this._window, this.session); |
|
77 }, |
|
78 writeNDEF: function writeNDEF(records) { |
|
79 return this._nfcContentHelper.writeNDEF(this._window, records, this.session); |
|
80 }, |
|
81 makeReadOnlyNDEF: function makeReadOnlyNDEF() { |
|
82 return this._nfcContentHelper.makeReadOnlyNDEF(this._window, this.session); |
|
83 }, |
|
84 connect: function connect(enum_tech_type) { |
|
85 let int_tech_type = this._techTypesMap[enum_tech_type]; |
|
86 return this._nfcContentHelper.connect(this._window, int_tech_type, this.session); |
|
87 }, |
|
88 close: function close() { |
|
89 return this._nfcContentHelper.close(this._window, this.session); |
|
90 }, |
|
91 |
|
92 classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"), |
|
93 contractID: "@mozilla.org/nfc/NFCTag;1", |
|
94 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
|
95 Ci.nsIDOMGlobalPropertyInitializer]), |
|
96 }; |
|
97 |
|
98 /** |
|
99 * NFCPeer |
|
100 */ |
|
101 function MozNFCPeer() { |
|
102 debug("In MozNFCPeer Constructor"); |
|
103 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
|
104 .getService(Ci.nsINfcContentHelper); |
|
105 this.session = null; |
|
106 } |
|
107 MozNFCPeer.prototype = { |
|
108 _nfcContentHelper: null, |
|
109 _window: null, |
|
110 |
|
111 initialize: function(aWindow, aSessionToken) { |
|
112 this._window = aWindow; |
|
113 this.setSessionToken(aSessionToken); |
|
114 }, |
|
115 |
|
116 // ChromeOnly interface |
|
117 setSessionToken: function setSessionToken(aSessionToken) { |
|
118 debug("Setting session token."); |
|
119 this.session = aSessionToken; |
|
120 // report to NFC worker: |
|
121 return this._nfcContentHelper.setSessionToken(aSessionToken); |
|
122 }, |
|
123 |
|
124 // NFCPeer interface: |
|
125 sendNDEF: function sendNDEF(records) { |
|
126 // Just forward sendNDEF to writeNDEF |
|
127 return this._nfcContentHelper.writeNDEF(this._window, records, this.session); |
|
128 }, |
|
129 |
|
130 sendFile: function sendFile(blob) { |
|
131 let data = { |
|
132 "blob": blob |
|
133 }; |
|
134 return this._nfcContentHelper.sendFile(this._window, |
|
135 Cu.cloneInto(data, this._window), |
|
136 this.session); |
|
137 }, |
|
138 |
|
139 classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"), |
|
140 contractID: "@mozilla.org/nfc/NFCPeer;1", |
|
141 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
|
142 Ci.nsIDOMGlobalPropertyInitializer]), |
|
143 }; |
|
144 |
|
145 /** |
|
146 * Navigator NFC object |
|
147 */ |
|
148 function mozNfc() { |
|
149 debug("In mozNfc Constructor"); |
|
150 try { |
|
151 this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
|
152 .getService(Ci.nsINfcContentHelper); |
|
153 } catch(e) { |
|
154 debug("No NFC support.") |
|
155 } |
|
156 } |
|
157 mozNfc.prototype = { |
|
158 _nfcContentHelper: null, |
|
159 _window: null, |
|
160 _wrap: function _wrap(obj) { |
|
161 return Cu.cloneInto(obj, this._window); |
|
162 }, |
|
163 |
|
164 init: function init(aWindow) { |
|
165 debug("mozNfc init called"); |
|
166 this._window = aWindow; |
|
167 }, |
|
168 |
|
169 // Only apps which have nfc-manager permission can call the following interfaces |
|
170 // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus', |
|
171 // 'startPoll', 'stopPoll', and 'powerOff'. |
|
172 checkP2PRegistration: function checkP2PRegistration(manifestUrl) { |
|
173 // Get the AppID and pass it to ContentHelper |
|
174 let appID = appsService.getAppLocalIdByManifestURL(manifestUrl); |
|
175 return this._nfcContentHelper.checkP2PRegistration(this._window, appID); |
|
176 }, |
|
177 |
|
178 notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) { |
|
179 let appID = appsService.getAppLocalIdByManifestURL(manifestUrl); |
|
180 // Notify chrome process of user's acknowledgement |
|
181 this._nfcContentHelper.notifyUserAcceptedP2P(this._window, appID); |
|
182 }, |
|
183 |
|
184 notifySendFileStatus: function notifySendFileStatus(status, requestId) { |
|
185 this._nfcContentHelper.notifySendFileStatus(this._window, |
|
186 status, requestId); |
|
187 }, |
|
188 |
|
189 startPoll: function startPoll() { |
|
190 return this._nfcContentHelper.startPoll(this._window); |
|
191 }, |
|
192 |
|
193 stopPoll: function stopPoll() { |
|
194 return this._nfcContentHelper.stopPoll(this._window); |
|
195 }, |
|
196 |
|
197 powerOff: function powerOff() { |
|
198 return this._nfcContentHelper.powerOff(this._window); |
|
199 }, |
|
200 |
|
201 getNFCTag: function getNFCTag(sessionToken) { |
|
202 let obj = new MozNFCTag(); |
|
203 let nfcTag = this._window.MozNFCTag._create(this._window, obj); |
|
204 if (nfcTag) { |
|
205 obj.initialize(this._window, sessionToken); |
|
206 return nfcTag; |
|
207 } else { |
|
208 debug("Error: Unable to create NFCTag"); |
|
209 return null; |
|
210 } |
|
211 }, |
|
212 |
|
213 getNFCPeer: function getNFCPeer(sessionToken) { |
|
214 let obj = new MozNFCPeer(); |
|
215 let nfcPeer = this._window.MozNFCPeer._create(this._window, obj); |
|
216 if (nfcPeer) { |
|
217 obj.initialize(this._window, sessionToken); |
|
218 return nfcPeer; |
|
219 } else { |
|
220 debug("Error: Unable to create NFCPeer"); |
|
221 return null; |
|
222 } |
|
223 }, |
|
224 |
|
225 // get/set onpeerready |
|
226 get onpeerready() { |
|
227 return this.__DOM_IMPL__.getEventHandler("onpeerready"); |
|
228 }, |
|
229 |
|
230 set onpeerready(handler) { |
|
231 this.__DOM_IMPL__.setEventHandler("onpeerready", handler); |
|
232 }, |
|
233 |
|
234 // get/set onpeerlost |
|
235 get onpeerlost() { |
|
236 return this.__DOM_IMPL__.getEventHandler("onpeerlost"); |
|
237 }, |
|
238 |
|
239 set onpeerlost(handler) { |
|
240 this.__DOM_IMPL__.setEventHandler("onpeerlost", handler); |
|
241 }, |
|
242 |
|
243 eventListenerWasAdded: function(evt) { |
|
244 let eventType = this.getEventType(evt); |
|
245 if (eventType == -1) |
|
246 return; |
|
247 this.registerTarget(eventType); |
|
248 }, |
|
249 |
|
250 eventListenerWasRemoved: function(evt) { |
|
251 let eventType = this.getEventType(evt); |
|
252 if (eventType == -1) |
|
253 return; |
|
254 this.unregisterTarget(eventType); |
|
255 }, |
|
256 |
|
257 registerTarget: function registerTarget(event) { |
|
258 let self = this; |
|
259 let appId = this._window.document.nodePrincipal.appId; |
|
260 this._nfcContentHelper.registerTargetForPeerEvent(this._window, appId, |
|
261 event, function(evt, sessionToken) { |
|
262 self.session = sessionToken; |
|
263 self.firePeerEvent(evt, sessionToken); |
|
264 }); |
|
265 }, |
|
266 |
|
267 unregisterTarget: function unregisterTarget(event) { |
|
268 let appId = this._window.document.nodePrincipal.appId; |
|
269 this._nfcContentHelper.unregisterTargetForPeerEvent(this._window, |
|
270 appId, event); |
|
271 }, |
|
272 |
|
273 getEventType: function getEventType(evt) { |
|
274 let eventType = -1; |
|
275 switch (evt) { |
|
276 case 'peerready': |
|
277 eventType = NFC_PEER_EVENT_READY; |
|
278 break; |
|
279 case 'peerlost': |
|
280 eventType = NFC_PEER_EVENT_LOST; |
|
281 break; |
|
282 default: |
|
283 break; |
|
284 } |
|
285 return eventType; |
|
286 }, |
|
287 |
|
288 firePeerEvent: function firePeerEvent(evt, sessionToken) { |
|
289 let peerEvent = (NFC_PEER_EVENT_READY === evt) ? "peerready" : "peerlost"; |
|
290 let detail = { |
|
291 "detail":sessionToken |
|
292 }; |
|
293 let event = new this._window.CustomEvent(peerEvent, this._wrap(detail)); |
|
294 this.__DOM_IMPL__.dispatchEvent(event); |
|
295 }, |
|
296 |
|
297 classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"), |
|
298 contractID: "@mozilla.org/navigatorNfc;1", |
|
299 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
|
300 Ci.nsIDOMGlobalPropertyInitializer]), |
|
301 }; |
|
302 |
|
303 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]); |