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

Side by Side Diff: lib/crawler.js

Issue 8402021: Crawler frontend (Closed)
Patch Set: Created Sept. 26, 2012, 8:25 a.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 | « lib/client.js ('k') | lib/main.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 Cu.import("resource://gre/modules/Services.jsm");
2
3 function abprequire(module)
4 {
5 let result = {};
6 result.wrappedJSObject = result;
7 Services.obs.notifyObservers(result, "adblockplus-require", module);
8 return result.exports;
9 }
10
11 let {Storage} = require("storage");
12 let {Client} = require("client");
13
14 let {Policy} = abprequire("contentPolicy");
15 let {Filter} = abprequire("filterClasses");
16 let {Utils} = abprequire("utils");
17
18 let origProcessNode = Policy.processNode;
19
20 let siteTabs;
21 let currentTabs;
22
23 function processNode(wnd, node, contentType, location, collapse)
24 {
25 let result = origProcessNode.apply(this, arguments);
26 let url = (contentType === Policy.type.ELEMHIDE) ? location.text :
27 location.spec;
28
29 let topWindow = wnd.top;
30 if (!topWindow.document)
31 {
32 Cu.reportError("No document associated with the node's top window");
33 return result;
34 }
35
36 let tabbrowser = Utils.getChromeWindow(topWindow).gBrowser;
37 if (!tabbrowser)
38 {
39 Cu.reportError("Unable to get a tabbrowser reference");
40 return result;
41 }
42
43 let browser = tabbrowser.getBrowserForDocument(topWindow.document);
44 if (!browser)
45 {
46 Cu.reportError("Unable to get browser for the tab");
47 return result;
48 }
49
50 let site = siteTabs.get(browser);
51 let filtered = !result;
52 Storage.write([url, site, filtered]);
53 return result;
54 }
55
56 function loadSite(site, window, callback)
57 {
58 if (!site)
59 return;
60
61 let tabbrowser = window.gBrowser;
62 let tab = tabbrowser.addTab(site);
63 let browser = tabbrowser.getBrowserForTab(tab);
64
65 siteTabs.set(browser, site);
66
67 let progressListener = {
68 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us)
69 {
70 if (browser !== aBrowser)
71 return;
72
73 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP))
74 return;
75
76 tabbrowser.removeTabsProgressListener(progressListener);
77 tabbrowser.removeTab(tab);
78 callback();
79 }
80 };
81 tabbrowser.addTabsProgressListener(progressListener);
82 }
83
84 function loadSites(backendUrl, parallelTabs, window, sites, callback)
85 {
86 while (currentTabs < parallelTabs && sites.length)
87 {
88 currentTabs++;
89 let site = sites.shift();
90 loadSite(site, window, function()
91 {
92 currentTabs--;
93 if (!sites.length && !currentTabs)
94 {
95 Storage.finish();
96 let dataFilePath = Storage.dataFile.path;
97 Client.sendCrawlerDataFile(backendUrl, dataFilePath, function()
98 {
99 Storage.destroy();
100 callback();
101 });
102 }
103 else
104 loadSites(backendUrl, parallelTabs, window, sites, callback);
105 });
106 }
107 }
108
109 let Crawler = exports.Crawler = {};
110
111 Crawler.crawl = function(backendUrl, parallelTabs, window, callback)
112 {
113 if (Policy.processNode != origProcessNode)
114 return;
115
116 Policy.processNode = processNode;
117
118 siteTabs = new WeakMap();
119 currentTabs = 0;
120
121 Storage.init();
122
123 Client.fetchCrawlableSites(backendUrl, function(sites)
124 {
125 loadSites(backendUrl, parallelTabs, window, sites, function()
126 {
127 Policy.processNode = origProcessNode;
128 siteTabs = null;
129 callback();
130 });
131 });
132 };
OLDNEW
« no previous file with comments | « lib/client.js ('k') | lib/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld