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.

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

mercurial