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: Created Sept. 25, 2012, 3:13 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 let {Utils} = abprequire("utils");
17 17
18 let origProcessNode = Policy.processNode; 18 let origProcessNode = Policy.processNode;
19 19
20 let siteTabs; 20 let siteTabs;
21 let currentTabs; 21 let currentTabs;
22 22
23 function processNode(wnd, node, contentType, location, collapse) 23 function processNode(wnd, node, contentType, location, collapse)
24 { 24 {
25 let result = origProcessNode.apply(this, arguments); 25 let result = origProcessNode.apply(this, arguments);
26 let url = (contentType === Policy.type.ELEMHIDE) ? location.text : 26 let url = (contentType === Policy.type.ELEMHIDE) ? location.text :
27 location.spec; 27 location.spec;
28
28 let topWindow = wnd.top; 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
29 let tabbrowser = Utils.getChromeWindow(topWindow).gBrowser; 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
30 let browser = tabbrowser.getBrowserForDocument(topWindow.document); 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
31 let site = siteTabs.get(browser); 50 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; 51 let filtered = !result;
33 Storage.write([url, site, filtered]); 52 Storage.write([url, site, filtered]);
34 return result; 53 return result;
35 } 54 }
36 55
37 function loadSite(site, window, callback) 56 function loadSite(site, window, callback)
38 { 57 {
39 if (!site) 58 if (!site)
40 return; 59 return;
41 60
42 let tabbrowser = window.gBrowser; 61 let tabbrowser = window.gBrowser;
43 let tab = tabbrowser.addTab(site); 62 let tab = tabbrowser.addTab(site);
44 let browser = tabbrowser.getBrowserForTab(tab); 63 let browser = tabbrowser.getBrowserForTab(tab);
45 64
65 siteTabs.set(browser, site);
66
46 let progressListener = { 67 let progressListener = {
47 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us) 68 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us)
48 { 69 {
49 if (browser !== aBrowser) 70 if (browser !== aBrowser)
50 return; 71 return;
51 72
52 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)) 73 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP))
53 return; 74 return;
54 75
55 tabbrowser.removeTabsProgressListener(progressListener); 76 tabbrowser.removeTabsProgressListener(progressListener);
56 tabbrowser.removeTab(tab); 77 tabbrowser.removeTab(tab);
57 callback(); 78 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 } 79 }
64 }; 80 };
65 tabbrowser.addTabsProgressListener(progressListener); 81 tabbrowser.addTabsProgressListener(progressListener);
66 } 82 }
67 83
68 function loadSites(backendUrl, parallelTabs, window, sites, callback) 84 function loadSites(backendUrl, parallelTabs, window, sites, callback)
69 { 85 {
70 while (currentTabs < parallelTabs && sites.length) 86 while (currentTabs < parallelTabs && sites.length)
71 { 87 {
72 currentTabs++; 88 currentTabs++;
(...skipping 19 matching lines...) Expand all
92 108
93 let Crawler = exports.Crawler = {}; 109 let Crawler = exports.Crawler = {};
94 110
95 Crawler.crawl = function(backendUrl, parallelTabs, window, callback) 111 Crawler.crawl = function(backendUrl, parallelTabs, window, callback)
96 { 112 {
97 if (Policy.processNode != origProcessNode) 113 if (Policy.processNode != origProcessNode)
98 return; 114 return;
99 115
100 Policy.processNode = processNode; 116 Policy.processNode = processNode;
101 117
102 siteTabs = new WeakMap(); 118 siteTabs = new WeakMap();
Wladimir Palant 2012/09/25 16:51:30 You should null out this variable when you are don
103 currentTabs = 0; 119 currentTabs = 0;
104 120
105 Storage.init(); 121 Storage.init();
106 122
107 Client.fetchCrawlableSites(backendUrl, function(sites) 123 Client.fetchCrawlableSites(backendUrl, function(sites)
108 { 124 {
109 loadSites(backendUrl, parallelTabs, window, sites, function() 125 loadSites(backendUrl, parallelTabs, window, sites, function()
110 { 126 {
111 Policy.processNode = origProcessNode; 127 Policy.processNode = origProcessNode;
128 siteTabs = null;
112 callback(); 129 callback();
113 }); 130 });
114 }); 131 });
115 }; 132 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld