Find file
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (57 sloc) 2.87 KB
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
self.uri = 'file://' + get_bundle_path() + '/web/index.html';
# 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[";1"]
prefs = pref_class.getService(components.interfaces.nsIPrefService)
'socialcalc.granted', 'UniversalXPConnect')
'', 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()
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[";1"].createInstance(
# Optionally pass data to the javascript
if parameter:
str = components.classes[";1"].createInstance(
components.interfaces.nsISupportsString) = parameter
array.appendElement(str, False)
# Use XPCom to send an event to a javascript observer (web/xo.js)
observerService = components.classes[";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