1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/gcli/commands/cookie.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,233 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const { Cc, Ci, Cu } = require("chrome"); 1.11 +const gcli = require("gcli/index"); 1.12 +const cookieMgr = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); 1.13 + 1.14 +/** 1.15 + * The cookie 'expires' value needs converting into something more readable 1.16 + */ 1.17 +function translateExpires(expires) { 1.18 + if (expires == 0) { 1.19 + return gcli.lookup("cookieListOutSession"); 1.20 + } 1.21 + return new Date(expires).toLocaleString(); 1.22 +} 1.23 + 1.24 +/** 1.25 + * Check if a given cookie matches a given host 1.26 + */ 1.27 +function isCookieAtHost(cookie, host) { 1.28 + if (cookie.host == null) { 1.29 + return host == null; 1.30 + } 1.31 + if (cookie.host.startsWith(".")) { 1.32 + return host.endsWith(cookie.host); 1.33 + } 1.34 + else { 1.35 + return cookie.host == host; 1.36 + } 1.37 +} 1.38 + 1.39 +exports.items = [ 1.40 + { 1.41 + name: "cookie", 1.42 + description: gcli.lookup("cookieDesc"), 1.43 + manual: gcli.lookup("cookieManual") 1.44 + }, 1.45 + { 1.46 + name: "cookie list", 1.47 + description: gcli.lookup("cookieListDesc"), 1.48 + manual: gcli.lookup("cookieListManual"), 1.49 + returnType: "cookies", 1.50 + exec: function(args, context) { 1.51 + let host = context.environment.document.location.host; 1.52 + if (host == null || host == "") { 1.53 + throw new Error(gcli.lookup("cookieListOutNonePage")); 1.54 + } 1.55 + 1.56 + let enm = cookieMgr.getCookiesFromHost(host); 1.57 + 1.58 + let cookies = []; 1.59 + while (enm.hasMoreElements()) { 1.60 + let cookie = enm.getNext().QueryInterface(Ci.nsICookie); 1.61 + if (isCookieAtHost(cookie, host)) { 1.62 + cookies.push({ 1.63 + host: cookie.host, 1.64 + name: cookie.name, 1.65 + value: cookie.value, 1.66 + path: cookie.path, 1.67 + expires: cookie.expires, 1.68 + secure: cookie.secure, 1.69 + httpOnly: cookie.httpOnly, 1.70 + sameDomain: cookie.sameDomain 1.71 + }); 1.72 + } 1.73 + } 1.74 + 1.75 + return cookies; 1.76 + } 1.77 + }, 1.78 + { 1.79 + name: "cookie remove", 1.80 + description: gcli.lookup("cookieRemoveDesc"), 1.81 + manual: gcli.lookup("cookieRemoveManual"), 1.82 + params: [ 1.83 + { 1.84 + name: "name", 1.85 + type: "string", 1.86 + description: gcli.lookup("cookieRemoveKeyDesc"), 1.87 + } 1.88 + ], 1.89 + exec: function(args, context) { 1.90 + let host = context.environment.document.location.host; 1.91 + let enm = cookieMgr.getCookiesFromHost(host); 1.92 + 1.93 + let cookies = []; 1.94 + while (enm.hasMoreElements()) { 1.95 + let cookie = enm.getNext().QueryInterface(Ci.nsICookie); 1.96 + if (isCookieAtHost(cookie, host)) { 1.97 + if (cookie.name == args.name) { 1.98 + cookieMgr.remove(cookie.host, cookie.name, cookie.path, false); 1.99 + } 1.100 + } 1.101 + } 1.102 + } 1.103 + }, 1.104 + { 1.105 + item: "converter", 1.106 + from: "cookies", 1.107 + to: "view", 1.108 + exec: function(cookies, context) { 1.109 + if (cookies.length == 0) { 1.110 + let host = context.environment.document.location.host; 1.111 + let msg = gcli.lookupFormat("cookieListOutNoneHost", [ host ]); 1.112 + return context.createView({ html: "<span>" + msg + "</span>" }); 1.113 + } 1.114 + 1.115 + for (let cookie of cookies) { 1.116 + cookie.expires = translateExpires(cookie.expires); 1.117 + 1.118 + let noAttrs = !cookie.secure && !cookie.httpOnly && !cookie.sameDomain; 1.119 + cookie.attrs = (cookie.secure ? "secure" : " ") + 1.120 + (cookie.httpOnly ? "httpOnly" : " ") + 1.121 + (cookie.sameDomain ? "sameDomain" : " ") + 1.122 + (noAttrs ? gcli.lookup("cookieListOutNone") : " "); 1.123 + } 1.124 + 1.125 + return context.createView({ 1.126 + html: 1.127 + "<ul class='gcli-cookielist-list'>" + 1.128 + " <li foreach='cookie in ${cookies}'>" + 1.129 + " <div>${cookie.name}=${cookie.value}</div>" + 1.130 + " <table class='gcli-cookielist-detail'>" + 1.131 + " <tr>" + 1.132 + " <td>" + gcli.lookup("cookieListOutHost") + "</td>" + 1.133 + " <td>${cookie.host}</td>" + 1.134 + " </tr>" + 1.135 + " <tr>" + 1.136 + " <td>" + gcli.lookup("cookieListOutPath") + "</td>" + 1.137 + " <td>${cookie.path}</td>" + 1.138 + " </tr>" + 1.139 + " <tr>" + 1.140 + " <td>" + gcli.lookup("cookieListOutExpires") + "</td>" + 1.141 + " <td>${cookie.expires}</td>" + 1.142 + " </tr>" + 1.143 + " <tr>" + 1.144 + " <td>" + gcli.lookup("cookieListOutAttributes") + "</td>" + 1.145 + " <td>${cookie.attrs}</td>" + 1.146 + " </tr>" + 1.147 + " <tr><td colspan='2'>" + 1.148 + " <span class='gcli-out-shortcut' onclick='${onclick}'" + 1.149 + " data-command='cookie set ${cookie.name} '" + 1.150 + " >" + gcli.lookup("cookieListOutEdit") + "</span>" + 1.151 + " <span class='gcli-out-shortcut'" + 1.152 + " onclick='${onclick}' ondblclick='${ondblclick}'" + 1.153 + " data-command='cookie remove ${cookie.name}'" + 1.154 + " >" + gcli.lookup("cookieListOutRemove") + "</span>" + 1.155 + " </td></tr>" + 1.156 + " </table>" + 1.157 + " </li>" + 1.158 + "</ul>", 1.159 + data: { 1.160 + options: { allowEval: true }, 1.161 + cookies: cookies, 1.162 + onclick: context.update, 1.163 + ondblclick: context.updateExec 1.164 + } 1.165 + }); 1.166 + } 1.167 + }, 1.168 + { 1.169 + name: "cookie set", 1.170 + description: gcli.lookup("cookieSetDesc"), 1.171 + manual: gcli.lookup("cookieSetManual"), 1.172 + params: [ 1.173 + { 1.174 + name: "name", 1.175 + type: "string", 1.176 + description: gcli.lookup("cookieSetKeyDesc") 1.177 + }, 1.178 + { 1.179 + name: "value", 1.180 + type: "string", 1.181 + description: gcli.lookup("cookieSetValueDesc") 1.182 + }, 1.183 + { 1.184 + group: gcli.lookup("cookieSetOptionsDesc"), 1.185 + params: [ 1.186 + { 1.187 + name: "path", 1.188 + type: { name: "string", allowBlank: true }, 1.189 + defaultValue: "/", 1.190 + description: gcli.lookup("cookieSetPathDesc") 1.191 + }, 1.192 + { 1.193 + name: "domain", 1.194 + type: "string", 1.195 + defaultValue: null, 1.196 + description: gcli.lookup("cookieSetDomainDesc") 1.197 + }, 1.198 + { 1.199 + name: "secure", 1.200 + type: "boolean", 1.201 + description: gcli.lookup("cookieSetSecureDesc") 1.202 + }, 1.203 + { 1.204 + name: "httpOnly", 1.205 + type: "boolean", 1.206 + description: gcli.lookup("cookieSetHttpOnlyDesc") 1.207 + }, 1.208 + { 1.209 + name: "session", 1.210 + type: "boolean", 1.211 + description: gcli.lookup("cookieSetSessionDesc") 1.212 + }, 1.213 + { 1.214 + name: "expires", 1.215 + type: "string", 1.216 + defaultValue: "Jan 17, 2038", 1.217 + description: gcli.lookup("cookieSetExpiresDesc") 1.218 + }, 1.219 + ] 1.220 + } 1.221 + ], 1.222 + exec: function(args, context) { 1.223 + let host = context.environment.document.location.host; 1.224 + let time = Date.parse(args.expires) / 1000; 1.225 + 1.226 + cookieMgr.add(args.domain ? "." + args.domain : host, 1.227 + args.path ? args.path : "/", 1.228 + args.name, 1.229 + args.value, 1.230 + args.secure, 1.231 + args.httpOnly, 1.232 + args.session, 1.233 + time); 1.234 + } 1.235 + } 1.236 +];