From 567fd0e731e1850dc00d7ceb72c8cbbbc3ed5e9f Mon Sep 17 00:00:00 2001 From: Chris Manchester Date: Tue, 11 Nov 2014 13:15:02 -0500 Subject: [PATCH] Bug 1095018 - Forward marionette's cookie interactions to the parent process for compatibility with e10s.;r=ato * * * Fixes based on review feedback. --- .../marionette/marionette-frame-manager.js | 6 + testing/marionette/marionette-listener.js | 103 ++++-------------- testing/marionette/marionette-server.js | 43 ++++++++ 3 files changed, 73 insertions(+), 79 deletions(-) diff --git a/testing/marionette/marionette-frame-manager.js b/testing/marionette/marionette-frame-manager.js index 46703c1231d..8243e92b7a1 100644 --- a/testing/marionette/marionette-frame-manager.js +++ b/testing/marionette/marionette-frame-manager.js @@ -202,6 +202,9 @@ FrameManager.prototype = { messageManager.addWeakMessageListener("Marionette:switchToModalOrigin", this.server); messageManager.addWeakMessageListener("Marionette:switchToFrame", this.server); messageManager.addWeakMessageListener("Marionette:switchedToFrame", this.server); + messageManager.addWeakMessageListener("Marionette:addCookie", this.server); + messageManager.addWeakMessageListener("Marionette:getVisibleCookies", this.server); + messageManager.addWeakMessageListener("Marionette:deleteCookie", this.server); messageManager.addWeakMessageListener("MarionetteFrame:handleModal", this); messageManager.addWeakMessageListener("MarionetteFrame:getCurrentFrameId", this); messageManager.addWeakMessageListener("MarionetteFrame:getInterruptedState", this); @@ -230,6 +233,9 @@ FrameManager.prototype = { messageManager.removeWeakMessageListener("Marionette:runEmulatorShell", this.server); messageManager.removeWeakMessageListener("Marionette:switchToFrame", this.server); messageManager.removeWeakMessageListener("Marionette:switchedToFrame", this.server); + messageManager.removeWeakMessageListener("Marionette:addCookie", this.server); + messageManager.removeWeakMessageListener("Marionette:getVisibleCookies", this.server); + messageManager.removeWeakMessageListener("Marionette:deleteCookie", this.server); messageManager.removeWeakMessageListener("MarionetteFrame:handleModal", this); messageManager.removeWeakMessageListener("MarionetteFrame:getCurrentFrameId", this); }, diff --git a/testing/marionette/marionette-listener.js b/testing/marionette/marionette-listener.js index 84696a35ccb..bf9e1f5a5fc 100644 --- a/testing/marionette/marionette-listener.js +++ b/testing/marionette/marionette-listener.js @@ -1800,8 +1800,7 @@ function switchToFrame(msg) { * Add a cookie to the document */ function addCookie(msg) { - cookie = msg.json.cookie; - + let cookie = msg.json.cookie; if (!cookie.expiry) { var date = new Date(); var thePresent = new Date(Date.now()); @@ -1832,17 +1831,12 @@ function addCookie(msg) { if (!document || !document.contentType.match(/html/i)) { sendError('You may only set cookies on html documents', 25, null, msg.json.command_id); } - let cookieManager; - try { - // Retrieving the cookie manager fails with e10s enabled. - cookieManager = Cc['@mozilla.org/cookiemanager;1']. - getService(Ci.nsICookieManager2); - } catch (ex) { - sendError("Error retrieving cookie manager: " + ex, 13, null, msg.json.command_id); + + let added = sendSyncMessage("Marionette:addCookie", {value: cookie}); + if (added[0] !== true) { + sendError("Error setting cookie", 13, null, msg.json.command_id); return; } - cookieManager.add(cookie.domain, cookie.path, cookie.name, cookie.value, - cookie.secure, false, false, cookie.expiry); sendOk(msg.json.command_id); } @@ -1852,12 +1846,7 @@ function addCookie(msg) { function getCookies(msg) { var toReturn = []; var cookies = getVisibleCookies(curFrame.location); - if (typeof cookies == "undefined") { - sendError("Error retrieving cookie manager", 13, null, msg.json.command_id); - return; - } - for (var i = 0; i < cookies.length; i++) { - var cookie = cookies[i]; + for (let cookie of cookies) { var expires = cookie.expires; if (expires == 0) { // Session cookie, don't return an expiry. expires = null; @@ -1873,7 +1862,6 @@ function getCookies(msg) { 'expiry': expires }); } - sendResponse({value: toReturn}, msg.json.command_id); } @@ -1881,23 +1869,15 @@ function getCookies(msg) { * Delete a cookie by name */ function deleteCookie(msg) { - var toDelete = msg.json.name; - - let cookieManager; - try { - // Retrieving the cookie manager fails with e10s enabled. - cookieManager = Cc['@mozilla.org/cookiemanager;1']. - getService(Ci.nsICookieManager); - } catch (ex) { - sendError("Error retrieving cookie manager: " + ex, 13, null, msg.json.command_id); - return; - } - - var cookies = getVisibleCookies(curFrame.location); - for (var i = 0; i < cookies.length; i++) { - var cookie = cookies[i]; + let toDelete = msg.json.name; + let cookies = getVisibleCookies(curFrame.location); + for (let cookie of cookies) { if (cookie.name == toDelete) { - cookieManager.remove(cookie.host, cookie.name, cookie.path, false); + let deleted = sendSyncMessage("Marionette:deleteCookie", {value: cookie}); + if (deleted[0] !== true) { + sendError("Could not delete cookie: " + msg.json.name, 13, null, msg.json.command_id); + return; + } } } @@ -1908,19 +1888,13 @@ function deleteCookie(msg) { * Delete all the visibile cookies on a page */ function deleteAllCookies(msg) { - let cookieManager; - try { - // Retrieving the cookie manager fails with e10s enabled. - cookieManager = Cc['@mozilla.org/cookiemanager;1']. - getService(Ci.nsICookieManager); - } catch (ex) { - sendError("Error retrieving cookie manager: " + ex, 13, null, msg.json.command_id); - return; - } let cookies = getVisibleCookies(curFrame.location); - for (let i = 0; i < cookies.length; i++) { - let cookie = cookies[i]; - cookieManager.remove(cookie.host, cookie.name, cookie.path, false); + for (let cookie of cookies) { + let deleted = sendSyncMessage("Marionette:deleteCookie", {value: cookie}); + if (!deleted[0]) { + sendError("Could not delete cookie: " + JSON.stringify(cookie), 13, null, msg.json.command_id); + return; + } } sendOk(msg.json.command_id); } @@ -1929,39 +1903,10 @@ function deleteAllCookies(msg) { * Get all the visible cookies from a location */ function getVisibleCookies(location) { - let results = []; - let currentPath = location.pathname; - if (!currentPath) currentPath = '/'; - let isForCurrentPath = function(aPath) { - return currentPath.indexOf(aPath) != -1; - } - - let cookieManager; - try { - // Retrieving the cookie manager fails with e10s enabled. - cookieManager = Cc['@mozilla.org/cookiemanager;1']. - getService(Ci.nsICookieManager); - } catch (ex) { - return; - } - - let enumerator = cookieManager.enumerator; - while (enumerator.hasMoreElements()) { - let cookie = enumerator.getNext().QueryInterface(Ci['nsICookie']); - - // Take the hostname and progressively shorten - let hostname = location.hostname; - do { - if ((cookie.host == '.' + hostname || cookie.host == hostname) - && isForCurrentPath(cookie.path)) { - results.push(cookie); - break; - } - hostname = hostname.replace(/^.*?\./, ''); - } while (hostname.indexOf('.') != -1); - } - - return results; + let currentPath = location.pathname || '/'; + let result = sendSyncMessage("Marionette:getVisibleCookies", + {value: [currentPath, location.hostname]}); + return result[0]; } function getAppCacheStatus(msg) { diff --git a/testing/marionette/marionette-server.js b/testing/marionette/marionette-server.js index 3279b7485bd..355e69341ce 100644 --- a/testing/marionette/marionette-server.js +++ b/testing/marionette/marionette-server.js @@ -49,6 +49,10 @@ let bypassOffline = false; let qemu = "0"; let device = null; +XPCOMUtils.defineLazyServiceGetter(this, "cookieManager", + "@mozilla.org/cookiemanager;1", + "nsICookieManager"); + try { XPCOMUtils.defineLazyGetter(this, "libcutils", function () { Cu.import("resource://gre/modules/systemlibs.js"); @@ -2678,6 +2682,45 @@ MarionetteServerConnection.prototype = { this.currentFrameElement = message.json.frameValue; } break; + case "Marionette:getVisibleCookies": + let [currentPath, host] = message.json.value; + let isForCurrentPath = function(aPath) { + return currentPath.indexOf(aPath) != -1; + } + let results = []; + let enumerator = cookieManager.enumerator; + while (enumerator.hasMoreElements()) { + let cookie = enumerator.getNext().QueryInterface(Ci['nsICookie']); + // Take the hostname and progressively shorten + let hostname = host; + do { + if ((cookie.host == '.' + hostname || cookie.host == hostname) + && isForCurrentPath(cookie.path)) { + results.push({ + 'name': cookie.name, + 'value': cookie.value, + 'path': cookie.path, + 'host': cookie.host, + 'secure': cookie.isSecure, + 'expiry': cookie.expires + }); + break; + } + hostname = hostname.replace(/^.*?\./, ''); + } while (hostname.indexOf('.') != -1); + } + return results; + case "Marionette:addCookie": + let cookieToAdd = message.json.value; + Services.cookies.add(cookieToAdd.domain, cookieToAdd.path, cookieToAdd.name, + cookieToAdd.value, cookieToAdd.secure, false, false, + cookieToAdd.expiry); + return true; + case "Marionette:deleteCookie": + let cookieToDelete = message.json.value; + cookieManager.remove(cookieToDelete.host, cookieToDelete.name, + cookieToDelete.path, false); + return true; case "Marionette:register": // This code processes the content listener's registration information // and either accepts the listener, or ignores it