mobile/android/components/HelperAppDialog.js
author Simone Bruno <sbruno@mozilla.com>
Mon, 31 Mar 2014 19:59:04 +0200
changeset 176187 ccd91b78561fb629333db0fd40114bc1f73c933a
parent 173287 709e9207641d12065f566ef1aa480a23a0e4f859
child 182803 ab1394be8e93f82c72576c7377de849e16290411
permissions -rw-r--r--
Bug 956451 - added mozconfig file to be used for macosx64_gecko-debug build, r=catlee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
     1
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
94475
Gervase Markham <gerv@gerv.net>
parents: 89885
diff changeset
     2
/* This Source Code Form is subject to the terms of the Mozilla Public
Gervase Markham <gerv@gerv.net>
parents: 89885
diff changeset
     3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Gervase Markham <gerv@gerv.net>
parents: 89885
diff changeset
     4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
     5
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
     6
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
     7
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
     8
const APK_MIME_TYPE = "application/vnd.android.package-archive";
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
     9
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
94927
7d34bf17109677b5b2059505cc306a90c6408c3b Bug 741430 - Use new icons for status alert notifications r=sriram
Mark Finkle <mfinkle@mozilla.com>
parents: 94475
diff changeset
    10
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    11
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    12
Cu.import("resource://gre/modules/FileUtils.jsm");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    13
Cu.import("resource://gre/modules/HelperApps.jsm");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    14
Cu.import("resource://gre/modules/Prompt.jsm");
66434
3065f2121b0929d858da19fb332a1bdac3c34b01 Fix Service/Services typo, r=mfinkle
Brian Crowder <crowderbt@gmail.com>
parents: 66362
diff changeset
    15
Cu.import("resource://gre/modules/Services.jsm");
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    16
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    17
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    18
// -----------------------------------------------------------------------
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    19
// HelperApp Launcher Dialog
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    20
// -----------------------------------------------------------------------
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    21
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    22
function HelperAppLauncherDialog() { }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    23
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    24
HelperAppLauncherDialog.prototype = {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    25
  classID: Components.ID("{e9d277a0-268a-4ec2-bb8c-10fdf3e44611}"),
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    26
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]),
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
    27
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    28
  getNativeWindow: function () {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    29
    try {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    30
      let win = Services.wm.getMostRecentWindow("navigator:browser");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    31
      if (win && win.NativeWindow) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    32
        return win.NativeWindow;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    33
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    34
    } catch (e) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    35
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    36
    return null;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    37
  },
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    38
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    39
  /**
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    40
   * Returns false if `url` represents a local or special URL that we don't
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    41
   * wish to ever download.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    42
   *
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    43
   * Returns true otherwise.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    44
   */
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    45
  _canDownload: function (url, alreadyResolved=false) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    46
    // The common case.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    47
    if (url.schemeIs("http") ||
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    48
        url.schemeIs("https") ||
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    49
        url.schemeIs("ftp")) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    50
      return true;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    51
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    52
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    53
    // The less-common opposite case.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    54
    if (url.schemeIs("chrome") ||
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    55
        url.schemeIs("jar") ||
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    56
        url.schemeIs("resource") ||
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    57
        url.schemeIs("wyciwyg")) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    58
      return false;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    59
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    60
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    61
    // For all other URIs, try to resolve them to an inner URI, and check that.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    62
    if (!alreadyResolved) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    63
      let ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    64
      let innerURI = ioSvc.newChannelFromURI(url).URI;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    65
      if (!url.equals(innerURI)) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    66
        return this._canDownload(innerURI, true);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    67
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    68
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    69
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    70
    if (url.schemeIs("file")) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    71
      // If it's in our app directory or profile directory, we never ever
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    72
      // want to do anything with it, including saving to disk or passing the
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    73
      // file to another application.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    74
      let file = url.QueryInterface(Ci.nsIFileURL).file;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    75
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    76
      // TODO: pref blacklist?
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    77
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    78
      let appRoot = FileUtils.getFile("XREExeF", []);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    79
      if (appRoot.contains(file, true)) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    80
        return false;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    81
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    82
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    83
      let profileRoot = FileUtils.getFile("ProfD", []);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    84
      if (profileRoot.contains(file, true)) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    85
        return false;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    86
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    87
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    88
      return true;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    89
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    90
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    91
    // Anything else is fine to download.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    92
    return true;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    93
  },
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    94
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    95
  /**
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    96
   * Returns true if `launcher` represents a download for which we wish
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    97
   * to prompt.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    98
   */
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
    99
  _shouldPrompt: function (launcher) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   100
    let mimeType = this._getMimeTypeFromLauncher(launcher);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   101
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   102
    // Straight equality: nsIMIMEInfo normalizes.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   103
    return APK_MIME_TYPE == mimeType;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   104
  },
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   105
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   106
  show: function hald_show(aLauncher, aContext, aReason) {
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   107
    if (!this._canDownload(aLauncher.source)) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   108
      aLauncher.cancel(Cr.NS_BINDING_ABORTED);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   109
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   110
      let win = this.getNativeWindow();
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   111
      if (!win) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   112
        // Oops.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   113
        Services.console.logStringMessage("Refusing download, but can't show a toast.");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   114
        return;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   115
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   116
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   117
      Services.console.logStringMessage("Refusing download of non-downloadable file.");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   118
      let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   119
      let failedText = bundle.GetStringFromName("protocol.failed");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   120
      win.toast.show(failedText, "long");
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   121
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   122
      return;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   123
    }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   124
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   125
    let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   126
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   127
    let defaultHandler = new Object();
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   128
    let apps = HelperApps.getAppsForUri(aLauncher.source, {
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   129
      mimeType: aLauncher.MIMEInfo.MIMEType,
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   130
    });
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   131
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   132
    // Add a fake intent for save to disk at the top of the list.
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   133
    apps.unshift({
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   134
      name: bundle.GetStringFromName("helperapps.saveToDisk"),
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   135
      packageName: "org.mozilla.gecko.Download",
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   136
      iconUri: "drawable://icon",
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   137
      launch: function() {
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   138
        // Reset the preferredAction here.
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   139
        aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   140
        aLauncher.saveToDisk(null, false);
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   141
        return true;
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   142
      }
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   143
    });
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   144
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   145
    // See if the user already marked something as the default for this mimetype,
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   146
    // and if that app is still installed.
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   147
    let preferredApp = this._getPreferredApp(aLauncher);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   148
    if (preferredApp) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   149
      let pref = apps.filter(function(app) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   150
        return app.packageName === preferredApp;
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   151
      });
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   152
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   153
      if (pref.length > 0) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   154
        pref[0].launch(aLauncher.source);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   155
        return;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   156
      }
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   157
    }
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   158
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   159
    let callback = function(app) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   160
      aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   161
      if (!app.launch(aLauncher.source)) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   162
        aLauncher.cancel(Cr.NS_BINDING_ABORTED);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   163
      }
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   164
    }
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   165
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   166
    // If there's only one choice, and we don't want to prompt, go right ahead
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   167
    // and choose that app automatically.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   168
    if (!this._shouldPrompt(aLauncher) && (apps.length === 1)) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   169
      callback(apps[0]);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   170
      return;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   171
    }
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   172
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   173
    // Otherwise, let's go through the prompt.
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   174
    HelperApps.prompt(apps, {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   175
      title: bundle.GetStringFromName("helperapps.pick"),
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   176
      buttons: [
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   177
        bundle.GetStringFromName("helperapps.alwaysUse"),
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   178
        bundle.GetStringFromName("helperapps.useJustOnce")
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   179
      ]
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   180
    }, (data) => {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   181
      if (data.button < 0) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   182
        return;
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   183
      }
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   184
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   185
      callback(apps[data.icongrid0]);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   186
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   187
      if (data.button === 0) {
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   188
        this._setPreferredApp(aLauncher, apps[data.icongrid0]);
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   189
      }
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   190
    });
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   191
  },
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   192
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   193
  _getPrefName: function getPrefName(mimetype) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   194
    return "browser.download.preferred." + mimetype.replace("\\", ".");
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   195
  },
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   196
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   197
  _getMimeTypeFromLauncher: function (launcher) {
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   198
    let mime = launcher.MIMEInfo.MIMEType;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   199
    if (!mime)
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   200
      mime = ContentAreaUtils.getMIMETypeForURI(launcher.source) || "";
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   201
    return mime;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   202
  },
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   203
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   204
  _getPreferredApp: function getPreferredApp(launcher) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   205
    let mime = this._getMimeTypeFromLauncher(launcher);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   206
    if (!mime)
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   207
      return;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   208
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   209
    try {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   210
      return Services.prefs.getCharPref(this._getPrefName(mime));
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   211
    } catch(ex) {
172978
Richard Newman <rnewman@mozilla.com>
parents: 152840
diff changeset
   212
      Services.console.logStringMessage("Error getting pref for " + mime + ".");
151538
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   213
    }
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   214
    return null;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   215
  },
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   216
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   217
  _setPreferredApp: function setPreferredApp(launcher, app) {
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   218
    let mime = this._getMimeTypeFromLauncher(launcher);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   219
    if (!mime)
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   220
      return;
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   221
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   222
    if (app)
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   223
      Services.prefs.setCharPref(this._getPrefName(mime), app.packageName);
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   224
    else
04f77c1e3337dcec793622c5b47b6f67fcc76c23 Bug 780379 - Add buttons to download prompts. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 151399
diff changeset
   225
      Services.prefs.clearUserPref(this._getPrefName(mime));
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   226
  },
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   227
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   228
  promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
149341
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   229
    // Retrieve the user's default download directory
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   230
    let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   231
    let defaultFolder = dnldMgr.userDownloadsDirectory;
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   232
149341
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   233
    try {
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   234
      file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   235
    } catch (e) { }
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   236
149341
d71579c316c17b0ca7aa1130075dc9d70d3ff5b3 Backout 41701d2c0341 (bug 875731), Caused a regression that breaks all downloads in Firefox for Android (bug 921944)
Mark Finkle <mfinkle@mozilla.com>
parents: 149082
diff changeset
   237
    return file;
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   238
  },
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   239
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   240
  validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   241
    if (!(aLocalFile && this.isUsableDirectory(aLocalFile)))
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   242
      return null;
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   243
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   244
    // Remove any leading periods, since we don't want to save hidden files
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   245
    // automatically.
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   246
    aLeafName = aLeafName.replace(/^\.+/, "");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   247
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   248
    if (aLeafName == "")
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   249
      aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : "");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   250
    aLocalFile.append(aLeafName);
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   251
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   252
    this.makeFileUnique(aLocalFile);
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   253
    return aLocalFile;
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   254
  },
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   255
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   256
  makeFileUnique: function hald_makeFileUnique(aLocalFile) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   257
    try {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   258
      // Note - this code is identical to that in
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   259
      //   toolkit/content/contentAreaUtils.js.
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   260
      // If you are updating this code, update that code too! We can't share code
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   261
      // here since this is called in a js component.
151399
0cc4a019835f096a5df82d9070db9b96addc75d6 Bug 780379 - Show a custom intent chooser for downloads that can be downloaded by an external app. r=mfinkle
Wes Johnston <wjohnston@mozilla.com>
parents: 149341
diff changeset
   262
      let collisionCount = 0;
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   263
      while (aLocalFile.exists()) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   264
        collisionCount++;
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   265
        if (collisionCount == 1) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   266
          // Append "(2)" before the last dot in (or at the end of) the filename
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   267
          // special case .ext.gz etc files so we don't wind up with .tar(2).gz
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   268
          if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i))
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   269
            aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   270
          else
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   271
            aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   272
        }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   273
        else {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   274
          // replace the last (n) in the filename with (n+1)
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   275
          aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   276
        }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   277
      }
66543
28e5592fb1902a699faa99d4c60bae5735a66921 Bug 592902 - Provide a way to cleanup or stop accidental downloads [r=mfinkle]
Wesley Johnston <wjohnston@mozilla.com>
parents: 66522
diff changeset
   278
      aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   279
    }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   280
    catch (e) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   281
      dump("*** exception in validateLeafName: " + e + "\n");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   282
66543
28e5592fb1902a699faa99d4c60bae5735a66921 Bug 592902 - Provide a way to cleanup or stop accidental downloads [r=mfinkle]
Wesley Johnston <wjohnston@mozilla.com>
parents: 66522
diff changeset
   283
      if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED)
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   284
        throw e;
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   285
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   286
      if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   287
        aLocalFile.append("unnamed");
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   288
        if (aLocalFile.exists())
66543
28e5592fb1902a699faa99d4c60bae5735a66921 Bug 592902 - Provide a way to cleanup or stop accidental downloads [r=mfinkle]
Wesley Johnston <wjohnston@mozilla.com>
parents: 66522
diff changeset
   289
          aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   290
      }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   291
    }
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   292
  },
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   293
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   294
  isUsableDirectory: function hald_isUsableDirectory(aDirectory) {
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   295
    return aDirectory.exists() && aDirectory.isDirectory() && aDirectory.isWritable();
66560
6b9420db098c03f2a7f870a33b0943079466ac77 Bug 594504 - Show a message to explain why downloads with no helper apps were cancelled [r=mfinkle]
Wesley Johnston <wjohnston@mozilla.com>
parents: 66543
diff changeset
   296
  },
65192
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   297
};
455077dd5abf4a6db66d724648714707e10a53be Bug 479808: Save As dialog is oversized and ugly, r=blassey
Mark Finkle <mfinkle@mozilla.com>
parents:
diff changeset
   298
111947
5ce71981e005a52d4cb0b831ad3db9284f2fb356 Bug 798491: Add an option to stick all chrome JSMs/JS components in the same compartment. r=mrbkap,philikon
Kyle Huey <khuey@kylehuey.com>
parents: 111876
diff changeset
   299
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);