browser/base/content/browser-thumbnails.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #ifdef 0
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #endif
michael@0 6
michael@0 7 Cu.import("resource://gre/modules/NewTabUtils.jsm");
michael@0 8
michael@0 9 /**
michael@0 10 * Keeps thumbnails of open web pages up-to-date.
michael@0 11 */
michael@0 12 let gBrowserThumbnails = {
michael@0 13 /**
michael@0 14 * Pref that controls whether we can store SSL content on disk
michael@0 15 */
michael@0 16 PREF_DISK_CACHE_SSL: "browser.cache.disk_cache_ssl",
michael@0 17
michael@0 18 _captureDelayMS: 1000,
michael@0 19
michael@0 20 /**
michael@0 21 * Used to keep track of disk_cache_ssl preference
michael@0 22 */
michael@0 23 _sslDiskCacheEnabled: null,
michael@0 24
michael@0 25 /**
michael@0 26 * Map of capture() timeouts assigned to their browsers.
michael@0 27 */
michael@0 28 _timeouts: null,
michael@0 29
michael@0 30 /**
michael@0 31 * List of tab events we want to listen for.
michael@0 32 */
michael@0 33 _tabEvents: ["TabClose", "TabSelect"],
michael@0 34
michael@0 35 init: function Thumbnails_init() {
michael@0 36 // Bug 863512 - Make page thumbnails work in electrolysis
michael@0 37 if (gMultiProcessBrowser)
michael@0 38 return;
michael@0 39
michael@0 40 PageThumbs.addExpirationFilter(this);
michael@0 41 gBrowser.addTabsProgressListener(this);
michael@0 42 Services.prefs.addObserver(this.PREF_DISK_CACHE_SSL, this, false);
michael@0 43
michael@0 44 this._sslDiskCacheEnabled =
michael@0 45 Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
michael@0 46
michael@0 47 this._tabEvents.forEach(function (aEvent) {
michael@0 48 gBrowser.tabContainer.addEventListener(aEvent, this, false);
michael@0 49 }, this);
michael@0 50
michael@0 51 this._timeouts = new WeakMap();
michael@0 52 },
michael@0 53
michael@0 54 uninit: function Thumbnails_uninit() {
michael@0 55 // Bug 863512 - Make page thumbnails work in electrolysis
michael@0 56 if (gMultiProcessBrowser)
michael@0 57 return;
michael@0 58
michael@0 59 PageThumbs.removeExpirationFilter(this);
michael@0 60 gBrowser.removeTabsProgressListener(this);
michael@0 61 Services.prefs.removeObserver(this.PREF_DISK_CACHE_SSL, this);
michael@0 62
michael@0 63 this._tabEvents.forEach(function (aEvent) {
michael@0 64 gBrowser.tabContainer.removeEventListener(aEvent, this, false);
michael@0 65 }, this);
michael@0 66 },
michael@0 67
michael@0 68 handleEvent: function Thumbnails_handleEvent(aEvent) {
michael@0 69 switch (aEvent.type) {
michael@0 70 case "scroll":
michael@0 71 let browser = aEvent.currentTarget;
michael@0 72 if (this._timeouts.has(browser))
michael@0 73 this._delayedCapture(browser);
michael@0 74 break;
michael@0 75 case "TabSelect":
michael@0 76 this._delayedCapture(aEvent.target.linkedBrowser);
michael@0 77 break;
michael@0 78 case "TabClose": {
michael@0 79 this._clearTimeout(aEvent.target.linkedBrowser);
michael@0 80 break;
michael@0 81 }
michael@0 82 }
michael@0 83 },
michael@0 84
michael@0 85 observe: function Thumbnails_observe() {
michael@0 86 this._sslDiskCacheEnabled =
michael@0 87 Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
michael@0 88 },
michael@0 89
michael@0 90 filterForThumbnailExpiration:
michael@0 91 function Thumbnails_filterForThumbnailExpiration(aCallback) {
michael@0 92 aCallback(this._topSiteURLs);
michael@0 93 },
michael@0 94
michael@0 95 /**
michael@0 96 * State change progress listener for all tabs.
michael@0 97 */
michael@0 98 onStateChange: function Thumbnails_onStateChange(aBrowser, aWebProgress,
michael@0 99 aRequest, aStateFlags, aStatus) {
michael@0 100 if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
michael@0 101 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK)
michael@0 102 this._delayedCapture(aBrowser);
michael@0 103 },
michael@0 104
michael@0 105 _capture: function Thumbnails_capture(aBrowser) {
michael@0 106 if (this._shouldCapture(aBrowser))
michael@0 107 PageThumbs.captureAndStoreIfStale(aBrowser);
michael@0 108 },
michael@0 109
michael@0 110 _delayedCapture: function Thumbnails_delayedCapture(aBrowser) {
michael@0 111 if (this._timeouts.has(aBrowser))
michael@0 112 clearTimeout(this._timeouts.get(aBrowser));
michael@0 113 else
michael@0 114 aBrowser.addEventListener("scroll", this, true);
michael@0 115
michael@0 116 let timeout = setTimeout(function () {
michael@0 117 this._clearTimeout(aBrowser);
michael@0 118 this._capture(aBrowser);
michael@0 119 }.bind(this), this._captureDelayMS);
michael@0 120
michael@0 121 this._timeouts.set(aBrowser, timeout);
michael@0 122 },
michael@0 123
michael@0 124 _shouldCapture: function Thumbnails_shouldCapture(aBrowser) {
michael@0 125 // Capture only if it's the currently selected tab.
michael@0 126 if (aBrowser != gBrowser.selectedBrowser)
michael@0 127 return false;
michael@0 128
michael@0 129 // Only capture about:newtab top sites.
michael@0 130 if (this._topSiteURLs.indexOf(aBrowser.currentURI.spec) < 0)
michael@0 131 return false;
michael@0 132
michael@0 133 // Don't capture in per-window private browsing mode.
michael@0 134 if (PrivateBrowsingUtils.isWindowPrivate(window))
michael@0 135 return false;
michael@0 136
michael@0 137 let doc = aBrowser.contentDocument;
michael@0 138
michael@0 139 // FIXME Bug 720575 - Don't capture thumbnails for SVG or XML documents as
michael@0 140 // that currently regresses Talos SVG tests.
michael@0 141 if (doc instanceof SVGDocument || doc instanceof XMLDocument)
michael@0 142 return false;
michael@0 143
michael@0 144 // There's no point in taking screenshot of loading pages.
michael@0 145 if (aBrowser.docShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE)
michael@0 146 return false;
michael@0 147
michael@0 148 // Don't take screenshots of about: pages.
michael@0 149 if (aBrowser.currentURI.schemeIs("about"))
michael@0 150 return false;
michael@0 151
michael@0 152 let channel = aBrowser.docShell.currentDocumentChannel;
michael@0 153
michael@0 154 // No valid document channel. We shouldn't take a screenshot.
michael@0 155 if (!channel)
michael@0 156 return false;
michael@0 157
michael@0 158 // Don't take screenshots of internally redirecting about: pages.
michael@0 159 // This includes error pages.
michael@0 160 let uri = channel.originalURI;
michael@0 161 if (uri.schemeIs("about"))
michael@0 162 return false;
michael@0 163
michael@0 164 let httpChannel;
michael@0 165 try {
michael@0 166 httpChannel = channel.QueryInterface(Ci.nsIHttpChannel);
michael@0 167 } catch (e) { /* Not an HTTP channel. */ }
michael@0 168
michael@0 169 if (httpChannel) {
michael@0 170 // Continue only if we have a 2xx status code.
michael@0 171 try {
michael@0 172 if (Math.floor(httpChannel.responseStatus / 100) != 2)
michael@0 173 return false;
michael@0 174 } catch (e) {
michael@0 175 // Can't get response information from the httpChannel
michael@0 176 // because mResponseHead is not available.
michael@0 177 return false;
michael@0 178 }
michael@0 179
michael@0 180 // Cache-Control: no-store.
michael@0 181 if (httpChannel.isNoStoreResponse())
michael@0 182 return false;
michael@0 183
michael@0 184 // Don't capture HTTPS pages unless the user explicitly enabled it.
michael@0 185 if (uri.schemeIs("https") && !this._sslDiskCacheEnabled)
michael@0 186 return false;
michael@0 187 }
michael@0 188
michael@0 189 return true;
michael@0 190 },
michael@0 191
michael@0 192 get _topSiteURLs() {
michael@0 193 return NewTabUtils.links.getLinks().reduce((urls, link) => {
michael@0 194 if (link)
michael@0 195 urls.push(link.url);
michael@0 196 return urls;
michael@0 197 }, []);
michael@0 198 },
michael@0 199
michael@0 200 _clearTimeout: function Thumbnails_clearTimeout(aBrowser) {
michael@0 201 if (this._timeouts.has(aBrowser)) {
michael@0 202 aBrowser.removeEventListener("scroll", this, false);
michael@0 203 clearTimeout(this._timeouts.get(aBrowser));
michael@0 204 this._timeouts.delete(aBrowser);
michael@0 205 }
michael@0 206 }
michael@0 207 };

mercurial