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 |
| 17 let origProcessNode = Policy.processNode; |
| 18 |
| 19 let siteTabs; |
| 20 let currentTabs; |
| 21 |
| 22 function processNode(wnd, node, contentType, location, collapse) |
| 23 { |
| 24 let result = origProcessNode.apply(this, arguments); |
| 25 let url = (contentType === Policy.type.ELEMHIDE) ? location.text : |
| 26 location.spec; |
| 27 let site = siteTabs[wnd.top.location.href]; |
| 28 let filtered = !result; |
| 29 Storage.write([url, site, filtered]); |
| 30 return result; |
| 31 } |
| 32 |
| 33 function loadSite(site, window, callback) |
| 34 { |
| 35 if (!site) |
| 36 return; |
| 37 |
| 38 let tabbrowser = window.gBrowser; |
| 39 let tab = tabbrowser.addTab(site); |
| 40 let browser = tabbrowser.getBrowserForTab(tab); |
| 41 |
| 42 let progressListener = { |
| 43 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat
us) |
| 44 { |
| 45 if (browser !== aBrowser) |
| 46 return; |
| 47 |
| 48 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)) |
| 49 return; |
| 50 |
| 51 tabbrowser.removeTabsProgressListener(progressListener); |
| 52 tabbrowser.removeTab(tab); |
| 53 callback(); |
| 54 }, |
| 55 onLocationChange: function(aBrowser, aWebProgress, aRequest, aLocation, aFla
gs) |
| 56 { |
| 57 if (browser === aBrowser) |
| 58 siteTabs[aLocation.spec] = site; |
| 59 } |
| 60 }; |
| 61 tabbrowser.addTabsProgressListener(progressListener); |
| 62 } |
| 63 |
| 64 function loadSites(backendUrl, parallelTabs, window, sites, callback) |
| 65 { |
| 66 while (currentTabs < parallelTabs && sites.length) |
| 67 { |
| 68 currentTabs++; |
| 69 let site = sites.shift(); |
| 70 loadSite(site, window, function() |
| 71 { |
| 72 currentTabs--; |
| 73 if (!sites.length && !currentTabs) |
| 74 { |
| 75 Storage.finish(); |
| 76 let dataFilePath = Storage.dataFile.path; |
| 77 Client.sendCrawlerDataFile(backendUrl, window, dataFilePath, function() |
| 78 { |
| 79 Storage.destroy(); |
| 80 callback(); |
| 81 }); |
| 82 } |
| 83 else |
| 84 loadSites(backendUrl, parallelTabs, window, sites, callback); |
| 85 }); |
| 86 } |
| 87 } |
| 88 |
| 89 let Crawler = exports.Crawler = {}; |
| 90 |
| 91 Crawler.crawl = function(backendUrl, parallelTabs, window, callback) |
| 92 { |
| 93 if (Policy.processNode != origProcessNode) |
| 94 return; |
| 95 |
| 96 Policy.processNode = processNode; |
| 97 |
| 98 siteTabs = {}; |
| 99 currentTabs = 0; |
| 100 |
| 101 Storage.init(); |
| 102 |
| 103 Client.fetchCrawlableSites(backendUrl, function(sites) |
| 104 { |
| 105 loadSites(backendUrl, parallelTabs, window, sites, function() |
| 106 { |
| 107 Policy.processNode = origProcessNode; |
| 108 callback(); |
| 109 }); |
| 110 }); |
| 111 }; |
OLD | NEW |