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: Created March 16, 2016, 2:18 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");
Wladimir Palant 2016/03/16 14:52:52 Please remove this if you don't use Promise.defer(
sergei 2016/03/16 15:47:00 Currently defer is still used by LoadListener.
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 * Allocates a new tab "about:blank" in the `browser`.
Wladimir Palant 2016/03/16 14:52:52 Please don't use backticks, these have no special
sergei 2016/03/16 15:47:00 Done.
29 * The method returns a {Promise} promise which is resolved with the `tab`
Wladimir Palant 2016/03/16 14:52:52 "{Promise} promise" => "promise" please, the type
sergei 2016/03/16 15:47:00 Done.
30 * when `outerWindowID` is already initialized.
Wladimir Palant 2016/03/16 14:52:52 Too many implementation details here. "which is re
sergei 2016/03/16 15:47:00 Done.
31 *
32 * See: https://bugzilla.mozilla.org/show_bug.cgi?id=1256602#c1
Wladimir Palant 2016/03/16 14:52:52 This belongs into a comment above the actual imple
sergei 2016/03/16 15:47:00 Done.
29 * 33 *
30 * @param {tabbrowser} browser 34 * @param {tabbrowser} browser
31 * The tabbed browser where tabs should be created 35 * The tabbed browser where tabs should be created
Wladimir Palant 2016/03/16 14:52:52 "tabs" => "the tab"
sergei 2016/03/16 15:47:01 Done.
36 * @return {Promise} promise which will be resolved with the tab.
Wladimir Palant 2016/03/16 14:52:52 Please use the more specific Promise.<tab> as type
sergei 2016/03/16 15:47:00 Done.
37 */
38 function createTab(browser)
39 {
40 let tab = browser.addTab("about:blank");
41 if (tab.linkedBrowser.outerWindowID)
42 return Promise.resolve(tab);
43 return new Promise((resolve, reject)=>
Wladimir Palant 2016/03/16 14:52:52 Nit: Space before => please.
sergei 2016/03/16 15:47:00 Done.
44 {
45 let onBrowserInit = (msg) =>
46 {
47 tab.linkedBrowser.messageManager.removeMessageListener("Browser:Init", onB rowserInit);
48 resolve(tab);
49 };
50 tab.linkedBrowser.messageManager.addMessageListener("Browser:Init", onBrowse rInit);
51 });
52 }
53
54 /**
55 * Allocates tabs on request but not more than `maxtabs` at the same time.
56 *
57 * @param {tabbrowser} browser
58 * The tabbed browser where tabs should be created
32 * @param {int} maxtabs 59 * @param {int} maxtabs
33 * The maximum number of tabs to be allocated 60 * The maximum number of tabs to be allocated
34 * @constructor 61 * @constructor
35 */ 62 */
36 function TabAllocator(browser, maxtabs) 63 function TabAllocator(browser, maxtabs)
37 { 64 {
65 this._browser = browser;
66 this._tabs = 0;
67 this._maxtabs = maxtabs;
68 // the array of `resolve` functions of {Promise} promises returned by `getTab` .
Wladimir Palant 2016/03/16 14:52:52 This is a code comment, it's intended to be read b
sergei 2016/03/16 15:47:01 Sounds good!
69 this._resolvers = [];
70 // Keep at least one tab alive to prevent browser from closing of it self.
Wladimir Palant 2016/03/16 14:52:52 "browser from closing of it self" => "the browser
sergei 2016/03/16 15:47:00 Done.
71 // That tab will be removed when the first tab is requested.
38 browser.removeAllTabsBut(browser.tabs[0]) 72 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 } 73 }
48 TabAllocator.prototype = { 74 TabAllocator.prototype = {
49 /** 75 /**
50 * Returns a promise that will resolve into a tab once a tab can be allocated. 76 * 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. 77 * The tab cannot be used by other tasks until releaseTab() is called.
52 * 78 *
53 * @result {Promise} 79 * @result {Promise}
Wladimir Palant 2016/03/16 14:52:52 Nit: Promise.<tab> here as well.
54 */ 80 */
55 getTab: function() 81 getTab: function()
56 { 82 {
57 if (this._tabs.length) 83 if (this._tabs < this._maxtabs)
58 return this._tabs.shift();
59 else
60 { 84 {
61 let deferred = Promise.defer(); 85 let tab = createTab(this._browser);
62 this._deferred.push(deferred); 86 // close initial tab, we don't need it anymore.
Wladimir Palant 2016/03/16 14:52:52 Nit: Please start sentences with a capital letter.
63 return deferred.promise; 87 if (this._tabs == 0)
88 this._browser.removeTab(this._browser.tabs[0]);
89 this._tabs++;
90 return tab;
64 } 91 }
92 return new Promise((resolve, reject) =>
93 {
94 this._resolvers.push(resolve);
95 });
65 }, 96 },
66 97
67 /** 98 /**
68 * Adds a tab back to the pool so that it can be used by other tasks. 99 * Adds a tab back to the pool so that it can be used by other tasks.
69 * 100 *
70 * @param {tab} tab 101 * @param {tab} tab
71 */ 102 */
72 releaseTab: function(tab) 103 releaseTab: function(tab)
73 { 104 {
74 let browser = tab.parentNode.tabbrowser; 105 let browser = tab.parentNode.tabbrowser;
75 browser.removeTab(tab); 106 browser.removeTab(tab);
76 tab = browser.addTab("about:blank");
77 107
78 if (this._deferred.length) 108 if (this._resolvers.length)
79 this._deferred.shift().resolve(tab); 109 this._resolvers.shift()(createTab(this._browser));
80 else 110 else
81 this._tabs.push(tab); 111 this._tabs--;
Wladimir Palant 2016/03/16 14:52:52 Now you have the problem that in the theoretical s
sergei 2016/03/16 15:47:00 Yes. However I think it should be addressed at ano
sergei 2016/04/11 15:20:22 I have addressed that issue. BTW, having an array
82 } 112 }
83 }; 113 };
84 114
85 /** 115 /**
86 * Observes page loads in a particular tabbed browser. 116 * Observes page loads in a particular tabbed browser.
87 * 117 *
88 * @param {tabbrowser} browser 118 * @param {tabbrowser} browser
89 * The tabbed browser to be observed 119 * The tabbed browser to be observed
90 * @param {int} timeout 120 * @param {int} timeout
91 * Load timeout in milliseconds 121 * Load timeout in milliseconds
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 386
357 function reportException(e) 387 function reportException(e)
358 { 388 {
359 let stack = ""; 389 let stack = "";
360 if (e && typeof e == "object" && "stack" in e) 390 if (e && typeof e == "object" && "stack" in e)
361 stack = e.stack + "\n"; 391 stack = e.stack + "\n";
362 392
363 Cu.reportError(e); 393 Cu.reportError(e);
364 dump(e + "\n" + stack + "\n"); 394 dump(e + "\n" + stack + "\n");
365 } 395 }
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