diff --git a/dom/nfc/NfcContentHelper.js b/dom/nfc/NfcContentHelper.js index e188b2e55067..62770afdd9ca 100644 --- a/dom/nfc/NfcContentHelper.js +++ b/dom/nfc/NfcContentHelper.js @@ -139,22 +139,19 @@ NfcContentHelper.prototype = { }, // NFCTag interface - readNDEF: function readNDEF(sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + readNDEF: function readNDEF(sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:ReadNDEF", { requestId: requestId, sessionToken: sessionToken }); - return request; }, - writeNDEF: function writeNDEF(records, sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + writeNDEF: function writeNDEF(records, sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; let encodedRecords = this.encodeNDEFRecords(records); cpmm.sendAsyncMessage("NFC:WriteNDEF", { @@ -162,69 +159,58 @@ NfcContentHelper.prototype = { sessionToken: sessionToken, records: encodedRecords }); - return request; }, - makeReadOnlyNDEF: function makeReadOnlyNDEF(sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + makeReadOnly: function makeReadOnly(sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:MakeReadOnly", { requestId: requestId, sessionToken: sessionToken }); - return request; }, - format: function format(sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + format: function format(sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:Format", { requestId: requestId, sessionToken: sessionToken }); - return request; }, - connect: function connect(techType, sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + connect: function connect(techType, sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:Connect", { requestId: requestId, sessionToken: sessionToken, techType: techType }); - return request; }, - close: function close(sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + close: function close(sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:Close", { requestId: requestId, sessionToken: sessionToken }); - return request; }, - sendFile: function sendFile(data, sessionToken) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + sendFile: function sendFile(data, sessionToken, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:SendFile", { requestId: requestId, sessionToken: sessionToken, blob: data.blob }); - return request; }, notifySendFileStatus: function notifySendFileStatus(status, requestId) { @@ -247,16 +233,14 @@ NfcContentHelper.prototype = { cpmm.sendAsyncMessage("NFC:UnregisterPeerReadyTarget", { appId: appId }); }, - checkP2PRegistration: function checkP2PRegistration(appId) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + checkP2PRegistration: function checkP2PRegistration(appId, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:CheckP2PRegistration", { appId: appId, requestId: requestId }); - return request; }, notifyUserAcceptedP2P: function notifyUserAcceptedP2P(appId) { @@ -265,16 +249,13 @@ NfcContentHelper.prototype = { }); }, - changeRFState: function changeRFState(rfState) { - let request = Services.DOMRequest.createRequest(this._window); - let requestId = btoa(this.getRequestId(request)); - this._requestMap[requestId] = this._window; + changeRFState: function changeRFState(rfState, callback) { + let requestId = callback.getCallbackId(); + this._requestMap[requestId] = callback; cpmm.sendAsyncMessage("NFC:ChangeRFState", {requestId: requestId, rfState: rfState}); - return request; - }, // nsIObserver @@ -295,31 +276,6 @@ NfcContentHelper.prototype = { }, // nsIMessageListener - - fireRequestSuccess: function fireRequestSuccess(requestId, result) { - let request = this.takeRequest(requestId); - if (!request) { - debug("not firing success for id: " + requestId); - return; - } - - debug("fire request success, id: " + requestId); - Services.DOMRequest.fireSuccess(request, result); - }, - - fireRequestError: function fireRequestError(requestId, errorMsg) { - let request = this.takeRequest(requestId); - if (!request) { - debug("not firing error for id: " + requestId + - ", errormsg: " + errorMsg); - return; - } - - debug("fire request error, id: " + requestId + - ", errormsg: " + errorMsg); - Services.DOMRequest.fireError(request, errorMsg); - }, - receiveMessage: function receiveMessage(message) { DEBUG && debug("Message received: " + JSON.stringify(message)); let result = message.json; @@ -338,11 +294,7 @@ NfcContentHelper.prototype = { case "NFC:FormatResponse": case "NFC:NotifySendFileStatusResponse": case "NFC:ChangeRFStateResponse": - if (result.errorMsg) { - this.fireRequestError(atob(result.requestId), result.errorMsg); - } else { - this.fireRequestSuccess(atob(result.requestId), result); - } + this.handleGeneralResponse(result); break; case "NFC:DOMEvent": switch (result.event) { @@ -381,37 +333,60 @@ NfcContentHelper.prototype = { } }, - handleReadNDEFResponse: function handleReadNDEFResponse(result) { - let requester = this._requestMap[result.requestId]; - if (!requester) { - debug("Response Invalid requestId=" + result.requestId); + handleGeneralResponse: function handleReadNDEFResponse(result) { + let requestId = result.requestId; + let callback = this._requestMap[requestId]; + if (!callback) { + debug("not firing message " + result.type + " for id: " + requestId); return; } - delete this._requestMap[result.requestId]; + delete this._requestMap[requestId]; if (result.errorMsg) { - this.fireRequestError(atob(result.requestId), result.errorMsg); + callback.notifyError(result.errorMsg); + } else { + callback.notifySuccess(); + } + }, + + handleReadNDEFResponse: function handleReadNDEFResponse(result) { + let requestId = result.requestId; + let callback = this._requestMap[requestId]; + if (!callback) { + debug("not firing message handleReadNDEFResponse for id: " + requestId); + return; + } + delete this._requestMap[requestId]; + + if (result.errorMsg) { + callback.notifyError(result.errorMsg); return; } - let requestId = atob(result.requestId); let ndefMsg = []; let records = result.records; for (let i = 0; i < records.length; i++) { let record = records[i]; - ndefMsg.push(new requester.MozNDEFRecord({tnf: record.tnf, - type: record.type, - id: record.id, - payload: record.payload})); + ndefMsg.push(new this._window.MozNDEFRecord({tnf: record.tnf, + type: record.type, + id: record.id, + payload: record.payload})); } - this.fireRequestSuccess(requestId, ndefMsg); + callback.notifySuccessWithNDEFRecords(ndefMsg); }, handleCheckP2PRegistrationResponse: function handleCheckP2PRegistrationResponse(result) { + let requestId = result.requestId; + let callback = this._requestMap[requestId]; + if (!callback) { + debug("not firing message handleCheckP2PRegistrationResponse for id: " + requestId); + return; + } + delete this._requestMap[requestId]; + // Privilaged status API. Always fire success to avoid using exposed props. // The receiver must check the boolean mapped status code to handle. - let requestId = atob(result.requestId); - this.fireRequestSuccess(requestId, !result.errorMsg); + callback.notifySuccessWithBoolean(!result.errorMsg); }, }; diff --git a/dom/nfc/nsINfcContentHelper.idl b/dom/nfc/nsINfcContentHelper.idl index c3e46e92a5ae..1e0c7f8d4712 100644 --- a/dom/nfc/nsINfcContentHelper.idl +++ b/dom/nfc/nsINfcContentHelper.idl @@ -3,9 +3,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsISupports.idl" -#include "nsIDOMDOMRequest.idl" interface nsIVariant; +interface nsIDOMWindow; [scriptable, uuid(9b43bdda-52f4-4712-b28c-ad7cba736e14)] interface nsINfcTagEvent : nsISupports @@ -65,20 +65,106 @@ interface nsINfcEventListener : nsISupports void notifyPeerLost(in DOMString sessionToken); }; -[scriptable, uuid(9343ae1a-6e2f-11e4-b5c4-fbb166b38b62)] +[scriptable, uuid(a8ef3590-d853-4766-b54a-a4547da4dde4)] +interface nsINfcRequestCallback : nsISupports +{ + DOMString getCallbackId(); + + void notifySuccess(); + + void notifySuccessWithBoolean(in boolean result); + + void notifySuccessWithNDEFRecords(in nsIVariant records); + + void notifyError(in DOMString errorMsg); +}; + +[scriptable, uuid(9da02537-c4d0-4b2d-b294-d3250ff1720e)] interface nsINfcContentHelper : nsISupports { void init(in nsIDOMWindow window); boolean checkSessionToken(in DOMString sessionToken, in boolean isP2P); - nsIDOMDOMRequest readNDEF(in DOMString sessionToken); - nsIDOMDOMRequest writeNDEF(in nsIVariant records, in DOMString sessionToken); - nsIDOMDOMRequest makeReadOnly(in DOMString sessionToken); - nsIDOMDOMRequest format(in DOMString sessionToken); + /** + * Read current NDEF data on the tag. + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void readNDEF(in DOMString sessionToken, + in nsINfcRequestCallback callback); - nsIDOMDOMRequest connect(in unsigned long techType, in DOMString sessionToken); - nsIDOMDOMRequest close(in DOMString sessionToken); + /** + * Write NDEF data to a peer device or a tag. + * + * @param records + * NDEF records to be written + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void writeNDEF(in nsIVariant records, + in DOMString sessionToken, + in nsINfcRequestCallback callback); + + /** + * Make a tag read-only + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void makeReadOnly(in DOMString sessionToken, + in nsINfcRequestCallback callback); + + /** + * Format a tag as NDEF + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void format(in DOMString sessionToken, + in nsINfcRequestCallback callback); + + /** + * Enable I/O operations to the tag + * + * @param techType + * Interface to a technology in a Tag + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void connect(in unsigned long techType, + in DOMString sessionToken, + in nsINfcRequestCallback callback); + + /** + * Disable I/O operations to the tag + * + * @param sessionToken + * Current token + * + * @param callback + * Called when request is finished + */ + void close(in DOMString sessionToken, + in nsINfcRequestCallback callback); /** * Initiate send file operation. @@ -91,11 +177,12 @@ interface nsINfcContentHelper : nsISupports * @param sessionToken * Current token * - * Returns DOMRequest, if initiation of send file operation is successful - * then 'onsuccess' is called else 'onerror' + * @param callback + * Called when request is finished */ - nsIDOMDOMRequest sendFile(in jsval blob, - in DOMString sessionToken); + void sendFile(in jsval blob, + in DOMString sessionToken, + in nsINfcRequestCallback callback); /** * Add the event listener. @@ -127,9 +214,11 @@ interface nsINfcContentHelper : nsISupports * @param appId * Application ID to be updated with parent process * - * Returns DOMRequest, if appId is registered then 'onsuccess' is called else 'onerror' + * @param callback + * Called when request is finished */ - nsIDOMDOMRequest checkP2PRegistration(in unsigned long appId); + void checkP2PRegistration(in unsigned long appId, + in nsINfcRequestCallback callback); /** * Notify the parent process that user has accepted to share nfc message on P2P UI @@ -155,6 +244,10 @@ interface nsINfcContentHelper : nsISupports * Change RF state. * * @param rfState. Possible values are 'idle', 'listen' and 'discovery'. + * + * @param callback + * Called when request is finished */ - nsIDOMDOMRequest changeRFState(in DOMString rfState); + void changeRFState(in DOMString rfState, + in nsINfcRequestCallback callback); }; diff --git a/dom/nfc/nsNfc.js b/dom/nfc/nsNfc.js index b7a1dfe347b8..921caa59857d 100644 --- a/dom/nfc/nsNfc.js +++ b/dom/nfc/nsNfc.js @@ -17,12 +17,78 @@ const Cu = Components.utils; Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); XPCOMUtils.defineLazyServiceGetter(this, "appsService", "@mozilla.org/AppsService;1", "nsIAppsService"); +function NfcCallback(aWindow) { + this.initDOMRequestHelper(aWindow, null); + this._createPromise(); +} +NfcCallback.prototype = { + __proto__: DOMRequestIpcHelper.prototype, + + promise: null, + _requestId: null, + + _createPromise: function _createPromise() { + this.promise = this.createPromise((aResolve, aReject) => { + this._requestId = btoa(this.getPromiseResolverId({ + resolve: aResolve, + reject: aReject + })); + }); + }, + + getCallbackId: function getCallbackId() { + return this._requestId; + }, + + notifySuccess: function notifySuccess() { + let resolver = this.takePromiseResolver(atob(this._requestId)); + if (!resolver) { + debug("can not find promise resolver for id: " + this._requestId); + return; + } + resolver.resolve(); + }, + + notifySuccessWithBoolean: function notifySuccessWithBoolean(aResult) { + let resolver = this.takePromiseResolver(atob(this._requestId)); + if (!resolver) { + debug("can not find promise resolver for id: " + this._requestId); + return; + } + resolver.resolve(aResult); + }, + + notifySuccessWithNDEFRecords: function notifySuccessWithNDEFRecords(aRecords) { + let resolver = this.takePromiseResolver(atob(this._requestId)); + if (!resolver) { + debug("can not find promise resolver for id: " + this._requestId); + return; + } + resolver.resolve(aRecords); + }, + + notifyError: function notifyError(aErrorMsg) { + let resolver = this.takePromiseResolver(atob(this._requestId)); + if (!resolver) { + debug("can not find promise resolver for id: " + this._requestId + + ", errormsg: " + aErrorMsg); + return; + } + resolver.reject(aErrorMsg); + }, + + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, + Ci.nsIObserver, + Ci.nsINfcRequestCallback]), +}; + /** * Implementation of NFCTag. * @@ -64,7 +130,9 @@ MozNFCTagImpl.prototype = { throw new this._window.DOMError("InvalidStateError", "NFCTag object is invalid"); } - return this._nfcContentHelper.readNDEF(this.session); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.readNDEF(this.session, callback); + return callback.promise; }, writeNDEF: function writeNDEF(records) { @@ -85,7 +153,9 @@ MozNFCTagImpl.prototype = { throw new this._window.DOMError("NotSupportedError", "Exceed max NDEF size"); } - return this._nfcContentHelper.writeNDEF(records, this.session); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.writeNDEF(records, this.session, callback); + return callback.promise; }, makeReadOnly: function makeReadOnly() { @@ -98,7 +168,9 @@ MozNFCTagImpl.prototype = { "NFCTag object cannot be made read-only"); } - return this._nfcContentHelper.makeReadOnly(this.session); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.makeReadOnly(this.session, callback); + return callback.promise; }, format: function format() { @@ -111,7 +183,9 @@ MozNFCTagImpl.prototype = { "NFCTag object is not formatable"); } - return this._nfcContentHelper.format(this.session); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.format(this.session, callback); + return callback.promise; }, classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"), @@ -146,7 +220,9 @@ MozNFCPeerImpl.prototype = { } // Just forward sendNDEF to writeNDEF - return this._nfcContentHelper.writeNDEF(records, this.session); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.writeNDEF(records, this.session, callback); + return callback.promise; }, sendFile: function sendFile(blob) { @@ -157,8 +233,11 @@ MozNFCPeerImpl.prototype = { let data = { "blob": blob }; - return this._nfcContentHelper.sendFile(Cu.cloneInto(data, this._window), - this.session); + + let callback = new NfcCallback(this._window); + this._nfcContentHelper.sendFile(Cu.cloneInto(data, this._window), + this.session, callback); + return callback.promise; }, classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"), @@ -217,7 +296,10 @@ MozNFCImpl.prototype = { checkP2PRegistration: function checkP2PRegistration(manifestUrl) { // Get the AppID and pass it to ContentHelper let appID = appsService.getAppLocalIdByManifestURL(manifestUrl); - return this._nfcContentHelper.checkP2PRegistration(appID); + + let callback = new NfcCallback(this._window); + this._nfcContentHelper.checkP2PRegistration(appID, callback); + return callback.promise; }, notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) { @@ -231,15 +313,21 @@ MozNFCImpl.prototype = { }, startPoll: function startPoll() { - return this._nfcContentHelper.changeRFState(this.rfState.DISCOVERY); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.changeRFState(this.rfState.DISCOVERY, callback); + return callback.promise; }, stopPoll: function stopPoll() { - return this._nfcContentHelper.changeRFState(this.rfState.LISTEN); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.changeRFState(this.rfState.LISTEN, callback); + return callback.promise; }, powerOff: function powerOff() { - return this._nfcContentHelper.changeRFState(this.rfState.IDLE); + let callback = new NfcCallback(this._window); + this._nfcContentHelper.changeRFState(this.rfState.IDLE, callback); + return callback.promise; }, _createNFCPeer: function _createNFCPeer(sessionToken) { diff --git a/dom/nfc/tests/marionette/head.js b/dom/nfc/tests/marionette/head.js index 2e0c5b46c499..1117dfa8f8f4 100644 --- a/dom/nfc/tests/marionette/head.js +++ b/dom/nfc/tests/marionette/head.js @@ -194,22 +194,20 @@ let SNEP = (function() { function toggleNFC(enabled) { let deferred = Promise.defer(); - let req; + let promise; if (enabled) { - req = nfc.startPoll(); + promise = nfc.startPoll(); } else { - req = nfc.powerOff(); + promise = nfc.powerOff(); } - req.onsuccess = function() { + promise.then(() => { deferred.resolve(); - }; - - req.onerror = function() { + }).catch(() => { ok(false, 'operation failed, error ' + req.error.name); deferred.reject(); finish(); - }; + }); return deferred.promise; } diff --git a/dom/nfc/tests/marionette/test_nfc_checkP2PRegistration.js b/dom/nfc/tests/marionette/test_nfc_checkP2PRegistration.js index 8eccf75f6a6c..e4e80c73fb0e 100644 --- a/dom/nfc/tests/marionette/test_nfc_checkP2PRegistration.js +++ b/dom/nfc/tests/marionette/test_nfc_checkP2PRegistration.js @@ -111,16 +111,14 @@ function registerOnpeerready() { function fireCheckP2PReg(manifestUrl) { let deferred = Promise.defer(); - let request = nfc.checkP2PRegistration(manifestUrl); - request.onsuccess = function() { + let promise = nfc.checkP2PRegistration(manifestUrl); + promise.then(() => { ok(true, 'checkP2PRegistration allways results in success'); deferred.resolve(request.result); - }; - - request.onerror = function() { + }).catch(() => { ok(false, 'see NfcContentHelper.handleCheckP2PRegistrationResponse'); deferred.reject(); - }; + }); return deferred.promise; } diff --git a/dom/nfc/tests/marionette/test_nfc_enabled.js b/dom/nfc/tests/marionette/test_nfc_enabled.js index a3ec741fb647..d7591edeca2a 100644 --- a/dom/nfc/tests/marionette/test_nfc_enabled.js +++ b/dom/nfc/tests/marionette/test_nfc_enabled.js @@ -6,41 +6,38 @@ MARIONETTE_HEAD_JS = 'head.js'; function testEnableNFC() { log('Running \'testEnableNFC\''); - let req = nfc.startPoll(); - req.onsuccess = function () { + let promise = nfc.startPoll(); + promise.then(() => { ok(true); runNextTest(); - }; - req.onerror = function () { + }).catch(() => { ok(false, "startPoll failed"); runNextTest(); - }; + }); } function testDisableNFC() { log('Running \'testDisableNFC\''); - let req = nfc.powerOff(); - req.onsuccess = function () { + let promise = nfc.powerOff(); + promise.then(() => { ok(true); runNextTest(); - }; - req.onerror = function () { + }).catch(() => { ok(false, "powerOff failed"); runNextTest(); - }; + }); } function testStopPollNFC() { log('Running \'testStopPollNFC\''); - let req = nfc.stopPoll(); - req.onsuccess = function () { + let promise = nfc.stopPoll(); + promise.then(() => { ok(true); runNextTest(); - }; - req.onerror = function () { + }).catch(() => { ok(false, "stopPoll failed"); runNextTest(); - }; + }); } let tests = [ diff --git a/dom/nfc/tests/marionette/test_nfc_peer.js b/dom/nfc/tests/marionette/test_nfc_peer.js index 984b650e74da..fb184e7e23a2 100644 --- a/dom/nfc/tests/marionette/test_nfc_peer.js +++ b/dom/nfc/tests/marionette/test_nfc_peer.js @@ -44,20 +44,18 @@ function handleTechnologyDiscoveredRE0ForP2PRegFailure(msg) { nfc.onpeerready = peerReadyCb; - let request = nfc.checkP2PRegistration(INCORRECT_MANIFEST_URL); - request.onsuccess = function (evt) { + let promise = nfc.checkP2PRegistration(INCORRECT_MANIFEST_URL); + promise.then(evt => { is(request.result, false, "check for P2P registration result"); nfc.onpeerready = null; NCI.deactivate().then(() => toggleNFC(false)).then(runNextTest); - } - - request.onerror = function () { + }).catch(() => { ok(false, "checkP2PRegistration failed."); nfc.onpeerready = null; NCI.deactivate().then(() => toggleNFC(false)).then(runNextTest); - } + }); } function testPeerReady() { diff --git a/dom/nfc/tests/marionette/test_nfc_peer_sendndef.js b/dom/nfc/tests/marionette/test_nfc_peer_sendndef.js index 97d6d0531ef3..d4da90e46240 100644 --- a/dom/nfc/tests/marionette/test_nfc_peer_sendndef.js +++ b/dom/nfc/tests/marionette/test_nfc_peer_sendndef.js @@ -13,8 +13,8 @@ function sendNDEF(techType, sessionToken) { let ndef = [new MozNDEFRecord({tnf: tnf, type: type, payload: payload})]; let peer = window.navigator.mozNfc.getNFCPeer(sessionToken); - let req = peer.sendNDEF(ndef); - req.onsuccess = function() { + let promise = peer.sendNDEF(ndef); + promise.then(() => { log("Successfully sent NDEF message"); let cmd = "nfc snep put -1 -1"; /* read last SNEP PUT from emulator */ @@ -24,11 +24,10 @@ function sendNDEF(techType, sessionToken) { NDEF.compare(ndef, NDEF.parseString(result.pop())); toggleNFC(false).then(runNextTest); }); - }; - req.onerror = function() { + }).catch(() => { ok(false, "Failed to send NDEF message, error \'" + this.error + "\'"); toggleNFC(false).then(runNextTest); - }; + }); } function handleTechnologyDiscoveredRE0(msg) { diff --git a/dom/webidl/MozNFC.webidl b/dom/webidl/MozNFC.webidl index 930e2de87758..5fa99daddc99 100644 --- a/dom/webidl/MozNFC.webidl +++ b/dom/webidl/MozNFC.webidl @@ -42,7 +42,7 @@ interface MozNFCManager { * otherwise error */ [CheckPermissions="nfc-manager"] - DOMRequest checkP2PRegistration(DOMString manifestUrl); + Promise checkP2PRegistration(DOMString manifestUrl); /** * Notify that user has accepted to share nfc message on P2P UI @@ -60,19 +60,19 @@ interface MozNFCManager { * Power on the NFC hardware and start polling for NFC tags or devices. */ [CheckPermissions="nfc-manager"] - DOMRequest startPoll(); + Promise startPoll(); /** * Stop polling for NFC tags or devices. i.e. enter low power mode. */ [CheckPermissions="nfc-manager"] - DOMRequest stopPoll(); + Promise stopPoll(); /** * Power off the NFC hardware. */ [CheckPermissions="nfc-manager"] - DOMRequest powerOff(); + Promise powerOff(); }; [JSImplementation="@mozilla.org/navigatorNfc;1", diff --git a/dom/webidl/MozNFCPeer.webidl b/dom/webidl/MozNFCPeer.webidl index 8f96b85ef5ea..d9c327766519 100644 --- a/dom/webidl/MozNFCPeer.webidl +++ b/dom/webidl/MozNFCPeer.webidl @@ -10,10 +10,17 @@ [JSImplementation="@mozilla.org/nfc/NFCPeer;1", AvailableIn="CertifiedApps"] interface MozNFCPeer { + /** + * Send NDEF data to peer device. + */ [Throws] - DOMRequest sendNDEF(sequence records); + Promise sendNDEF(sequence records); + + /** + * Send file to peer device. + */ [Throws] - DOMRequest sendFile(Blob blob); + Promise sendFile(Blob blob); }; // Mozilla Only diff --git a/dom/webidl/MozNFCTag.webidl b/dom/webidl/MozNFCTag.webidl index b2107411d8f2..6ed7059edefd 100644 --- a/dom/webidl/MozNFCTag.webidl +++ b/dom/webidl/MozNFCTag.webidl @@ -66,17 +66,29 @@ interface MozNFCTag { */ readonly attribute boolean? canBeMadeReadOnly; + /** + * Read current NDEF data on the tag. + */ [Throws] - DOMRequest readNDEF(); + Promise> readNDEF(); + /** + * Write NDEF data to the tag. + */ [Throws] - DOMRequest writeNDEF(sequence records); + Promise writeNDEF(sequence records); + /** + * Make a tag read-only. + */ [Throws] - DOMRequest makeReadOnly(); + Promise makeReadOnly(); + /** + * Format a tag as NDEF. + */ [Throws] - DOMRequest format(); + Promise format(); }; // Mozilla Only