LEFT | RIGHT |
1 /* | 1 /* |
2 * This Source Code is subject to the terms of the Mozilla Public License | 2 * This Source Code is subject to the terms of the Mozilla Public License |
3 * version 2.0 (the "License"). You can obtain a copy of the License at | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
5 */ | 5 */ |
6 | 6 |
7 /** | 7 /** |
8 * @module crawler | 8 * @module crawler |
9 */ | 9 */ |
10 | 10 |
11 Cu.import("resource://gre/modules/Services.jsm"); | 11 Cu.import("resource://gre/modules/Services.jsm"); |
12 Cu.import("resource://gre/modules/Task.jsm"); | 12 Cu.import("resource://gre/modules/Task.jsm"); |
13 Cu.import("resource://gre/modules/Promise.jsm"); | 13 Cu.import("resource://gre/modules/Promise.jsm"); |
14 | 14 |
15 function abprequire(module) | 15 function abprequire(module) |
16 { | 16 { |
17 let result = {}; | 17 let result = {}; |
18 result.wrappedJSObject = result; | 18 result.wrappedJSObject = result; |
19 Services.obs.notifyObservers(result, "adblockplus-require", module); | 19 Services.obs.notifyObservers(result, "adblockplus-require", module); |
20 return result.exports; | 20 return result.exports; |
21 } | 21 } |
22 | 22 |
23 let {RequestNotifier} = abprequire("requestNotifier"); | 23 let {RequestNotifier} = abprequire("requestNotifier"); |
24 let {FilterNotifier} = abprequire("filterNotifier"); | 24 let {FilterNotifier} = abprequire("filterNotifier"); |
25 let {FilterStorage} = abprequire("filterStorage"); | 25 let {FilterStorage} = abprequire("filterStorage"); |
26 | |
27 /** | |
28 * Creates a blank tab in the browser. | |
29 * | |
30 * @param {tabbrowser} browser | |
31 * The tabbed browser where the tab should be created. | |
32 * @return {Promise.<tab>} promise which resolves once the tab is fully initiali
zed. | |
33 */ | |
34 function createTab(browser) | |
35 { | |
36 let tab = browser.addTab("about:blank"); | |
37 if (tab.linkedBrowser.outerWindowID) | |
38 return Promise.resolve(tab); | |
39 return new Promise((resolve, reject) => | |
40 { | |
41 let onBrowserInit = (msg) => | |
42 { | |
43 // https://bugzilla.mozilla.org/show_bug.cgi?id=1256602#c1 | |
44 tab.linkedBrowser.messageManager.removeMessageListener("Browser:Init", onB
rowserInit); | |
45 resolve(tab); | |
46 }; | |
47 tab.linkedBrowser.messageManager.addMessageListener("Browser:Init", onBrowse
rInit); | |
48 }); | |
49 } | |
50 | 26 |
51 /** | 27 /** |
52 * Allocates tabs on request but not more than maxtabs at the same time. | 28 * Allocates tabs on request but not more than maxtabs at the same time. |
53 * | 29 * |
54 * @param {tabbrowser} browser | 30 * @param {tabbrowser} browser |
55 * The tabbed browser where tabs should be created | 31 * The tabbed browser where tabs should be created |
56 * @param {int} maxtabs | 32 * @param {int} maxtabs |
57 * The maximum number of tabs to be allocated | 33 * The maximum number of tabs to be allocated |
58 * @constructor | 34 * @constructor |
59 */ | 35 */ |
60 function TabAllocator(browser, maxtabs) | 36 function TabAllocator(browser, maxtabs) |
61 { | 37 { |
62 this._browser = browser; | 38 this._browser = browser; |
63 this._tabs = 0; | 39 this._tabs = 0; |
64 this._maxtabs = maxtabs; | 40 this._maxtabs = maxtabs; |
65 // The queue containing resolve functions of promises waiting for a tab. | 41 // The queue containing resolve functions of promises waiting for a tab. |
66 this._resolvers = []; | 42 this._resolvers = []; |
67 // Keep at least one tab alive to prevent browser from closing itself. | 43 // Keep at least one tab alive to prevent browser from closing itself. |
68 // That tab will be removed when the first tab is requested. | 44 this._tabKeepingWindowAlive = this._browser.tabs[0]; |
69 browser.removeAllTabsBut(browser.tabs[0]); | 45 this._browser.removeAllTabsBut(this._tabKeepingWindowAlive); |
70 } | 46 } |
71 TabAllocator.prototype = { | 47 TabAllocator.prototype = { |
| 48 _removeTabKeepingWindowAlive: function() |
| 49 { |
| 50 if (!this._tabKeepingWindowAlive) |
| 51 return; |
| 52 this._browser.removeTab(this._tabKeepingWindowAlive); |
| 53 delete this._tabKeepingWindowAlive; |
| 54 }, |
| 55 |
| 56 /** |
| 57 * Creates a blank tab in this._browser. |
| 58 * |
| 59 * @return {Promise.<tab>} promise which resolves once the tab is fully initia
lized. |
| 60 */ |
| 61 _createTab: function() |
| 62 { |
| 63 this._tabs++; |
| 64 let tab = this._browser.addTab("about:blank"); |
| 65 if (tab.linkedBrowser.outerWindowID) |
| 66 { |
| 67 this._removeTabKeepingWindowAlive(); |
| 68 return Promise.resolve(tab); |
| 69 } |
| 70 return new Promise((resolve, reject) => |
| 71 { |
| 72 let onBrowserInit = (msg) => |
| 73 { |
| 74 tab.linkedBrowser.messageManager.removeMessageListener("Browser:Init", o
nBrowserInit); |
| 75 this._removeTabKeepingWindowAlive(); |
| 76 resolve(tab); |
| 77 }; |
| 78 // "Browser:Init" message is sent once the browser is ready, see |
| 79 // https://bugzil.la/1256602#c1 |
| 80 tab.linkedBrowser.messageManager.addMessageListener("Browser:Init", onBrow
serInit); |
| 81 }); |
| 82 }, |
| 83 |
72 /** | 84 /** |
73 * Returns a promise that will resolve into a tab once a tab is allocated. | 85 * Returns a promise that will resolve into a tab once a tab is allocated. |
74 * The tab cannot be used by other tasks until releaseTab() is called. | 86 * The tab cannot be used by other tasks until releaseTab() is called. |
75 * | 87 * |
76 * @result {Promise.<tab>} | 88 * @result {Promise.<tab>} |
77 */ | 89 */ |
78 getTab: function() | 90 getTab: function() |
79 { | 91 { |
80 if (this._tabs < this._maxtabs) | 92 if (this._tabs < this._maxtabs) |
81 { | 93 return this._createTab(); |
82 let tab = createTab(this._browser); | 94 return new Promise((resolve, reject) => this._resolvers.push(resolve)); |
83 // Close initial tab, we don't need it anymore. | |
84 if (this._tabs == 0) | |
85 this._browser.removeTab(this._browser.tabs[0]); | |
86 this._tabs++; | |
87 return tab; | |
88 } | |
89 return new Promise((resolve, reject) => | |
90 { | |
91 this._resolvers.push(resolve); | |
92 }); | |
93 }, | 95 }, |
94 | 96 |
95 /** | 97 /** |
96 * Adds a tab back to the pool so that it can be used by other tasks. | 98 * Adds a tab back to the pool so that it can be used by other tasks. |
97 * | 99 * |
98 * @param {tab} tab | 100 * @param {tab} tab |
99 */ | 101 */ |
100 releaseTab: function(tab) | 102 releaseTab: function(tab) |
101 { | 103 { |
102 let browser = tab.parentNode.tabbrowser; | 104 // If we are about to close last tab don't close it immediately to keep |
103 browser.removeTab(tab); | 105 // the window alive. It will be closed when a new tab is created. |
104 | 106 if (this._tabs > 1) |
105 if (this._resolvers.length) | 107 this._browser.removeTab(tab); |
106 this._resolvers.shift()(createTab(this._browser)); | |
107 else | 108 else |
108 this._tabs--; | 109 { |
109 } | 110 // navigate away from early opened URL |
| 111 tab.linkedBrowser.loadURI('about:blank', null, null); |
| 112 this._tabKeepingWindowAlive = tab; |
| 113 } |
| 114 |
| 115 this._tabs--; |
| 116 if (this._resolvers.length && this._tabs < this._maxtabs) |
| 117 { |
| 118 this._resolvers.shift()(this._createTab()); |
| 119 } |
| 120 }, |
110 }; | 121 }; |
111 | 122 |
112 /** | 123 /** |
113 * Observes page loads in a particular tabbed browser. | 124 * Observes page loads in a particular tabbed browser. |
114 * | 125 * |
115 * @param {tabbrowser} browser | 126 * @param {tabbrowser} browser |
116 * The tabbed browser to be observed | 127 * The tabbed browser to be observed |
117 * @param {int} timeout | 128 * @param {int} timeout |
118 * Load timeout in milliseconds | 129 * Load timeout in milliseconds |
119 * @constructor | 130 * @constructor |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 | 394 |
384 function reportException(e) | 395 function reportException(e) |
385 { | 396 { |
386 let stack = ""; | 397 let stack = ""; |
387 if (e && typeof e == "object" && "stack" in e) | 398 if (e && typeof e == "object" && "stack" in e) |
388 stack = e.stack + "\n"; | 399 stack = e.stack + "\n"; |
389 | 400 |
390 Cu.reportError(e); | 401 Cu.reportError(e); |
391 dump(e + "\n" + stack + "\n"); | 402 dump(e + "\n" + stack + "\n"); |
392 } | 403 } |
LEFT | RIGHT |