michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let Cu = Components.utils; michael@0: let Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/PageThumbs.jsm"); michael@0: michael@0: // Page for which the start UI is shown michael@0: const kStartURI = "about:newtab"; michael@0: michael@0: // allow panning after this timeout on pages with registered touch listeners michael@0: const kTouchTimeout = 300; michael@0: const kSetInactiveStateTimeout = 100; michael@0: michael@0: const kTabThumbnailDelayCapture = 500; michael@0: michael@0: const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; michael@0: michael@0: // See grid.xml, we use this to cache style info across loads of the startui. michael@0: var _richgridTileSizes = {}; michael@0: michael@0: // Override sizeToContent in the main window. It breaks things (bug 565887) michael@0: window.sizeToContent = function() { michael@0: Cu.reportError("window.sizeToContent is not allowed in this window"); michael@0: } michael@0: michael@0: function getTabModalPromptBox(aWindow) { michael@0: let browser = Browser.getBrowserForWindow(aWindow); michael@0: return Browser.getTabModalPromptBox(browser); michael@0: } michael@0: michael@0: /* michael@0: * Returns the browser for the currently displayed tab. michael@0: */ michael@0: function getBrowser() { michael@0: return Browser.selectedBrowser; michael@0: } michael@0: michael@0: var Browser = { michael@0: _debugEvents: false, michael@0: _tabs: [], michael@0: _selectedTab: null, michael@0: _tabId: 0, michael@0: windowUtils: window.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils), michael@0: michael@0: get defaultBrowserWidth() { michael@0: return window.innerWidth; michael@0: }, michael@0: michael@0: startup: function startup() { michael@0: var self = this; michael@0: michael@0: try { michael@0: messageManager.loadFrameScript("chrome://browser/content/Util.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/Content.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/FormHelper.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/library/SelectionPrototype.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/SelectionHandler.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ContextMenuHandler.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ConsoleAPIObserver.js", true); michael@0: messageManager.loadFrameScript("chrome://browser/content/contenthandlers/PluginHelper.js", true); michael@0: } catch (e) { michael@0: // XXX whatever is calling startup needs to dump errors! michael@0: dump("###########" + e + "\n"); michael@0: } michael@0: michael@0: if (!Services.metro) { michael@0: // Services.metro is only available on Windows Metro. We want to be able michael@0: // to test metro on other platforms, too, so we provide a minimal shim. michael@0: Services.metro = { michael@0: activationURI: "", michael@0: pinTileAsync: function () {}, michael@0: unpinTileAsync: function () {} michael@0: }; michael@0: } michael@0: michael@0: /* handles dispatching clicks on browser into clicks in content or zooms */ michael@0: Elements.browsers.customDragger = new Browser.MainDragger(); michael@0: michael@0: /* handles web progress management for open browsers */ michael@0: Elements.browsers.webProgress = WebProgress.init(); michael@0: michael@0: // Call InputSourceHelper first so global listeners get called before michael@0: // we start processing input in TouchModule. michael@0: InputSourceHelper.init(); michael@0: ClickEventHandler.init(); michael@0: michael@0: TouchModule.init(); michael@0: GestureModule.init(); michael@0: BrowserTouchHandler.init(); michael@0: PopupBlockerObserver.init(); michael@0: APZCObserver.init(); michael@0: michael@0: // Init the touch scrollbox michael@0: this.contentScrollbox = Elements.browsers; michael@0: this.contentScrollboxScroller = { michael@0: scrollBy: function(aDx, aDy) { michael@0: let view = getBrowser().getRootView(); michael@0: view.scrollBy(aDx, aDy); michael@0: }, michael@0: michael@0: scrollTo: function(aX, aY) { michael@0: let view = getBrowser().getRootView(); michael@0: view.scrollTo(aX, aY); michael@0: }, michael@0: michael@0: getPosition: function(aScrollX, aScrollY) { michael@0: let view = getBrowser().getRootView(); michael@0: let scroll = view.getPosition(); michael@0: aScrollX.value = scroll.x; michael@0: aScrollY.value = scroll.y; michael@0: } michael@0: }; michael@0: michael@0: ContentAreaObserver.init(); michael@0: michael@0: function fullscreenHandler() { michael@0: if (Browser.selectedBrowser.contentWindow.document.mozFullScreenElement) michael@0: Elements.stack.setAttribute("fullscreen", "true"); michael@0: else michael@0: Elements.stack.removeAttribute("fullscreen"); michael@0: } michael@0: window.addEventListener("mozfullscreenchange", fullscreenHandler, true); michael@0: michael@0: BrowserUI.init(); michael@0: michael@0: window.controllers.appendController(this); michael@0: window.controllers.appendController(BrowserUI); michael@0: michael@0: Services.obs.addObserver(SessionHistoryObserver, "browser:purge-session-history", false); michael@0: michael@0: window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = new nsBrowserAccess(); michael@0: michael@0: Elements.browsers.addEventListener("DOMUpdatePageReport", PopupBlockerObserver.onUpdatePageReport, false); michael@0: michael@0: // Make sure we're online before attempting to load michael@0: Util.forceOnline(); michael@0: michael@0: // If this is an intial window launch the commandline handler passes us the default michael@0: // page as an argument. michael@0: let commandURL = null; michael@0: try { michael@0: let argsObj = window.arguments[0].wrappedJSObject; michael@0: if (argsObj && argsObj.pageloadURL) { michael@0: // Talos tp-cmdline parameter michael@0: commandURL = argsObj.pageloadURL; michael@0: } else if (window.arguments && window.arguments[0]) { michael@0: // BrowserCLH paramerter michael@0: commandURL = window.arguments[0]; michael@0: } michael@0: } catch (ex) { michael@0: Util.dumpLn(ex); michael@0: } michael@0: michael@0: messageManager.addMessageListener("DOMLinkAdded", this); michael@0: messageManager.addMessageListener("Browser:FormSubmit", this); michael@0: messageManager.addMessageListener("Browser:CanUnload:Return", this); michael@0: messageManager.addMessageListener("scroll", this); michael@0: messageManager.addMessageListener("Browser:CertException", this); michael@0: messageManager.addMessageListener("Browser:BlockedSite", this); michael@0: michael@0: Task.spawn(function() { michael@0: // Activation URIs come from protocol activations, secondary tiles, and file activations michael@0: let activationURI = yield this.getShortcutOrURI(Services.metro.activationURI); michael@0: michael@0: let self = this; michael@0: function loadStartupURI() { michael@0: if (activationURI) { michael@0: let webNav = Ci.nsIWebNavigation; michael@0: let flags = webNav.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP | michael@0: webNav.LOAD_FLAGS_FIXUP_SCHEME_TYPOS; michael@0: self.addTab(activationURI, true, null, { flags: flags }); michael@0: } else { michael@0: let uri = commandURL || Browser.getHomePage(); michael@0: self.addTab(uri, true); michael@0: } michael@0: } michael@0: michael@0: // Should we restore the previous session (crash or some other event) michael@0: let ss = Cc["@mozilla.org/browser/sessionstore;1"] michael@0: .getService(Ci.nsISessionStore); michael@0: let shouldRestore = ss.shouldRestore(); michael@0: if (shouldRestore) { michael@0: let bringFront = false; michael@0: // First open any commandline URLs, except the homepage michael@0: if (activationURI && activationURI != kStartURI) { michael@0: this.addTab(activationURI, true, null, { flags: Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP }); michael@0: } else if (commandURL && commandURL != kStartURI) { michael@0: this.addTab(commandURL, true); michael@0: } else { michael@0: bringFront = true; michael@0: // Initial window resizes call functions that assume a tab is in the tab list michael@0: // and restored tabs are added too late. We add a dummy to to satisfy the resize michael@0: // code and then remove the dummy after the session has been restored. michael@0: let dummy = this.addTab("about:blank", true); michael@0: let dummyCleanup = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(dummyCleanup, "sessionstore-windows-restored"); michael@0: if (aData == "fail") michael@0: loadStartupURI(); michael@0: dummy.chromeTab.ignoreUndo = true; michael@0: Browser.closeTab(dummy, { forceClose: true }); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(dummyCleanup, "sessionstore-windows-restored", false); michael@0: } michael@0: ss.restoreLastSession(bringFront); michael@0: } else { michael@0: loadStartupURI(); michael@0: } michael@0: michael@0: // Notify about our input type michael@0: InputSourceHelper.fireUpdate(); michael@0: michael@0: // Broadcast a UIReady message so add-ons know we are finished with startup michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("UIReady", true, false); michael@0: window.dispatchEvent(event); michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: shutdown: function shutdown() { michael@0: APZCObserver.shutdown(); michael@0: BrowserUI.uninit(); michael@0: ClickEventHandler.uninit(); michael@0: ContentAreaObserver.shutdown(); michael@0: Appbar.shutdown(); michael@0: michael@0: messageManager.removeMessageListener("Browser:FormSubmit", this); michael@0: messageManager.removeMessageListener("scroll", this); michael@0: messageManager.removeMessageListener("Browser:CertException", this); michael@0: messageManager.removeMessageListener("Browser:BlockedSite", this); michael@0: michael@0: Services.obs.removeObserver(SessionHistoryObserver, "browser:purge-session-history"); michael@0: michael@0: window.controllers.removeController(this); michael@0: window.controllers.removeController(BrowserUI); michael@0: }, michael@0: michael@0: getHomePage: function getHomePage(aOptions) { michael@0: aOptions = aOptions || { useDefault: false }; michael@0: michael@0: let url = kStartURI; michael@0: try { michael@0: let prefs = aOptions.useDefault ? Services.prefs.getDefaultBranch(null) : Services.prefs; michael@0: url = prefs.getComplexValue("browser.startup.homepage", Ci.nsIPrefLocalizedString).data; michael@0: } michael@0: catch(e) { } michael@0: michael@0: return url; michael@0: }, michael@0: michael@0: get browsers() { michael@0: return this._tabs.map(function(tab) { return tab.browser; }); michael@0: }, michael@0: michael@0: /** michael@0: * Load a URI in the current tab, or a new tab if necessary. michael@0: * @param aURI String michael@0: * @param aParams Object with optional properties that will be passed to loadURIWithFlags: michael@0: * flags, referrerURI, charset, postData. michael@0: */ michael@0: loadURI: function loadURI(aURI, aParams) { michael@0: let browser = this.selectedBrowser; michael@0: michael@0: // We need to keep about: pages opening in new "local" tabs. We also want to spawn michael@0: // new "remote" tabs if opening web pages from a "local" about: page. michael@0: dump("loadURI=" + aURI + "\ncurrentURI=" + browser.currentURI.spec + "\n"); michael@0: michael@0: let params = aParams || {}; michael@0: try { michael@0: let flags = params.flags || Ci.nsIWebNavigation.LOAD_FLAGS_NONE; michael@0: let postData = ("postData" in params && params.postData) ? params.postData.value : null; michael@0: let referrerURI = "referrerURI" in params ? params.referrerURI : null; michael@0: let charset = "charset" in params ? params.charset : null; michael@0: dump("loading tab: " + aURI + "\n"); michael@0: browser.loadURIWithFlags(aURI, flags, referrerURI, charset, postData); michael@0: } catch(e) { michael@0: dump("Error: " + e + "\n"); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Determine if the given URL is a shortcut/keyword and, if so, expand it michael@0: * @param aURL String michael@0: * @param aPostDataRef Out param contains any required post data for a search michael@0: * @return {Promise} michael@0: * @result the expanded shortcut, or the original URL if not a shortcut michael@0: */ michael@0: getShortcutOrURI: function getShortcutOrURI(aURL, aPostDataRef) { michael@0: return Task.spawn(function() { michael@0: if (!aURL) michael@0: throw new Task.Result(aURL); michael@0: michael@0: let shortcutURL = null; michael@0: let keyword = aURL; michael@0: let param = ""; michael@0: michael@0: let offset = aURL.indexOf(" "); michael@0: if (offset > 0) { michael@0: keyword = aURL.substr(0, offset); michael@0: param = aURL.substr(offset + 1); michael@0: } michael@0: michael@0: if (!aPostDataRef) michael@0: aPostDataRef = {}; michael@0: michael@0: let engine = Services.search.getEngineByAlias(keyword); michael@0: if (engine) { michael@0: let submission = engine.getSubmission(param); michael@0: aPostDataRef.value = submission.postData; michael@0: throw new Task.Result(submission.uri.spec); michael@0: } michael@0: michael@0: try { michael@0: [shortcutURL, aPostDataRef.value] = PlacesUtils.getURLAndPostDataForKeyword(keyword); michael@0: } catch (e) {} michael@0: michael@0: if (!shortcutURL) michael@0: throw new Task.Result(aURL); michael@0: michael@0: let postData = ""; michael@0: if (aPostDataRef.value) michael@0: postData = unescape(aPostDataRef.value); michael@0: michael@0: if (/%s/i.test(shortcutURL) || /%s/i.test(postData)) { michael@0: let charset = ""; michael@0: const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/; michael@0: let matches = shortcutURL.match(re); michael@0: if (matches) michael@0: [, shortcutURL, charset] = matches; michael@0: else { michael@0: // Try to get the saved character-set. michael@0: try { michael@0: // makeURI throws if URI is invalid. michael@0: // Will return an empty string if character-set is not found. michael@0: charset = yield PlacesUtils.getCharsetForURI(Util.makeURI(shortcutURL)); michael@0: } catch (e) { dump("--- error " + e + "\n"); } michael@0: } michael@0: michael@0: let encodedParam = ""; michael@0: if (charset) michael@0: encodedParam = escape(convertFromUnicode(charset, param)); michael@0: else // Default charset is UTF-8 michael@0: encodedParam = encodeURIComponent(param); michael@0: michael@0: shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param); michael@0: michael@0: if (/%s/i.test(postData)) // POST keyword michael@0: aPostDataRef.value = getPostDataStream(postData, param, encodedParam, "application/x-www-form-urlencoded"); michael@0: } else if (param) { michael@0: // This keyword doesn't take a parameter, but one was provided. Just return michael@0: // the original URL. michael@0: aPostDataRef.value = null; michael@0: michael@0: throw new Task.Result(aURL); michael@0: } michael@0: michael@0: throw new Task.Result(shortcutURL); michael@0: }); michael@0: }, michael@0: michael@0: /** michael@0: * Return the currently active object michael@0: */ michael@0: get selectedBrowser() { michael@0: return (this._selectedTab && this._selectedTab.browser); michael@0: }, michael@0: michael@0: get tabs() { michael@0: return this._tabs; michael@0: }, michael@0: michael@0: getTabModalPromptBox: function(aBrowser) { michael@0: let browser = (aBrowser || getBrowser()); michael@0: let stack = browser.parentNode; michael@0: let self = this; michael@0: michael@0: let promptBox = { michael@0: appendPrompt : function(args, onCloseCallback) { michael@0: let newPrompt = document.createElementNS(XUL_NS, "tabmodalprompt"); michael@0: newPrompt.setAttribute("promptType", args.promptType); michael@0: stack.appendChild(newPrompt); michael@0: browser.setAttribute("tabmodalPromptShowing", true); michael@0: newPrompt.clientTop; // style flush to assure binding is attached michael@0: michael@0: let tab = self.getTabForBrowser(browser); michael@0: tab = tab.chromeTab; michael@0: michael@0: newPrompt.metroInit(args, tab, onCloseCallback); michael@0: return newPrompt; michael@0: }, michael@0: michael@0: removePrompt : function(aPrompt) { michael@0: stack.removeChild(aPrompt); michael@0: michael@0: let prompts = this.listPrompts(); michael@0: if (prompts.length) { michael@0: let prompt = prompts[prompts.length - 1]; michael@0: prompt.Dialog.setDefaultFocus(); michael@0: } else { michael@0: browser.removeAttribute("tabmodalPromptShowing"); michael@0: browser.focus(); michael@0: } michael@0: }, michael@0: michael@0: listPrompts : function(aPrompt) { michael@0: let els = stack.getElementsByTagNameNS(XUL_NS, "tabmodalprompt"); michael@0: // NodeList --> real JS array michael@0: let prompts = Array.slice(els); michael@0: return prompts; michael@0: }, michael@0: }; michael@0: michael@0: return promptBox; michael@0: }, michael@0: michael@0: getBrowserForWindowId: function getBrowserForWindowId(aWindowId) { michael@0: for (let i = 0; i < this.browsers.length; i++) { michael@0: if (this.browsers[i].contentWindowId == aWindowId) michael@0: return this.browsers[i]; michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: getBrowserForWindow: function(aWindow) { michael@0: let windowID = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID; michael@0: return this.getBrowserForWindowId(windowID); michael@0: }, michael@0: michael@0: getTabForBrowser: function getTabForBrowser(aBrowser) { michael@0: let tabs = this._tabs; michael@0: for (let i = 0; i < tabs.length; i++) { michael@0: if (tabs[i].browser == aBrowser) michael@0: return tabs[i]; michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: getTabAtIndex: function getTabAtIndex(index) { michael@0: if (index >= this._tabs.length || index < 0) michael@0: return null; michael@0: return this._tabs[index]; michael@0: }, michael@0: michael@0: getTabFromChrome: function getTabFromChrome(chromeTab) { michael@0: for (var t = 0; t < this._tabs.length; t++) { michael@0: if (this._tabs[t].chromeTab == chromeTab) michael@0: return this._tabs[t]; michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: createTabId: function createTabId() { michael@0: return this._tabId++; michael@0: }, michael@0: michael@0: /** michael@0: * Create a new tab and add it to the tab list. michael@0: * michael@0: * If you are opening a new foreground tab in response to a user action, use michael@0: * BrowserUI.addAndShowTab which will also show the tab strip. michael@0: * michael@0: * @param aURI String specifying the URL to load. michael@0: * @param aBringFront Boolean (optional) Open the new tab in the foreground? michael@0: * @param aOwner Tab object (optional) The "parent" of the new tab. michael@0: * This is the tab responsible for opening the new tab. When the new tab michael@0: * is closed, we will return to a parent or "sibling" tab if possible. michael@0: * @param aParams Object (optional) with optional properties: michael@0: * index: Number specifying where in the tab list to insert the new tab. michael@0: * private: If true, the new tab should be have Private Browsing active. michael@0: * flags, postData, charset, referrerURI: See loadURIWithFlags. michael@0: */ michael@0: addTab: function browser_addTab(aURI, aBringFront, aOwner, aParams) { michael@0: let params = aParams || {}; michael@0: michael@0: if (aOwner && !('index' in params)) { michael@0: // Position the new tab to the right of its owner... michael@0: params.index = this._tabs.indexOf(aOwner) + 1; michael@0: // ...and to the right of any siblings. michael@0: while (this._tabs[params.index] && this._tabs[params.index].owner == aOwner) { michael@0: params.index++; michael@0: } michael@0: } michael@0: michael@0: let newTab = new Tab(aURI, params, aOwner); michael@0: michael@0: if (params.index >= 0) { michael@0: this._tabs.splice(params.index, 0, newTab); michael@0: } else { michael@0: this._tabs.push(newTab); michael@0: } michael@0: michael@0: if (aBringFront) michael@0: this.selectedTab = newTab; michael@0: michael@0: this._announceNewTab(newTab); michael@0: return newTab; michael@0: }, michael@0: michael@0: closeTab: function closeTab(aTab, aOptions) { michael@0: let tab = aTab instanceof XULElement ? this.getTabFromChrome(aTab) : aTab; michael@0: if (!tab) { michael@0: return; michael@0: } michael@0: michael@0: if (aOptions && "forceClose" in aOptions && aOptions.forceClose) { michael@0: this._doCloseTab(tab); michael@0: return; michael@0: } michael@0: michael@0: tab.browser.messageManager.sendAsyncMessage("Browser:CanUnload", {}); michael@0: }, michael@0: michael@0: savePage: function() { michael@0: ContentAreaUtils.saveDocument(this.selectedBrowser.contentWindow.document); michael@0: }, michael@0: michael@0: /* michael@0: * helper for addTab related methods. Fires events related to michael@0: * new tab creation. michael@0: */ michael@0: _announceNewTab: function (aTab) { michael@0: let event = document.createEvent("UIEvents"); michael@0: event.initUIEvent("TabOpen", true, false, window, 0); michael@0: aTab.chromeTab.dispatchEvent(event); michael@0: aTab.browser.messageManager.sendAsyncMessage("Browser:TabOpen"); michael@0: }, michael@0: michael@0: _doCloseTab: function _doCloseTab(aTab) { michael@0: if (this._tabs.length === 1) { michael@0: Browser.addTab(this.getHomePage()); michael@0: } michael@0: michael@0: let nextTab = this.getNextTab(aTab); michael@0: michael@0: // Tabs owned by the closed tab are now orphaned. michael@0: this._tabs.forEach(function(item, index, array) { michael@0: if (item.owner == aTab) michael@0: item.owner = null; michael@0: }); michael@0: michael@0: // tray tab michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("TabClose", true, false); michael@0: aTab.chromeTab.dispatchEvent(event); michael@0: michael@0: // tab window michael@0: event = document.createEvent("Events"); michael@0: event.initEvent("TabClose", true, false); michael@0: aTab.browser.contentWindow.dispatchEvent(event); michael@0: michael@0: aTab.browser.messageManager.sendAsyncMessage("Browser:TabClose"); michael@0: michael@0: let container = aTab.chromeTab.parentNode; michael@0: aTab.destroy(); michael@0: this._tabs.splice(this._tabs.indexOf(aTab), 1); michael@0: michael@0: this.selectedTab = nextTab; michael@0: michael@0: event = document.createEvent("Events"); michael@0: event.initEvent("TabRemove", true, false); michael@0: container.dispatchEvent(event); michael@0: }, michael@0: michael@0: getNextTab: function getNextTab(aTab) { michael@0: let tabIndex = this._tabs.indexOf(aTab); michael@0: if (tabIndex == -1) michael@0: return null; michael@0: michael@0: if (this._selectedTab == aTab || this._selectedTab.chromeTab.hasAttribute("closing")) { michael@0: let nextTabIndex = tabIndex + 1; michael@0: let nextTab = null; michael@0: michael@0: while (nextTabIndex < this._tabs.length && (!nextTab || nextTab.chromeTab.hasAttribute("closing"))) { michael@0: nextTab = this.getTabAtIndex(nextTabIndex); michael@0: nextTabIndex++; michael@0: } michael@0: michael@0: nextTabIndex = tabIndex - 1; michael@0: while (nextTabIndex >= 0 && (!nextTab || nextTab.chromeTab.hasAttribute("closing"))) { michael@0: nextTab = this.getTabAtIndex(nextTabIndex); michael@0: nextTabIndex--; michael@0: } michael@0: michael@0: if (!nextTab || nextTab.chromeTab.hasAttribute("closing")) michael@0: return null; michael@0: michael@0: // If the next tab is not a sibling, switch back to the parent. michael@0: if (aTab.owner && nextTab.owner != aTab.owner) michael@0: nextTab = aTab.owner; michael@0: michael@0: if (!nextTab) michael@0: return null; michael@0: michael@0: return nextTab; michael@0: } michael@0: michael@0: return this._selectedTab; michael@0: }, michael@0: michael@0: get selectedTab() { michael@0: return this._selectedTab; michael@0: }, michael@0: michael@0: set selectedTab(tab) { michael@0: if (tab instanceof XULElement) michael@0: tab = this.getTabFromChrome(tab); michael@0: michael@0: if (!tab) michael@0: return; michael@0: michael@0: if (this._selectedTab == tab) { michael@0: // Deck does not update its selectedIndex when children michael@0: // are removed. See bug 602708 michael@0: Elements.browsers.selectedPanel = tab.notification; michael@0: return; michael@0: } michael@0: michael@0: let isFirstTab = this._selectedTab == null; michael@0: let lastTab = this._selectedTab; michael@0: let browser = tab.browser; michael@0: michael@0: this._selectedTab = tab; michael@0: michael@0: if (lastTab) michael@0: lastTab.active = false; michael@0: michael@0: if (tab) michael@0: tab.active = true; michael@0: michael@0: BrowserUI.update(); michael@0: michael@0: if (isFirstTab) { michael@0: BrowserUI._titleChanged(browser); michael@0: } else { michael@0: // Update all of our UI to reflect the new tab's location michael@0: BrowserUI.updateURI(); michael@0: michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("TabSelect", true, false); michael@0: event.lastTab = lastTab; michael@0: tab.chromeTab.dispatchEvent(event); michael@0: } michael@0: michael@0: tab.lastSelected = Date.now(); michael@0: }, michael@0: michael@0: supportsCommand: function(cmd) { michael@0: return false; michael@0: }, michael@0: michael@0: isCommandEnabled: function(cmd) { michael@0: return false; michael@0: }, michael@0: michael@0: doCommand: function(cmd) { michael@0: }, michael@0: michael@0: getNotificationBox: function getNotificationBox(aBrowser) { michael@0: let browser = aBrowser || this.selectedBrowser; michael@0: return browser.parentNode.parentNode; michael@0: }, michael@0: michael@0: /** michael@0: * Handle cert exception message from content. michael@0: */ michael@0: _handleCertException: function _handleCertException(aMessage) { michael@0: let json = aMessage.json; michael@0: if (json.action == "leave") { michael@0: // Get the start page from the *default* pref branch, not the user's michael@0: let url = Browser.getHomePage({ useDefault: true }); michael@0: this.loadURI(url); michael@0: } else { michael@0: // Handle setting an cert exception and reloading the page michael@0: try { michael@0: // Add a new SSL exception for this URL michael@0: let uri = Services.io.newURI(json.url, null, null); michael@0: let sslExceptions = new SSLExceptions(); michael@0: michael@0: if (json.action == "permanent") michael@0: sslExceptions.addPermanentException(uri, window); michael@0: else michael@0: sslExceptions.addTemporaryException(uri, window); michael@0: } catch (e) { michael@0: dump("EXCEPTION handle content command: " + e + "\n" ); michael@0: } michael@0: michael@0: // Automatically reload after the exception was added michael@0: aMessage.target.reload(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Handle blocked site message from content. michael@0: */ michael@0: _handleBlockedSite: function _handleBlockedSite(aMessage) { michael@0: let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter); michael@0: let json = aMessage.json; michael@0: switch (json.action) { michael@0: case "leave": { michael@0: // Get the start page from the *default* pref branch, not the user's michael@0: let url = Browser.getHomePage({ useDefault: true }); michael@0: this.loadURI(url); michael@0: break; michael@0: } michael@0: case "report-malware": { michael@0: // Get the stop badware "why is this blocked" report url, append the current url, and go there. michael@0: try { michael@0: let reportURL = formatter.formatURLPref("browser.safebrowsing.malware.reportURL"); michael@0: reportURL += json.url; michael@0: this.loadURI(reportURL); michael@0: } catch (e) { michael@0: Cu.reportError("Couldn't get malware report URL: " + e); michael@0: } michael@0: break; michael@0: } michael@0: case "report-phishing": { michael@0: // It's a phishing site, just link to the generic information page michael@0: let url = Services.urlFormatter.formatURLPref("app.support.baseURL"); michael@0: this.loadURI(url + "phishing-malware"); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: pinSite: function browser_pinSite() { michael@0: // Get a path to our app tile michael@0: var file = Components.classes["@mozilla.org/file/directory_service;1"]. michael@0: getService(Components.interfaces.nsIProperties). michael@0: get("CurProcD", Components.interfaces.nsIFile); michael@0: // Get rid of the current working directory's metro subidr michael@0: file = file.parent; michael@0: file.append("tileresources"); michael@0: file.append("VisualElements_logo.png"); michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"]. michael@0: getService(Components.interfaces.nsIIOService); michael@0: var uriSpec = ios.newFileURI(file).spec; michael@0: Services.metro.pinTileAsync(this._currentPageTileID, michael@0: Browser.selectedBrowser.contentTitle, // short name michael@0: Browser.selectedBrowser.contentTitle, // display name michael@0: "-url " + Browser.selectedBrowser.currentURI.spec, michael@0: uriSpec, uriSpec); michael@0: }, michael@0: michael@0: get _currentPageTileID() { michael@0: // We use a unique ID per URL, so just use an MD5 hash of the URL as the ID. michael@0: let hasher = Cc["@mozilla.org/security/hash;1"]. michael@0: createInstance(Ci.nsICryptoHash); michael@0: hasher.init(Ci.nsICryptoHash.MD5); michael@0: let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: stringStream.data = Browser.selectedBrowser.currentURI.spec; michael@0: hasher.updateFromStream(stringStream, -1); michael@0: let hashASCII = hasher.finish(true); michael@0: // Replace '/' with a valid filesystem character michael@0: return ("FFTileID_" + hashASCII).replace('/', '_', 'g'); michael@0: }, michael@0: michael@0: unpinSite: function browser_unpinSite() { michael@0: if (!Services.metro.immersive) michael@0: return; michael@0: michael@0: Services.metro.unpinTileAsync(this._currentPageTileID); michael@0: }, michael@0: michael@0: isSitePinned: function browser_isSitePinned() { michael@0: if (!Services.metro.immersive) michael@0: return false; michael@0: michael@0: return Services.metro.isTilePinned(this._currentPageTileID); michael@0: }, michael@0: michael@0: starSite: function browser_starSite(callback) { michael@0: let uri = this.selectedBrowser.currentURI; michael@0: let title = this.selectedBrowser.contentTitle; michael@0: michael@0: Bookmarks.addForURI(uri, title, callback); michael@0: }, michael@0: michael@0: unstarSite: function browser_unstarSite(callback) { michael@0: let uri = this.selectedBrowser.currentURI; michael@0: Bookmarks.removeForURI(uri, callback); michael@0: }, michael@0: michael@0: isSiteStarredAsync: function browser_isSiteStarredAsync(callback) { michael@0: let uri = this.selectedBrowser.currentURI; michael@0: Bookmarks.isURIBookmarked(uri, callback); michael@0: }, michael@0: michael@0: /** michael@0: * Convenience function for getting the scrollbox position off of a michael@0: * scrollBoxObject interface. Returns the actual values instead of the michael@0: * wrapping objects. michael@0: * michael@0: * @param scroller a scrollBoxObject on which to call scroller.getPosition() michael@0: */ michael@0: getScrollboxPosition: function getScrollboxPosition(scroller) { michael@0: let x = {}; michael@0: let y = {}; michael@0: scroller.getPosition(x, y); michael@0: return new Point(x.value, y.value); michael@0: }, michael@0: michael@0: forceChromeReflow: function forceChromeReflow() { michael@0: let dummy = getComputedStyle(document.documentElement, "").width; michael@0: }, michael@0: michael@0: receiveMessage: function receiveMessage(aMessage) { michael@0: let json = aMessage.json; michael@0: let browser = aMessage.target; michael@0: michael@0: switch (aMessage.name) { michael@0: case "DOMLinkAdded": { michael@0: // checks for an icon to use for a web app michael@0: // apple-touch-icon size is 57px and default size is 16px michael@0: let rel = json.rel.toLowerCase().split(" "); michael@0: if (rel.indexOf("icon") != -1) { michael@0: // We use the sizes attribute if available michael@0: // see http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon michael@0: let size = 16; michael@0: if (json.sizes) { michael@0: let sizes = json.sizes.toLowerCase().split(" "); michael@0: sizes.forEach(function(item) { michael@0: if (item != "any") { michael@0: let [w, h] = item.split("x"); michael@0: size = Math.max(Math.min(w, h), size); michael@0: } michael@0: }); michael@0: } michael@0: if (size > browser.appIcon.size) { michael@0: browser.appIcon.href = json.href; michael@0: browser.appIcon.size = size; michael@0: } michael@0: } michael@0: else if ((rel.indexOf("apple-touch-icon") != -1) && (browser.appIcon.size < 57)) { michael@0: // XXX should we support apple-touch-icon-precomposed ? michael@0: // see http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/configuringwebapplications/configuringwebapplications.html michael@0: browser.appIcon.href = json.href; michael@0: browser.appIcon.size = 57; michael@0: } michael@0: break; michael@0: } michael@0: case "Browser:FormSubmit": michael@0: browser.lastLocation = null; michael@0: break; michael@0: michael@0: case "Browser:CanUnload:Return": { michael@0: if (json.permit) { michael@0: let tab = this.getTabForBrowser(browser); michael@0: BrowserUI.animateClosingTab(tab); michael@0: } michael@0: break; michael@0: } michael@0: case "Browser:CertException": michael@0: this._handleCertException(aMessage); michael@0: break; michael@0: case "Browser:BlockedSite": michael@0: this._handleBlockedSite(aMessage); michael@0: break; michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: Browser.MainDragger = function MainDragger() { michael@0: this._horizontalScrollbar = document.getElementById("horizontal-scroller"); michael@0: this._verticalScrollbar = document.getElementById("vertical-scroller"); michael@0: this._scrollScales = { x: 0, y: 0 }; michael@0: michael@0: Elements.browsers.addEventListener("PanBegin", this, false); michael@0: Elements.browsers.addEventListener("PanFinished", this, false); michael@0: }; michael@0: michael@0: Browser.MainDragger.prototype = { michael@0: isDraggable: function isDraggable(target, scroller) { michael@0: return { x: true, y: true }; michael@0: }, michael@0: michael@0: dragStart: function dragStart(clientX, clientY, target, scroller) { michael@0: let browser = getBrowser(); michael@0: let bcr = browser.getBoundingClientRect(); michael@0: this._contentView = browser.getViewAt(clientX - bcr.left, clientY - bcr.top); michael@0: }, michael@0: michael@0: dragStop: function dragStop(dx, dy, scroller) { michael@0: if (this._contentView && this._contentView._updateCacheViewport) michael@0: this._contentView._updateCacheViewport(); michael@0: this._contentView = null; michael@0: }, michael@0: michael@0: dragMove: function dragMove(dx, dy, scroller, aIsKinetic) { michael@0: let doffset = new Point(dx, dy); michael@0: michael@0: this._panContent(doffset); michael@0: michael@0: if (aIsKinetic && doffset.x != 0) michael@0: return false; michael@0: michael@0: this._updateScrollbars(); michael@0: michael@0: return !doffset.equals(dx, dy); michael@0: }, michael@0: michael@0: handleEvent: function handleEvent(aEvent) { michael@0: let browser = getBrowser(); michael@0: switch (aEvent.type) { michael@0: case "PanBegin": { michael@0: let width = ContentAreaObserver.width, height = ContentAreaObserver.height; michael@0: let contentWidth = browser.contentDocumentWidth * browser.scale; michael@0: let contentHeight = browser.contentDocumentHeight * browser.scale; michael@0: michael@0: // Allow a small margin on both sides to prevent adding scrollbars michael@0: // on small viewport approximation michael@0: const ALLOWED_MARGIN = 5; michael@0: const SCROLL_CORNER_SIZE = 8; michael@0: this._scrollScales = { michael@0: x: (width + ALLOWED_MARGIN) < contentWidth ? (width - SCROLL_CORNER_SIZE) / contentWidth : 0, michael@0: y: (height + ALLOWED_MARGIN) < contentHeight ? (height - SCROLL_CORNER_SIZE) / contentHeight : 0 michael@0: } michael@0: michael@0: this._showScrollbars(); michael@0: break; michael@0: } michael@0: case "PanFinished": michael@0: this._hideScrollbars(); michael@0: michael@0: // Update the scroll position of the content michael@0: browser._updateCSSViewport(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: _panContent: function md_panContent(aOffset) { michael@0: if (this._contentView && !this._contentView.isRoot()) { michael@0: this._panContentView(this._contentView, aOffset); michael@0: // XXX we may need to have "escape borders" for iframe panning michael@0: // XXX does not deal with scrollables within scrollables michael@0: } michael@0: // Do content panning michael@0: this._panContentView(getBrowser().getRootView(), aOffset); michael@0: }, michael@0: michael@0: /** Pan scroller by the given amount. Updates doffset with leftovers. */ michael@0: _panContentView: function _panContentView(contentView, doffset) { michael@0: let pos0 = contentView.getPosition(); michael@0: contentView.scrollBy(doffset.x, doffset.y); michael@0: let pos1 = contentView.getPosition(); michael@0: doffset.subtract(pos1.x - pos0.x, pos1.y - pos0.y); michael@0: }, michael@0: michael@0: _updateScrollbars: function _updateScrollbars() { michael@0: let scaleX = this._scrollScales.x, scaleY = this._scrollScales.y; michael@0: let contentScroll = Browser.getScrollboxPosition(Browser.contentScrollboxScroller); michael@0: michael@0: if (scaleX) michael@0: this._horizontalScrollbar.style.MozTransform = michael@0: "translateX(" + Math.round(contentScroll.x * scaleX) + "px)"; michael@0: michael@0: if (scaleY) { michael@0: let y = Math.round(contentScroll.y * scaleY); michael@0: let x = 0; michael@0: michael@0: this._verticalScrollbar.style.MozTransform = michael@0: "translate(" + x + "px," + y + "px)"; michael@0: } michael@0: }, michael@0: michael@0: _showScrollbars: function _showScrollbars() { michael@0: this._updateScrollbars(); michael@0: let scaleX = this._scrollScales.x, scaleY = this._scrollScales.y; michael@0: if (scaleX) { michael@0: this._horizontalScrollbar.width = ContentAreaObserver.width * scaleX; michael@0: this._horizontalScrollbar.setAttribute("panning", "true"); michael@0: } michael@0: michael@0: if (scaleY) { michael@0: this._verticalScrollbar.height = ContentAreaObserver.height * scaleY; michael@0: this._verticalScrollbar.setAttribute("panning", "true"); michael@0: } michael@0: }, michael@0: michael@0: _hideScrollbars: function _hideScrollbars() { michael@0: this._scrollScales.x = 0; michael@0: this._scrollScales.y = 0; michael@0: this._horizontalScrollbar.removeAttribute("panning"); michael@0: this._verticalScrollbar.removeAttribute("panning"); michael@0: this._horizontalScrollbar.removeAttribute("width"); michael@0: this._verticalScrollbar.removeAttribute("height"); michael@0: this._horizontalScrollbar.style.MozTransform = ""; michael@0: this._verticalScrollbar.style.MozTransform = ""; michael@0: } michael@0: }; michael@0: michael@0: michael@0: function nsBrowserAccess() { } michael@0: michael@0: nsBrowserAccess.prototype = { michael@0: QueryInterface: function(aIID) { michael@0: if (aIID.equals(Ci.nsIBrowserDOMWindow) || aIID.equals(Ci.nsISupports)) michael@0: return this; michael@0: throw Cr.NS_NOINTERFACE; michael@0: }, michael@0: michael@0: _getOpenAction: function _getOpenAction(aURI, aOpener, aWhere, aContext) { michael@0: let where = aWhere; michael@0: /* michael@0: * aWhere: michael@0: * OPEN_DEFAULTWINDOW: default action michael@0: * OPEN_CURRENTWINDOW: current window/tab michael@0: * OPEN_NEWWINDOW: not allowed, converted to newtab below michael@0: * OPEN_NEWTAB: open a new tab michael@0: * OPEN_SWITCHTAB: open in an existing tab if it matches, otherwise open michael@0: * a new tab. afaict we always open these in the current tab. michael@0: */ michael@0: if (where == Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW) { michael@0: // query standard browser prefs indicating what to do for default action michael@0: switch (aContext) { michael@0: // indicates this is an open request from a 3rd party app. michael@0: case Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL : michael@0: where = Services.prefs.getIntPref("browser.link.open_external"); michael@0: break; michael@0: // internal request michael@0: default : michael@0: where = Services.prefs.getIntPref("browser.link.open_newwindow"); michael@0: } michael@0: } michael@0: if (where == Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW) { michael@0: Util.dumpLn("Invalid request - we can't open links in new windows."); michael@0: where = Ci.nsIBrowserDOMWindow.OPEN_NEWTAB; michael@0: } michael@0: return where; michael@0: }, michael@0: michael@0: _getBrowser: function _getBrowser(aURI, aOpener, aWhere, aContext) { michael@0: let isExternal = (aContext == Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL); michael@0: // We don't allow externals apps opening chrome docs michael@0: if (isExternal && aURI && aURI.schemeIs("chrome")) michael@0: return null; michael@0: michael@0: let location; michael@0: let browser; michael@0: let loadflags = isExternal ? michael@0: Ci.nsIWebNavigation.LOAD_FLAGS_FROM_EXTERNAL : michael@0: Ci.nsIWebNavigation.LOAD_FLAGS_NONE; michael@0: let openAction = this._getOpenAction(aURI, aOpener, aWhere, aContext); michael@0: michael@0: if (openAction == Ci.nsIBrowserDOMWindow.OPEN_NEWTAB) { michael@0: let owner = isExternal ? null : Browser.selectedTab; michael@0: let tab = BrowserUI.openLinkInNewTab("about:blank", true, owner); michael@0: browser = tab.browser; michael@0: } else { michael@0: browser = Browser.selectedBrowser; michael@0: } michael@0: michael@0: try { michael@0: let referrer; michael@0: if (aURI && browser) { michael@0: if (aOpener) { michael@0: location = aOpener.location; michael@0: referrer = Services.io.newURI(location, null, null); michael@0: } michael@0: browser.loadURIWithFlags(aURI.spec, loadflags, referrer, null, null); michael@0: } michael@0: browser.focus(); michael@0: } catch(e) { } michael@0: michael@0: return browser; michael@0: }, michael@0: michael@0: openURI: function browser_openURI(aURI, aOpener, aWhere, aContext) { michael@0: let browser = this._getBrowser(aURI, aOpener, aWhere, aContext); michael@0: return browser ? browser.contentWindow : null; michael@0: }, michael@0: michael@0: openURIInFrame: function browser_openURIInFrame(aURI, aOpener, aWhere, aContext) { michael@0: let browser = this._getBrowser(aURI, aOpener, aWhere, aContext); michael@0: return browser ? browser.QueryInterface(Ci.nsIFrameLoaderOwner) : null; michael@0: }, michael@0: michael@0: isTabContentWindow: function(aWindow) { michael@0: return Browser.browsers.some(function (browser) browser.contentWindow == aWindow); michael@0: }, michael@0: michael@0: get contentWindow() { michael@0: return Browser.selectedBrowser.contentWindow; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Handler for blocked popups, triggered by DOMUpdatePageReport events in browser.xml michael@0: */ michael@0: var PopupBlockerObserver = { michael@0: init: function init() { michael@0: Elements.browsers.addEventListener("mousedown", this, true); michael@0: }, michael@0: michael@0: handleEvent: function handleEvent(aEvent) { michael@0: switch (aEvent.type) { michael@0: case "mousedown": michael@0: let box = Browser.getNotificationBox(); michael@0: let notification = box.getNotificationWithValue("popup-blocked"); michael@0: if (notification && !notification.contains(aEvent.target)) michael@0: box.removeNotification(notification); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: onUpdatePageReport: function onUpdatePageReport(aEvent) { michael@0: var cBrowser = Browser.selectedBrowser; michael@0: if (aEvent.originalTarget != cBrowser) michael@0: return; michael@0: michael@0: if (!cBrowser.pageReport) michael@0: return; michael@0: michael@0: let result = Services.perms.testExactPermission(Browser.selectedBrowser.currentURI, "popup"); michael@0: if (result == Ci.nsIPermissionManager.DENY_ACTION) michael@0: return; michael@0: michael@0: // Only show the notification again if we've not already shown it. Since michael@0: // notifications are per-browser, we don't need to worry about re-adding michael@0: // it. michael@0: if (!cBrowser.pageReport.reported) { michael@0: if (Services.prefs.getBoolPref("privacy.popups.showBrowserMessage")) { michael@0: var brandShortName = Strings.brand.GetStringFromName("brandShortName"); michael@0: var popupCount = cBrowser.pageReport.length; michael@0: michael@0: let strings = Strings.browser; michael@0: let message = PluralForm.get(popupCount, strings.GetStringFromName("popupWarning.message")) michael@0: .replace("#1", brandShortName) michael@0: .replace("#2", popupCount); michael@0: michael@0: var notificationBox = Browser.getNotificationBox(); michael@0: var notification = notificationBox.getNotificationWithValue("popup-blocked"); michael@0: if (notification) { michael@0: notification.label = message; michael@0: } michael@0: else { michael@0: var buttons = [ michael@0: { michael@0: isDefault: false, michael@0: label: strings.GetStringFromName("popupButtonAllowOnce2"), michael@0: accessKey: "", michael@0: callback: function() { PopupBlockerObserver.showPopupsForSite(); } michael@0: }, michael@0: { michael@0: label: strings.GetStringFromName("popupButtonAlwaysAllow3"), michael@0: accessKey: "", michael@0: callback: function() { PopupBlockerObserver.allowPopupsForSite(true); } michael@0: }, michael@0: { michael@0: label: strings.GetStringFromName("popupButtonNeverWarn3"), michael@0: accessKey: "", michael@0: callback: function() { PopupBlockerObserver.allowPopupsForSite(false); } michael@0: } michael@0: ]; michael@0: michael@0: const priority = notificationBox.PRIORITY_WARNING_MEDIUM; michael@0: notificationBox.appendNotification(message, "popup-blocked", michael@0: "chrome://browser/skin/images/infobar-popup.png", michael@0: priority, buttons); michael@0: } michael@0: } michael@0: // Record the fact that we've reported this blocked popup, so we don't michael@0: // show it again. michael@0: cBrowser.pageReport.reported = true; michael@0: } michael@0: }, michael@0: michael@0: allowPopupsForSite: function allowPopupsForSite(aAllow) { michael@0: var currentURI = Browser.selectedBrowser.currentURI; michael@0: Services.perms.add(currentURI, "popup", aAllow michael@0: ? Ci.nsIPermissionManager.ALLOW_ACTION michael@0: : Ci.nsIPermissionManager.DENY_ACTION); michael@0: michael@0: Browser.getNotificationBox().removeCurrentNotification(); michael@0: }, michael@0: michael@0: showPopupsForSite: function showPopupsForSite() { michael@0: let uri = Browser.selectedBrowser.currentURI; michael@0: let pageReport = Browser.selectedBrowser.pageReport; michael@0: if (pageReport) { michael@0: for (let i = 0; i < pageReport.length; ++i) { michael@0: var popupURIspec = pageReport[i].popupWindowURI.spec; michael@0: michael@0: // Sometimes the popup URI that we get back from the pageReport michael@0: // isn't useful (for instance, netscape.com's popup URI ends up michael@0: // being "http://www.netscape.com", which isn't really the URI of michael@0: // the popup they're trying to show). This isn't going to be michael@0: // useful to the user, so we won't create a menu item for it. michael@0: if (popupURIspec == "" || !Util.isURLMemorable(popupURIspec) || popupURIspec == uri.spec) michael@0: continue; michael@0: michael@0: let popupFeatures = pageReport[i].popupWindowFeatures; michael@0: let popupName = pageReport[i].popupWindowName; michael@0: michael@0: Browser.addTab(popupURIspec, false, Browser.selectedTab); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: var SessionHistoryObserver = { michael@0: observe: function sho_observe(aSubject, aTopic, aData) { michael@0: if (aTopic != "browser:purge-session-history") michael@0: return; michael@0: michael@0: let newTab = Browser.addTab("about:start", true); michael@0: let tab = Browser._tabs[0]; michael@0: while(tab != newTab) { michael@0: Browser.closeTab(tab, { forceClose: true } ); michael@0: tab = Browser._tabs[0]; michael@0: } michael@0: michael@0: PlacesUtils.history.removeAllPages(); michael@0: michael@0: // Clear undo history of the URL bar michael@0: BrowserUI._edit.editor.transactionManager.clear(); michael@0: } michael@0: }; michael@0: michael@0: function getNotificationBox(aBrowser) { michael@0: return Browser.getNotificationBox(aBrowser); michael@0: } michael@0: michael@0: function showDownloadManager(aWindowContext, aID, aReason) { michael@0: // TODO: Bug 883962: Toggle the downloads infobar as our current "download manager". michael@0: } michael@0: michael@0: function Tab(aURI, aParams, aOwner) { michael@0: this._id = null; michael@0: this._browser = null; michael@0: this._notification = null; michael@0: this._loading = false; michael@0: this._progressActive = false; michael@0: this._progressCount = 0; michael@0: this._chromeTab = null; michael@0: this._eventDeferred = null; michael@0: this._updateThumbnailTimeout = null; michael@0: michael@0: this._private = false; michael@0: if ("private" in aParams) { michael@0: this._private = aParams.private; michael@0: } else if (aOwner) { michael@0: this._private = aOwner._private; michael@0: } michael@0: michael@0: this.owner = aOwner || null; michael@0: michael@0: // Set to 0 since new tabs that have not been viewed yet are good tabs to michael@0: // toss if app needs more memory. michael@0: this.lastSelected = 0; michael@0: michael@0: // aParams is an object that contains some properties for the initial tab michael@0: // loading like flags, a referrerURI, a charset or even a postData. michael@0: this.create(aURI, aParams || {}, aOwner); michael@0: michael@0: // default tabs to inactive (i.e. no display port) michael@0: this.active = false; michael@0: } michael@0: michael@0: Tab.prototype = { michael@0: get browser() { michael@0: return this._browser; michael@0: }, michael@0: michael@0: get notification() { michael@0: return this._notification; michael@0: }, michael@0: michael@0: get chromeTab() { michael@0: return this._chromeTab; michael@0: }, michael@0: michael@0: get isPrivate() { michael@0: return this._private; michael@0: }, michael@0: michael@0: get pageShowPromise() { michael@0: return this._eventDeferred ? this._eventDeferred.promise : null; michael@0: }, michael@0: michael@0: startLoading: function startLoading() { michael@0: if (this._loading) { michael@0: let stack = new Error().stack; michael@0: throw "Already Loading!\n" + stack; michael@0: } michael@0: this._loading = true; michael@0: }, michael@0: michael@0: endLoading: function endLoading() { michael@0: this._loading = false; michael@0: this.updateFavicon(); michael@0: }, michael@0: michael@0: isLoading: function isLoading() { michael@0: return this._loading; michael@0: }, michael@0: michael@0: create: function create(aURI, aParams, aOwner) { michael@0: this._eventDeferred = Promise.defer(); michael@0: michael@0: this._chromeTab = Elements.tabList.addTab(aParams.index); michael@0: if (this.isPrivate) { michael@0: this._chromeTab.setAttribute("private", "true"); michael@0: } michael@0: michael@0: this._id = Browser.createTabId(); michael@0: let browser = this._createBrowser(aURI, null); michael@0: michael@0: let self = this; michael@0: function onPageShowEvent(aEvent) { michael@0: browser.removeEventListener("pageshow", onPageShowEvent); michael@0: if (self._eventDeferred) { michael@0: self._eventDeferred.resolve(self); michael@0: } michael@0: self._eventDeferred = null; michael@0: } michael@0: browser.addEventListener("pageshow", onPageShowEvent, true); michael@0: browser.addEventListener("DOMWindowCreated", this, false); michael@0: browser.addEventListener("StartUIChange", this, false); michael@0: Elements.browsers.addEventListener("SizeChanged", this, false); michael@0: michael@0: browser.messageManager.addMessageListener("Content:StateChange", this); michael@0: michael@0: if (aOwner) michael@0: this._copyHistoryFrom(aOwner); michael@0: this._loadUsingParams(browser, aURI, aParams); michael@0: }, michael@0: michael@0: updateViewport: function (aEvent) { michael@0: // is not yet supported; just use the browser size. michael@0: let browser = this.browser; michael@0: michael@0: // On the start page we add padding to keep the browser above the navbar. michael@0: let paddingBottom = parseInt(getComputedStyle(browser).paddingBottom, 10); michael@0: let height = browser.clientHeight - paddingBottom; michael@0: michael@0: browser.setWindowSize(browser.clientWidth, height); michael@0: }, michael@0: michael@0: handleEvent: function (aEvent) { michael@0: switch (aEvent.type) { michael@0: case "DOMWindowCreated": michael@0: case "StartUIChange": michael@0: this.updateViewport(); michael@0: break; michael@0: case "SizeChanged": michael@0: this.updateViewport(); michael@0: this._delayUpdateThumbnail(); michael@0: break; michael@0: case "AlertClose": { michael@0: if (this == Browser.selectedTab) { michael@0: this.updateViewport(); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: receiveMessage: function(aMessage) { michael@0: switch (aMessage.name) { michael@0: case "Content:StateChange": michael@0: // update the thumbnail now... michael@0: this.updateThumbnail(); michael@0: // ...and in a little while to capture page after load. michael@0: if (aMessage.json.stateFlags & Ci.nsIWebProgressListener.STATE_STOP) { michael@0: this._delayUpdateThumbnail(); michael@0: } michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: _delayUpdateThumbnail: function() { michael@0: clearTimeout(this._updateThumbnailTimeout); michael@0: this._updateThumbnailTimeout = setTimeout(() => { michael@0: this.updateThumbnail(); michael@0: }, kTabThumbnailDelayCapture); michael@0: }, michael@0: michael@0: destroy: function destroy() { michael@0: this._browser.messageManager.removeMessageListener("Content:StateChange", this); michael@0: this._browser.removeEventListener("DOMWindowCreated", this, false); michael@0: this._browser.removeEventListener("StartUIChange", this, false); michael@0: Elements.browsers.removeEventListener("SizeChanged", this, false); michael@0: clearTimeout(this._updateThumbnailTimeout); michael@0: michael@0: Elements.tabList.removeTab(this._chromeTab); michael@0: this._chromeTab = null; michael@0: this._destroyBrowser(); michael@0: }, michael@0: michael@0: resurrect: function resurrect() { michael@0: let dead = this._browser; michael@0: let active = this.active; michael@0: michael@0: // Hold onto the session store data michael@0: let session = { data: dead.__SS_data, extra: dead.__SS_extdata }; michael@0: michael@0: // We need this data to correctly create and position the new browser michael@0: // If this browser is already a zombie, fallback to the session data michael@0: let currentURL = dead.__SS_restore ? session.data.entries[0].url : dead.currentURI.spec; michael@0: let sibling = dead.nextSibling; michael@0: michael@0: // Destory and re-create the browser michael@0: this._destroyBrowser(); michael@0: let browser = this._createBrowser(currentURL, sibling); michael@0: if (active) michael@0: this.active = true; michael@0: michael@0: // Reattach session store data and flag this browser so it is restored on select michael@0: browser.__SS_data = session.data; michael@0: browser.__SS_extdata = session.extra; michael@0: browser.__SS_restore = true; michael@0: }, michael@0: michael@0: _copyHistoryFrom: function _copyHistoryFrom(tab) { michael@0: let otherHistory = tab._browser._webNavigation.sessionHistory; michael@0: let history = this._browser._webNavigation.sessionHistory; michael@0: michael@0: // Ensure that history is initialized michael@0: history.QueryInterface(Ci.nsISHistoryInternal); michael@0: michael@0: for (let i = 0, length = otherHistory.index; i <= length; i++) michael@0: history.addEntry(otherHistory.getEntryAtIndex(i, false), true); michael@0: }, michael@0: michael@0: _loadUsingParams: function _loadUsingParams(aBrowser, aURI, aParams) { michael@0: let flags = aParams.flags || Ci.nsIWebNavigation.LOAD_FLAGS_NONE; michael@0: let postData = ("postData" in aParams && aParams.postData) ? aParams.postData.value : null; michael@0: let referrerURI = "referrerURI" in aParams ? aParams.referrerURI : null; michael@0: let charset = "charset" in aParams ? aParams.charset : null; michael@0: aBrowser.loadURIWithFlags(aURI, flags, referrerURI, charset, postData); michael@0: }, michael@0: michael@0: _createBrowser: function _createBrowser(aURI, aInsertBefore) { michael@0: if (this._browser) michael@0: throw "Browser already exists"; michael@0: michael@0: // Create a notification box around the browser. Note this includes michael@0: // the input overlay we use to shade content from input events when michael@0: // we're intercepting touch input. michael@0: let notification = this._notification = document.createElement("notificationbox"); michael@0: michael@0: let browser = this._browser = document.createElement("browser"); michael@0: browser.id = "browser-" + this._id; michael@0: this._chromeTab.linkedBrowser = browser; michael@0: michael@0: browser.setAttribute("type", "content-targetable"); michael@0: michael@0: let useRemote = Services.appinfo.browserTabsRemote; michael@0: let useLocal = Util.isLocalScheme(aURI); michael@0: browser.setAttribute("remote", (!useLocal && useRemote) ? "true" : "false"); michael@0: michael@0: // Append the browser to the document, which should start the page load michael@0: let stack = document.createElementNS(XUL_NS, "stack"); michael@0: stack.className = "browserStack"; michael@0: stack.appendChild(browser); michael@0: stack.setAttribute("flex", "1"); michael@0: notification.appendChild(stack); michael@0: Elements.browsers.insertBefore(notification, aInsertBefore); michael@0: michael@0: notification.dir = "reverse"; michael@0: notification.addEventListener("AlertClose", this); michael@0: michael@0: // let the content area manager know about this browser. michael@0: ContentAreaObserver.onBrowserCreated(browser); michael@0: michael@0: if (this.isPrivate) { michael@0: let ctx = browser.docShell.QueryInterface(Ci.nsILoadContext); michael@0: ctx.usePrivateBrowsing = true; michael@0: } michael@0: michael@0: // stop about:blank from loading michael@0: browser.stop(); michael@0: michael@0: let fl = browser.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader; michael@0: fl.renderMode = Ci.nsIFrameLoader.RENDER_MODE_ASYNC_SCROLL; michael@0: michael@0: return browser; michael@0: }, michael@0: michael@0: _destroyBrowser: function _destroyBrowser() { michael@0: if (this._browser) { michael@0: let notification = this._notification; michael@0: notification.removeEventListener("AlertClose", this); michael@0: let browser = this._browser; michael@0: browser.active = false; michael@0: michael@0: this._notification = null; michael@0: this._browser = null; michael@0: this._loading = false; michael@0: michael@0: Elements.browsers.removeChild(notification); michael@0: } michael@0: }, michael@0: michael@0: updateThumbnail: function updateThumbnail() { michael@0: if (!this.isPrivate) { michael@0: PageThumbs.captureToCanvas(this.browser.contentWindow, this._chromeTab.thumbnailCanvas); michael@0: } michael@0: }, michael@0: michael@0: updateFavicon: function updateFavicon() { michael@0: this._chromeTab.updateFavicon(this._browser.mIconURL); michael@0: }, michael@0: michael@0: set active(aActive) { michael@0: if (!this._browser) michael@0: return; michael@0: michael@0: let notification = this._notification; michael@0: let browser = this._browser; michael@0: michael@0: if (aActive) { michael@0: notification.classList.add("active-tab-notificationbox"); michael@0: browser.setAttribute("type", "content-primary"); michael@0: Elements.browsers.selectedPanel = notification; michael@0: browser.active = true; michael@0: Elements.tabList.selectedTab = this._chromeTab; michael@0: browser.focus(); michael@0: } else { michael@0: notification.classList.remove("active-tab-notificationbox"); michael@0: browser.messageManager.sendAsyncMessage("Browser:Blur", { }); michael@0: browser.setAttribute("type", "content-targetable"); michael@0: browser.active = false; michael@0: } michael@0: }, michael@0: michael@0: get active() { michael@0: if (!this._browser) michael@0: return false; michael@0: return this._browser.getAttribute("type") == "content-primary"; michael@0: }, michael@0: michael@0: toString: function() { michael@0: return "[Tab " + (this._browser ? this._browser.currentURI.spec : "(no browser)") + "]"; michael@0: } michael@0: }; michael@0: michael@0: // Helper used to hide IPC / non-IPC differences for rendering to a canvas michael@0: function rendererFactory(aBrowser, aCanvas) { michael@0: let wrapper = {}; michael@0: michael@0: if (aBrowser.contentWindow) { michael@0: let ctx = aCanvas.getContext("2d"); michael@0: let draw = function(browser, aLeft, aTop, aWidth, aHeight, aColor, aFlags) { michael@0: ctx.drawWindow(browser.contentWindow, aLeft, aTop, aWidth, aHeight, aColor, aFlags); michael@0: let e = document.createEvent("HTMLEvents"); michael@0: e.initEvent("MozAsyncCanvasRender", true, true); michael@0: aCanvas.dispatchEvent(e); michael@0: }; michael@0: wrapper.checkBrowser = function(browser) { michael@0: return browser.contentWindow; michael@0: }; michael@0: wrapper.drawContent = function(callback) { michael@0: callback(ctx, draw); michael@0: }; michael@0: } michael@0: else { michael@0: let ctx = aCanvas.MozGetIPCContext("2d"); michael@0: let draw = function(browser, aLeft, aTop, aWidth, aHeight, aColor, aFlags) { michael@0: ctx.asyncDrawXULElement(browser, aLeft, aTop, aWidth, aHeight, aColor, aFlags); michael@0: }; michael@0: wrapper.checkBrowser = function(browser) { michael@0: return !browser.contentWindow; michael@0: }; michael@0: wrapper.drawContent = function(callback) { michael@0: callback(ctx, draw); michael@0: }; michael@0: } michael@0: michael@0: return wrapper; michael@0: }; michael@0: michael@0: // Based on ClickEventHandler from /browser/base/content/content.js michael@0: let ClickEventHandler = { michael@0: init: function () { michael@0: gEventListenerService.addSystemEventListener(Elements.browsers, "click", this, true); michael@0: }, michael@0: michael@0: uninit: function () { michael@0: gEventListenerService.removeSystemEventListener(Elements.browsers, "click", this, true); michael@0: }, michael@0: michael@0: handleEvent: function (aEvent) { michael@0: if (!aEvent.isTrusted || aEvent.defaultPrevented) { michael@0: return; michael@0: } michael@0: let [href, node] = this._hrefAndLinkNodeForClickEvent(aEvent); michael@0: if (href && (aEvent.button == 1 || aEvent.ctrlKey)) { michael@0: // Open link in a new tab for middle-click or ctrl-click michael@0: BrowserUI.openLinkInNewTab(href, aEvent.shiftKey, Browser.selectedTab); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Extracts linkNode and href for the current click target. michael@0: * michael@0: * @param event michael@0: * The click event. michael@0: * @return [href, linkNode]. michael@0: * michael@0: * @note linkNode will be null if the click wasn't on an anchor michael@0: * element (or XLink). michael@0: */ michael@0: _hrefAndLinkNodeForClickEvent: function(event) { michael@0: function isHTMLLink(aNode) { michael@0: return ((aNode instanceof content.HTMLAnchorElement && aNode.href) || michael@0: (aNode instanceof content.HTMLAreaElement && aNode.href) || michael@0: aNode instanceof content.HTMLLinkElement); michael@0: } michael@0: michael@0: let node = event.target; michael@0: while (node && !isHTMLLink(node)) { michael@0: node = node.parentNode; michael@0: } michael@0: michael@0: if (node) michael@0: return [node.href, node]; michael@0: michael@0: // If there is no linkNode, try simple XLink. michael@0: let href, baseURI; michael@0: node = event.target; michael@0: while (node && !href) { michael@0: if (node.nodeType == content.Node.ELEMENT_NODE) { michael@0: href = node.getAttributeNS("http://www.w3.org/1999/xlink", "href"); michael@0: if (href) michael@0: baseURI = node.ownerDocument.baseURIObject; michael@0: } michael@0: node = node.parentNode; michael@0: } michael@0: michael@0: // In case of XLink, we don't return the node we got href from since michael@0: // callers expect -like elements. michael@0: // Note: makeURI() will throw if aUri is not a valid URI. michael@0: return [href ? Services.io.newURI(href, null, baseURI).spec : null, null]; michael@0: } michael@0: };