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

Delta Between Two Patch Sets: lib/crawler.js

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

Powered by Google App Engine
This is Rietveld