Left: | ||
Right: |
OLD | NEW |
---|---|
(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 let topWindow = wnd.top; | |
29 let tabbrowser = Utils.getChromeWindow(topWindow).gBrowser; | |
30 let browser = tabbrowser.getBrowserForDocument(topWindow.document); | |
31 let site = siteTabs.get(browser); | |
Wladimir Palant
2012/09/25 16:51:30
I would probably add a few more checks here. The t
| |
32 let filtered = !result; | |
33 Storage.write([url, site, filtered]); | |
34 return result; | |
35 } | |
36 | |
37 function loadSite(site, window, callback) | |
38 { | |
39 if (!site) | |
40 return; | |
41 | |
42 let tabbrowser = window.gBrowser; | |
43 let tab = tabbrowser.addTab(site); | |
44 let browser = tabbrowser.getBrowserForTab(tab); | |
45 | |
46 let progressListener = { | |
47 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us) | |
48 { | |
49 if (browser !== aBrowser) | |
50 return; | |
51 | |
52 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)) | |
53 return; | |
54 | |
55 tabbrowser.removeTabsProgressListener(progressListener); | |
56 tabbrowser.removeTab(tab); | |
57 callback(); | |
58 }, | |
59 onLocationChange: function(aBrowser, aWebProgress, aRequest, aLocation, aFla gs) | |
60 { | |
61 if (browser === aBrowser) | |
62 siteTabs.set(browser, site); | |
Wladimir Palant
2012/09/25 16:51:30
Why are we doing this in the progress listener and
| |
63 } | |
64 }; | |
65 tabbrowser.addTabsProgressListener(progressListener); | |
66 } | |
67 | |
68 function loadSites(backendUrl, parallelTabs, window, sites, callback) | |
69 { | |
70 while (currentTabs < parallelTabs && sites.length) | |
71 { | |
72 currentTabs++; | |
73 let site = sites.shift(); | |
74 loadSite(site, window, function() | |
75 { | |
76 currentTabs--; | |
77 if (!sites.length && !currentTabs) | |
78 { | |
79 Storage.finish(); | |
80 let dataFilePath = Storage.dataFile.path; | |
81 Client.sendCrawlerDataFile(backendUrl, dataFilePath, function() | |
82 { | |
83 Storage.destroy(); | |
84 callback(); | |
85 }); | |
86 } | |
87 else | |
88 loadSites(backendUrl, parallelTabs, window, sites, callback); | |
89 }); | |
90 } | |
91 } | |
92 | |
93 let Crawler = exports.Crawler = {}; | |
94 | |
95 Crawler.crawl = function(backendUrl, parallelTabs, window, callback) | |
96 { | |
97 if (Policy.processNode != origProcessNode) | |
98 return; | |
99 | |
100 Policy.processNode = processNode; | |
101 | |
102 siteTabs = new WeakMap(); | |
Wladimir Palant
2012/09/25 16:51:30
You should null out this variable when you are don
| |
103 currentTabs = 0; | |
104 | |
105 Storage.init(); | |
106 | |
107 Client.fetchCrawlableSites(backendUrl, function(sites) | |
108 { | |
109 loadSites(backendUrl, parallelTabs, window, sites, function() | |
110 { | |
111 Policy.processNode = origProcessNode; | |
112 callback(); | |
113 }); | |
114 }); | |
115 }; | |
OLD | NEW |