|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = ["PrivacyLevel"]; |
|
8 |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 |
|
13 const PREF_NORMAL = "browser.sessionstore.privacy_level"; |
|
14 const PREF_DEFERRED = "browser.sessionstore.privacy_level_deferred"; |
|
15 |
|
16 // The following constants represent the different possible privacy levels that |
|
17 // can be set by the user and that we need to consider when collecting text |
|
18 // data, and cookies. |
|
19 // |
|
20 // Collect data from all sites (http and https). |
|
21 const PRIVACY_NONE = 0; |
|
22 // Collect data from unencrypted sites (http), only. |
|
23 const PRIVACY_ENCRYPTED = 1; |
|
24 // Collect no data. |
|
25 const PRIVACY_FULL = 2; |
|
26 |
|
27 /** |
|
28 * Returns whether we will resume the session automatically on next startup. |
|
29 */ |
|
30 function willResumeAutomatically() { |
|
31 return Services.prefs.getIntPref("browser.startup.page") == 3 || |
|
32 Services.prefs.getBoolPref("browser.sessionstore.resume_session_once"); |
|
33 } |
|
34 |
|
35 /** |
|
36 * Determines the current privacy level as set by the user. |
|
37 * |
|
38 * @param isPinned |
|
39 * Whether to return the privacy level for pinned tabs. |
|
40 * @return {int} The privacy level as read from the user's preferences. |
|
41 */ |
|
42 function getCurrentLevel(isPinned) { |
|
43 let pref = PREF_NORMAL; |
|
44 |
|
45 // If we're in the process of quitting and we're not autoresuming the session |
|
46 // then we will use the deferred privacy level for non-pinned tabs. |
|
47 if (!isPinned && Services.startup.shuttingDown && !willResumeAutomatically()) { |
|
48 pref = PREF_DEFERRED; |
|
49 } |
|
50 |
|
51 return Services.prefs.getIntPref(pref); |
|
52 } |
|
53 |
|
54 /** |
|
55 * The external API as exposed by this module. |
|
56 */ |
|
57 let PrivacyLevel = Object.freeze({ |
|
58 /** |
|
59 * Checks whether we're allowed to save data for a specific site. |
|
60 * |
|
61 * @param {isHttps: boolean, isPinned: boolean} |
|
62 * An object that must have two properties: 'isHttps' and 'isPinned'. |
|
63 * 'isHttps' tells whether the site us secure communication (HTTPS). |
|
64 * 'isPinned' tells whether the site is loaded in a pinned tab. |
|
65 * @return {bool} Whether we can save data for the specified site. |
|
66 */ |
|
67 canSave: function ({isHttps, isPinned}) { |
|
68 let level = getCurrentLevel(isPinned); |
|
69 |
|
70 // Never save any data when full privacy is requested. |
|
71 if (level == PRIVACY_FULL) { |
|
72 return false; |
|
73 } |
|
74 |
|
75 // Don't save data for encrypted sites when requested. |
|
76 if (isHttps && level == PRIVACY_ENCRYPTED) { |
|
77 return false; |
|
78 } |
|
79 |
|
80 return true; |
|
81 } |
|
82 }); |