toolkit/components/downloads/nsDownloadManagerUI.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     7 ////////////////////////////////////////////////////////////////////////////////
     8 //// Constants
    10 const Cc = Components.classes;
    11 const Ci = Components.interfaces;
    12 const Cr = Components.results;
    13 const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
    14 const PREF_FLASH_COUNT = "browser.download.manager.flashCount";
    16 ////////////////////////////////////////////////////////////////////////////////
    17 //// nsDownloadManagerUI class
    19 function nsDownloadManagerUI() {}
    21 nsDownloadManagerUI.prototype = {
    22   classID: Components.ID("7dfdf0d1-aff6-4a34-bad1-d0fe74601642"),
    24   //////////////////////////////////////////////////////////////////////////////
    25   //// nsIDownloadManagerUI
    27   show: function show(aWindowContext, aDownload, aReason, aUsePrivateUI)
    28   {
    29     if (!aReason)
    30       aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED;
    32     // First we see if it is already visible
    33     let window = this.recentWindow;
    34     if (window) {
    35       window.focus();
    37       // If we are being asked to show again, with a user interaction reason,
    38       // set the appropriate variable.
    39       if (aReason == Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED)
    40         window.gUserInteracted = true;
    41       return;
    42     }
    44     let parent = null;
    45     // We try to get a window to use as the parent here.  If we don't have one,
    46     // the download manager will close immediately after opening if the pref
    47     // browser.download.manager.closeWhenDone is set to true.
    48     try {
    49       if (aWindowContext)
    50         parent = aWindowContext.getInterface(Ci.nsIDOMWindow);
    51     } catch (e) { /* it's OK to not have a parent window */ }
    53     // We pass the download manager and the nsIDownload we want selected (if any)
    54     var params = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
    55     params.appendElement(aDownload, false);
    57     // Pass in the reason as well
    58     let reason = Cc["@mozilla.org/supports-PRInt16;1"].
    59                  createInstance(Ci.nsISupportsPRInt16);
    60     reason.data = aReason;
    61     params.appendElement(reason, false);
    63     var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
    64              getService(Ci.nsIWindowWatcher);
    65     ww.openWindow(parent,
    66                   DOWNLOAD_MANAGER_URL,
    67                   "Download:Manager",
    68                   "chrome,dialog=no,resizable",
    69                   params);
    70   },
    72   get visible() {
    73     return (null != this.recentWindow);
    74   },
    76   getAttention: function getAttention()
    77   {
    78     if (!this.visible)
    79       throw Cr.NS_ERROR_UNEXPECTED;
    81     var prefs = Cc["@mozilla.org/preferences-service;1"].
    82                 getService(Ci.nsIPrefBranch);
    83     // This preference may not be set, so defaulting to two.
    84     let flashCount = 2;
    85     try {
    86       flashCount = prefs.getIntPref(PREF_FLASH_COUNT);
    87     } catch (e) { }
    89     var win = this.recentWindow.QueryInterface(Ci.nsIDOMChromeWindow);
    90     win.getAttentionWithCycleCount(flashCount);
    91   },
    93   //////////////////////////////////////////////////////////////////////////////
    94   //// nsDownloadManagerUI
    96   get recentWindow() {
    97     var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
    98              getService(Ci.nsIWindowMediator);
    99     return wm.getMostRecentWindow("Download:Manager");
   100   },
   102   //////////////////////////////////////////////////////////////////////////////
   103   //// nsISupports
   105   QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI])
   106 };
   108 ////////////////////////////////////////////////////////////////////////////////
   109 //// Module
   111 let components = [nsDownloadManagerUI];
   112 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

mercurial