/usr/lib/python2.7/dist-packages/jarabe/apisocket.py is in python-jarabe 0.112-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | # Copyright (C) 2013, Daniel Narvaez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import os
import struct
import time
import dbus
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import Gio
from gwebsockets.server import Server
from gwebsockets.server import Message
from sugar3 import env
from jarabe.model import shell
from jarabe.model import session
from jarabe.journal.objectchooser import ObjectChooser
class StreamMonitor(object):
def __init__(self):
self.on_data = None
self.on_close = None
class API(object):
def __init__(self, client):
self._client = client
self._activity = None
for activity in shell.get_model():
if activity.get_activity_id() == client.activity_id:
self._activity = activity
class ActivityAPI(API):
def __init__(self, client):
API.__init__(self, client)
self._activity.connect('pause', self._pause_cb)
self._activity.connect('resume', self._resume_cb)
self._activity.connect('stop', self._stop_cb)
session.get_session_manager().shutdown_signal.connect(
self._session_manager_shutdown_cb)
def get_xo_color(self, request):
settings = Gio.Settings('org.sugarlabs.user')
color_string = settings.get_string('color')
self._client.send_result(request, [color_string.split(",")])
def close(self, request):
self._activity.get_window().close(GLib.get_current_time())
self._client.send_result(request, [])
def _pause_cb(self, event):
self._client.send_notification("activity.pause")
def _resume_cb(self, event):
self._client.send_notification("activity.resume")
def _stop_cb(self, event):
# When the web activity receives this notification, it has
# time for saving the state and do any cleanup needed. Then
# it must call 'window.close' to complete the activity
# closing.
self._client.send_notification("activity.stop")
return True
def _session_manager_shutdown_cb(self, event):
self._client.send_notification("activity.stop")
def show_object_chooser(self, request):
chooser = ObjectChooser(self._activity)
chooser.connect('response', self._chooser_response_cb, request)
chooser.show()
def _chooser_response_cb(self, chooser, response_id, request):
if response_id == Gtk.ResponseType.ACCEPT:
object_id = chooser.get_selected_object_id()
self._client.send_result(request, [object_id])
else:
self._client.send_result(request, [None])
chooser.destroy()
class DatastoreAPI(API):
def __init__(self, client):
API.__init__(self, client)
bus = dbus.SessionBus()
bus_object = bus.get_object("org.laptop.sugar.DataStore",
"/org/laptop/sugar/DataStore")
self._data_store = dbus.Interface(bus_object,
"org.laptop.sugar.DataStore")
def _create_file(self):
activity_root = env.get_profile_path(self._activity.get_type())
instance_path = os.path.join(activity_root, "instance")
file_path = os.path.join(instance_path, "%i" % time.time())
file_object = open(file_path, "w")
return file_path, file_object
def get_metadata(self, request):
def get_properties_reply_handler(properties):
self._client.send_result(request, [properties])
def error_handler(error):
self._client.send_error(request, error)
self._data_store.get_properties(
request["params"][0], byte_arrays=True,
reply_handler=get_properties_reply_handler,
error_handler=error_handler)
def set_metadata(self, request):
def reply_handler():
self._client.send_result(request, [])
def error_handler(error):
self._client.send_error(request, error)
uid, metadata = request["params"]
self._data_store.update(uid, metadata, "", True,
reply_handler=reply_handler,
error_handler=error_handler)
def load(self, request):
def get_filename_reply_handler(file_name):
file_object = open(file_name)
info["file_object"] = file_object
if "requested_size" in info:
send_binary(file_object.read(info["requested_size"]))
if "stream_closed" in info:
info["file_object"].close()
def get_properties_reply_handler(properties):
self._client.send_result(request, [properties])
def error_handler(error):
self._client.send_error(request, error)
def send_binary(data):
self._client.send_binary(chr(stream_id) + data)
def on_data(data):
size = struct.unpack("ii", data)[1]
if "file_object" in info:
send_binary(info["file_object"].read(size))
else:
info["requested_size"] = size
def on_close(close_request):
if "file_object" in info:
info["file_object"].close()
else:
info["stream_closed"] = True
self._client.send_result(close_request, [])
info = {}
uid, stream_id = request["params"]
self._data_store.get_filename(
uid,
reply_handler=get_filename_reply_handler,
error_handler=error_handler)
self._data_store.get_properties(
uid, byte_arrays=True,
reply_handler=get_properties_reply_handler,
error_handler=error_handler)
stream_monitor = self._client.stream_monitors[stream_id]
stream_monitor.on_data = on_data
stream_monitor.on_close = on_close
def save(self, request):
def reply_handler():
self._client.send_result(info["close_request"], [])
def error_handler(error):
self._client.send_error(info["close_request"], error)
def on_data(data):
file_object.write(data[1:])
def on_close(close_request):
file_object.close()
info["close_request"] = close_request
self._data_store.update(uid, metadata, file_path, True,
reply_handler=reply_handler,
error_handler=error_handler)
info = {}
uid, metadata, stream_id = request["params"]
file_path, file_object = self._create_file()
stream_monitor = self._client.stream_monitors[stream_id]
stream_monitor.on_data = on_data
stream_monitor.on_close = on_close
self._client.send_result(request, [])
def create(self, request):
def reply_handler(object_id):
self._client.send_result(request, [object_id])
def error_handler(error):
self._client.send_error(request, error)
self._data_store.create(request["params"][0], "", True,
reply_handler=reply_handler,
error_handler=error_handler)
class APIClient(object):
def __init__(self, session):
self._session = session
self.activity_id = None
self.stream_monitors = {}
def send_result(self, request, result):
response = {"result": result,
"error": None,
"id": request["id"]}
self._session.send_message(json.dumps(response))
def send_error(self, request, error):
response = {"result": None,
"error": error,
"id": request["id"]}
self._session.send_message(json.dumps(response))
def send_notification(self, method, params=None):
if params is None:
params = []
response = {"method": method,
"params": params}
self._session.send_message(json.dumps(response))
def send_binary(self, data):
self._session.send_message(data, binary=True)
class APIServer(object):
def __init__(self):
self._stream_monitors = {}
self._server = Server()
self._server.connect("session-started", self._session_started_cb)
self._port = self._server.start()
self._key = os.urandom(16).encode("hex")
self._apis = {}
self._apis["activity"] = ActivityAPI
self._apis["datastore"] = DatastoreAPI
def setup_environment(self):
os.environ["SUGAR_APISOCKET_PORT"] = str(self._port)
os.environ["SUGAR_APISOCKET_KEY"] = self._key
def _open_stream(self, client, request):
for stream_id in xrange(0, 255):
if stream_id not in client.stream_monitors:
client.stream_monitors[stream_id] = StreamMonitor()
break
client.send_result(request, [stream_id])
def _close_stream(self, client, request):
stream_id = request["params"][0]
stream_monitor = client.stream_monitors[stream_id]
if stream_monitor.on_close:
stream_monitor.on_close(request)
del client.stream_monitors[stream_id]
def _session_started_cb(self, server, session):
session.connect("message-received",
self._message_received_cb, APIClient(session))
def _message_received_cb(self, session, message, client):
if message.message_type == Message.TYPE_BINARY:
stream_id = ord(message.data[0])
stream_monitor = client.stream_monitors[stream_id]
stream_monitor.on_data(message.data)
return
request = json.loads(message.data)
if request["method"] == "authenticate":
params = request["params"]
if self._key == params[1]:
client.activity_id = params[0]
return
activity_id = client.activity_id
if activity_id is None:
return
if request["method"] == "open_stream":
self._open_stream(client, request)
elif request["method"] == "close_stream":
self._close_stream(client, request)
else:
api_name, method_name = request["method"].split(".")
getattr(self._apis[api_name](client), method_name)(request)
def start():
server = APIServer()
server.setup_environment()
|