// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Mobile Browser. * * The Initial Developer of the Original Code is * Mozilla Corporation. * Portions created by the Initial Developer are Copyright (C) 2008 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Brad Lassey * Mark Finkle * Aleks Totic * Johnathan Nightingale * Stuart Parmenter * Taras Glek * Roy Frostig * Ben Combee * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ let Cc = Components.classes; let Ci = Components.interfaces; let Cu = Components.utils; const FINDSTATE_FIND = 0; const FINDSTATE_FIND_AGAIN = 1; const FINDSTATE_FIND_PREVIOUS = 2; const endl = '\n'; Cu.import("resource://gre/modules/SpatialNavigation.js"); function getBrowser() { return Browser.selectedBrowser; } const kDefaultTextZoom = 1.2; const kDefaultBrowserWidth = 1024; function debug() { let bv = Browser._browserView; let tc = bv._tileManager._tileCache; let scrollbox = document.getElementById("tile-container-container") .boxObject .QueryInterface(Components.interfaces.nsIScrollBoxObject); let x = {}; let y = {}; let w = {}; let h = {}; scrollbox.getPosition(x, y); scrollbox.getScrolledSize(w, h); let container = document.getElementById("tile-container"); let [x, y] = [x.value, y.value]; let [w, h] = [w.value, h.value]; if (bv) { dump('----------------------DEBUG!-------------------------\n'); dump(bv._browserViewportState.toString() + endl); dump(endl); dump('location from Browser: ' + Browser.selectedBrowser.contentWindow.location + endl); dump('location from BV : ' + bv.getBrowser().contentWindow.location + endl); dump(endl + endl); let cr = bv._tileManager._criticalRect; dump('criticalRect from BV: ' + (cr ? cr.toString() : null) + endl); dump('visibleRect from BV : ' + bv.getVisibleRect().toString() + endl); dump('visibleRect from foo: ' + Browser.getVisibleRect().toString() + endl); dump('bv batchops depth: ' + bv._batchOps.length + endl); dump('renderpause depth: ' + bv._renderMode + endl); dump(endl); dump('window.innerWidth : ' + window.innerWidth + endl); dump('window.innerHeight: ' + window.innerHeight + endl); dump(endl); dump('container width,height from BV: ' + bv._container.style.width + ', ' + bv._container.style.height + endl); dump('container width,height via DOM: ' + container.style.width + ', ' + container.style.height + endl); dump(endl); dump('scrollbox position : ' + x + ', ' + y + endl); dump('scrollbox scrolledsize: ' + w + ', ' + h + endl); let sb = document.getElementById("tile-container-container"); dump('container location: ' + Math.round(container.getBoundingClientRect().left) + " " + Math.round(container.getBoundingClientRect().top) + endl); dump(endl); let mouseModule = ih._modules[0]; dump('ih grabber : ' + ih._grabber + endl); dump('ih grabdepth: ' + ih._grabDepth + endl); dump('ih listening: ' + !ih._ignoreEvents + endl); dump('ih suppress : ' + ih._suppressNextClick + endl); dump('mouseModule : ' + mouseModule + endl); dump(endl); dump('tilecache capacity: ' + bv._tileManager._tileCache.getCapacity() + endl); dump('tilecache size : ' + bv._tileManager._tileCache.size + endl); dump('tilecache iBound : ' + bv._tileManager._tileCache.iBound + endl); dump('tilecache jBound : ' + bv._tileManager._tileCache.jBound + endl); dump('-----------------------------------------------------\n'); } } function debugTile(i, j) { let bv = Browser._browserView; let tc = bv._tileManager._tileCache; let t = tc.getTile(i, j); dump('------ DEBUGGING TILE (' + i + ',' + j + ') --------\n'); dump('in bounds: ' + tc.inBounds(i, j) + endl); dump('occupied : ' + tc.isOccupied(i, j) + endl); if (t) { dump('toString : ' + t.toString(true) + endl); dump('free : ' + t.free + endl); dump('dirtyRect: ' + t._dirtyTileCanvasRect + endl); let len = tc._tilePool.length; for (let k = 0; k < len; ++k) if (tc._tilePool[k] === t) dump('found in tilePool at index ' + k + endl); } dump('------------------------------------\n'); } function onDebugKeyPress(ev) { let bv = Browser._browserView; if (!ev.ctrlKey) return; // use capitals so we require SHIFT here too const a = 65; // debug all critical tiles const b = 66; // dump an ASCII graphic of the tile map const c = 67; // set tilecache capacity const d = 68; // debug dump const e = 69; const f = 70; const g = 71; const h = 72; const i = 73; // toggle info click mode const j = 74; const k = 75; const l = 76; // restart lazy crawl const m = 77; // fix mouseout const n = 78; const o = 79; const p = 80; // debug tiles in pool order const q = 81; const r = 82; // reset visible rect const s = 83; const t = 84; // debug given list of tiles separated by space const u = 85; const v = 86; const w = 87; const x = 88; const y = 89; const z = 90; // set zoom level to 1 if (window.tileMapMode) { function putChar(ev, col, row) { let tile = tc.getTile(col, row); switch (ev.charCode) { case h: // held tiles dump(tile ? (tile.free ? '*' : 'h') : ' '); break; case d: // dirty tiles dump(tile ? (tile.isDirty() ? 'd' : '*') : ' '); break; case o: // occupied tileholders dump(tc.isOccupied(col, row) ? 'o' : ' '); break; } } let tc = Browser._browserView._tileManager._tileCache; let col, row; dump(endl); dump(' '); for (col = 0; col < tc.iBound; ++col) dump(col % 10); dump(endl); for (row = 0; row < tc.jBound; ++row) { dump((row % 10) + ' '); for (col = 0; col < tc.iBound; ++col) { putChar(ev, col, row); } dump(endl); } dump(endl + endl); for (let ii = 0; ii < tc._tilePool.length; ++ii) { let tile = tc._tilePool[ii]; putChar(ev, tile.i, tile.j); } dump(endl + endl); window.tileMapMode = false; return; } switch (ev.charCode) { case r: bv.onAfterVisibleMove(); //bv.setVisibleRect(Browser.getVisibleRect()); case d: debug(); break; case l: bv._tileManager.restartLazyCrawl(bv._tileManager._criticalRect); break; case c: let cap = parseInt(window.prompt('new capacity')); bv._tileManager._tileCache.setCapacity(cap); break; case b: window.tileMapMode = true; break; case t: let ijstrs = window.prompt('row,col plz').split(' '); for each (let ijstr in ijstrs) { let [i, j] = ijstr.split(',').map(function (x) { return parseInt(x); }); debugTile(i, j); } break; case a: let cr = bv._tileManager._criticalRect; dump('>>>>>> critical rect is ' + (cr ? cr.toString() : cr) + '\n'); if (cr) { let starti = cr.left >> kTileExponentWidth; let endi = cr.right >> kTileExponentWidth; let startj = cr.top >> kTileExponentHeight; let endj = cr.bottom >> kTileExponentHeight; for (var jj = startj; jj <= endj; ++jj) for (var ii = starti; ii <= endi; ++ii) debugTile(ii, jj); } break; case i: window.infoMode = !window.infoMode; break; case m: bv.resumeRendering(); break; case p: let tc = bv._tileManager._tileCache; dump('************* TILE POOL ****************\n'); for (let ii = 0, len = tc._tilePool.length; ii < len; ++ii) { if (window.infoMode) debugTile(tc._tilePool[ii].i, tc._tilePool[ii].j); else dump(tc._tilePool[ii].i + ',' + tc._tilePool[ii].j + '\n'); } dump('****************************************\n'); break; case z: bv.setZoomLevel(1.0); break; default: break; } } window.infoMode = false; window.tileMapMode = false; var ih = null; var Browser = { _tabs : [], _browsers : [], _selectedTab : null, windowUtils: window.QueryInterface(Ci.nsIInterfaceRequestor) .getInterface(Ci.nsIDOMWindowUtils), contentScrollbox: null, contentScrollboxScroller: null, controlsScrollbox: null, controlsScrollboxScroller: null, startup: function() { var self = this; dump("begin startup\n"); let container = document.getElementById("tile-container"); let bv = this._browserView = new BrowserView(container, Browser.getVisibleRect); /* handles dispatching clicks on tiles into clicks in content or zooms */ container.customClicker = this._createContentCustomClicker(bv); /* vertically scrolling box that contains tiles and the urlbar */ let contentScrollbox = this.contentScrollbox = document.getElementById("tile-container-container"); this.contentScrollboxScroller = contentScrollbox.boxObject.QueryInterface(Ci.nsIScrollBoxObject); contentScrollbox.customDragger = new Browser.MainDragger(bv); /* horizontally scrolling box that holds the sidebars as well as the contentScrollbox */ let controlsScrollbox = this.controlsScrollbox = document.getElementById("scrollbox"); this.controlsScrollboxScroller = controlsScrollbox.boxObject.QueryInterface(Ci.nsIScrollBoxObject); controlsScrollbox.customDragger = { allowRealtimeDownUp: true, dragStart: function dragStart(cx, cy, target, scroller) {}, dragStop: function dragStop(dx, dy, scroller) { return false; }, dragMove: function dragMove(dx, dy, scroller) { return false; } }; // during startup a lot of viewportHandler calls happen due to content and window resizes bv.beginBatchOperation(); function resizeHandler(e) { if (e.target != window) return; dump(window.innerWidth + "," + window.innerHeight + "\n"); // XXX is this code right here actually needed? let w = window.innerWidth; let h = window.innerHeight; let maximize = (document.documentElement.getAttribute("sizemode") == "maximized"); if (maximize && w > screen.width) return; bv.beginBatchOperation(); contentScrollbox.style.width = w + 'px'; contentScrollbox.style.height = h + 'px'; controlsScrollbox.style.width = w + 'px'; controlsScrollbox.style.height = h + 'px'; let toolbarHeight = Math.round(document.getElementById("toolbar-main").getBoundingClientRect().height); let spacers = document.getElementsByClassName("sidebar-spacer"); for (let i = 0, len = spacers.length; i < len; i++) spacers[i].style.height = toolbarHeight + 'px'; // toolbar UI document.getElementById("toolbar-main").width = w; // Tell the UI to resize the browser controls before calling updateSize BrowserUI.sizeControls(w, h); // Resize the browsers... let browsers = Browser.browsers; if (browsers) { let scaledH = (kDefaultBrowserWidth * (h / w)); for (let i=0; is SpatialNavigation.init(browsers, panCallback); /* Login Manager */ Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); /* Command line arguments/initial homepage */ // If this is an intial window launch (was a nsICommandLine passed via window params) // we execute some logic to load the initial launch page if (window.arguments && window.arguments[0]) { var whereURI = null; try { // Try to access the commandline var cmdLine = window.arguments[0].QueryInterface(Ci.nsICommandLine); try { // Check for and use a default homepage whereURI = gPrefService.getCharPref("browser.startup.homepage"); } catch (e) {} // Check for and use a single commandline parameter if (cmdLine.length == 1) { // Assume the first arg is a URI if it is not a flag var uri = cmdLine.getArgument(0); if (uri != "" && uri[0] != '-') { whereURI = cmdLine.resolveURI(uri); if (whereURI) whereURI = whereURI.spec; } } // Check for the "url" flag var uriFlag = cmdLine.handleFlagWithParam("url", false); if (uriFlag) { whereURI = cmdLine.resolveURI(uriFlag); if (whereURI) whereURI = whereURI.spec; } } catch (e) {} if (whereURI) this.addTab(whereURI, true); } // JavaScript Error Console if (gPrefService.getBoolPref("browser.console.showInPanel")){ let tool_console = document.getElementById("tool-console"); tool_console.hidden = false; } // Re-enable plugins if we had previously disabled them. We should get rid of // this code eventually... if (gPrefService.prefHasUserValue("temporary.disablePlugins")) { gPrefService.clearUserPref("temporary.disablePlugins"); this.setPluginState(true); } bv.commitBatchOperation(); dump("end startup\n"); }, shutdown: function() { this._browserView.setBrowser(null, null, false); BrowserUI.uninit(); var os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); os.removeObserver(gXPInstallObserver, "xpinstall-install-blocked"); os.removeObserver(gSessionHistoryObserver, "browser:purge-session-history"); #ifdef WINCE os.removeObserver(SoftKeyboardObserver, "softkb-change"); #endif window.controllers.removeController(this); window.controllers.removeController(BrowserUI); }, setPluginState: function(enabled) { var phs = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost); var plugins = phs.getPluginTags({ }); for (var i = 0; i < plugins.length; ++i) plugins[i].disabled = !enabled; }, get browsers() { return this._browsers; }, scrollContentToTop: function scrollContentToTop() { this.contentScrollboxScroller.scrollTo(0, 0); this._browserView.onAfterVisibleMove(); }, hideSidebars: function scrollSidebarsOffscreen() { let container = document.getElementById("tile-container"); let containerBCR = container.getBoundingClientRect(); let dx = containerBCR.left; if (dx < 0) dx = Math.min(containerBCR.right - window.innerWidth, 0); this.controlsScrollboxScroller.scrollBy(Math.round(dx), 0); Browser.contentScrollbox.customDragger.scrollingOuterX = false; // XXX ugh. this._browserView.onAfterVisibleMove(); }, /** * Return the currently active object */ get selectedBrowser() { return this._selectedTab.browser; }, getTabAtIndex: function(index) { if (index > this._tabs.length || index < 0) return null; return this._tabs[index]; }, getTabFromChrome: function(chromeTab) { for (var t = 0; t < this._tabs.length; t++) { if (this._tabs[t].chromeTab == chromeTab) return this._tabs[t]; } return null; }, addTab: function(uri, bringFront) { let newTab = new Tab(); this._tabs.push(newTab); this._browsers.push(newTab.browser); if (bringFront) this.selectedTab = newTab; newTab.load(uri); let event = document.createEvent("Events"); event.initEvent("TabOpen", true, false); newTab.chromeTab.dispatchEvent(event); return newTab; }, closeTab: function(tab) { if (tab instanceof XULElement) tab = this.getTabFromChrome(tab); if (!tab) return; let tabIndex = this._tabs.indexOf(tab); let nextTab = this._selectedTab; if (this._selectedTab == tab) { nextTab = this.getTabAtIndex(tabIndex + 1) || this.getTabAtIndex(tabIndex - 1); if (!nextTab) return; } let event = document.createEvent("Events"); event.initEvent("TabClose", true, false); tab.chromeTab.dispatchEvent(event); this.selectedTab = nextTab; tab.destroy(); this._tabs.splice(tabIndex, 1); this._browsers.splice(tabIndex, 1); // redraw the tabs for (let t = tabIndex; t < this._tabs.length; t++) this._tabs[t].updateThumbnail(); }, get selectedTab() { return this._selectedTab; }, set selectedTab(tab) { let bv = this._browserView; if (tab instanceof XULElement) tab = this.getTabFromChrome(tab); if (!tab || this._selectedTab == tab) return; let firstTab = this._selectedTab == null; this._selectedTab = tab; bv.beginBatchOperation(); bv.setBrowser(tab.browser, tab.browserViewportState, false); bv.forceContainerResize(); // XXX these should probably be computed less hackily so they don't // potentially break if we change something in browser.xul let offY = Math.round(document.getElementById("toolbar-container").getBoundingClientRect().height); let restoreX = Math.max(0, tab.browserViewportState.visibleX); let restoreY = Math.max(0, tab.browserViewportState.visibleY) + offY; dump('Switch tab scrolls to: ' + restoreX + ', ' + restoreY + '\n'); Browser.contentScrollboxScroller.scrollTo(restoreX, restoreY); document.getElementById("tabs").selectedItem = tab.chromeTab; if (!firstTab) { let webProgress = this.selectedBrowser.webProgress; let securityUI = this.selectedBrowser.securityUI; try { tab._listener.onLocationChange(webProgress, null, tab.browser.currentURI); if (securityUI) tab._listener.onSecurityChange(webProgress, null, securityUI.state); } catch (e) { // don't inhibit other listeners or following code Components.utils.reportError(e); } let event = document.createEvent("Events"); event.initEvent("TabSelect", true, false); tab.chromeTab.dispatchEvent(event); } bv.commitBatchOperation(); }, supportsCommand: function(cmd) { var isSupported = false; switch (cmd) { case "cmd_fullscreen": isSupported = true; break; default: isSupported = false; break; } return isSupported; }, isCommandEnabled: function(cmd) { return true; }, doCommand: function(cmd) { switch (cmd) { case "cmd_fullscreen": window.fullScreen = !window.fullScreen; break; } }, getNotificationBox: function() { return document.getElementById("notifications"); }, findState: FINDSTATE_FIND, openFind: function(aState) { this.findState = aState; var findbar = document.getElementById("findbar"); if (!findbar.browser) findbar.browser = this.selectedBrowser; var panel = document.getElementById("findbar-container"); if (panel.hidden) { panel.hidden = false; } this.doFind(); }, doFind: function() { var findbar = document.getElementById("findbar"); if (Browser.findState == FINDSTATE_FIND) findbar.onFindCommand(); else findbar.onFindAgainCommand(Browser.findState == FINDSTATE_FIND_PREVIOUS); var panel = document.getElementById("findbar-container"); panel.top = window.innerHeight - Math.floor(findbar.getBoundingClientRect().height); }, translatePhoneNumbers: function() { let doc = getBrowser().contentDocument; // jonas black magic (only match text nodes that contain a sequence of 4 numbers) let textnodes = doc.evaluate('//text()[contains(translate(., "0123456789", "^^^^^^^^^^"), "^^^^")]', doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); let s, node, lastLastIndex; let re = /(\+?1? ?-?\(?\d{3}\)?[ +-\.]\d{3}[ +-\.]\d{4})/; for (var i = 0; i < textnodes.snapshotLength; i++) { node = textnodes.snapshotItem(i); s = node.data; if (s.match(re)) { s = s.replace(re, " $1 "); try { let replacement = doc.createElement("span"); replacement.innerHTML = s; node.parentNode.insertBefore(replacement, node); node.parentNode.removeChild(node); } catch(e) { //do nothing, but continue } } } }, /** * Handle command event bubbling up from content. This allows us to do chrome- * privileged things based on buttons in, e.g., unprivileged error pages. * Obviously, care should be taken not to trust events that web pages could have * synthesized. */ _handleContentCommand: function _handleContentCommand(aEvent) { // Don't trust synthetic events if (!aEvent.isTrusted) return; var ot = aEvent.originalTarget; var errorDoc = ot.ownerDocument; // If the event came from an ssl error page, it is probably either the "Add // Exception…" or "Get me out of here!" button if (/^about:neterror\?e=nssBadCert/.test(errorDoc.documentURI)) { if (ot == errorDoc.getElementById('exceptionDialogButton')) { var params = { exceptionAdded : false }; try { switch (gPrefService.getIntPref("browser.ssl_override_behavior")) { case 2 : // Pre-fetch & pre-populate params.prefetchCert = true; case 1 : // Pre-populate params.location = errorDoc.location.href; } } catch (e) { Components.utils.reportError("Couldn't get ssl_override pref: " + e); } window.openDialog('chrome://pippki/content/exceptionDialog.xul', '','chrome,centerscreen,modal', params); // If the user added the exception cert, attempt to reload the page if (params.exceptionAdded) errorDoc.location.reload(); } else if (ot == errorDoc.getElementById('getMeOutOfHereButton')) { // Get the start page from the *default* pref branch, not the user's var defaultPrefs = Cc["@mozilla.org/preferences-service;1"] .getService(Ci.nsIPrefService).getDefaultBranch(null); var url = "about:blank"; try { url = defaultPrefs.getCharPref("browser.startup.homepage"); // If url is a pipe-delimited set of pages, just take the first one. if (url.indexOf("|") != -1) url = url.split("|")[0]; } catch (e) { /* Fall back on about blank */ } Browser.selectedBrowser.loadURI(url, null, null, false); } } }, _createContentCustomClicker: function _createContentCustomClicker(browserView) { // XXX we probably shouldn't generate this dynamically like this, but // actually make it a prototype somewhere and instantiate it and such... function dispatchContentClick(browser, x, y) { let cwu = BrowserView.Util.getBrowserDOMWindowUtils(browser); let scrollX = { value: 0 }; let scrollY = { value: 0 }; cwu.getScrollXY(false, scrollX, scrollY); cwu.sendMouseEvent("mousedown", x - scrollX.value, y - scrollY.value, 0, 1, 0, true); cwu.getScrollXY(false, scrollX, scrollY); cwu.sendMouseEvent("mouseup", x - scrollX.value, y - scrollY.value, 0, 1, 0, true); } return { singleClick: function singleClick(cX, cY) { let browser = browserView.getBrowser(); if (browser) { let [x, y] = Browser.transformClientToBrowser(cX, cY); dispatchContentClick(browser, x, y); } }, doubleClick: function doubleClick(cX1, cY1, cX2, cY2) { if (!Browser.zoomToPoint(cX2, cY2)) Browser.zoomFromPoint(cX2, cY2); }, toString: function toString() { return "[ContentCustomClicker] { }"; } }; }, /** * Compute the sidebar percentage visibility. * * @param [optional] dx * @param [optional] dy an offset distance at which to perform the visibility * computation * @return [leftVisibility, rightVisiblity, leftTotalWidth, rightTotalWidth] */ computeSidebarVisibility: function computeSidebarVisibility(dx, dy) { // XXX these should return 0 if the sidebars aren't visible function visibility(bar, visrect) { try { let w = bar.width; let h = bar.height; bar.restrictTo(visrect); // throws exception if intersection of rects is empty return bar.width / w; } catch (e) { return 0; } } if (!dx) dx = 0; if (!dy) dy = 0; let leftbarCBR = document.getElementById('tabs-container').getBoundingClientRect(); let ritebarCBR = document.getElementById('browser-controls').getBoundingClientRect(); let leftbar = new wsRect(Math.round(leftbarCBR.left) - dx, 0, Math.round(leftbarCBR.width), 1); let ritebar = new wsRect(Math.round(ritebarCBR.left) - dx, 0, Math.round(ritebarCBR.width), 1); let leftw = leftbar.width; let ritew = ritebar.width; let visrect = new wsRect(0, 0, window.innerWidth, 1); let leftvis = visibility(leftbar, visrect); let ritevis = visibility(ritebar, visrect); return [leftvis, ritevis, leftw, ritew]; }, /** * Compute the horizontal distance needed to scroll in order to snap the * sidebars into place. * * Visibility is computed by creating dummy rectangles for the sidebar and the * visible rect. Sidebar rectangles come from getBoundingClientRect(), so * they are in absolute client coordinates (and since we're in a scrollbox, * this means they are positioned relative to the window, which is anchored at * (0, 0) regardless of the scrollbox's scroll position. The rectangles are * made to have a top of 0 and a height of 1, since these do not affect how we * compute visibility (we care only about width), and using rectangles allows * us to use restrictTo(), which comes in handy. * * @return scrollBy dx needed to make snap happen */ snapSidebars: function snapSidebars() { let [leftvis, ritevis, leftw, ritew] = Browser.computeSidebarVisibility(); let snappedX = 0; if (leftvis != 0 && leftvis != 1) { if (leftvis >= 0.6666) { snappedX = -((1 - leftvis) * leftw); } else { snappedX = leftvis * leftw; } } else if (ritevis != 0 && ritevis != 1) { if (ritevis >= 0.6666) { snappedX = (1 - ritevis) * ritew; } else { snappedX = -ritevis * ritew; } } return Math.round(snappedX); }, tryFloatToolbar: function tryFloatToolbar(dx, dy) { if (this.floatedWhileDragging) return; let [leftvis, ritevis, leftw, ritew] = Browser.computeSidebarVisibility(dx, dy); // XXX computeSideBarVisibility will normally return 0.0015... for ritevis if (leftvis > 0.002 || ritevis > 0.002) { BrowserUI.lockToolbar(); this.floatedWhileDragging = true; } }, tryUnfloatToolbar: function tryUnfloatToolbar(dx, dy) { if (!this.floatedWhileDragging) return true; let [leftvis, ritevis, leftw, ritew] = Browser.computeSidebarVisibility(dx, dy); if (leftvis <= 0.002 && ritevis <= 0.002) { BrowserUI.unlockToolbar(); this.floatedWhileDragging = false; return true; } return false; }, zoom: function zoom(aDirection) { Browser._browserView.zoom(aDirection); //Browser.forceChromeReflow(); // Zoom causes a width/height change to the // BrowserView's containing element, but // sometimes doesn't cause the reflow that // resizes the parent right away, so we // can preempt it here. Not needed anywhere // currently but if this becomes API or is // needed then uncomment this line. }, zoomToPoint: function zoomToPoint(cX, cY) { const margin = 15; let [elementX, elementY] = Browser.transformClientToBrowser(cX, cY); let aElement = Browser.elementFromPoint(elementX, elementY); let bv = Browser._browserView; let scroller = Browser.contentScrollboxScroller; let elRect = Browser.getBoundingContentRect(aElement); let elWidth = elRect.width; let vis = bv.getVisibleRect(); let vrWidth = vis.width; /* Try to set zoom-level such that once zoomed element is as wide * as the visible rect */ let zoomLevel = BrowserView.Util.clampZoomLevel((vrWidth + (2 * margin)) / elWidth); let oldZoomLevel = bv.getZoomLevel(); //dump("element width: " + elWidth + "\n"); //dump("old zoom level: " + oldZoomLevel + "\n"); //dump("new zoom level: " + zoomLevel + "\n"); /* If the new zoom level we've calculated is less than or equal to * the current zoom level, return early and don't progress further. * If the new zoom level is higher, continue to zoom inward. */ if (oldZoomLevel >= zoomLevel) return false; bv.beginBatchOperation(); /* XXX * This isn't ideal. */ this.hideSidebars(); bv.setZoomLevel(zoomLevel); bv.forceContainerResize(); //Browser.forceChromeReflow(); let dx = Math.round(bv.browserToViewport(elRect.left) - margin - vis.left); let dy = Math.round(bv.browserToViewport(elementY) - (window.innerHeight / 2) - vis.top); /* * Animation code for zooming to zoom to the correct place * let callback = { scroller: scroller, sx: (dx > 0) ? 1 : -1, sy: (dy > 0) ? 1 : -1, dx: dx, dy: dy, notify: function kineticTimerCallback(timer) { if (this.dx == 0 && this.dy == 0) { timer.cancel(); return; } Browser.contentScrollbox.customDragger.dragMove(this.sx, this.sy, this.scroller); if (this.dx != 0) this.dx -= this.sx; if (this.dy != 0) this.dy -= this.sy; } }; this._zoomTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); //initialize our timer with updateInterval this._zoomTimer.initWithCallback(callback, 2, this._zoomTimer.TYPE_REPEATING_PRECISE); */ Browser.contentScrollbox.customDragger.dragMove(dx, dy, scroller); bv.commitBatchOperation(); return true; }, zoomFromPoint: function zoomFromPoint(cX, cY) { let [elementX, elementY] = this.transformClientToBrowser(cX, cY); let bv = Browser._browserView; bv.beginBatchOperation(); bv.zoomToPage(); bv.forceContainerResize(); let dy = Math.round(bv.browserToViewport(elementY) - (window.innerHeight / 2) - bv.getVisibleRect().top); this.contentScrollbox.customDragger.dragMove(0, dy, this.contentScrollboxScroller); Browser.forceChromeReflow(); this.hideSidebars(); bv.commitBatchOperation(); }, getBoundingContentRect: function getBoundingContentRect(contentElem) { let browser = Browser._browserView.getBrowser(); if (!browser) return null; let scrollX = { value: 0 }; let scrollY = { value: 0 }; let cwu = BrowserView.Util.getBrowserDOMWindowUtils(browser); cwu.getScrollXY(false, scrollX, scrollY); let r = contentElem.getBoundingClientRect(); //dump('getBoundingContentRect: clientRect is at ' + r.left + ', ' + r.top + '; scrolls are ' + scrollX.value + ', ' + scrollY.value + '\n'); return new wsRect(r.left + scrollX.value, r.top + scrollY.value, r.width, r.height); }, /** * Transform x and y from client coordinates to BrowserView coordinates. */ clientToBrowserView: function clientToBrowserView(x, y) { let container = document.getElementById("tile-container"); let containerBCR = container.getBoundingClientRect(); let x0 = Math.round(containerBCR.left); let y0; if (arguments.length > 1) y0 = Math.round(containerBCR.top); return (arguments.length > 1) ? [x - x0, y - y0] : (x - x0); }, browserViewToClient: function browserViewToClient(x, y) { let container = document.getElementById("tile-container"); let containerBCR = container.getBoundingClientRect(); let x0 = Math.round(-containerBCR.left); let y0; if (arguments.length > 1) y0 = Math.round(-containerBCR.top); return (arguments.length > 1) ? [x - x0, y - y0] : (x - x0); }, /** * turn client coordinates into page-relative ones (adjusted for * zoom and page position) */ transformClientToBrowser: function transformClientToBrowser(cX, cY) { return this.clientToBrowserView(cX, cY).map(this._browserView.viewportToBrowser); }, /** * return element at client coordinates of browser, returns null if * there's no active browser or if no element can be found */ elementFromPoint: function elementFromPoint(x, y) { Util.dumpLn("*** elementFromPoint: page ", x, ",", y); let browser = this._browserView.getBrowser(); if (!browser) return null; let cwu = BrowserView.Util.getBrowserDOMWindowUtils(browser); let scrollX = { value: 0 }, scrollY = { value: 0 }; cwu.getScrollXY(false, scrollX, scrollY); x = x - scrollX.value; y = y - scrollY.value; let elem = cwu.elementFromPoint(x, y, true, /* ignore root scroll frame*/ false); /* don't flush layout */ // step through layers of IFRAMEs and FRAMES to find innermost element while (elem && (elem instanceof HTMLIFrameElement || elem instanceof HTMLFrameElement)) { let frameWin = elem.ownerDocument.defaultView; let frameUtils = frameWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); frameUtils.getScrollXY(false, scrollX, scrollY); x = x - elem.offsetLeft + scrollX.value; y = y - elem.offsetTop + scrollY.value; elem = elem.contentDocument.elementFromPoint(x, y); } return elem; }, /** * Return the visible rect in coordinates with origin at the (left, top) of * the tile container, i.e. BrowserView coordinates. */ getVisibleRect: function getVisibleRect() { let container = document.getElementById("tile-container"); let containerBCR = container.getBoundingClientRect(); let x = Math.round(-containerBCR.left); let y = Math.round(-containerBCR.top); let w = window.innerWidth; let h = window.innerHeight; return new wsRect(x, y, w, h); }, /** * Convenience function for getting the scrollbox position off of a * scrollBoxObject interface. Returns the actual values instead of the * wrapping objects. * * @param scroller a scrollBoxObject on which to call scroller.getPosition() */ getScrollboxPosition: function getScrollboxPosition(scroller) { let x = {}; let y = {}; scroller.getPosition(x, y); return [x.value, y.value]; }, forceChromeReflow: function forceChromeReflow() { let dummy = getComputedStyle(document.documentElement, "").width; } }; Browser.MainDragger = function MainDragger(browserView) { this.allowRealtimeDownUp = true; this.scrollingOuterX = false; this.bv = browserView; this.floatedWhileDragging = false; this.draggedFrame = null; }; Browser.MainDragger.prototype = { dragStart: function dragStart(clientX, clientY, target, scroller) { this.draggedFrame = null; if (this._targetIsContent(target)) { // since we're dealing with content, look to see if user has started // a drag while over a IFRAME/FRAME element let [x, y] = Browser.transformClientToBrowser(clientX, clientY); let element = Browser.elementFromPoint(x, y); if (element && element.ownerDocument != Browser.selectedBrowser.contentDocument) { Util.dumpLn("*** dragStart got element ", element, " ownerDoc ", element.ownerDocument, " selectedBrowser.contentDoc ", Browser.selectedBrowser.contentDocument); this.draggedFrame = element.ownerDocument.defaultView; } } this.bv.pauseRendering(); }, dragStop: function dragStop(dx, dy, scroller) { let dx = this.dragMove(dx, dy, scroller, true); dx += this.dragMove(Browser.snapSidebars(), 0, scroller, true); /* XXX * Set scrollingOuterX to be true when the sidebars are open. * We should really just take a look at our geometry at this point * and determine this ourselves rather than relying on our helper * functions to give us this information */ this.scrollingOuterX = !Browser.tryUnfloatToolbar(); this.bv.resumeRendering(); return (dx != 0) || (dy != 0); }, dragMove: function dragMove(dx, dy, scroller, doReturnDX) { let outrv = 0; if (this._panFrame(dx, dy)) // first see if we need to adjust internal IFRAME/FRAME return true; //dump("enter drag move\n"); if (this.scrollingOuterX) { let odx = 0; let ody = 0; let snappedX = false; if (dx > 0) { let contentleft = Math.round(Browser.contentScrollbox.getBoundingClientRect().left); odx = (contentleft > 0) ? Math.min(contentleft, dx) : dx; snappedX = (contentleft > 0 && odx == contentleft); } else if (dx < 0) { let contentright = Math.round(Browser.contentScrollbox.getBoundingClientRect().right); let w = window.innerWidth; odx = (contentright < w) ? Math.max(contentright - w, dx) : dx; snappedX = (contentright < w && odx == (contentright - w)); } //dump(" odx, ody, snappedX: " + odx + " " + ody + " " + snappedX + "\n"); if (odx) { outrv = this.outerDragMove(odx, ody, Browser.controlsScrollboxScroller, doReturnDX); } if (snappedX || ody != dy) { if (snappedX) this.scrollingOuterX = false; dx -= odx; dy -= ody; } else { return outrv; } } //dump(" scrolling inner x\n"); this.bv.onBeforeVisibleMove(dx, dy); let [x0, y0] = Browser.getScrollboxPosition(scroller); scroller.scrollBy(dx, dy); let [x1, y1] = Browser.getScrollboxPosition(scroller); let realdx = x1 - x0; let realdy = y1 - y0; this.bv.onAfterVisibleMove(); //dump(" dx, realdx: " + dx + " " + realdx + "\n"); if (realdx != dx) { let restdx = dx - realdx; this.scrollingOuterX = true; this.dragMove(restdx, 0, scroller, doReturnDX); } return (doReturnDX) ? (outrv + realdx) : (outrv || realdx != 0 || realdy != 0); }, outerDragMove: function outerDragMove(dx, dy, scroller, doReturnDX) { this.bv.onBeforeVisibleMove(dx, dy); Browser.tryFloatToolbar(dx, dy); let [x0, y0] = Browser.getScrollboxPosition(scroller); scroller.scrollBy(dx, dy); let [x1, y1] = Browser.getScrollboxPosition(scroller); let realdx = x1 - x0; let realdy = y1 - y0; this.bv.onAfterVisibleMove(); return (doReturnDX) ? realdx : (realdx != 0 || realdy != 0); }, _panFrame: function _panFrame(dx, dy) { if (this.draggedFrame === null) return false; if (dx == 0 && dy == 0) return true; let panned = false; let elem = this.draggedFrame; // top-level window will have itself as its parent, so stop // there to allow canvasbrowser/widgetstack to pan instead // of doing scrolling while (elem && elem !== elem.parent.document.defaultView) { let windowUtils = elem.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); let origX = {}, origY = {}; windowUtils.getScrollXY(false, origX, origY); elem.scrollBy(dx, dy); let newX = {}, newY = {}; windowUtils.getScrollXY(false, newX, newY); panned = (origX.value != newX.value) || (origY.value != newY.value); if (panned) { // get critical area to redraw after we move frame // NOTE: may need to rate limit these for performance this.bv.renderNow(); break; } elem = elem.parent.document.defaultView; } return panned; }, _targetIsContent: function _targetIsContent(target) { let tileBox = document.getElementById("tile-container"); while (target) { if (target === window) return false; if (target === tileBox) return true; target = target.parentNode; } return false; } }; function nsBrowserAccess() { } nsBrowserAccess.prototype = { QueryInterface: function(aIID) { if (aIID.equals(Ci.nsIBrowserDOMWindow) || aIID.equals(Ci.nsISupports)) return this; throw Components.results.NS_NOINTERFACE; }, openURI: function(aURI, aOpener, aWhere, aContext) { var isExternal = (aContext == Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL); if (isExternal && aURI && aURI.schemeIs("chrome")) { dump("use -chrome command-line option to load external chrome urls\n"); return null; } var loadflags = isExternal ? Ci.nsIWebNavigation.LOAD_FLAGS_FROM_EXTERNAL : Ci.nsIWebNavigation.LOAD_FLAGS_NONE; var location; if (aWhere == Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW) { switch (aContext) { case Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL : aWhere = gPrefService.getIntPref("browser.link.open_external"); break; default : // OPEN_NEW or an illegal value aWhere = gPrefService.getIntPref("browser.link.open_newwindow"); } } var newWindow; if (aWhere == Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW) { var url = aURI ? aURI.spec : "about:blank"; newWindow = openDialog("chrome://browser/content/browser.xul", "_blank", "all,dialog=no", url, null, null, null); } else { if (aWhere == Ci.nsIBrowserDOMWindow.OPEN_NEWTAB) newWindow = Browser.addTab("about:blank", true).browser.contentWindow; else newWindow = aOpener ? aOpener.top : browser.contentWindow; } try { var referrer; if (aURI) { if (aOpener) { location = aOpener.location; referrer = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService) .newURI(location, null, null); } newWindow.QueryInterface(Ci.nsIInterfaceRequestor) .getInterface(Ci.nsIWebNavigation) .loadURI(aURI.spec, loadflags, referrer, null, null); } newWindow.focus(); } catch(e) { } return newWindow; }, isTabContentWindow: function(aWindow) { return Browser.browsers.some(function (browser) browser.contentWindow == aWindow); } }; const BrowserSearch = { engines: null, _allEngines: [], get _currentEngines() { let doc = getBrowser().contentDocument; return this._allEngines.filter(function(element) element.doc === doc, this); }, addPageSearchEngine: function (aEngine, aDocument) { // Clean the engine referenced for document that didn't exist anymore let browsers = Browser.browsers; this._allEngines = this._allEngines.filter(function(element) { return browsers.some(function (browser) browser.contentDocument == element.doc); }, this); // Prevent duplicate if (!this._allEngines.some(function (e) { return (e.engine.title == aEngine.title) && (e.doc == aDocument); })) this._allEngines.push( {engine:aEngine, doc:aDocument}); }, updatePageSearchEngines: function() { // Check to see whether we've already added an engine with this title in // the search list let newEngines = this._currentEngines.filter(function(element) { return !this.engines.some(function (e) e.name == element.engine.title); }, this); let container = document.getElementById('search-container'); let buttons = container.getElementsByAttribute("class", "search-engine-button button-dark"); for (let i=0; i