toolkit/components/cookie/content/cookieAcceptDialog.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/cookie/content/cookieAcceptDialog.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,203 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const nsICookieAcceptDialog = Components.interfaces.nsICookieAcceptDialog;
    1.11 +const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
    1.12 +const nsICookie = Components.interfaces.nsICookie;
    1.13 +const nsICookiePromptService = Components.interfaces.nsICookiePromptService;
    1.14 +
    1.15 +Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
    1.16 +
    1.17 +var params; 
    1.18 +var cookieBundle;
    1.19 +var gDateService = null;
    1.20 +
    1.21 +var showDetails = "";
    1.22 +var hideDetails = "";
    1.23 +var detailsAccessKey = "";
    1.24 +
    1.25 +function onload()
    1.26 +{
    1.27 +  doSetOKCancel(cookieAcceptNormal, cookieDeny, cookieAcceptSession);
    1.28 +
    1.29 +  var dialog = document.documentElement;
    1.30 +
    1.31 +  document.getElementById("Button2").collapsed = false;
    1.32 +
    1.33 +  document.getElementById("ok").label = dialog.getAttribute("acceptLabel");
    1.34 +  document.getElementById("ok").accessKey = dialog.getAttribute("acceptKey");
    1.35 +  document.getElementById("Button2").label = dialog.getAttribute("extra1Label");
    1.36 +  document.getElementById("Button2").accessKey = dialog.getAttribute("extra1Key");
    1.37 +  document.getElementById("cancel").label = dialog.getAttribute("cancelLabel");
    1.38 +  document.getElementById("cancel").accessKey = dialog.getAttribute("cancelKey");
    1.39 +
    1.40 +  // hook up button icons where implemented
    1.41 +  document.getElementById("ok").setAttribute("icon","accept");
    1.42 +  document.getElementById("cancel").setAttribute("icon","cancel");
    1.43 +  document.getElementById("disclosureButton").setAttribute("icon","properties");
    1.44 +
    1.45 +  if (!gDateService) {
    1.46 +    const nsScriptableDateFormat_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1";
    1.47 +    const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat;
    1.48 +    gDateService = Components.classes[nsScriptableDateFormat_CONTRACTID]
    1.49 +                             .getService(nsIScriptableDateFormat);
    1.50 +  }
    1.51 +
    1.52 +  cookieBundle = document.getElementById("cookieBundle");
    1.53 +
    1.54 +  //cache strings
    1.55 +  if (!showDetails) {
    1.56 +    showDetails = cookieBundle.getString('showDetails');
    1.57 +  }
    1.58 +  if (!hideDetails) {
    1.59 +    hideDetails = cookieBundle.getString('hideDetails');
    1.60 +  }
    1.61 +  detailsAccessKey = cookieBundle.getString('detailsAccessKey');
    1.62 +
    1.63 +  if (document.getElementById('infobox').hidden) {
    1.64 +    document.getElementById('disclosureButton').setAttribute("label",showDetails);
    1.65 +  } else {
    1.66 +    document.getElementById('disclosureButton').setAttribute("label",hideDetails);
    1.67 +  }
    1.68 +  document.getElementById('disclosureButton').setAttribute("accesskey",detailsAccessKey);
    1.69 +
    1.70 +  if ("arguments" in window && window.arguments.length >= 1 && window.arguments[0]) {
    1.71 +    try {
    1.72 +      params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
    1.73 +      var objects = params.objects;
    1.74 +      var cookie = params.objects.queryElementAt(0,nsICookie);
    1.75 +      var cookiesFromHost = params.GetInt(nsICookieAcceptDialog.COOKIESFROMHOST);
    1.76 +
    1.77 +      var messageFormat;
    1.78 +      if (params.GetInt(nsICookieAcceptDialog.CHANGINGCOOKIE))
    1.79 +        messageFormat = 'permissionToModifyCookie';
    1.80 +      else if (cookiesFromHost > 1)
    1.81 +        messageFormat = 'permissionToSetAnotherCookie';
    1.82 +      else if (cookiesFromHost == 1)
    1.83 +        messageFormat = 'permissionToSetSecondCookie';
    1.84 +      else
    1.85 +        messageFormat = 'permissionToSetACookie';
    1.86 +
    1.87 +      var hostname = params.GetString(nsICookieAcceptDialog.HOSTNAME);
    1.88 +
    1.89 +      var messageText;
    1.90 +      if (cookie)
    1.91 +        messageText = cookieBundle.getFormattedString(messageFormat,[hostname, cookiesFromHost]);
    1.92 +      else
    1.93 +        // No cookies means something went wrong. Bring up the dialog anyway
    1.94 +        // to not make the mess worse.
    1.95 +        messageText = cookieBundle.getFormattedString(messageFormat,["",cookiesFromHost]);
    1.96 +
    1.97 +      var messageParent = document.getElementById("dialogtextbox");
    1.98 +      var messageParagraphs = messageText.split("\n");
    1.99 +
   1.100 +      // use value for the header, so it doesn't wrap.
   1.101 +      var headerNode = document.getElementById("dialog-header");
   1.102 +      headerNode.setAttribute("value",messageParagraphs[0]);
   1.103 +
   1.104 +      // use childnodes here, the text can wrap
   1.105 +      for (var i = 1; i < messageParagraphs.length; i++) {
   1.106 +        var descriptionNode = document.createElement("description");
   1.107 +        text = document.createTextNode(messageParagraphs[i]);
   1.108 +        descriptionNode.appendChild(text);
   1.109 +        messageParent.appendChild(descriptionNode);
   1.110 +      }
   1.111 +
   1.112 +      if (cookie) {
   1.113 +        document.getElementById('ifl_name').setAttribute("value",cookie.name);
   1.114 +        document.getElementById('ifl_value').setAttribute("value",cookie.value);
   1.115 +        document.getElementById('ifl_host').setAttribute("value",cookie.host);
   1.116 +        document.getElementById('ifl_path').setAttribute("value",cookie.path);
   1.117 +        document.getElementById('ifl_isSecure').setAttribute("value",
   1.118 +                                                                 cookie.isSecure ?
   1.119 +                                                                    cookieBundle.getString("forSecureOnly") : cookieBundle.getString("forAnyConnection")
   1.120 +                                                          );
   1.121 +        document.getElementById('ifl_expires').setAttribute("value",GetExpiresString(cookie.expires));
   1.122 +        document.getElementById('ifl_isDomain').setAttribute("value",
   1.123 +                                                                 cookie.isDomain ?
   1.124 +                                                                    cookieBundle.getString("domainColon") : cookieBundle.getString("hostColon")
   1.125 +                                                            );
   1.126 +      }
   1.127 +      // set default result to not accept the cookie
   1.128 +      params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, 0);
   1.129 +      // and to not persist
   1.130 +      params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, 0);
   1.131 +    } catch (e) {
   1.132 +    }
   1.133 +  }
   1.134 +
   1.135 +  // The Private Browsing service might not be available
   1.136 +  try {
   1.137 +    if (window.opener && PrivateBrowsingUtils.isWindowPrivate(window.opener)) {
   1.138 +      var persistCheckbox = document.getElementById("persistDomainAcceptance");
   1.139 +      persistCheckbox.removeAttribute("checked");
   1.140 +      persistCheckbox.setAttribute("disabled", "true");
   1.141 +    }
   1.142 +  } catch (ex) {}
   1.143 +}
   1.144 +
   1.145 +function showhideinfo()
   1.146 +{
   1.147 +  var infobox=document.getElementById('infobox');
   1.148 +
   1.149 +  if (infobox.hidden) {
   1.150 +    infobox.setAttribute("hidden","false");
   1.151 +    document.getElementById('disclosureButton').setAttribute("label",hideDetails);
   1.152 +  } else {
   1.153 +    infobox.setAttribute("hidden","true");
   1.154 +    document.getElementById('disclosureButton').setAttribute("label",showDetails);
   1.155 +  }
   1.156 +  sizeToContent();
   1.157 +}
   1.158 +
   1.159 +function cookieAcceptNormal()
   1.160 +{
   1.161 +  // accept the cookie normally
   1.162 +  params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_COOKIE); 
   1.163 +  // And remember that when needed
   1.164 +  params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked);
   1.165 +  window.close();
   1.166 +}
   1.167 +
   1.168 +function cookieAcceptSession()
   1.169 +{
   1.170 +  // accept for the session only
   1.171 +  params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_SESSION_COOKIE);
   1.172 +  // And remember that when needed
   1.173 +  params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked);
   1.174 +  window.close();
   1.175 +}
   1.176 +
   1.177 +function cookieDeny()
   1.178 +{
   1.179 +  // say that the cookie was rejected
   1.180 +  params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.DENY_COOKIE); 
   1.181 +  // And remember that when needed
   1.182 +  params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked);
   1.183 +  window.close();
   1.184 +}
   1.185 +
   1.186 +function GetExpiresString(secondsUntilExpires) {
   1.187 +  if (secondsUntilExpires) {
   1.188 +    var date = new Date(1000*secondsUntilExpires);
   1.189 +
   1.190 +    // if a server manages to set a really long-lived cookie, the dateservice
   1.191 +    // can't cope with it properly, so we'll just return a blank string
   1.192 +    // see bug 238045 for details
   1.193 +    var expiry = "";
   1.194 +    try {
   1.195 +      expiry = gDateService.FormatDateTime("", gDateService.dateFormatLong,
   1.196 +                                           gDateService.timeFormatSeconds, 
   1.197 +                                           date.getFullYear(), date.getMonth()+1, 
   1.198 +                                           date.getDate(), date.getHours(),
   1.199 +                                           date.getMinutes(), date.getSeconds());
   1.200 +    } catch(ex) {
   1.201 +      // do nothing
   1.202 +    }
   1.203 +    return expiry;
   1.204 +  }
   1.205 +  return cookieBundle.getString("expireAtEndOfSession");
   1.206 +}

mercurial