Bug 1142292 - Run cookie gcli commands on the client-side until we can run them on the server's parent process; r=jwalker
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 06 Nov 2015 11:20:23 +0100
changeset 307641 beb71b4e9def9f662705d275d57bd510bde5c922
parent 307618 e2e7cd350ea934e8584413086c0e986df14904e0
child 307642 6f44b15e0ec68424bd67c89f5769a3151c5c28ae
push id7333
push usernalexander@mozilla.com
push dateMon, 09 Nov 2015 18:31:16 +0000
reviewersjwalker
bugs1142292
milestone45.0a1
Bug 1142292 - Run cookie gcli commands on the client-side until we can run them on the server's parent process; r=jwalker
devtools/shared/gcli/commands/cookie.js
--- a/devtools/shared/gcli/commands/cookie.js
+++ b/devtools/shared/gcli/commands/cookie.js
@@ -1,34 +1,53 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
+/**
+ * XXX: bug 1221488 is required to make these commands run on the server.
+ * If we want these commands to run on remote devices/connections, they need to
+ * run on the server (runAt=server). Unfortunately, cookie commands not only
+ * need to run on the server, they also need to access to the parent process to
+ * retrieve and manipulate cookies via nsICookieManager2.
+ * However, server-running commands have no way of accessing the parent process
+ * for now.
+ *
+ * So, because these cookie commands, as of today, only run in the developer
+ * toolbar (the gcli command bar), and because this toolbar is only available on
+ * a local Firefox desktop tab (not in webide or the browser toolbox), we can
+ * make the commands run on the client.
+ * This way, they'll always run in the parent process.
+ *
+ * Note that the commands also need access to the content (see
+ * context.environment.document) which means that as long as they run on the
+ * client, they'll be using CPOWs (when e10s is enabled).
+ */
+
 const { Ci, Cc } = require("chrome");
 const l10n = require("gcli/l10n");
 const URL = require("sdk/url").URL;
 
 XPCOMUtils.defineLazyGetter(this, "cookieMgr", function() {
-  const { Cc, Ci } = require("chrome");
   return Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
 });
 
 /**
  * Check host value and remove port part as it is not used
  * for storing cookies.
  *
  * Parameter will usually be context.environment.document.location.host
  */
 function sanitizeHost(host) {
   if (host == null || host == "") {
     throw new Error(l10n.lookup("cookieListOutNonePage"));
   }
-  return host.split(':')[0];
+  return host.split(":")[0];
 }
 
 /**
  * The cookie 'expires' value needs converting into something more readable
  */
 function translateExpires(expires) {
   if (expires == 0) {
     return l10n.lookup("cookieListOutSession");
@@ -41,35 +60,37 @@ function translateExpires(expires) {
  */
 function isCookieAtHost(cookie, host) {
   if (cookie.host == null) {
     return host == null;
   }
   if (cookie.host.startsWith(".")) {
     return host.endsWith(cookie.host);
   }
-  else {
-    return cookie.host == host;
-  }
+  return cookie.host == host;
 }
 
 exports.items = [
   {
     name: "cookie",
     description: l10n.lookup("cookieDesc"),
     manual: l10n.lookup("cookieManual")
   },
   {
     item: "command",
-    runAt: "server",
+    runAt: "client",
     name: "cookie list",
     description: l10n.lookup("cookieListDesc"),
     manual: l10n.lookup("cookieListManual"),
     returnType: "cookies",
     exec: function(args, context) {
+      if (context.environment.target.isRemote) {
+        throw new Error("The cookie gcli commands only work in a local tab, " +
+                        "see bug 1221488");
+      }
       let host = sanitizeHost(context.environment.document.location.host);
 
       let enm = cookieMgr.getCookiesFromHost(host);
 
       let cookies = [];
       while (enm.hasMoreElements()) {
         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
         if (isCookieAtHost(cookie, host)) {
@@ -86,32 +107,35 @@ exports.items = [
         }
       }
 
       return cookies;
     }
   },
   {
     item: "command",
-    runAt: "server",
+    runAt: "client",
     name: "cookie remove",
     description: l10n.lookup("cookieRemoveDesc"),
     manual: l10n.lookup("cookieRemoveManual"),
     params: [
       {
         name: "name",
         type: "string",
         description: l10n.lookup("cookieRemoveKeyDesc"),
       }
     ],
     exec: function(args, context) {
+      if (context.environment.target.isRemote) {
+        throw new Error("The cookie gcli commands only work in a local tab, " +
+                        "see bug 1221488");
+      }
       let host = sanitizeHost(context.environment.document.location.host);
       let enm = cookieMgr.getCookiesFromHost(host);
 
-      let cookies = [];
       while (enm.hasMoreElements()) {
         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
         if (isCookieAtHost(cookie, host)) {
           if (cookie.name == args.name) {
             cookieMgr.remove(cookie.host, cookie.name, cookie.path, false);
           }
         }
       }
@@ -179,17 +203,17 @@ exports.items = [
           onclick: context.update,
           ondblclick: context.updateExec
         }
       });
     }
   },
   {
     item: "command",
-    runAt: "server",
+    runAt: "client",
     name: "cookie set",
     description: l10n.lookup("cookieSetDesc"),
     manual: l10n.lookup("cookieSetManual"),
     params: [
       {
         name: "name",
         type: "string",
         description: l10n.lookup("cookieSetKeyDesc")
@@ -234,16 +258,20 @@ exports.items = [
             type: "string",
             defaultValue: "Jan 17, 2038",
             description: l10n.lookup("cookieSetExpiresDesc")
           },
         ]
       }
     ],
     exec: function(args, context) {
+      if (context.environment.target.isRemote) {
+        throw new Error("The cookie gcli commands only work in a local tab, " +
+                        "see bug 1221488");
+      }
       let host = sanitizeHost(context.environment.document.location.host);
       let time = Date.parse(args.expires) / 1000;
 
       cookieMgr.add(args.domain ? "." + args.domain : host,
                     args.path ? args.path : "/",
                     args.name,
                     args.value,
                     args.secure,