Permalink
Browse files

refactor but it doesn't work. :(

  • Loading branch information...
1 parent ba35781 commit bae4b7131724f9d8bcb1a5db886388be4ed4dad2 Luke Closs committed Apr 15, 2008
Showing with 3,552 additions and 59 deletions.
  1. +64 −0 XOCom.py
  2. +9 −12 index.html
  3. +20 −47 test-web-view.py
  4. +3,408 −0 web/jquery.js
  5. +51 −0 web/xo-code.js
View
@@ -0,0 +1,64 @@
+from hulahop.webview import WebView
+from xpcom import components
+
+class XOCom:
+ # Constructor gives full XPCom access by default
+ # This should be improved for future apps that may not need/want full access
+ def __init__(self):
+ self.give_full_xpcom_access()
+
+ # Give the browser permission to use XPCom interfaces
+ # This is necessary for XPCom communication to work
+ # Note: Not all of these preferences may be required - requires further
+ # investigation
+ def give_full_xpcom_access(self):
+ pref_class = components.classes["@mozilla.org/preferences-service;1"]
+ prefs = pref_class.getService(components.interfaces.nsIPrefService)
+ prefs.getBranch('signed.applets.').setBoolPref('codebase_principal_support',
+ True);
+ prefs.getBranch('capability.principal.').setCharPref(
+ 'socialcalc.granted', 'UniversalXPConnect')
+ prefs.getBranch('capability.principal.').setCharPref(
+ 'socialcalc.id',
+ 'file:///home/olpc/src/testapp/web/index.html')
+
+
+ # Wrapper method to create a new webview embedded browser component
+ # Uses hulahop's WebView. Assumes that you'll want to serve
+ # web/index.html relative to your activity directory.
+ def create_webview(self, uri=None):
+ web_view = WebView()
+ if not uri:
+ uri = 'file:///home/olpc/src/testapp/web/index.html'
+ web_view.load_uri(uri)
+ web_view.show()
+ return web_view
+
+ # Use XPCom to execute a javascript callback registered with XO.js
+ # The command will execute a javascript method registered with the same name,
+ # and return any value received from the javascript
+ def send_to_browser(self, command, parameter=None):
+ # Set up an array for parameters and return values for the XPCom call
+ array = components.classes["@mozilla.org/array;1"].createInstance(
+ components.interfaces.nsIMutableArray)
+
+ # Optionally pass data to the javascript
+ if parameter:
+ str = components.classes["@mozilla.org/supports-string;1"].createInstance(
+ components.interfaces.nsISupportsString)
+ str.data = parameter
+ array.appendElement(str, False)
+
+ # Use XPCom to send an event to a javascript observer (web/xo.js)
+ observerService = components.classes["@mozilla.org/observer-service;1"]
+ ob_serv = observerService.getService(components.interfaces.nsIObserverService);
+ ob_serv.notifyObservers(array, "xo-message", command);
+
+ # check if the browser returned anything
+ if array.length:
+ iter = array.enumerate()
+ result = iter.getNext()
+ result = result.QueryInterface(components.interfaces.nsISupportsString)
+ return result.toString()
+ return None
+
View
@@ -4,26 +4,23 @@
</head>
<script type="text/javascript" src="web/jquery-1.2.3.js"></script>
<script type="text/javascript" src="web/xo-code.js"></script>
+ <body>
+ <div id="xostatus">No status</div><br />
+ <textarea width="600" height="400">Default text</textarea>
+ </body>
<script type="text/javascript">
- document.set_msg = function (msg) {
- jQuery('#bar').html(msg)
- }
-
try {
- XO.read_hook = function (data) {
+ XO.register('read', function(data) {
jQuery('textarea').html(data)
- }
- XO.write_hook = function () {
+ })
+ XO.register('write', function() {
return jQuery('textarea').html()
}
+ jQuery('#xostatus').html("Registered XOCom hooks")
}
catch (err) {
- document.set_msg(err)
+ jQuery('#xostatus').html("Error registering hooks: " + err)
}
</script>
- <body>
- <div id="status">No status</div><br />
- <textarea width="600" height="400">Default text</textarea>
- </body>
</html>
View
@@ -1,69 +1,42 @@
import os
import gtk
+import time
import hulahop
hulahop.startup(os.path.expanduser('~/.test-hulahop'))
-from hulahop.webview import WebView
-from xpcom import components
+from XOCom import XOCom
-def quit(window):
- hulahop.shutdown()
- gtk.main_quit()
+# The XOCom object helps us communicate with the browser
+xocom = XOCom()
# This is just a simple way to trigger events from python for testing
-def keypress(window, key2):
- key = key2.string
- if key == 'w': # Send an event to the javascript handler
- result = send_to_browser('write')
+# Handler for keypresses in the GTK Application
+# r - call the javascript 'read' hook with a dummy string
+# w - call the javascript 'write' hook and print out the result
+def keypress(window, event):
+ key = event.string
+ if key == 'w':
+ result = xocom.send_to_browser('write')
if result:
print "Result from browser - '%s'"%result
else:
print "No data received in the browser's response"
if key == 'r':
- import time
- result = send_to_browser('read', "I am a monkey - %s"%time.time())
+ message = "I am a monkey - %s"%time.time()
+ print "Sending '%s'"%message
+ result = xocom.send_to_browser('read', message)
if key == 'q':
quit(None)
-observerService = components.classes["@mozilla.org/observer-service;1"]
-ob_serv = observerService.getService(components.interfaces.nsIObserverService);
-
-def send_to_browser(command, data=None):
- array = components.classes["@mozilla.org/array;1"].createInstance(
- components.interfaces.nsIMutableArray)
- if data:
- str = components.classes["@mozilla.org/supports-string;1"].createInstance(
- components.interfaces.nsISupportsString)
- str.data = data
- print "Sending (%s) to browser"%str.data
- array.appendElement(str, False)
- ob_serv.notifyObservers(array, "xo-message", command);
- # check if the browser returned anything
- if array.length:
- iter = array.enumerate()
- result = iter.getNext()
- result = result.QueryInterface(components.interfaces.nsISupportsString)
- return result.toString()
- return None
+# Shut down our app
+def quit(window):
+# hulahop.shutdown()
+ gtk.main_quit()
+# Create our GTK window containing the embedded browser, and hook up some handlers
window = gtk.Window()
window.set_default_size(600, 400)
window.connect("destroy", quit)
window.connect("key_press_event", keypress)
-
-# Give the browser permission to use XPCom interfaces
-pref_class = components.classes["@mozilla.org/preferences-service;1"]
-prefs = pref_class.getService(components.interfaces.nsIPrefService)
-prefs.getBranch('signed.applets.').setBoolPref('codebase_principal_support', True);
-prefs.getBranch('capability.principal.').setCharPref(
- 'socialcalc.granted', 'UniversalXPConnect')
-prefs.getBranch('capability.principal.').setCharPref(
- 'socialcalc.id', 'file:///home/olpc/src/testapp/index.html')
-
-# Create the embedded browser
-web_view = WebView()
-web_view.load_uri('file:///home/olpc/src/testapp/index.html')
-web_view.show()
-
-window.add(web_view)
+window.add(xocom.create_webview())
window.show()
gtk.main()
Oops, something went wrong.

0 comments on commit bae4b71

Please sign in to comment.