From 6c31dc6433157997a939c8dc5a8490205d030404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez?= Date: Thu, 16 Oct 2014 11:25:35 +0200 Subject: [PATCH] Bug 1081873 - [Loop][Contacts API] mozContacts API should trigger DOMRequest.onerror if no permissions are granted to the consumer. r=anygregor --- dom/contacts/ContactManager.js | 95 +++++++++++++------ dom/contacts/tests/mochitest.ini | 2 +- .../tests/test_permission_denied.html | 92 ++++++++++++++++++ 3 files changed, 161 insertions(+), 28 deletions(-) create mode 100644 dom/contacts/tests/test_permission_denied.html diff --git a/dom/contacts/ContactManager.js b/dom/contacts/ContactManager.js index 22d172776007..6e9a39b853e2 100644 --- a/dom/contacts/ContactManager.js +++ b/dom/contacts/ContactManager.js @@ -256,7 +256,7 @@ ContactManager.prototype = { } else if (permValue == Ci.nsIPermissionManager.DENY_ACTION || permValue == Ci.nsIPermissionManager.UNKNOWN_ACTION) { if (aCancelCallback) { - aCancelCallback(); + aCancelCallback("PERMISSION_DENIED"); } return; } @@ -276,16 +276,14 @@ ContactManager.prototype = { types: typeArray, principal: principal, QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionRequest]), - allow: aAllowCallback || - function() { - if (DEBUG) - debug("Default allow contacts callback. " + access +"\n"); - }, - cancel: aCancelCallback || - function() { - if (DEBUG) - debug("Default cancel contacts callback. " + access +"\n"); - }, + allow: function() { + aAllowCallback && aAllowCallback(); + DEBUG && debug("Permission granted. Access " + access +"\n"); + }, + cancel: function() { + aCancelCallback && aCancelCallback("PERMISSION_DENIED"); + DEBUG && debug("Permission denied. Access " + access +"\n"); + }, window: this._window }; @@ -336,9 +334,17 @@ ContactManager.prototype = { let options = { contact: newContact, reason: reason }; let allowCallback = function() { - cpmm.sendAsyncMessage("Contact:Save", {requestID: requestID, options: options}); - }.bind(this) - this.askPermission(reason, request, allowCallback); + cpmm.sendAsyncMessage("Contact:Save", { + requestID: requestID, + options: options + }); + }.bind(this); + + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); + }; + + this.askPermission(reason, request, allowCallback, cancelCallback); return request; }, @@ -346,10 +352,19 @@ ContactManager.prototype = { if (DEBUG) debug("find! " + JSON.stringify(aOptions)); let request = this.createRequest(); let options = { findOptions: aOptions }; + let allowCallback = function() { - cpmm.sendAsyncMessage("Contacts:Find", {requestID: this.getRequestId({request: request, reason: "find"}), options: options}); - }.bind(this) - this.askPermission("find", request, allowCallback); + cpmm.sendAsyncMessage("Contacts:Find", { + requestID: this.getRequestId({request: request, reason: "find"}), + options: options + }); + }.bind(this); + + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); + }; + + this.askPermission("find", request, allowCallback, cancelCallback); return request; }, @@ -369,11 +384,19 @@ ContactManager.prototype = { getAll: function CM_getAll(aOptions) { if (DEBUG) debug("getAll: " + JSON.stringify(aOptions)); let [cursorId, cursor] = this.createCursor(); + let allowCallback = function() { cpmm.sendAsyncMessage("Contacts:GetAll", { - cursorId: cursorId, findOptions: aOptions}); + cursorId: cursorId, + findOptions: aOptions + }); }.bind(this); - this.askPermission("find", cursor, allowCallback); + + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(cursor, reason); + }; + + this.askPermission("find", cursor, allowCallback, cancelCallback); return cursor; }, @@ -412,10 +435,19 @@ ContactManager.prototype = { } let options = { id: id }; + let allowCallback = function() { - cpmm.sendAsyncMessage("Contact:Remove", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options}); + cpmm.sendAsyncMessage("Contact:Remove", { + requestID: this.getRequestId({request: request, reason: "remove"}), + options: options + }); }.bind(this); - this.askPermission("remove", request, allowCallback); + + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); + }; + + this.askPermission("remove", request, allowCallback, cancelCallback); return request; }, @@ -423,10 +455,19 @@ ContactManager.prototype = { if (DEBUG) debug("clear"); let request = this.createRequest(); let options = {}; + let allowCallback = function() { - cpmm.sendAsyncMessage("Contacts:Clear", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options}); + cpmm.sendAsyncMessage("Contacts:Clear", { + requestID: this.getRequestId({request: request, reason: "remove"}), + options: options + }); }.bind(this); - this.askPermission("remove", request, allowCallback); + + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); + }; + + this.askPermission("remove", request, allowCallback, cancelCallback); return request; }, @@ -439,8 +480,8 @@ ContactManager.prototype = { }); }.bind(this); - let cancelCallback = function() { - Services.DOMRequest.fireError(request, ""); + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); }; this.askPermission("revision", request, allowCallback, cancelCallback); @@ -456,8 +497,8 @@ ContactManager.prototype = { }); }.bind(this); - let cancelCallback = function() { - Services.DOMRequest.fireError(request, ""); + let cancelCallback = function(reason) { + Services.DOMRequest.fireErrorAsync(request, reason); }; this.askPermission("count", request, allowCallback, cancelCallback); diff --git a/dom/contacts/tests/mochitest.ini b/dom/contacts/tests/mochitest.ini index 1dcffd9ca417..49bb1600ea54 100644 --- a/dom/contacts/tests/mochitest.ini +++ b/dom/contacts/tests/mochitest.ini @@ -21,4 +21,4 @@ skip-if = (toolkit == 'gonk' && debug) #debug-only failure support-files = test_migration_chrome.js skip-if = os == "android" - +[test_permission_denied.html] diff --git a/dom/contacts/tests/test_permission_denied.html b/dom/contacts/tests/test_permission_denied.html new file mode 100644 index 000000000000..61025f7160fa --- /dev/null +++ b/dom/contacts/tests/test_permission_denied.html @@ -0,0 +1,92 @@ + + + + + Test for Bug 1081873 + + + + + + +Mozilla Bug 1081873 +

+ +
+
+
+
+ +