diff enso_pyxpcom.py @ 0:b1c0ab5d5f14

Origination, originally taken from the about:python source code from pyxpcom.
author Atul Varma <varmaa@toolness.com>
date Thu, 08 May 2008 11:06:36 -0700
parents
children 6e4335fa3321
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enso_pyxpcom.py	Thu May 08 11:06:36 2008 -0700
@@ -0,0 +1,56 @@
+from xpcom import components, verbose
+import sys, os
+import logging
+import platform
+
+_ensoStarted = False
+
+def getAbout():
+    global _ensoStarted
+
+    import enso
+    if not _ensoStarted:
+        logging.info("Launching Enso.")
+        _ensoStarted = True
+        enso.run()
+    return "Enso is running."
+
+class ShutdownObserver:
+    _com_interfaces_ = components.interfaces.nsIObserver
+
+    def observe(self, subject, topic, data):
+        print "quit-application received; calling sys.exitfunc()."
+        sys.exitfunc()
+
+class AboutPython:
+    _com_interfaces_ = components.interfaces.nsIAboutModule
+    _reg_contractid_ = '@mozilla.org/network/protocol/about;1?what=enso'
+    _reg_clsid_ = '{6d5d462e-6de7-4bca-bbc6-c488d481351c}'
+    _reg_desc_ = "about:enso handler"
+
+    def __init__(self):
+        logging.basicConfig( level=logging.INFO )
+        logging.info("Setting shutdown observer.")
+        observerService = components.classes["@mozilla.org/observer-service;1"].getService(components.interfaces.nsIObserverService)
+        observerService.addObserver(ShutdownObserver(), "quit-application", False)
+
+    def newChannel(self, aURI):
+        ioService = components.classes["@mozilla.org/network/io-service;1"] \
+            .getService();
+
+        istream = components.classes["@mozilla.org/io/string-input-stream;1"] \
+            .createInstance()
+
+        about = getAbout()
+        istream.setData(about, len(about))
+
+        channel = components.classes["@mozilla.org/network/input-stream-channel;1"] \
+            .createInstance(components.interfaces.nsIInputStreamChannel)
+
+        channel.setURI(aURI)
+        #channel.contentType = "text/html"
+        channel.contentStream = istream
+        return channel
+
+    def getURIFlags(self, aURI):
+        return 0;