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

Unified Diff: lib/main.js

Issue 5288886037118976: Adblock Plus Crawler rewrite (Closed)
Patch Set: Addressed comments Created May 7, 2015, 12:04 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/debug.js ('K') | « lib/debug.js ('k') | metadata.gecko » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/main.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/main.js
@@ -0,0 +1,88 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+/**
+ * @module main
+ */
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+
+require("commandLine");
+let {run} = require("crawler");
+let {reportException} = require("debug");
+
+let baseURL = null;
+
+/**
+ * Waits for the application to initialize.
+ * @type {Promise}
+ */
+let applicationReady = (function()
+{
+ return new Promise((resolve, reject) =>
+ {
+ let observer = {
+ observe: function(subject, topic, data)
+ {
+ Services.obs.removeObserver(this, "sessionstore-windows-restored");
+ resolve();
+ },
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference])
+ };
+
+ Services.obs.addObserver(observer, "sessionstore-windows-restored", true);
+ onShutdown.add(() => Services.obs.removeObserver(observer, "sessionstore-windows-restored"));
+ });
+})();
+
+/**
+ * Startup function, called from command line handler.
+ *
+ * @param {int} port Port to communicate with
+ */
+function startup(port)
+{
+ baseURL = "http://localhost:" + port + "/";
+
+ let request = new XMLHttpRequest();
+ request.open("GET", baseURL + "parameters");
+ request.addEventListener("load", onParametersLoaded, false);
+ request.addEventListener("error", onParametersFailed, false);
+ request.responseType = "json";
+ request.send();
+}
+exports.startup = startup;
+
+/**
+ * Called if parameters loaded succesfully.
+ *
+ * @param {Event} event
+ */
+function onParametersLoaded(event)
+{
+ let {urls, timeout, maxtabs} = event.target.response;
+
+ applicationReady.then(function()
+ {
+ let window = Services.wm.getMostRecentWindow("navigator:browser");
+ run(window, urls, timeout, maxtabs, baseURL + "save", function()
+ {
+ Services.startup.quit(Services.startup.eAttemptQuit);
+ });
+ }, reportException);
+}
+
+/**
+ * Called if requesting parameters failed.
+ *
+ * @param {Event} event
+ */
+function onParametersFailed(event)
+{
+ reportException(new Error("Failed loading parameters"));
+}
« lib/debug.js ('K') | « lib/debug.js ('k') | metadata.gecko » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld