1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/downloads/src/DownloadsUI.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/** 1.11 + * This component implements the nsIDownloadManagerUI interface and opens the 1.12 + * Downloads view for the most recent browser window when requested. 1.13 + */ 1.14 + 1.15 +"use strict"; 1.16 + 1.17 +//////////////////////////////////////////////////////////////////////////////// 1.18 +//// Globals 1.19 + 1.20 +const Cc = Components.classes; 1.21 +const Ci = Components.interfaces; 1.22 +const Cu = Components.utils; 1.23 +const Cr = Components.results; 1.24 + 1.25 +Cu.import("resource://gre/modules/Services.jsm"); 1.26 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.27 + 1.28 +XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon", 1.29 + "resource:///modules/DownloadsCommon.jsm"); 1.30 +XPCOMUtils.defineLazyServiceGetter(this, "gBrowserGlue", 1.31 + "@mozilla.org/browser/browserglue;1", 1.32 + "nsIBrowserGlue"); 1.33 +XPCOMUtils.defineLazyModuleGetter(this, "RecentWindow", 1.34 + "resource:///modules/RecentWindow.jsm"); 1.35 +XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", 1.36 + "resource://gre/modules/PrivateBrowsingUtils.jsm"); 1.37 + 1.38 +//////////////////////////////////////////////////////////////////////////////// 1.39 +//// DownloadsUI 1.40 + 1.41 +function DownloadsUI() 1.42 +{ 1.43 +} 1.44 + 1.45 +DownloadsUI.prototype = { 1.46 + classID: Components.ID("{4d99321e-d156-455b-81f7-e7aa2308134f}"), 1.47 + 1.48 + _xpcom_factory: XPCOMUtils.generateSingletonFactory(DownloadsUI), 1.49 + 1.50 + ////////////////////////////////////////////////////////////////////////////// 1.51 + //// nsISupports 1.52 + 1.53 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]), 1.54 + 1.55 + ////////////////////////////////////////////////////////////////////////////// 1.56 + //// nsIDownloadManagerUI 1.57 + 1.58 + show: function DUI_show(aWindowContext, aDownload, aReason, aUsePrivateUI) 1.59 + { 1.60 + if (!aReason) { 1.61 + aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED; 1.62 + } 1.63 + 1.64 + if (aReason == Ci.nsIDownloadManagerUI.REASON_NEW_DOWNLOAD) { 1.65 + const kMinimized = Ci.nsIDOMChromeWindow.STATE_MINIMIZED; 1.66 + let browserWin = gBrowserGlue.getMostRecentBrowserWindow(); 1.67 + 1.68 + if (!browserWin || browserWin.windowState == kMinimized) { 1.69 + this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); 1.70 + } 1.71 + else { 1.72 + // If the indicator is visible, then new download notifications are 1.73 + // already handled by the panel service. 1.74 + browserWin.DownloadsButton.checkIsVisible(function(isVisible) { 1.75 + if (!isVisible) { 1.76 + this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); 1.77 + } 1.78 + }.bind(this)); 1.79 + } 1.80 + } else { 1.81 + this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); 1.82 + } 1.83 + }, 1.84 + 1.85 + get visible() true, 1.86 + 1.87 + getAttention: function () {}, 1.88 + 1.89 + ////////////////////////////////////////////////////////////////////////////// 1.90 + //// Private 1.91 + 1.92 + /** 1.93 + * Helper function that opens the download manager UI. 1.94 + */ 1.95 + _showDownloadManagerUI: function (aWindowContext, aUsePrivateUI) 1.96 + { 1.97 + // If we weren't given a window context, try to find a browser window 1.98 + // to use as our parent - and if that doesn't work, error out and give up. 1.99 + let parentWindow = aWindowContext; 1.100 + if (!parentWindow) { 1.101 + parentWindow = RecentWindow.getMostRecentBrowserWindow({ private: !!aUsePrivateUI }); 1.102 + if (!parentWindow) { 1.103 + Components.utils.reportError( 1.104 + "Couldn't find a browser window to open the Places Downloads View " + 1.105 + "from."); 1.106 + return; 1.107 + } 1.108 + } 1.109 + 1.110 + // If window is private then show it in a tab. 1.111 + if (PrivateBrowsingUtils.isWindowPrivate(parentWindow)) { 1.112 + parentWindow.openUILinkIn("about:downloads", "tab"); 1.113 + return; 1.114 + } else { 1.115 + let organizer = Services.wm.getMostRecentWindow("Places:Organizer"); 1.116 + if (!organizer) { 1.117 + parentWindow.openDialog("chrome://browser/content/places/places.xul", 1.118 + "", "chrome,toolbar=yes,dialog=no,resizable", 1.119 + "Downloads"); 1.120 + } else { 1.121 + organizer.PlacesOrganizer.selectLeftPaneQuery("Downloads"); 1.122 + organizer.focus(); 1.123 + } 1.124 + } 1.125 + } 1.126 +}; 1.127 + 1.128 +//////////////////////////////////////////////////////////////////////////////// 1.129 +//// Module 1.130 + 1.131 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DownloadsUI]);