Skip to content
Browse files

these files should only live in the XOCom activity

  • Loading branch information...
1 parent 729e2c4 commit 22ada3a1f02859bd4a1b65c48ee12be2d38eb54a Luke Closs committed
Showing with 0 additions and 7,116 deletions.
  1. +0 −64 XOCom.py
  2. +0 −45 test-web-view.py
  3. +0 −3,408 web/jquery.js
  4. +0 −85 web/xo.js
  5. +0 −3,494 web/xocom.js
  6. +0 −20 xocom-activity-skeleton
View
64 XOCom.py
@@ -1,64 +0,0 @@
-from sugar.activity.activity import get_bundle_path
-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, uri=None):
- if uri:
- self.uri = uri
- else:
- self.uri = 'file://' + get_bundle_path() + '/web/index.html';
- 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', self.uri)
-
- # 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):
- web_view = WebView()
- web_view.load_uri(self.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
45 test-web-view.py
@@ -1,45 +0,0 @@
-import os
-import gtk
-import time
-import hulahop
-hulahop.startup(os.path.expanduser('~/.test-hulahop'))
-from XOCom import XOCom
-
-# The XOCom object helps us communicate with the browser
-uri = "file:///home/olpc/src/testapp/web/index.html"
-xocom = XOCom(uri)
-
-# This is just a simple way to trigger events from python for testing
-# 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':
- message = "I am a monkey - %s"%time.time()
- print "Sending '%s'"%message
- result = xocom.send_to_browser('read', message)
- if key == 'q':
- quit(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)
-
-window.add( xocom.create_webview() )
-window.show()
-gtk.main()
View
3,408 web/jquery.js
0 additions, 3,408 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
85 web/xo.js
@@ -1,85 +0,0 @@
-/*
- * XOCom Javascript Source
- *
- * This source creates a global XO object that communicates
- * with the python XPCom code running this activity.
- *
- * Your HTML file should register several callbacks with
- * the XO object to handle requests from the activity:
- *
- * Example: A handler for the activity read_file and write_file
- *
- * <script type="text/javascript">
- * XO.register('read', function(content) {
- * // Your code to consume the supplied content
- * })
- * XO.register('write', function() {
- * // Your code to return the content to save
- * return 'monkey'
- * })
- * </script>
- *
- * Some commands for debugging are put in an element with the
- * id 'xo-status' if it exists.
- */
-
-var XO = window.XO = {
- callbacks: { },
- set_status: function (msg) {
- jQuery('#xo-status').html(msg)
- },
- observer: {
- observe: function(req_obj,topic,command) {
- now = new Date().getTime()
- XO.set_status("(" + now + ") Handling " + command)
- try {
- // We need access to use the XPCom functions below
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
-
- // Unwrap the XPCom objects to get at the data passed to us
- req_obj = req_obj.QueryInterface(Components.interfaces.nsIMutableArray)
- var command_arg = undefined
- if (req_obj.length) {
- var iter = req_obj.enumerate()
- xp_arg = iter.getNext()
- xp_arg = xp_arg.QueryInterface(Components.interfaces.nsISupportsString)
- command_arg = xp_arg.toString()
- }
-
- // Execute the registered callback method
- return_value = XO.callbacks[command](command_arg) || ''
-
- // Wrap the return value back into the XPCom object
- var result = Components.classes["@mozilla.org/supports-string;1"].createInstance(
- Components.interfaces.nsISupportsString)
- result.data = return_value
- req_obj.clear()
- req_obj.appendElement(result, false)
- XO.set_status("(" + now + ") Handled " + command + ": (" + return_value + ")")
- }
- catch (err) {
- XO.set_status("Error handling event: " + err)
- }
- }
- },
- register: function(command, callback) {
- XO.callbacks[command] = callback
- }
-}
-
-/*
- * This snippet registers the XO observer to receive commands
- * from the python XPCom code
- */
-try {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
- observerService.addObserver(XO.observer, 'xo-message', false);
-}
-catch(err) {
- // Wait a bit to show this error, so the page has time to load up.
- setTimeout( function() {
- jQuery('#xo-status', 'JS Error: ' + err);
- }, 1000)
-}
View
3,494 web/xocom.js
0 additions, 3,494 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
20 xocom-activity-skeleton
@@ -1,20 +0,0 @@
-#!/bin/bash
-set -e
-
-echo "This will download the XOCom sources to your current directory."
-echo
-echo "Hit Ctrl-C to cancel, enter to continue."
-read
-
-if [ ! -d web ]; then
- echo "Creating initial directory structure"
- mkdir web
- echo "Hello World" > web/index.html
-fi
-
-echo "Updating XOCom libraries"
-for f in XOCom.py web/xocom.js; do
- echo "--> Fetching $f"
- wget --quiet -O $f "http://github.com/lukec/xocom/tree/master%2F$f?raw=true"
-done
-

0 comments on commit 22ada3a

Please sign in to comment.
Something went wrong with that request. Please try again.