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