toolkit/components/jsdownloads/src/DownloadUIHelper.jsm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Provides functions to handle status and messages in the user interface.
michael@0 9 */
michael@0 10
michael@0 11 "use strict";
michael@0 12
michael@0 13 this.EXPORTED_SYMBOLS = [
michael@0 14 "DownloadUIHelper",
michael@0 15 ];
michael@0 16
michael@0 17 ////////////////////////////////////////////////////////////////////////////////
michael@0 18 //// Globals
michael@0 19
michael@0 20 const Cc = Components.classes;
michael@0 21 const Ci = Components.interfaces;
michael@0 22 const Cu = Components.utils;
michael@0 23 const Cr = Components.results;
michael@0 24
michael@0 25 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 26
michael@0 27 XPCOMUtils.defineLazyModuleGetter(this, "OS",
michael@0 28 "resource://gre/modules/osfile.jsm");
michael@0 29 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 30 "resource://gre/modules/Promise.jsm");
michael@0 31 XPCOMUtils.defineLazyModuleGetter(this, "Services",
michael@0 32 "resource://gre/modules/Services.jsm");
michael@0 33
michael@0 34 const kStringBundleUrl =
michael@0 35 "chrome://mozapps/locale/downloads/downloads.properties";
michael@0 36
michael@0 37 const kStringsRequiringFormatting = {
michael@0 38 fileExecutableSecurityWarning: true,
michael@0 39 cancelDownloadsOKTextMultiple: true,
michael@0 40 quitCancelDownloadsAlertMsgMultiple: true,
michael@0 41 quitCancelDownloadsAlertMsgMacMultiple: true,
michael@0 42 offlineCancelDownloadsAlertMsgMultiple: true,
michael@0 43 leavePrivateBrowsingWindowsCancelDownloadsAlertMsgMultiple: true
michael@0 44 };
michael@0 45
michael@0 46 ////////////////////////////////////////////////////////////////////////////////
michael@0 47 //// DownloadUIHelper
michael@0 48
michael@0 49 /**
michael@0 50 * Provides functions to handle status and messages in the user interface.
michael@0 51 */
michael@0 52 this.DownloadUIHelper = {
michael@0 53 /**
michael@0 54 * Returns an object that can be used to display prompts related to downloads.
michael@0 55 *
michael@0 56 * The prompts may be either anchored to a specified window, or anchored to
michael@0 57 * the most recently active window, for example if the prompt is displayed in
michael@0 58 * response to global notifications that are not associated with any window.
michael@0 59 *
michael@0 60 * @param aParent
michael@0 61 * If specified, should reference the nsIDOMWindow to which the prompts
michael@0 62 * should be attached. If omitted, the prompts will be attached to the
michael@0 63 * most recently active window.
michael@0 64 *
michael@0 65 * @return A DownloadPrompter object.
michael@0 66 */
michael@0 67 getPrompter: function (aParent)
michael@0 68 {
michael@0 69 return new DownloadPrompter(aParent || null);
michael@0 70 },
michael@0 71 };
michael@0 72
michael@0 73 /**
michael@0 74 * Returns an object whose keys are the string names from the downloads string
michael@0 75 * bundle, and whose values are either the translated strings or functions
michael@0 76 * returning formatted strings.
michael@0 77 */
michael@0 78 XPCOMUtils.defineLazyGetter(DownloadUIHelper, "strings", function () {
michael@0 79 let strings = {};
michael@0 80 let sb = Services.strings.createBundle(kStringBundleUrl);
michael@0 81 let enumerator = sb.getSimpleEnumeration();
michael@0 82 while (enumerator.hasMoreElements()) {
michael@0 83 let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement);
michael@0 84 let stringName = string.key;
michael@0 85 if (stringName in kStringsRequiringFormatting) {
michael@0 86 strings[stringName] = function () {
michael@0 87 // Convert "arguments" to a real array before calling into XPCOM.
michael@0 88 return sb.formatStringFromName(stringName,
michael@0 89 Array.slice(arguments, 0),
michael@0 90 arguments.length);
michael@0 91 };
michael@0 92 } else {
michael@0 93 strings[stringName] = string.value;
michael@0 94 }
michael@0 95 }
michael@0 96 return strings;
michael@0 97 });
michael@0 98
michael@0 99 ////////////////////////////////////////////////////////////////////////////////
michael@0 100 //// DownloadPrompter
michael@0 101
michael@0 102 /**
michael@0 103 * Allows displaying prompts related to downloads.
michael@0 104 *
michael@0 105 * @param aParent
michael@0 106 * The nsIDOMWindow to which prompts should be attached, or null to
michael@0 107 * attach prompts to the most recently active window.
michael@0 108 */
michael@0 109 this.DownloadPrompter = function (aParent)
michael@0 110 {
michael@0 111 #ifdef MOZ_B2G
michael@0 112 // On B2G there is no prompter implementation.
michael@0 113 this._prompter = null;
michael@0 114 #else
michael@0 115 this._prompter = Services.ww.getNewPrompter(aParent);
michael@0 116 #endif
michael@0 117 }
michael@0 118
michael@0 119 this.DownloadPrompter.prototype = {
michael@0 120 /**
michael@0 121 * Constants with the different type of prompts.
michael@0 122 */
michael@0 123 ON_QUIT: "prompt-on-quit",
michael@0 124 ON_OFFLINE: "prompt-on-offline",
michael@0 125 ON_LEAVE_PRIVATE_BROWSING: "prompt-on-leave-private-browsing",
michael@0 126
michael@0 127 /**
michael@0 128 * nsIPrompt instance for displaying messages.
michael@0 129 */
michael@0 130 _prompter: null,
michael@0 131
michael@0 132 /**
michael@0 133 * Displays a warning message box that informs that the specified file is
michael@0 134 * executable, and asks whether the user wants to launch it. The user is
michael@0 135 * given the option of disabling future instances of this warning.
michael@0 136 *
michael@0 137 * @param aPath
michael@0 138 * String containing the full path to the file to be opened.
michael@0 139 *
michael@0 140 * @return {Promise}
michael@0 141 * @resolves Boolean indicating whether the launch operation can continue.
michael@0 142 * @rejects JavaScript exception.
michael@0 143 */
michael@0 144 confirmLaunchExecutable: function (aPath)
michael@0 145 {
michael@0 146 const kPrefAlertOnEXEOpen = "browser.download.manager.alertOnEXEOpen";
michael@0 147
michael@0 148 try {
michael@0 149 // Always launch in case we have no prompter implementation.
michael@0 150 if (!this._prompter) {
michael@0 151 return Promise.resolve(true);
michael@0 152 }
michael@0 153
michael@0 154 try {
michael@0 155 if (!Services.prefs.getBoolPref(kPrefAlertOnEXEOpen)) {
michael@0 156 return Promise.resolve(true);
michael@0 157 }
michael@0 158 } catch (ex) {
michael@0 159 // If the preference does not exist, continue with the prompt.
michael@0 160 }
michael@0 161
michael@0 162 let leafName = OS.Path.basename(aPath);
michael@0 163
michael@0 164 let s = DownloadUIHelper.strings;
michael@0 165 let checkState = { value: false };
michael@0 166 let shouldLaunch = this._prompter.confirmCheck(
michael@0 167 s.fileExecutableSecurityWarningTitle,
michael@0 168 s.fileExecutableSecurityWarning(leafName, leafName),
michael@0 169 s.fileExecutableSecurityWarningDontAsk,
michael@0 170 checkState);
michael@0 171
michael@0 172 if (shouldLaunch) {
michael@0 173 Services.prefs.setBoolPref(kPrefAlertOnEXEOpen, !checkState.value);
michael@0 174 }
michael@0 175
michael@0 176 return Promise.resolve(shouldLaunch);
michael@0 177 } catch (ex) {
michael@0 178 return Promise.reject(ex);
michael@0 179 }
michael@0 180 },
michael@0 181
michael@0 182 /**
michael@0 183 * Displays a warning message box that informs that there are active
michael@0 184 * downloads, and asks whether the user wants to cancel them or not.
michael@0 185 *
michael@0 186 * @param aDownloadsCount
michael@0 187 * The current downloads count.
michael@0 188 * @param aPromptType
michael@0 189 * The type of prompt notification depending on the observer.
michael@0 190 *
michael@0 191 * @return False to cancel the downloads and continue, true to abort the
michael@0 192 * operation.
michael@0 193 */
michael@0 194 confirmCancelDownloads: function DP_confirmCancelDownload(aDownloadsCount,
michael@0 195 aPromptType)
michael@0 196 {
michael@0 197 // Always continue in case we have no prompter implementation, or if there
michael@0 198 // are no active downloads.
michael@0 199 if (!this._prompter || aDownloadsCount <= 0) {
michael@0 200 return false;
michael@0 201 }
michael@0 202
michael@0 203 let s = DownloadUIHelper.strings;
michael@0 204 let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
michael@0 205 (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1);
michael@0 206 let okButton = aDownloadsCount > 1 ? s.cancelDownloadsOKTextMultiple(aDownloadsCount)
michael@0 207 : s.cancelDownloadsOKText;
michael@0 208 let title, message, cancelButton;
michael@0 209
michael@0 210 switch (aPromptType) {
michael@0 211 case this.ON_QUIT:
michael@0 212 title = s.quitCancelDownloadsAlertTitle;
michael@0 213 #ifndef XP_MACOSX
michael@0 214 message = aDownloadsCount > 1
michael@0 215 ? s.quitCancelDownloadsAlertMsgMultiple(aDownloadsCount)
michael@0 216 : s.quitCancelDownloadsAlertMsg;
michael@0 217 cancelButton = s.dontQuitButtonWin;
michael@0 218 #else
michael@0 219 message = aDownloadsCount > 1
michael@0 220 ? s.quitCancelDownloadsAlertMsgMacMultiple(aDownloadsCount)
michael@0 221 : s.quitCancelDownloadsAlertMsgMac;
michael@0 222 cancelButton = s.dontQuitButtonMac;
michael@0 223 #endif
michael@0 224 break;
michael@0 225 case this.ON_OFFLINE:
michael@0 226 title = s.offlineCancelDownloadsAlertTitle;
michael@0 227 message = aDownloadsCount > 1
michael@0 228 ? s.offlineCancelDownloadsAlertMsgMultiple(aDownloadsCount)
michael@0 229 : s.offlineCancelDownloadsAlertMsg;
michael@0 230 cancelButton = s.dontGoOfflineButton;
michael@0 231 break;
michael@0 232 case this.ON_LEAVE_PRIVATE_BROWSING:
michael@0 233 title = s.leavePrivateBrowsingCancelDownloadsAlertTitle;
michael@0 234 message = aDownloadsCount > 1
michael@0 235 ? s.leavePrivateBrowsingWindowsCancelDownloadsAlertMsgMultiple(aDownloadsCount)
michael@0 236 : s.leavePrivateBrowsingWindowsCancelDownloadsAlertMsg;
michael@0 237 cancelButton = s.dontLeavePrivateBrowsingButton;
michael@0 238 break;
michael@0 239 }
michael@0 240
michael@0 241 let rv = this._prompter.confirmEx(title, message, buttonFlags, okButton,
michael@0 242 cancelButton, null, null, {});
michael@0 243 return (rv == 1);
michael@0 244 }
michael@0 245 };

mercurial