privacy.js
Go to the documentation of this file.
1 /*
2 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is the Firefox Preferences System.
17 #
18 # The Initial Developer of the Original Code is
19 # Ben Goodger.
20 # Portions created by the Initial Developer are Copyright (C) 2005
21 # the Initial Developer. All Rights Reserved.
22 #
23 # Contributor(s):
24 # Ben Goodger <ben@mozilla.org>
25 # Jeff Walden <jwalden+code@mit.edu>
26 # Ehsan Akhgari <ehsan.akhgari@gmail.com>
27 # Roberto Estrada <roberto.estrada@yahoo.es>
28 #
29 # Alternatively, the contents of this file may be used under the terms of
30 # either the GNU General Public License Version 2 or later (the "GPL"), or
31 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 # in which case the provisions of the GPL or the LGPL are applicable instead
33 # of those above. If you wish to allow use of your version of this file only
34 # under the terms of either the GPL or the LGPL, and not to allow others to
35 # use your version of this file under the terms of the MPL, indicate your
36 # decision by deleting the provisions above and replace them with the notice
37 # and other provisions required by the GPL or the LGPL. If you do not delete
38 # the provisions above, a recipient may use your version of this file under
39 # the terms of any one of the MPL, the GPL or the LGPL.
40 #
41 # ***** END LICENSE BLOCK *****
42 */
43 
44 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
45 
46 var gPrivacyPane = {
47 
51  _autoStartPrivateBrowsing: false,
52 
57  init: function ()
58  {
59  this._updateHistoryDaysUI();
60  this._updateSanitizeSettingsButton();
61  this.initializeHistoryMode();
62  this.updateHistoryModePane();
63  this.updatePrivacyMicroControls();
64  this.initAutoStartPrivateBrowsingObserver();
65  },
66 
67  // HISTORY MODE
68 
80  prefsForDefault: [
81  "browser.history_expire_days",
82  "browser.history_expire_days_min",
83  "browser.download.manager.retention",
84  "browser.formfill.enable",
85  "network.cookie.cookieBehavior",
86  "network.cookie.lifetimePolicy",
87  "privacy.sanitize.sanitizeOnShutdown"
88  ],
89 
97  dependentControls: [
98  "rememberHistoryDays",
99  "rememberAfter",
100  "rememberDownloads",
101  "rememberForms",
102  "keepUntil",
103  "keepCookiesUntil",
104  "alwaysClear",
105  "clearDataSettings"
106  ],
107 
115  _checkDefaultValues: function(aPrefs) {
116  for (let i = 0; i < aPrefs.length; ++i) {
117  let pref = document.getElementById(aPrefs[i]);
118  if (pref.value != pref.defaultValue)
119  return false;
120  }
121  return true;
122  },
123 
127  initializeHistoryMode: function PPP_initializeHistoryMode()
128  {
129  let mode;
130  let getVal = function (aPref)
131  document.getElementById(aPref).value;
132 
133  if (this._checkDefaultValues(this.prefsForDefault)) {
134  if (getVal("browser.privatebrowsing.autostart"))
135  mode = "dontremember";
136  else
137  mode = "remember";
138  }
139  else
140  mode = "custom";
141 
142  document.getElementById("historyMode").value = mode;
143  },
144 
148  updateHistoryModePane: function PPP_updateHistoryModePane()
149  {
150  let selectedIndex = -1;
151  switch (document.getElementById("historyMode").value) {
152  case "remember":
153  selectedIndex = 0;
154  break;
155  case "dontremember":
156  selectedIndex = 1;
157  break;
158  case "custom":
159  selectedIndex = 2;
160  break;
161  }
162  document.getElementById("historyPane").selectedIndex = selectedIndex;
163  },
164 
169  updateHistoryModePrefs: function PPP_updateHistoryModePrefs()
170  {
171  let pref = document.getElementById("browser.privatebrowsing.autostart");
172  switch (document.getElementById("historyMode").value) {
173  case "remember":
174  pref.value = false;
175 
176  // select the remember history option if needed
177  let rememberHistoryCheckbox = document.getElementById("rememberHistoryDays");
178  if (!rememberHistoryCheckbox.checked) {
179  rememberHistoryCheckbox.checked = true;
180  this.onchangeHistoryDaysCheck();
181  }
182 
183  // select the remember downloads option if needed
184  if (!document.getElementById("rememberDownloads").checked)
185  document.getElementById("browser.download.manager.retention").value = 2;
186 
187  // select the remember forms history option
188  document.getElementById("browser.formfill.enable").value = true;
189 
190  // select the accept cookies option
191  document.getElementById("network.cookie.cookieBehavior").value = 0;
192  // select the cookie lifetime policy option
193  document.getElementById("network.cookie.lifetimePolicy").value = 0;
194 
195  // select the clear on close option
196  document.getElementById("privacy.sanitize.sanitizeOnShutdown").value = false;
197  break;
198  case "dontremember":
199  pref.value = true;
200  break;
201  }
202  },
203 
208  updatePrivacyMicroControls: function PPP_updatePrivacyMicroControls()
209  {
210  if (document.getElementById("historyMode").value == "custom") {
211  let disabled = this._autoStartPrivateBrowsing =
212  /* XXX SONGBIRD: we don't support private browsing yet
213  document.getElementById("privateBrowsingAutoStart").checked;
214  */
215  false;
216  this.dependentControls
217  .forEach(function (aElement)
218  document.getElementById(aElement).disabled = disabled);
219 
220  // adjust the cookie controls status
221  this.readAcceptCookies();
222  document.getElementById("keepCookiesUntil").value = disabled ? 2 :
223  document.getElementById("network.cookie.lifetimePolicy").value;
224 
225  // adjust the checked state of the sanitizeOnShutdown checkbox
226  document.getElementById("alwaysClear").checked = disabled ? false :
227  document.getElementById("privacy.sanitize.sanitizeOnShutdown").value;
228 
229  // adjust the checked state of the remember history checkboxes
230  document.getElementById("rememberHistoryDays").checked = disabled ? false :
231  document.getElementById("browser.history_expire_days").value > 0;
232  this.onchangeHistoryDaysCheck();
233  document.getElementById("rememberDownloads").checked = disabled ? false :
234  this.readDownloadRetention();
235  document.getElementById("rememberForms").checked = disabled ? false :
236  document.getElementById("browser.formfill.enable").value;
237 
238  if (!disabled) {
239  // adjust the Settings button for sanitizeOnShutdown
240  this._updateSanitizeSettingsButton();
241  }
242  }
243  },
244 
245  // PRIVATE BROWSING
246 
250  initAutoStartPrivateBrowsingObserver: function PPP_initAutoStartPrivateBrowsingObserver()
251  {
252  let prefService = document.getElementById("privacyPreferences")
253  .service
254  .QueryInterface(Components.interfaces.nsIPrefBranch2);
255  prefService.addObserver("browser.privatebrowsing.autostart",
256  this.autoStartPrivateBrowsingObserver,
257  true);
258  },
259 
260  autoStartPrivateBrowsingObserver:
261  {
262  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver,
263  Components.interfaces.nsISupportsWeakReference]),
264 
265  observe: function PPP_observe(aSubject, aTopic, aData)
266  {
267  let privateBrowsingService = Components.classes["@mozilla.org/privatebrowsing;1"].
268  getService(Components.interfaces.nsIPrivateBrowsingService);
269 
270  // Toggle the private browsing mode without switching the session
271  let prefValue = document.getElementById("browser.privatebrowsing.autostart").value;
272  let keepCurrentSession = document.getElementById("browser.privatebrowsing.keep_current_session");
273  keepCurrentSession.value = true;
274  // If activating from within the private browsing mode, reset the
275  // private session
276  if (prefValue && privateBrowsingService.privateBrowsingEnabled)
277  privateBrowsingService.privateBrowsingEnabled = false;
278  privateBrowsingService.privateBrowsingEnabled = prefValue;
279  keepCurrentSession.reset();
280  }
281  },
282 
283  // HISTORY
284 
289  readSuggestionPref: function PPP_readSuggestionPref()
290  {
291  let getVal = function(aPref)
292  document.getElementById("browser.urlbar." + aPref).value;
293 
294  // Suggest nothing if autocomplete is not enabled
295  if (!getVal("autocomplete.enabled"))
296  return -1;
297 
298  // Bottom 2 bits of default.behavior specify history/bookmark
299  return getVal("default.behavior") & 3;
300  },
301 
306  writeSuggestionPref: function PPP_writeSuggestionPref()
307  {
308  let menuVal = document.getElementById("locationBarSuggestion").value;
309  let enabled = menuVal != -1;
310 
311  // Only update default.behavior if we're giving suggestions
312  if (enabled) {
313  // Put the selected menu item's value directly into the bottom 2 bits
314  let behavior = document.getElementById("browser.urlbar.default.behavior");
315  behavior.value = behavior.value >> 2 << 2 | menuVal;
316  }
317 
318  // Always update the enabled pref
319  return enabled;
320  },
321 
322  /*
323  * Preferences:
324  *
325  * NOTE: These first two are no longer shown in the UI. They're controlled
326  * via the checkbox, which uses the zero state of the pref to turn
327  * history off.
328  * browser.history_expire_days
329  * - the number of days of history to remember
330  * browser.history_expire_days.mirror
331  * - a preference whose value mirrors that of browser.history_expire_days, to
332  * make the "days of history" checkbox easier to code
333  *
334  * browser.history_expire_days_min
335  * - the mininum number of days of history to remember
336  * browser.history_expire_days_min.mirror
337  * - a preference whose value mirrors that of browser.history_expire_days_min
338  * to make the "days of history" checkbox easier to code
339  * browser.formfill.enable
340  * - true if entries in forms and the search bar should be saved, false
341  * otherwise
342  * browser.download.manager.retention
343  * - determines when downloads are automatically removed from the download
344  * manager:
345  *
346  * 0 means remove downloads when they finish downloading
347  * 1 means downloads will be removed when the browser quits
348  * 2 means never remove downloads
349  */
350 
356  _updateHistoryDaysUI: function ()
357  {
358  var pref = document.getElementById("browser.history_expire_days");
359  var mirror = document.getElementById("browser.history_expire_days.mirror");
360  var pref_min = document.getElementById("browser.history_expire_days_min");
361  var textbox = document.getElementById("historyDays");
362  var checkbox = document.getElementById("rememberHistoryDays");
363 
364  // handle mirror non-existence or mirror/pref unsync
365  if (mirror.value === null || mirror.value != pref.value ||
366  (mirror.value == pref.value && mirror.value == 0) )
367  mirror.value = pref.value ? pref.value : pref.defaultValue;
368 
369  checkbox.checked = (pref.value > 0);
370  textbox.disabled = !checkbox.checked;
371  },
372 
378  onchangeHistoryDaysCheck: function ()
379  {
380  var pref = document.getElementById("browser.history_expire_days");
381  var mirror = document.getElementById("browser.history_expire_days.mirror");
382  var textbox = document.getElementById("historyDays");
383  var checkbox = document.getElementById("rememberHistoryDays");
384 
385  if (!this._autoStartPrivateBrowsing)
386  pref.value = checkbox.checked ? mirror.value : 0;
387  textbox.disabled = !checkbox.checked;
388  },
389 
395  onkeyupHistoryDaysText: function ()
396  {
397  var textbox = document.getElementById("historyDays");
398  var checkbox = document.getElementById("rememberHistoryDays");
399 
400  checkbox.checked = textbox.value != 0;
401  },
402 
408  readDownloadRetention: function ()
409  {
410  var pref = document.getElementById("browser.download.manager.retention");
411  return (pref.value == 2);
412  },
413 
418  writeDownloadRetention: function ()
419  {
420  var checkbox = document.getElementById("rememberDownloads");
421  return checkbox.checked ? 2 : 0;
422  },
423 
424  // COOKIES
425 
426  /*
427  * Preferences:
428  *
429  * network.cookie.cookieBehavior
430  * - determines how the browser should handle cookies:
431  * 0 means enable all cookies
432  * 1 means reject third party cookies; see
433  * netwerk/cookie/src/nsCookieService.cpp for a hairier definition
434  * 2 means disable all cookies
435  * network.cookie.lifetimePolicy
436  * - determines how long cookies are stored:
437  * 0 means keep cookies until they expire
438  * 1 means ask how long to keep each cookie
439  * 2 means keep cookies until the browser is closed
440  */
441 
447  readAcceptCookies: function ()
448  {
449  var pref = document.getElementById("network.cookie.cookieBehavior");
450  var acceptThirdParty = document.getElementById("acceptThirdParty");
451  var keepUntil = document.getElementById("keepUntil");
452  var menu = document.getElementById("keepCookiesUntil");
453 
454  // enable the rest of the UI for anything other than "disable all cookies"
455  var acceptCookies = (pref.value != 2);
456 
457  acceptThirdParty.disabled = !acceptCookies;
458  keepUntil.disabled = menu.disabled = this._autoStartPrivateBrowsing || !acceptCookies;
459 
460  return acceptCookies;
461  },
462 
463  readAcceptThirdPartyCookies: function ()
464  {
465  var pref = document.getElementById("network.cookie.cookieBehavior");
466  return pref.value == 0;
467  },
468 
473  writeAcceptCookies: function ()
474  {
475  var accept = document.getElementById("acceptCookies");
476  var acceptThirdParty = document.getElementById("acceptThirdParty");
477 
478  // if we're enabling cookies, automatically check 'accept third party'
479  if (accept.checked)
480  acceptThirdParty.checked = true;
481 
482  return accept.checked ? (acceptThirdParty.checked ? 0 : 1) : 2;
483  },
484 
485  writeAcceptThirdPartyCookies: function ()
486  {
487  var accept = document.getElementById("acceptCookies");
488  var acceptThirdParty = document.getElementById("acceptThirdParty");
489  return accept.checked ? (acceptThirdParty.checked ? 0 : 1) : 2;
490  },
491 
495  showCookieExceptions: function ()
496  {
497  var bundlePreferences = document.getElementById("bundlePreferences");
498  var params = { blockVisible : true,
499  sessionVisible : true,
500  allowVisible : true,
501  prefilledHost : "",
502  permissionType : "cookie",
503  windowTitle : bundlePreferences.getString("cookiepermissionstitle"),
504  introText : bundlePreferences.getString("cookiepermissionstext") };
505  document.documentElement.openWindow("Browser:Permissions",
506  "chrome://browser/content/preferences/permissions.xul",
507  "", params);
508  },
509 
513  showCookies: function (aCategory)
514  {
515  document.documentElement.openWindow("Browser:Cookies",
516  "chrome://browser/content/preferences/cookies.xul",
517  "", null);
518  },
519 
520  // CLEAR PRIVATE DATA
521 
522  /*
523  * Preferences:
524  *
525  * privacy.sanitize.sanitizeOnShutdown
526  * - true if the user's private data is cleared on startup according to the
527  * Clear Private Data settings, false otherwise
528  */
529 
533  showClearPrivateDataSettings: function ()
534  {
535  document.documentElement.openSubDialog("chrome://browser/content/preferences/sanitize.xul",
536  "", null);
537  },
538 
539 
544  clearPrivateDataNow: function (aClearEverything)
545  {
546  var ts = document.getElementById("privacy.sanitize.timeSpan");
547  var timeSpanOrig = ts.value;
548  if (aClearEverything)
549  ts.value = 0;
550 
551  const Cc = Components.classes, Ci = Components.interfaces;
552  var glue = Cc["@mozilla.org/browser/browserglue;1"]
553  .getService(Ci.nsIBrowserGlue);
554  glue.sanitize(window);
555 
556  // reset the timeSpan pref
557  if (aClearEverything)
558  ts.value = timeSpanOrig;
559  },
560 
565  _updateSanitizeSettingsButton: function () {
566  var settingsButton = document.getElementById("clearDataSettings");
567  var sanitizeOnShutdownPref = document.getElementById("privacy.sanitize.sanitizeOnShutdown");
568 
569  settingsButton.disabled = !sanitizeOnShutdownPref.value;
570  }
571 
572 };
const Cc
var pref
Definition: openLocation.js:44
var menu
var gPrivacyPane
Definition: privacy.js:46
sbOSDControlService prototype QueryInterface
getService(Ci.sbIFaceplateManager)
let window
_window init
Definition: FeedWriter.js:1144
return null
Definition: FeedWriter.js:1143
const Ci
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe