michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const nsICookieAcceptDialog = Components.interfaces.nsICookieAcceptDialog; michael@0: const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock; michael@0: const nsICookie = Components.interfaces.nsICookie; michael@0: const nsICookiePromptService = Components.interfaces.nsICookiePromptService; michael@0: michael@0: Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); michael@0: michael@0: var params; michael@0: var cookieBundle; michael@0: var gDateService = null; michael@0: michael@0: var showDetails = ""; michael@0: var hideDetails = ""; michael@0: var detailsAccessKey = ""; michael@0: michael@0: function onload() michael@0: { michael@0: doSetOKCancel(cookieAcceptNormal, cookieDeny, cookieAcceptSession); michael@0: michael@0: var dialog = document.documentElement; michael@0: michael@0: document.getElementById("Button2").collapsed = false; michael@0: michael@0: document.getElementById("ok").label = dialog.getAttribute("acceptLabel"); michael@0: document.getElementById("ok").accessKey = dialog.getAttribute("acceptKey"); michael@0: document.getElementById("Button2").label = dialog.getAttribute("extra1Label"); michael@0: document.getElementById("Button2").accessKey = dialog.getAttribute("extra1Key"); michael@0: document.getElementById("cancel").label = dialog.getAttribute("cancelLabel"); michael@0: document.getElementById("cancel").accessKey = dialog.getAttribute("cancelKey"); michael@0: michael@0: // hook up button icons where implemented michael@0: document.getElementById("ok").setAttribute("icon","accept"); michael@0: document.getElementById("cancel").setAttribute("icon","cancel"); michael@0: document.getElementById("disclosureButton").setAttribute("icon","properties"); michael@0: michael@0: if (!gDateService) { michael@0: const nsScriptableDateFormat_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1"; michael@0: const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat; michael@0: gDateService = Components.classes[nsScriptableDateFormat_CONTRACTID] michael@0: .getService(nsIScriptableDateFormat); michael@0: } michael@0: michael@0: cookieBundle = document.getElementById("cookieBundle"); michael@0: michael@0: //cache strings michael@0: if (!showDetails) { michael@0: showDetails = cookieBundle.getString('showDetails'); michael@0: } michael@0: if (!hideDetails) { michael@0: hideDetails = cookieBundle.getString('hideDetails'); michael@0: } michael@0: detailsAccessKey = cookieBundle.getString('detailsAccessKey'); michael@0: michael@0: if (document.getElementById('infobox').hidden) { michael@0: document.getElementById('disclosureButton').setAttribute("label",showDetails); michael@0: } else { michael@0: document.getElementById('disclosureButton').setAttribute("label",hideDetails); michael@0: } michael@0: document.getElementById('disclosureButton').setAttribute("accesskey",detailsAccessKey); michael@0: michael@0: if ("arguments" in window && window.arguments.length >= 1 && window.arguments[0]) { michael@0: try { michael@0: params = window.arguments[0].QueryInterface(nsIDialogParamBlock); michael@0: var objects = params.objects; michael@0: var cookie = params.objects.queryElementAt(0,nsICookie); michael@0: var cookiesFromHost = params.GetInt(nsICookieAcceptDialog.COOKIESFROMHOST); michael@0: michael@0: var messageFormat; michael@0: if (params.GetInt(nsICookieAcceptDialog.CHANGINGCOOKIE)) michael@0: messageFormat = 'permissionToModifyCookie'; michael@0: else if (cookiesFromHost > 1) michael@0: messageFormat = 'permissionToSetAnotherCookie'; michael@0: else if (cookiesFromHost == 1) michael@0: messageFormat = 'permissionToSetSecondCookie'; michael@0: else michael@0: messageFormat = 'permissionToSetACookie'; michael@0: michael@0: var hostname = params.GetString(nsICookieAcceptDialog.HOSTNAME); michael@0: michael@0: var messageText; michael@0: if (cookie) michael@0: messageText = cookieBundle.getFormattedString(messageFormat,[hostname, cookiesFromHost]); michael@0: else michael@0: // No cookies means something went wrong. Bring up the dialog anyway michael@0: // to not make the mess worse. michael@0: messageText = cookieBundle.getFormattedString(messageFormat,["",cookiesFromHost]); michael@0: michael@0: var messageParent = document.getElementById("dialogtextbox"); michael@0: var messageParagraphs = messageText.split("\n"); michael@0: michael@0: // use value for the header, so it doesn't wrap. michael@0: var headerNode = document.getElementById("dialog-header"); michael@0: headerNode.setAttribute("value",messageParagraphs[0]); michael@0: michael@0: // use childnodes here, the text can wrap michael@0: for (var i = 1; i < messageParagraphs.length; i++) { michael@0: var descriptionNode = document.createElement("description"); michael@0: text = document.createTextNode(messageParagraphs[i]); michael@0: descriptionNode.appendChild(text); michael@0: messageParent.appendChild(descriptionNode); michael@0: } michael@0: michael@0: if (cookie) { michael@0: document.getElementById('ifl_name').setAttribute("value",cookie.name); michael@0: document.getElementById('ifl_value').setAttribute("value",cookie.value); michael@0: document.getElementById('ifl_host').setAttribute("value",cookie.host); michael@0: document.getElementById('ifl_path').setAttribute("value",cookie.path); michael@0: document.getElementById('ifl_isSecure').setAttribute("value", michael@0: cookie.isSecure ? michael@0: cookieBundle.getString("forSecureOnly") : cookieBundle.getString("forAnyConnection") michael@0: ); michael@0: document.getElementById('ifl_expires').setAttribute("value",GetExpiresString(cookie.expires)); michael@0: document.getElementById('ifl_isDomain').setAttribute("value", michael@0: cookie.isDomain ? michael@0: cookieBundle.getString("domainColon") : cookieBundle.getString("hostColon") michael@0: ); michael@0: } michael@0: // set default result to not accept the cookie michael@0: params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, 0); michael@0: // and to not persist michael@0: params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, 0); michael@0: } catch (e) { michael@0: } michael@0: } michael@0: michael@0: // The Private Browsing service might not be available michael@0: try { michael@0: if (window.opener && PrivateBrowsingUtils.isWindowPrivate(window.opener)) { michael@0: var persistCheckbox = document.getElementById("persistDomainAcceptance"); michael@0: persistCheckbox.removeAttribute("checked"); michael@0: persistCheckbox.setAttribute("disabled", "true"); michael@0: } michael@0: } catch (ex) {} michael@0: } michael@0: michael@0: function showhideinfo() michael@0: { michael@0: var infobox=document.getElementById('infobox'); michael@0: michael@0: if (infobox.hidden) { michael@0: infobox.setAttribute("hidden","false"); michael@0: document.getElementById('disclosureButton').setAttribute("label",hideDetails); michael@0: } else { michael@0: infobox.setAttribute("hidden","true"); michael@0: document.getElementById('disclosureButton').setAttribute("label",showDetails); michael@0: } michael@0: sizeToContent(); michael@0: } michael@0: michael@0: function cookieAcceptNormal() michael@0: { michael@0: // accept the cookie normally michael@0: params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_COOKIE); michael@0: // And remember that when needed michael@0: params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); michael@0: window.close(); michael@0: } michael@0: michael@0: function cookieAcceptSession() michael@0: { michael@0: // accept for the session only michael@0: params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.ACCEPT_SESSION_COOKIE); michael@0: // And remember that when needed michael@0: params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); michael@0: window.close(); michael@0: } michael@0: michael@0: function cookieDeny() michael@0: { michael@0: // say that the cookie was rejected michael@0: params.SetInt(nsICookieAcceptDialog.ACCEPT_COOKIE, nsICookiePromptService.DENY_COOKIE); michael@0: // And remember that when needed michael@0: params.SetInt(nsICookieAcceptDialog.REMEMBER_DECISION, document.getElementById('persistDomainAcceptance').checked); michael@0: window.close(); michael@0: } michael@0: michael@0: function GetExpiresString(secondsUntilExpires) { michael@0: if (secondsUntilExpires) { michael@0: var date = new Date(1000*secondsUntilExpires); michael@0: michael@0: // if a server manages to set a really long-lived cookie, the dateservice michael@0: // can't cope with it properly, so we'll just return a blank string michael@0: // see bug 238045 for details michael@0: var expiry = ""; michael@0: try { michael@0: expiry = gDateService.FormatDateTime("", gDateService.dateFormatLong, michael@0: gDateService.timeFormatSeconds, michael@0: date.getFullYear(), date.getMonth()+1, michael@0: date.getDate(), date.getHours(), michael@0: date.getMinutes(), date.getSeconds()); michael@0: } catch(ex) { michael@0: // do nothing michael@0: } michael@0: return expiry; michael@0: } michael@0: return cookieBundle.getString("expireAtEndOfSession"); michael@0: }