nonbrowser-mac.js (5207B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 var NonBrowserWindow = { 7 delayedStartupTimeoutId: null, 8 MAC_HIDDEN_WINDOW: "chrome://browser/content/hiddenWindowMac.xhtml", 9 10 openBrowserWindowFromDockMenu(options = {}) { 11 let existingWindow = BrowserWindowTracker.getTopWindow(); 12 options.openerWindow = existingWindow || window; 13 let win = OpenBrowserWindow(options); 14 win.addEventListener( 15 "load", 16 () => this.dockSupport.activateApplication(true), 17 { once: true } 18 ); 19 20 return win; 21 }, 22 23 startup() { 24 // Disable inappropriate commands / submenus 25 var disabledItems = [ 26 "Browser:SavePage", 27 "Browser:SendLink", 28 "cmd_pageSetup", 29 "cmd_print", 30 "cmd_find", 31 "cmd_findAgain", 32 "viewToolbarsMenu", 33 "viewSidebarMenuMenu", 34 "Browser:Reload", 35 "viewFullZoomMenu", 36 "pageStyleMenu", 37 "repair-text-encoding", 38 "View:PageSource", 39 "View:FullScreen", 40 "enterFullScreenItem", 41 "viewHistorySidebar", 42 "Browser:AddBookmarkAs", 43 "Browser:BookmarkAllTabs", 44 "View:PageInfo", 45 "History:UndoCloseTab", 46 "menu_openFirefoxView", 47 ]; 48 var element; 49 50 for (let disabledItem of disabledItems) { 51 element = document.getElementById(disabledItem); 52 if (element) { 53 element.setAttribute("disabled", "true"); 54 } 55 } 56 57 // Show menus that are only visible in non-browser windows 58 let shownItems = ["menu_openLocation"]; 59 for (let shownItem of shownItems) { 60 element = document.getElementById(shownItem); 61 if (element) { 62 element.removeAttribute("hidden"); 63 } 64 } 65 66 if (window.location.href == this.MAC_HIDDEN_WINDOW) { 67 // If no windows are active (i.e. we're the hidden window), disable the 68 // close, minimize and zoom menu commands as well. 69 var hiddenWindowDisabledItems = [ 70 "cmd_close", 71 "cmd_minimizeWindow", 72 "zoomWindow", 73 ]; 74 for (let hiddenWindowDisabledItem of hiddenWindowDisabledItems) { 75 element = document.getElementById(hiddenWindowDisabledItem); 76 if (element) { 77 element.setAttribute("disabled", "true"); 78 } 79 } 80 81 // Also hide the window-list separator. 82 element = document.getElementById("sep-window-list"); 83 element.hidden = true; 84 85 // Setup the dock menu. 86 let dockMenuElement = document.getElementById("menu_mac_dockmenu"); 87 if (dockMenuElement != null) { 88 let nativeMenu = Cc[ 89 "@mozilla.org/widget/standalonenativemenu;1" 90 ].createInstance(Ci.nsIStandaloneNativeMenu); 91 92 try { 93 nativeMenu.init(dockMenuElement); 94 this.dockSupport.dockMenu = nativeMenu; 95 } catch (e) {} 96 } 97 98 dockMenuElement.addEventListener("command", this); 99 100 // Hide menuitems that don't apply to private contexts. 101 if (PrivateBrowsingUtils.permanentPrivateBrowsing) { 102 document.getElementById("macDockMenuNewWindow").hidden = true; 103 } 104 if (!PrivateBrowsingUtils.enabled) { 105 document.getElementById("macDockMenuNewPrivateWindow").hidden = true; 106 } 107 if (BrowserUIUtils.quitShortcutDisabled) { 108 document.getElementById("key_quitApplication").remove(); 109 document.getElementById("menu_FileQuitItem").removeAttribute("key"); 110 } 111 } 112 113 this.delayedStartupTimeoutId = setTimeout(() => this.delayedStartup(), 0); 114 }, 115 116 delayedStartup() { 117 this.delayedStartupTimeoutId = null; 118 119 // initialise the offline listener 120 BrowserOffline.init(); 121 122 // Initialize the private browsing UI only if window is private 123 if (PrivateBrowsingUtils.isWindowPrivate(window)) { 124 PrivateBrowsingUI.init(window); 125 } 126 }, 127 128 shutdown() { 129 // If this is the hidden window being closed, release our reference to 130 // the dock menu element to prevent leaks on shutdown 131 if (window.location.href == this.MAC_HIDDEN_WINDOW) { 132 this.dockSupport.dockMenu = null; 133 } 134 135 // If nonBrowserWindowDelayedStartup hasn't run yet, we have no work to do - 136 // just cancel the pending timeout and return; 137 if (this.delayedStartupTimeoutId) { 138 clearTimeout(this.delayedStartupTimeoutId); 139 return; 140 } 141 142 BrowserOffline.uninit(); 143 }, 144 145 handleEvent(event) { 146 switch (event.type) { 147 case "load": 148 this.startup(); 149 break; 150 case "unload": 151 this.shutdown(); 152 break; 153 case "command": 154 if (event.target.id.startsWith("macDockMenuNew")) { 155 let wantPrivate = event.target.id == "macDockMenuNewPrivateWindow"; 156 this.openBrowserWindowFromDockMenu( 157 wantPrivate ? { private: true } : {} 158 ); 159 } 160 break; 161 } 162 }, 163 }; 164 165 addEventListener("load", NonBrowserWindow, false); 166 addEventListener("unload", NonBrowserWindow, false); 167 XPCOMUtils.defineLazyServiceGetter( 168 NonBrowserWindow, 169 "dockSupport", 170 "@mozilla.org/widget/macdocksupport;1", 171 Ci.nsIMacDockSupport 172 );