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

Side by Side Diff: lib/crawler.js

Issue 29338153: Issue 3780 - wait for the loading of filters and only afterwards start to fetch pages (Closed)
Patch Set: remove onFiltersLoaded listener and fit 80 chars Created March 15, 2016, 2:57 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 24 let {FilterNotifier} = abprequire("filterNotifier");
25 let {FilterStorage} = abprequire("filterStorage");
25 26
26 /** 27 /**
27 * Creates a pool of tabs and allocates them to tasks on request. 28 * Creates a pool of tabs and allocates them to tasks on request.
28 * 29 *
29 * @param {tabbrowser} browser 30 * @param {tabbrowser} browser
30 * The tabbed browser where tabs should be created 31 * The tabbed browser where tabs should be created
31 * @param {int} maxtabs 32 * @param {int} maxtabs
32 * The maximum number of tabs to be allocated 33 * The maximum number of tabs to be allocated
33 * @constructor 34 * @constructor
34 */ 35 */
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 }; 191 };
191 192
192 /** 193 /**
193 * Starts the crawling session. The crawler opens each URL in a tab and stores 194 * Starts the crawling session. The crawler opens each URL in a tab and stores
194 * the results. 195 * the results.
195 * 196 *
196 * @param {Window} window 197 * @param {Window} window
197 * The browser window we're operating in 198 * The browser window we're operating in
198 * @param {String[]} urls 199 * @param {String[]} urls
199 * URLs to be crawled 200 * URLs to be crawled
200 * @param {int} number_of_tabs 201 * @param {int} timeout
202 * Load timeout in milliseconds
203 * @param {int} maxtabs
201 * Maximum number of tabs to be opened 204 * Maximum number of tabs to be opened
202 * @param {String} targetURL 205 * @param {String} targetURL
203 * URL that should receive the results 206 * URL that should receive the results
207 * @param {Function} onDone
208 * The callback which is called after finishing of crawling of all URLs.
204 */ 209 */
205 function run(window, urls, timeout, maxtabs, targetURL, onDone) 210 function run(window, urls, timeout, maxtabs, targetURL, onDone)
206 { 211 {
212 new Promise((resolve, reject) =>
213 {
214 if (FilterStorage.subscriptions.length > 0)
215 {
216 resolve();
217 return;
218 }
219 let onFiltersLoaded = (action, item, newValue, oldValue) =>
220 {
221 if (action == "load")
222 {
223 FilterNotifier.removeListener(onFiltersLoaded);
224 resolve();
225 }
226 };
227 FilterNotifier.addListener(onFiltersLoaded);
228 }).then(() => crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone))
229 .catch(reportException);
230 }
231 exports.run = run;
232
233 /**
234 * Spawns a {Task} task to crawl each url from `urls` argument and calls
235 * `onDone` when all tasks are finished.
236 * @param {Window} window
237 * The browser window we're operating in
238 * @param {String[]} urls
239 * URLs to be crawled
240 * @param {int} timeout
241 * Load timeout in milliseconds
242 * @param {int} maxtabs
243 * Maximum number of tabs to be opened
244 * @param {String} targetURL
245 * URL that should receive the results
246 * @param {Function} onDone
247 * The callback which is called after finishing of all tasks.
248 */
249 function crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone)
250 {
207 let tabAllocator = new TabAllocator(window.getBrowser(), maxtabs); 251 let tabAllocator = new TabAllocator(window.getBrowser(), maxtabs);
208 let loadListener = new LoadListener(window.getBrowser(), timeout); 252 let loadListener = new LoadListener(window.getBrowser(), timeout);
209 let running = 0; 253 let running = 0;
210 let windowCloser = new WindowCloser(); 254 let windowCloser = new WindowCloser();
211 let taskDone = function() 255 let taskDone = function()
212 { 256 {
213 running--; 257 running--;
214 if (running <= 0) 258 if (running <= 0)
215 { 259 {
216 loadListener.stop(); 260 loadListener.stop();
(...skipping 21 matching lines...) Expand all
238 request.addEventListener("load", taskDone, false); 282 request.addEventListener("load", taskDone, false);
239 request.addEventListener("error", taskDone, false); 283 request.addEventListener("error", taskDone, false);
240 request.send(JSON.stringify({ 284 request.send(JSON.stringify({
241 url: url, 285 url: url,
242 startTime: Date.now(), 286 startTime: Date.now(),
243 error: String(exception) 287 error: String(exception)
244 })); 288 }));
245 }.bind(null, url)); 289 }.bind(null, url));
246 } 290 }
247 } 291 }
248 exports.run = run;
249 292
250 /** 293 /**
251 * Crawls a URL. This is a generator meant to be used via a Task object. 294 * Crawls a URL. This is a generator meant to be used via a Task object.
252 * 295 *
253 * @param {String} url 296 * @param {String} url
254 * @param {TabAllocator} tabAllocator 297 * @param {TabAllocator} tabAllocator
255 * @param {loadListener} loadListener 298 * @param {loadListener} loadListener
256 * @result {Object} 299 * @result {Object}
257 * Crawling result 300 * Crawling result
258 */ 301 */
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 356
314 function reportException(e) 357 function reportException(e)
315 { 358 {
316 let stack = ""; 359 let stack = "";
317 if (e && typeof e == "object" && "stack" in e) 360 if (e && typeof e == "object" && "stack" in e)
318 stack = e.stack + "\n"; 361 stack = e.stack + "\n";
319 362
320 Cu.reportError(e); 363 Cu.reportError(e);
321 dump(e + "\n" + stack + "\n"); 364 dump(e + "\n" + stack + "\n");
322 } 365 }
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