changeset 2:889c2fd4c9cf

window.powerbox.request() is now available on every content window.
author Atul Varma <varmaa@toolness.com>
date Thu, 06 Aug 2009 14:53:41 -0700
parents ec7bc2ca0bbc
children 413435fc6202
files extension/chrome.manifest extension/content/browser.xul extension/content/js/xul/browser.js extension/modules/content-injector.js extension/modules/setup.js manage.py
diffstat 6 files changed, 179 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extension/chrome.manifest	Thu Aug 06 14:53:41 2009 -0700
@@ -0,0 +1,3 @@
+resource powerbox ./
+content powerbox content/
+overlay chrome://browser/content/browser.xul chrome://powerbox/content/browser.xul
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extension/content/browser.xul	Thu Aug 06 14:53:41 2009 -0700
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript;version=1.7"
+          src="chrome://powerbox/content/js/xul/browser.js"/>
+</overlay>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extension/content/js/xul/browser.js	Thu Aug 06 14:53:41 2009 -0700
@@ -0,0 +1,5 @@
+(function() {
+   var jsm = {};
+   Components.utils.import("resource://powerbox/modules/setup.js", jsm);
+   jsm.PowerboxSetup.installToWindow(window);
+ })();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extension/modules/content-injector.js	Thu Aug 06 14:53:41 2009 -0700
@@ -0,0 +1,72 @@
+var EXPORTED_SYMBOLS = ["init"];
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+function buildRequestor(window) {
+  return function request(caps, callback) {
+    if (!(typeof(caps) == "object" ||
+          typeof(caps) == "string"))
+      throw new Error("Must provide capabilities as first parameter.");
+
+    if (typeof(callback) != "function")
+      throw new Error("Must provide callback as second parameter");
+
+    if (typeof(caps) == "object")
+      caps = new XPCSafeJSObjectWrapper(caps);
+    callback = new XPCSafeJSObjectWrapper(callback);
+
+    // TODO: Present UI to user and inject requested capabilities.
+  };
+}
+
+var listener = {
+  QueryInterface : XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
+                                          Ci.nsISupportsWeakReference]),
+
+  onStateChange : function (aWebProgress, aRequest,
+                            aStateFlags,  aStatus) {
+    // STATE_START is too early, doc is still the old page.
+    // STATE_STOP is inconviently late (it's onload)
+    if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_TRANSFERRING))
+      return;
+
+    var window = aWebProgress.DOMWindow;
+
+    if (window.wrappedJSObject) {
+      var sandbox = Cu.Sandbox(window);
+      sandbox.importFunction(buildRequestor(window));
+      sandbox.window = window.wrappedJSObject;
+
+      function setupPowerbox() {
+        window.powerbox = {request: request};
+      }
+
+      Cu.evalInSandbox("(" + setupPowerbox.toString() + ")();", sandbox);
+    }
+  },
+
+  // stubs for the nsIWebProgressListener interfaces which we don't use.
+  onProgressChange : function() { },
+  onLocationChange : function() { },
+  onStatusChange   : function() { },
+  onSecurityChange : function() { }
+};
+
+function init() {
+  // WebProgressListener for getting notification of new doc loads.
+  // XXX Ugh. Since we're a chrome overlay, it would be nice to just
+  // use gBrowser.addProgressListener(). But that isn't sending
+  // STATE_TRANSFERRING, and the earliest we can get at the page is
+  // STATE_STOP (which is onload, and is inconviently late).
+  // We'll use the docloader service instead, but that means we need to
+  // filter out loads for other windows.
+  var docsvc = Cc["@mozilla.org/docloaderservice;1"].
+               getService(Ci.nsIWebProgress);
+
+  docsvc.addProgressListener(listener,
+                             Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extension/modules/setup.js	Thu Aug 06 14:53:41 2009 -0700
@@ -0,0 +1,92 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Ubiquity.
+ *
+ * The Initial Developer of the Original Code is Mozilla.
+ * Portions created by the Initial Developer are Copyright (C) 2007
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Atul Varma <atul@mozilla.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+let EXPORTED_SYMBOLS = ["PowerboxSetup"];
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+let Application = Cc["@mozilla.org/fuel/application;1"] ?
+                  Cc["@mozilla.org/fuel/application;1"]
+                    .getService(Ci.fuelIApplication) :
+                  Cc["@mozilla.org/steel/application;1"]
+                    .getService(Ci.steelIApplication);
+
+let gIsInitialized = false;
+
+let PowerboxSetup = {
+  getExtensionDirectory: function getExtensionDirectory() {
+    let extMgr = Cc["@mozilla.org/extensions/manager;1"]
+                 .getService(Components.interfaces.nsIExtensionManager);
+    let loc = extMgr.getInstallLocation("powerbox@labs.mozilla.com");
+    let extDir = loc.getItemLocation("powerbox@labs.mozilla.com");
+
+    return extDir;
+  },
+
+  getBaseUri: function getBaseUri() {
+    let ioSvc = Components.classes["@mozilla.org/network/io-service;1"]
+                .getService(Components.interfaces.nsIIOService);
+    let extDir = this.getExtensionDirectory();
+    let baseUri = ioSvc.newFileURI(extDir).spec;
+
+    return baseUri;
+  },
+
+  isInstalledAsXpi: function isInstalledAsXpi() {
+    let profileDir = Cc["@mozilla.org/file/directory_service;1"]
+                     .getService(Components.interfaces.nsIProperties)
+                     .get("ProfD", Components.interfaces.nsIFile);
+    let extDir = this.getExtensionDirectory();
+    if (profileDir.contains(extDir, false))
+      return true;
+    return false;
+  },
+
+  get version() {
+    return Application.extensions.get("powerbox@labs.mozilla.com").version;
+  },
+
+  installToWindow: function installToWindow(window) {
+    if (!gIsInitialized) {
+      gIsInitialized = true;
+      var Injector = {};
+      Cu.import("resource://powerbox/modules/content-injector.js", Injector);
+      Injector.init();
+    }
+  }
+};
--- a/manage.py	Thu Aug 06 10:50:32 2009 -0700
+++ b/manage.py	Thu Aug 06 14:53:41 2009 -0700
@@ -51,6 +51,8 @@
 DEFAULT_FIREFOX_PREFS = {
     'browser.startup.homepage' : 'about:blank',
     'startup.homepage_welcome_url' : 'about:blank',
+    'browser.dom.window.dump.enabled' : True,
+    'javascript.options.showInConsole' : True
     }
 
 # When launching a temporary new Thunderbird profile, use these preferences.