diff options
author | Daniel Narvaez <dwnarvaez@gmail.com> | 2013-05-10 18:44:58 (GMT) |
---|---|---|
committer | Daniel Narvaez <dwnarvaez@gmail.com> | 2013-06-10 13:46:54 (GMT) |
commit | b276f61da1146b82d2c8dc35ae52603c2cda7f56 (patch) | |
tree | 45167f88d0a9e8c7470aa6d0425f86636c080567 | |
parent | 3f908ef9995144e7453f53083cce913334ca49b7 (diff) |
Use javascript to setup websocket port and key
-rw-r--r-- | src/sugar3/activity/htmlactivity.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/sugar3/activity/htmlactivity.py b/src/sugar3/activity/htmlactivity.py index b76c2ed..ef4826d 100644 --- a/src/sugar3/activity/htmlactivity.py +++ b/src/sugar3/activity/htmlactivity.py @@ -50,6 +50,8 @@ class HTMLActivity(activity.Activity): context.register_uri_scheme("activity", self._app_scheme_cb, None) self._web_view = WebKit2.WebView() + self._web_view.connect("load-changed", self._loading_changed_cb) + self.set_canvas(self._web_view) self._web_view.show() @@ -60,18 +62,25 @@ class HTMLActivity(activity.Activity): self._server = Server() self._server.connect("session-started", self._session_started_cb) - port = self._server.start() + self._port = self._server.start() + self._key = os.urandom(16).encode("hex") index_path = os.path.join(activity.get_bundle_path(), "index.html") - self._key = os.urandom(16).encode("hex") - self._web_view.load_uri("activity://%s/%s?port=%s&key=%s" % - (self.get_bundle_id(), - index_path, - port, self._key)) + self._web_view.load_uri("activity://%s/%s" % + (self.get_bundle_id(), index_path)) self._apis = {} self._apis["activity"] = ActivityAPI(self) + def _loading_changed_cb(self, web_view, load_event): + if load_event == WebKit2.LoadEvent.FINISHED: + script = "window.sugarKey = '%s'; " \ + "window.sugarPort = %d; " \ + "if (window.onSugarKeySet) " \ + "window.onSugarKeySet();" % (self._key, self._port) + + self._web_view.run_javascript(script, None, None, None) + def _key_press_event_cb(self, window, event): key_name = Gdk.keyval_name(event.keyval) |