Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | const nsICookieAcceptDialog = Components.interfaces.nsICookieAcceptDialog; |
michael@0 | 8 | const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock; |
michael@0 | 9 | const nsICookie = Components.interfaces.nsICookie; |
michael@0 | 10 | const nsICookiePromptService = Components.interfaces.nsICookiePromptService; |
michael@0 | 11 | |
michael@0 | 12 | Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | var params; |
michael@0 | 15 | var cookieBundle; |
michael@0 | 16 | var gDateService = null; |
michael@0 | 17 | |
michael@0 | 18 | var showDetails = ""; |
michael@0 | 19 | var hideDetails = ""; |
michael@0 | 20 | var detailsAccessKey = ""; |
michael@0 | 21 | |
michael@0 | 22 | function onload() |
michael@0 | 23 | { |
michael@0 | 24 | doSetOKCancel(cookieAcceptNormal, cookieDeny, cookieAcceptSession); |
michael@0 | 25 | |
michael@0 | 26 | var dialog = document.documentElement; |
michael@0 | 27 | |
michael@0 | 28 | document.getElementById("Button2").collapsed = false; |
michael@0 | 29 | |
michael@0 | 30 | document.getElementById("ok").label = dialog.getAttribute("acceptLabel"); |
michael@0 | 31 | document.getElementById("ok").accessKey = dialog.getAttribute("acceptKey"); |
michael@0 | 32 | document.getElementById("Button2").label = dialog.getAttribute("extra1Label"); |
michael@0 | 33 | document.getElementById("Button2").accessKey = dialog.getAttribute("extra1Key"); |
michael@0 | 34 | document.getElementById("cancel").label = dialog.getAttribute("cancelLabel"); |
michael@0 | 35 | document.getElementById("cancel").accessKey = dialog.getAttribute("cancelKey"); |
michael@0 | 36 | |
michael@0 | 37 | // hook up button icons where implemented |
michael@0 | 38 | document.getElementById("ok").setAttribute("icon","accept"); |
michael@0 | 39 | document.getElementById("cancel").setAttribute("icon","cancel"); |
michael@0 | 40 | document.getElementById("disclosureButton").setAttribute("icon","properties"); |
michael@0 | 41 | |
michael@0 | 42 | if (!gDateService) { |
michael@0 | 43 | const nsScriptableDateFormat_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1"; |
michael@0 | 44 | const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat; |
michael@0 | 45 | gDateService = Components.classes[nsScriptableDateFormat_CONTRACTID] |
michael@0 | 46 | .getService(nsIScriptableDateFormat); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | cookieBundle = document.getElementById("cookieBundle"); |
michael@0 | 50 | |
michael@0 | 51 | //cache strings |
michael@0 | 52 | if (!showDetails) { |
michael@0 | 53 | showDetails = cookieBundle.getString('showDetails'); |
michael@0 | 54 | } |
michael@0 | 55 | if (!hideDetails) { |
michael@0 | 56 | hideDetails = cookieBundle.getString('hideDetails'); |
michael@0 | 57 | } |
michael@0 | 58 | detailsAccessKey = cookieBundle.getString('detailsAccessKey'); |
michael@0 | 59 | |
michael@0 | 60 | if (document.getElementById('infobox').hidden) { |
michael@0 | 61 | document.getElementById('disclosureButton').setAttribute("label",showDetails); |
michael@0 | 62 | } else { |
michael@0 | 63 | document.getElementById('disclosureButton').setAttribute("label",hideDetails); |
michael@0 | 64 | } |
michael@0 | 65 | document.getElementById('disclosureButton').setAttribute("accesskey",detailsAccessKey); |
michael@0 | 66 | |
michael@0 | 67 | if ("arguments" in window && window.arguments.length >= 1 && window.arguments[0]) { |
michael@0 | 68 | try { |
michael@0 | 69 | params = window.arguments[0].QueryInterface(nsIDialogParamBlock); |
michael@0 | 70 | var objects = params.objects; |
michael@0 | 71 | var cookie = params.objects.queryElementAt(0,nsICookie); |
michael@0 | 72 | var cookiesFromHost = params.GetInt(nsICookieAcceptDialog.COOKIESFROMHOST); |
michael@0 | 73 | |
michael@0 | 74 | var messageFormat; |
michael@0 | 75 | if (params.GetInt(nsICookieAcceptDialog.CHANGINGCOOKIE)) |
michael@0 | 76 | messageFormat = 'permissionToModifyCookie'; |
michael@0 | 77 | else if (cookiesFromHost > 1) |
michael@0 | 78 | messageFormat = 'permissionToSetAnotherCookie'; |
michael@0 | 79 | else if (cookiesFromHost == 1) |
michael@0 | 80 | messageFormat = 'permissionToSetSecondCookie'; |
michael@0 | 81 | else |
michael@0 | 82 | messageFormat = 'permissionToSetACookie'; |
michael@0 | 83 | |
michael@0 | 84 | var hostname = params.GetString(nsICookieAcceptDialog.HOSTNAME); |
michael@0 | 85 | |
michael@0 | 86 | var messageText; |
michael@0 | 87 | if (cookie) |
michael@0 | 88 | messageText = cookieBundle.getFormattedString(messageFormat,[hostname, cookiesFromHost]); |
michael@0 | 89 | else |
michael@0 | 90 | // No cookies means something went wrong. Bring up the dialog anyway |
michael@0 | 91 | // to not make the mess worse. |
michael@0 | 92 | messageText = cookieBundle.getFormattedString(messageFormat,["",cookiesFromHost]); |
michael@0 | 93 | |
michael@0 | 94 | var messageParent = document.getElementById("dialogtextbox"); |
michael@0 | 95 | var messageParagraphs = messageText.split("\n"); |
michael@0 | 96 | |
michael@0 | 97 | // use value for the header, so it doesn't wrap. |
michael@0 | 98 | var headerNode = document.getElementById("dialog-header"); |
michael@0 | 99 | headerNode.setAttribute("value",messageParagraphs[0]); |
michael@0 | 100 | |
michael@0 | 101 | // use childnodes here, the text can wrap |
michael@0 | 102 | for (var i = 1; i < messageParagraphs.length; i++) { |
michael@0 | 103 | var descriptionNode = document.createElement("description"); |
michael@0 | 104 | text = document.createTextNode(messageParagraphs[i]); |
michael@0 | 105 | descriptionNode.appendChild(text); |
michael@0 | 106 | messageParent.appendChild(descriptionNode); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | if (cookie) { |
michael@0 | 110 | document.getElementById('ifl_name').setAttribute("value",cookie.name); |
michael@0 | 111 | document.getElementById('ifl_value').setAttribute("value",cookie.value); |
michael@0 | 112 | document.getElementById('ifl_host').setAttribute("value",cookie.host); |
michael@0 | 113 | document.getElementById('ifl_path').setAttribute("value",cookie.path); |
michael@0 | 114 | document.getElementById('ifl_isSecure').setAttribute("value", |
michael@0 | 115 | cookie.isSecure ? |
michael@0 | 116 | cookieBundle.getString("forSecureOnly") : cookieBundle.getString("forAnyConnection") |
michael@0 | 117 | ); |
michael@0 | 118 | document.getElementById('ifl_expires').setAttribute("value",GetExpiresString(cookie.expires)); |
michael@0 | 119 | document.getElementById('ifl_isDomain').setAttribute("value", |
michael@0 | 120 | cookie.isDomain ? |
michael@0 | 121 | cookieBundle.getString("domainColon") : cookieBundle.getString("hostColon") |
michael@0 | 122 | ); |
michael@0 | 123 | } |
michael@0 | 124 | // set default result to not accept the cookie |
michael@0 | 125 | params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, 0); |
michael@0 | 126 | // and to not persist |
michael@0 | 127 | params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, 0); |
michael@0 | 128 | } catch (e) { |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // The Private Browsing service might not be available |
michael@0 | 133 | try { |
michael@0 | 134 | if (window.opener && PrivateBrowsingUtils.isWindowPrivate(window.opener)) { |
michael@0 | 135 | var persistCheckbox = document.getElementById("persistDomainAcceptance"); |
michael@0 | 136 | persistCheckbox.removeAttribute("checked"); |
michael@0 | 137 | persistCheckbox.setAttribute("disabled", "true"); |
michael@0 | 138 | } |
michael@0 | 139 | } catch (ex) {} |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | function showhideinfo() |
michael@0 | 143 | { |
michael@0 | 144 | var infobox=document.getElementById('infobox'); |
michael@0 | 145 | |
michael@0 | 146 | if (infobox.hidden) { |
michael@0 | 147 | infobox.setAttribute("hidden","false"); |
michael@0 | 148 | document.getElementById('disclosureButton').setAttribute("label",hideDetails); |
michael@0 | 149 | } else { |
michael@0 | 150 | infobox.setAttribute("hidden","true"); |
michael@0 | 151 | document.getElementById('disclosureButton').setAttribute("label",showDetails); |
michael@0 | 152 | } |
michael@0 | 153 | sizeToContent(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | function cookieAcceptNormal() |
michael@0 | 157 | { |
michael@0 | 158 | // accept the cookie normally |
michael@0 | 159 | params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_COOKIE); |
michael@0 | 160 | // And remember that when needed |
michael@0 | 161 | params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); |
michael@0 | 162 | window.close(); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | function cookieAcceptSession() |
michael@0 | 166 | { |
michael@0 | 167 | // accept for the session only |
michael@0 | 168 | params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_SESSION_COOKIE); |
michael@0 | 169 | // And remember that when needed |
michael@0 | 170 | params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); |
michael@0 | 171 | window.close(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | function cookieDeny() |
michael@0 | 175 | { |
michael@0 | 176 | // say that the cookie was rejected |
michael@0 | 177 | params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.DENY_COOKIE); |
michael@0 | 178 | // And remember that when needed |
michael@0 | 179 | params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); |
michael@0 | 180 | window.close(); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | function GetExpiresString(secondsUntilExpires) { |
michael@0 | 184 | if (secondsUntilExpires) { |
michael@0 | 185 | var date = new Date(1000*secondsUntilExpires); |
michael@0 | 186 | |
michael@0 | 187 | // if a server manages to set a really long-lived cookie, the dateservice |
michael@0 | 188 | // can't cope with it properly, so we'll just return a blank string |
michael@0 | 189 | // see bug 238045 for details |
michael@0 | 190 | var expiry = ""; |
michael@0 | 191 | try { |
michael@0 | 192 | expiry = gDateService.FormatDateTime("", gDateService.dateFormatLong, |
michael@0 | 193 | gDateService.timeFormatSeconds, |
michael@0 | 194 | date.getFullYear(), date.getMonth()+1, |
michael@0 | 195 | date.getDate(), date.getHours(), |
michael@0 | 196 | date.getMinutes(), date.getSeconds()); |
michael@0 | 197 | } catch(ex) { |
michael@0 | 198 | // do nothing |
michael@0 | 199 | } |
michael@0 | 200 | return expiry; |
michael@0 | 201 | } |
michael@0 | 202 | return cookieBundle.getString("expireAtEndOfSession"); |
michael@0 | 203 | } |