Mozilla Cross-Reference mozilla-central
mozilla/ browser/ base/ content/ browser-thumbnails.js
Hg Log
Hg Blame
Diff file
Raw file
view using tree:
1 #ifdef 0
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #endif
6 
7 /**
8  * Keeps thumbnails of open web pages up-to-date.
9  */
10 let gBrowserThumbnails = {
11   /**
12    * Pref that controls whether we can store SSL content on disk
13    */
14   PREF_DISK_CACHE_SSL: "browser.cache.disk_cache_ssl",
15 
16   _captureDelayMS: 1000,
17 
18   /**
19    * Used to keep track of disk_cache_ssl preference
20    */
21   _sslDiskCacheEnabled: null,
22 
23   /**
24    * Map of capture() timeouts assigned to their browsers.
25    */
26   _timeouts: null,
27 
28   /**
29    * List of tab events we want to listen for.
30    */
31   _tabEvents: ["TabClose", "TabSelect"],
32 
33   init: function Thumbnails_init() {
34     PageThumbs.addExpirationFilter(this);
35     gBrowser.addTabsProgressListener(this);
36     Services.prefs.addObserver(this.PREF_DISK_CACHE_SSL, this, false);
37 
38     this._sslDiskCacheEnabled =
39       Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
40 
41     this._tabEvents.forEach(function (aEvent) {
42       gBrowser.tabContainer.addEventListener(aEvent, this, false);
43     }, this);
44 
45     this._timeouts = new WeakMap();
46   },
47 
48   uninit: function Thumbnails_uninit() {
49     PageThumbs.removeExpirationFilter(this);
50     gBrowser.removeTabsProgressListener(this);
51     Services.prefs.removeObserver(this.PREF_DISK_CACHE_SSL, this);
52 
53     this._tabEvents.forEach(function (aEvent) {
54       gBrowser.tabContainer.removeEventListener(aEvent, this, false);
55     }, this);
56   },
57 
58   handleEvent: function Thumbnails_handleEvent(aEvent) {
59     switch (aEvent.type) {
60       case "scroll":
61         let browser = aEvent.currentTarget;
62         if (this._timeouts.has(browser))
63           this._delayedCapture(browser);
64         break;
65       case "TabSelect":
66         this._delayedCapture(aEvent.target.linkedBrowser);
67         break;
68       case "TabClose": {
69         this._clearTimeout(aEvent.target.linkedBrowser);
70         break;
71       }
72     }
73   },
74 
75   observe: function Thumbnails_observe() {
76     this._sslDiskCacheEnabled =
77       Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
78   },
79 
80   filterForThumbnailExpiration:
81   function Thumbnails_filterForThumbnailExpiration(aCallback) {
82     aCallback(this._topSiteURLs);
83   },
84 
85   /**
86    * State change progress listener for all tabs.
87    */
88   onStateChange: function Thumbnails_onStateChange(aBrowser, aWebProgress,
89                                                    aRequest, aStateFlags, aStatus) {
90     if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
91         aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK)
92       this._delayedCapture(aBrowser);
93   },
94 
95   _capture: function Thumbnails_capture(aBrowser) {
96     // Only capture about:newtab top sites.
97     if (this._topSiteURLs.indexOf(aBrowser.currentURI.spec) == -1)
98       return;
99     this._shouldCapture(aBrowser, function (aResult) {
100       if (aResult) {
101         PageThumbs.captureAndStoreIfStale(aBrowser);
102       }
103     });
104   },
105 
106   _delayedCapture: function Thumbnails_delayedCapture(aBrowser) {
107     if (this._timeouts.has(aBrowser))
108       clearTimeout(this._timeouts.get(aBrowser));
109     else
110       aBrowser.addEventListener("scroll", this, true);
111 
112     let timeout = setTimeout(function () {
113       this._clearTimeout(aBrowser);
114       this._capture(aBrowser);
115     }.bind(this), this._captureDelayMS);
116 
117     this._timeouts.set(aBrowser, timeout);
118   },
119 
120   _shouldCapture: function Thumbnails_shouldCapture(aBrowser, aCallback) {
121     // Capture only if it's the currently selected tab.
122     if (aBrowser != gBrowser.selectedBrowser) {
123       aCallback(false);
124       return;
125     }
126     PageThumbs.shouldStoreThumbnail(aBrowser, aCallback);
127   },
128 
129   get _topSiteURLs() {
130     return NewTabUtils.links.getLinks().reduce((urls, link) => {
131       if (link)
132         urls.push(link.url);
133       return urls;
134     }, []);
135   },
136 
137   _clearTimeout: function Thumbnails_clearTimeout(aBrowser) {
138     if (this._timeouts.has(aBrowser)) {
139       aBrowser.removeEventListener("scroll", this, false);
140       clearTimeout(this._timeouts.get(aBrowser));
141       this._timeouts.delete(aBrowser);
142     }
143   }
144 };
145 
view http://hg.mozilla.org/mozilla-central/rev/ /browser/base/content/browser-thumbnails.js