Index: lib/crawler.js |
diff --git a/lib/crawler.js b/lib/crawler.js |
index 21a4cf970d14f3569bb0a05783547b251695ce08..e66e55bdbfd07cd7ff4e248940de4c7550fa4f07 100644 |
--- a/lib/crawler.js |
+++ b/lib/crawler.js |
@@ -11,6 +11,7 @@ |
Cu.import("resource://gre/modules/Services.jsm"); |
Cu.import("resource://gre/modules/Task.jsm"); |
Cu.import("resource://gre/modules/Promise.jsm"); |
+Cu.import("resource://gre/modules/Timer.jsm"); |
function abprequire(module) |
{ |
@@ -113,79 +114,6 @@ TabAllocator.prototype = { |
}; |
/** |
- * Observes page loads in a particular tabbed browser. |
- * |
- * @param {tabbrowser} browser |
- * The tabbed browser to be observed |
- * @param {int} timeout |
- * Load timeout in milliseconds |
- * @constructor |
- */ |
-function LoadListener(browser, timeout) |
-{ |
- this._browser = browser; |
- this._deferred = new Map(); |
- this._timeout = timeout; |
- browser.addTabsProgressListener(this); |
-} |
-LoadListener.prototype = { |
- /** |
- * Returns a promise that will be resolved when the page in the specified tab |
- * finishes loading. Loading will be stopped if the timeout is reached. |
- * |
- * @param {tab} tab |
- * @result {Promise} |
- */ |
- waitForLoad: function(tab) |
- { |
- let deferred = Promise.defer(); |
- this._deferred.set(tab.linkedBrowser, deferred); |
- |
- tab.ownerDocument.defaultView.setTimeout(function() |
- { |
- tab.linkedBrowser.stop(); |
- }, this._timeout); |
- |
- return deferred.promise; |
- }, |
- |
- /** |
- * Deactivates this object. |
- */ |
- stop: function() |
- { |
- this._browser.removeTabsProgressListener(this); |
- }, |
- |
- onStateChange: function(browser, progress, request, flags, status) |
- { |
- if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && (flags & Ci.nsIWebProgressListener.STATE_IS_WINDOW)) |
- { |
- let deferred = this._deferred.get(browser); |
- if (deferred) |
- { |
- this._deferred.delete(browser); |
- |
- let headers = []; |
- if (request instanceof Ci.nsIHttpChannel) |
- { |
- try |
- { |
- headers.push("HTTP/x.x " + request.responseStatus + " " + request.responseStatusText); |
- request.visitResponseHeaders((header, value) => headers.push(header + ": " + value)); |
- } |
- catch (e) |
- { |
- // Exceptions are expected here |
- } |
- } |
- deferred.resolve([status, headers]); |
- } |
- } |
- } |
-}; |
- |
-/** |
* Once created, this object will make sure all new windows are dismissed |
* immediately. |
* |
@@ -279,7 +207,7 @@ exports.run = run; |
function crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone) |
{ |
let tabAllocator = new TabAllocator(window.getBrowser(), maxtabs); |
- let loadListener = new LoadListener(window.getBrowser(), timeout); |
+ |
let running = 0; |
let windowCloser = new WindowCloser(); |
let taskDone = function() |
@@ -287,7 +215,6 @@ function crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone) |
running--; |
if (running <= 0) |
{ |
- loadListener.stop(); |
windowCloser.stop(); |
onDone(); |
} |
@@ -296,7 +223,7 @@ function crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone) |
for (let url of urls) |
{ |
running++; |
- Task.spawn(crawl_url.bind(null, url, tabAllocator, loadListener)).then(function(result) |
+ Task.spawn(crawl_url.bind(null, url, tabAllocator, timeout)).then(function(result) |
{ |
let request = new XMLHttpRequest(); |
request.open("POST", targetURL); |
@@ -321,15 +248,43 @@ function crawl_urls(window, urls, timeout, maxtabs, targetURL, onDone) |
} |
/** |
+ * Expects to receive page info gathered in a content process for the specified |
+ * `tab`. If there is no relevant message within specified `timeout` then |
+ * the result promise is resolve with error object. |
+ * @param tab |
+ * Tab in which we are interested in |
+ * @param {int} timeout |
+ * Timeout in milliseconds |
+ * @return {Promise} promise which will be resolved with the received page info |
+ */ |
+function getPageInfo(tab, timeout) |
+{ |
+ return new Promise((resolve, result) => |
+ { |
+ let mm = tab.linkedBrowser.messageManager; |
+ let timerID; |
+ let onDone = (pageInfo) => |
+ { |
+ mm.removeMessageListener("abpcrawler:pageInfoGathered", onDone); |
+ clearTimeout(timerID); |
+ resolve(pageInfo); |
+ } |
+ mm.addMessageListener("abpcrawler:pageInfoGathered", (msg) => onDone(msg.data));; |
+ timerID = setTimeout(onDone.bind(this, {error: "timeout"}), timeout); |
+ }); |
+} |
+ |
+/** |
* Crawls a URL. This is a generator meant to be used via a Task object. |
* |
* @param {String} url |
* @param {TabAllocator} tabAllocator |
- * @param {loadListener} loadListener |
+ * @param {int} timeout |
+ * Load timeout in milliseconds |
* @result {Object} |
* Crawling result |
*/ |
-function* crawl_url(url, tabAllocator, loadListener) |
+function* crawl_url(url, tabAllocator, timeout) |
{ |
let tab = yield tabAllocator.getTab(); |
let result = {url, requests: []}; |
@@ -347,33 +302,10 @@ function* crawl_url(url, tabAllocator, loadListener) |
}); |
tab.linkedBrowser.loadURI(url, null, null); |
- [result.channelStatus, result.headers] = yield loadListener.waitForLoad(tab); |
- result.endTime = Date.now(); |
- result.finalUrl = tab.linkedBrowser.currentURI.spec; |
- |
- let document = tab.linkedBrowser.contentDocument; |
- if (document.documentElement) |
- { |
- try |
- { |
- let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); |
- canvas.width = document.documentElement.scrollWidth; |
- canvas.height = document.documentElement.scrollHeight; |
- |
- let context = canvas.getContext("2d"); |
- context.drawWindow(document.defaultView, 0, 0, canvas.width, canvas.height, "rgb(255, 255, 255)"); |
- result.screenshot = canvas.toDataURL("image/jpeg", 0.8); |
- } |
- catch (e) |
- { |
- reportException(e); |
- result.error = "Capturing screenshot failed: " + e; |
- } |
- // TODO: Capture frames as well? |
- let serializer = new tab.ownerDocument.defaultView.XMLSerializer(); |
- result.source = serializer.serializeToString(document.documentElement); |
- } |
+ result.finalUrl = tab.linkedBrowser.currentURI.spec; |
+ Object.assign(result, yield getPageInfo(tab, timeout)); |
+ result.endTime = Date.now(); |
} |
finally |
{ |
@@ -393,3 +325,13 @@ function reportException(e) |
Cu.reportError(e); |
dump(e + "\n" + stack + "\n"); |
} |
+ |
+let {addonRoot} = require("info"); |
+let frameScriptPath = addonRoot + "/lib/child/frameScript.js"; |
+let globalMessageManager = Services.mm; |
+globalMessageManager.loadFrameScript(frameScriptPath, true); |
+ |
+onShutdown.add(() => |
+{ |
+ globalMessageManager.removeDelayedFrameScript(frameScriptPath); |
+}); |