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.

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

mercurial