toolkit/devtools/gcli/commands/cookie.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 const { Cc, Ci, Cu } = require("chrome");
     8 const gcli = require("gcli/index");
     9 const cookieMgr = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
    11 /**
    12  * The cookie 'expires' value needs converting into something more readable
    13  */
    14 function translateExpires(expires) {
    15   if (expires == 0) {
    16     return gcli.lookup("cookieListOutSession");
    17   }
    18   return new Date(expires).toLocaleString();
    19 }
    21 /**
    22  * Check if a given cookie matches a given host
    23  */
    24 function isCookieAtHost(cookie, host) {
    25   if (cookie.host == null) {
    26     return host == null;
    27   }
    28   if (cookie.host.startsWith(".")) {
    29     return host.endsWith(cookie.host);
    30   }
    31   else {
    32     return cookie.host == host;
    33   }
    34 }
    36 exports.items = [
    37   {
    38     name: "cookie",
    39     description: gcli.lookup("cookieDesc"),
    40     manual: gcli.lookup("cookieManual")
    41   },
    42   {
    43     name: "cookie list",
    44     description: gcli.lookup("cookieListDesc"),
    45     manual: gcli.lookup("cookieListManual"),
    46     returnType: "cookies",
    47     exec: function(args, context) {
    48       let host = context.environment.document.location.host;
    49       if (host == null || host == "") {
    50         throw new Error(gcli.lookup("cookieListOutNonePage"));
    51       }
    53       let enm = cookieMgr.getCookiesFromHost(host);
    55       let cookies = [];
    56       while (enm.hasMoreElements()) {
    57         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
    58         if (isCookieAtHost(cookie, host)) {
    59           cookies.push({
    60             host: cookie.host,
    61             name: cookie.name,
    62             value: cookie.value,
    63             path: cookie.path,
    64             expires: cookie.expires,
    65             secure: cookie.secure,
    66             httpOnly: cookie.httpOnly,
    67             sameDomain: cookie.sameDomain
    68           });
    69         }
    70       }
    72       return cookies;
    73     }
    74   },
    75   {
    76     name: "cookie remove",
    77     description: gcli.lookup("cookieRemoveDesc"),
    78     manual: gcli.lookup("cookieRemoveManual"),
    79     params: [
    80       {
    81         name: "name",
    82         type: "string",
    83         description: gcli.lookup("cookieRemoveKeyDesc"),
    84       }
    85     ],
    86     exec: function(args, context) {
    87       let host = context.environment.document.location.host;
    88       let enm = cookieMgr.getCookiesFromHost(host);
    90       let cookies = [];
    91       while (enm.hasMoreElements()) {
    92         let cookie = enm.getNext().QueryInterface(Ci.nsICookie);
    93         if (isCookieAtHost(cookie, host)) {
    94           if (cookie.name == args.name) {
    95             cookieMgr.remove(cookie.host, cookie.name, cookie.path, false);
    96           }
    97         }
    98       }
    99     }
   100   },
   101   {
   102     item: "converter",
   103     from: "cookies",
   104     to: "view",
   105     exec: function(cookies, context) {
   106       if (cookies.length == 0) {
   107         let host = context.environment.document.location.host;
   108         let msg = gcli.lookupFormat("cookieListOutNoneHost", [ host ]);
   109         return context.createView({ html: "<span>" + msg + "</span>" });
   110       }
   112       for (let cookie of cookies) {
   113         cookie.expires = translateExpires(cookie.expires);
   115         let noAttrs = !cookie.secure && !cookie.httpOnly && !cookie.sameDomain;
   116         cookie.attrs = (cookie.secure ? "secure" : " ") +
   117                        (cookie.httpOnly ? "httpOnly" : " ") +
   118                        (cookie.sameDomain ? "sameDomain" : " ") +
   119                        (noAttrs ? gcli.lookup("cookieListOutNone") : " ");
   120       }
   122       return context.createView({
   123         html:
   124           "<ul class='gcli-cookielist-list'>" +
   125           "  <li foreach='cookie in ${cookies}'>" +
   126           "    <div>${cookie.name}=${cookie.value}</div>" +
   127           "    <table class='gcli-cookielist-detail'>" +
   128           "      <tr>" +
   129           "        <td>" + gcli.lookup("cookieListOutHost") + "</td>" +
   130           "        <td>${cookie.host}</td>" +
   131           "      </tr>" +
   132           "      <tr>" +
   133           "        <td>" + gcli.lookup("cookieListOutPath") + "</td>" +
   134           "        <td>${cookie.path}</td>" +
   135           "      </tr>" +
   136           "      <tr>" +
   137           "        <td>" + gcli.lookup("cookieListOutExpires") + "</td>" +
   138           "        <td>${cookie.expires}</td>" +
   139           "      </tr>" +
   140           "      <tr>" +
   141           "        <td>" + gcli.lookup("cookieListOutAttributes") + "</td>" +
   142           "        <td>${cookie.attrs}</td>" +
   143           "      </tr>" +
   144           "      <tr><td colspan='2'>" +
   145           "        <span class='gcli-out-shortcut' onclick='${onclick}'" +
   146           "            data-command='cookie set ${cookie.name} '" +
   147           "            >" + gcli.lookup("cookieListOutEdit") + "</span>" +
   148           "        <span class='gcli-out-shortcut'" +
   149           "            onclick='${onclick}' ondblclick='${ondblclick}'" +
   150           "            data-command='cookie remove ${cookie.name}'" +
   151           "            >" + gcli.lookup("cookieListOutRemove") + "</span>" +
   152           "      </td></tr>" +
   153           "    </table>" +
   154           "  </li>" +
   155           "</ul>",
   156         data: {
   157           options: { allowEval: true },
   158           cookies: cookies,
   159           onclick: context.update,
   160           ondblclick: context.updateExec
   161         }
   162       });
   163     }
   164   },
   165   {
   166     name: "cookie set",
   167     description: gcli.lookup("cookieSetDesc"),
   168     manual: gcli.lookup("cookieSetManual"),
   169     params: [
   170       {
   171         name: "name",
   172         type: "string",
   173         description: gcli.lookup("cookieSetKeyDesc")
   174       },
   175       {
   176         name: "value",
   177         type: "string",
   178         description: gcli.lookup("cookieSetValueDesc")
   179       },
   180       {
   181         group: gcli.lookup("cookieSetOptionsDesc"),
   182         params: [
   183           {
   184             name: "path",
   185             type: { name: "string", allowBlank: true },
   186             defaultValue: "/",
   187             description: gcli.lookup("cookieSetPathDesc")
   188           },
   189           {
   190             name: "domain",
   191             type: "string",
   192             defaultValue: null,
   193             description: gcli.lookup("cookieSetDomainDesc")
   194           },
   195           {
   196             name: "secure",
   197             type: "boolean",
   198             description: gcli.lookup("cookieSetSecureDesc")
   199           },
   200           {
   201             name: "httpOnly",
   202             type: "boolean",
   203             description: gcli.lookup("cookieSetHttpOnlyDesc")
   204           },
   205           {
   206             name: "session",
   207             type: "boolean",
   208             description: gcli.lookup("cookieSetSessionDesc")
   209           },
   210           {
   211             name: "expires",
   212             type: "string",
   213             defaultValue: "Jan 17, 2038",
   214             description: gcli.lookup("cookieSetExpiresDesc")
   215           },
   216         ]
   217       }
   218     ],
   219     exec: function(args, context) {
   220       let host = context.environment.document.location.host;
   221       let time = Date.parse(args.expires) / 1000;
   223       cookieMgr.add(args.domain ? "." + args.domain : host,
   224                     args.path ? args.path : "/",
   225                     args.name,
   226                     args.value,
   227                     args.secure,
   228                     args.httpOnly,
   229                     args.session,
   230                     time);
   231     }
   232   }
   233 ];

mercurial