Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/crawler.js

Issue 29338442: Issue 3815 - Fix TabAllocator. Now it returns tab with initialized outerWindowID (Closed)
Patch Set: address comments Created March 16, 2016, 3:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 26
27 /** 27 /**
28 * Creates a pool of tabs and allocates them to tasks on request. 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
51 /**
52 * Allocates tabs on request but not more than maxtabs at the same time.
29 * 53 *
30 * @param {tabbrowser} browser 54 * @param {tabbrowser} browser
31 * The tabbed browser where tabs should be created 55 * The tabbed browser where tabs should be created
32 * @param {int} maxtabs 56 * @param {int} maxtabs
33 * The maximum number of tabs to be allocated 57 * The maximum number of tabs to be allocated
34 * @constructor 58 * @constructor
35 */ 59 */
36 function TabAllocator(browser, maxtabs) 60 function TabAllocator(browser, maxtabs)
37 { 61 {
62 this._browser = browser;
63 this._tabs = 0;
64 this._maxtabs = maxtabs;
65 // The queue containing resolve functions of promises waiting for a tab.
66 this._resolvers = [];
67 // Keep at least one tab alive to prevent browser from closing itself.
68 // That tab will be removed when the first tab is requested.
38 browser.removeAllTabsBut(browser.tabs[0]) 69 browser.removeAllTabsBut(browser.tabs[0])
39
40 this._tabs = [];
41 for (let i = 0; i < maxtabs; i++)
42 this._tabs.push(browser.addTab("about:blank"));
43
44 browser.removeTab(browser.tabs[0]);
45
46 this._deferred = [];
47 } 70 }
48 TabAllocator.prototype = { 71 TabAllocator.prototype = {
49 /** 72 /**
50 * Returns a promise that will resolve into a tab once a tab can be allocated. 73 * Returns a promise that will resolve into a tab once a tab is allocated.
51 * The tab cannot be used by other tasks until releaseTab() is called. 74 * The tab cannot be used by other tasks until releaseTab() is called.
52 * 75 *
53 * @result {Promise} 76 * @result {Promise.<tab>}
54 */ 77 */
55 getTab: function() 78 getTab: function()
56 { 79 {
57 if (this._tabs.length) 80 if (this._tabs < this._maxtabs)
58 return this._tabs.shift();
59 else
60 { 81 {
61 let deferred = Promise.defer(); 82 let tab = createTab(this._browser);
62 this._deferred.push(deferred); 83 // Close initial tab, we don't need it anymore.
63 return deferred.promise; 84 if (this._tabs == 0)
85 this._browser.removeTab(this._browser.tabs[0]);
86 this._tabs++;
87 return tab;
64 } 88 }
89 return new Promise((resolve, reject) =>
90 {
91 this._resolvers.push(resolve);
92 });
65 }, 93 },
66 94
67 /** 95 /**
68 * Adds a tab back to the pool so that it can be used by other tasks. 96 * Adds a tab back to the pool so that it can be used by other tasks.
69 * 97 *
70 * @param {tab} tab 98 * @param {tab} tab
71 */ 99 */
72 releaseTab: function(tab) 100 releaseTab: function(tab)
73 { 101 {
74 let browser = tab.parentNode.tabbrowser; 102 let browser = tab.parentNode.tabbrowser;
75 browser.removeTab(tab); 103 browser.removeTab(tab);
76 tab = browser.addTab("about:blank");
77 104
78 if (this._deferred.length) 105 if (this._resolvers.length)
79 this._deferred.shift().resolve(tab); 106 this._resolvers.shift()(createTab(this._browser));
80 else 107 else
81 this._tabs.push(tab); 108 this._tabs--;
82 } 109 }
83 }; 110 };
84 111
85 /** 112 /**
86 * Observes page loads in a particular tabbed browser. 113 * Observes page loads in a particular tabbed browser.
87 * 114 *
88 * @param {tabbrowser} browser 115 * @param {tabbrowser} browser
89 * The tabbed browser to be observed 116 * The tabbed browser to be observed
90 * @param {int} timeout 117 * @param {int} timeout
91 * Load timeout in milliseconds 118 * Load timeout in milliseconds
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 resolve(); 251 resolve();
225 } 252 }
226 }; 253 };
227 FilterNotifier.addListener(onFiltersLoaded); 254 FilterNotifier.addListener(onFiltersLoaded);
228 }).then(() => crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone)) 255 }).then(() => crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone))
229 .catch(reportException); 256 .catch(reportException);
230 } 257 }
231 exports.run = run; 258 exports.run = run;
232 259
233 /** 260 /**
234 * Spawns a {Task} task to crawl each url from `urls` argument and calls 261 * Spawns a {Task} task to crawl each url from urls argument and calls
235 * `onDone` when all tasks are finished. 262 * onDone when all tasks are finished.
236 * @param {Window} window 263 * @param {Window} window
237 * The browser window we're operating in 264 * The browser window we're operating in
238 * @param {String[]} urls 265 * @param {String[]} urls
239 * URLs to be crawled 266 * URLs to be crawled
240 * @param {int} timeout 267 * @param {int} timeout
241 * Load timeout in milliseconds 268 * Load timeout in milliseconds
242 * @param {int} maxtabs 269 * @param {int} maxtabs
243 * Maximum number of tabs to be opened 270 * Maximum number of tabs to be opened
244 * @param {String} targetURL 271 * @param {String} targetURL
245 * URL that should receive the results 272 * URL that should receive the results
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 383
357 function reportException(e) 384 function reportException(e)
358 { 385 {
359 let stack = ""; 386 let stack = "";
360 if (e && typeof e == "object" && "stack" in e) 387 if (e && typeof e == "object" && "stack" in e)
361 stack = e.stack + "\n"; 388 stack = e.stack + "\n";
362 389
363 Cu.reportError(e); 390 Cu.reportError(e);
364 dump(e + "\n" + stack + "\n"); 391 dump(e + "\n" + stack + "\n");
365 } 392 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld