mobile/android/modules/RokuApp.jsm

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 "use strict";
michael@0 7
michael@0 8 this.EXPORTED_SYMBOLS = ["RokuApp"];
michael@0 9
michael@0 10 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 11
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13
michael@0 14 function log(msg) {
michael@0 15 //Services.console.logStringMessage(msg);
michael@0 16 }
michael@0 17
michael@0 18 const PROTOCOL_VERSION = 1;
michael@0 19
michael@0 20 /* RokuApp is a wrapper for interacting with a Roku channel.
michael@0 21 * The basic interactions all use a REST API.
michael@0 22 * spec: http://sdkdocs.roku.com/display/sdkdoc/External+Control+Guide
michael@0 23 */
michael@0 24 function RokuApp(service, app) {
michael@0 25 this.service = service;
michael@0 26 this.resourceURL = this.service.location;
michael@0 27 this.app = app;
michael@0 28 this.appID = -1;
michael@0 29 }
michael@0 30
michael@0 31 RokuApp.prototype = {
michael@0 32 status: function status(callback) {
michael@0 33 // We have no way to know if the app is running, so just return "unknown"
michael@0 34 // but we use this call to fetch the appID for the given app name
michael@0 35 let url = this.resourceURL + "query/apps";
michael@0 36 let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
michael@0 37 xhr.open("GET", url, true);
michael@0 38 xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
michael@0 39 xhr.overrideMimeType("text/xml");
michael@0 40
michael@0 41 xhr.addEventListener("load", (function() {
michael@0 42 if (xhr.status == 200) {
michael@0 43 let doc = xhr.responseXML;
michael@0 44 let apps = doc.querySelectorAll("app");
michael@0 45 for (let app of apps) {
michael@0 46 if (app.textContent == this.app) {
michael@0 47 this.appID = app.id;
michael@0 48 }
michael@0 49 }
michael@0 50 }
michael@0 51
michael@0 52 // Since ECP has no way of telling us if an app is running, we always return "unknown"
michael@0 53 if (callback) {
michael@0 54 callback({ state: "unknown" });
michael@0 55 }
michael@0 56 }).bind(this), false);
michael@0 57
michael@0 58 xhr.addEventListener("error", (function() {
michael@0 59 if (callback) {
michael@0 60 callback({ state: "unknown" });
michael@0 61 }
michael@0 62 }).bind(this), false);
michael@0 63
michael@0 64 xhr.send(null);
michael@0 65 },
michael@0 66
michael@0 67 start: function start(callback) {
michael@0 68 // We need to make sure we have cached the appID
michael@0 69 if (this.appID == -1) {
michael@0 70 this.status(function() {
michael@0 71 // If we found the appID, use it to make a new start call
michael@0 72 if (this.appID != -1) {
michael@0 73 this.start(callback);
michael@0 74 } else {
michael@0 75 // We failed to start the app, so let the caller know
michael@0 76 callback(false);
michael@0 77 }
michael@0 78 }.bind(this));
michael@0 79 return;
michael@0 80 }
michael@0 81
michael@0 82 // Start a given app with any extra query data. Each app uses it's own data scheme.
michael@0 83 // NOTE: Roku will also pass "source=external-control" as a param
michael@0 84 let url = this.resourceURL + "launch/" + this.appID + "?version=" + parseInt(PROTOCOL_VERSION);
michael@0 85 let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
michael@0 86 xhr.open("POST", url, true);
michael@0 87 xhr.overrideMimeType("text/plain");
michael@0 88
michael@0 89 xhr.addEventListener("load", (function() {
michael@0 90 if (callback) {
michael@0 91 callback(xhr.status === 200);
michael@0 92 }
michael@0 93 }).bind(this), false);
michael@0 94
michael@0 95 xhr.addEventListener("error", (function() {
michael@0 96 if (callback) {
michael@0 97 callback(false);
michael@0 98 }
michael@0 99 }).bind(this), false);
michael@0 100
michael@0 101 xhr.send(null);
michael@0 102 },
michael@0 103
michael@0 104 stop: function stop(callback) {
michael@0 105 // Roku doesn't seem to support stopping an app, so let's just go back to
michael@0 106 // the Home screen
michael@0 107 let url = this.resourceURL + "keypress/Home";
michael@0 108 let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
michael@0 109 xhr.open("POST", url, true);
michael@0 110 xhr.overrideMimeType("text/plain");
michael@0 111
michael@0 112 xhr.addEventListener("load", (function() {
michael@0 113 if (callback) {
michael@0 114 callback(xhr.status === 200);
michael@0 115 }
michael@0 116 }).bind(this), false);
michael@0 117
michael@0 118 xhr.addEventListener("error", (function() {
michael@0 119 if (callback) {
michael@0 120 callback(false);
michael@0 121 }
michael@0 122 }).bind(this), false);
michael@0 123
michael@0 124 xhr.send(null);
michael@0 125 },
michael@0 126
michael@0 127 remoteMedia: function remoteMedia(callback, listener) {
michael@0 128 if (this.appID != -1) {
michael@0 129 if (callback) {
michael@0 130 callback(new RemoteMedia(this.resourceURL, listener));
michael@0 131 }
michael@0 132 } else {
michael@0 133 if (callback) {
michael@0 134 callback();
michael@0 135 }
michael@0 136 }
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 /* RemoteMedia provides a wrapper for using TCP socket to control Roku apps.
michael@0 141 * The server implementation must be built into the Roku receiver app.
michael@0 142 */
michael@0 143 function RemoteMedia(url, listener) {
michael@0 144 this._url = url;
michael@0 145 this._listener = listener;
michael@0 146 this._status = "uninitialized";
michael@0 147
michael@0 148 let serverURI = Services.io.newURI(this._url , null, null);
michael@0 149 this._socket = Cc["@mozilla.org/network/socket-transport-service;1"].getService(Ci.nsISocketTransportService).createTransport(null, 0, serverURI.host, 9191, null);
michael@0 150 this._outputStream = this._socket.openOutputStream(0, 0, 0);
michael@0 151
michael@0 152 this._scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
michael@0 153
michael@0 154 this._inputStream = this._socket.openInputStream(0, 0, 0);
michael@0 155 this._pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(Ci.nsIInputStreamPump);
michael@0 156 this._pump.init(this._inputStream, -1, -1, 0, 0, true);
michael@0 157 this._pump.asyncRead(this, null);
michael@0 158 }
michael@0 159
michael@0 160 RemoteMedia.prototype = {
michael@0 161 onStartRequest: function(request, context) {
michael@0 162 },
michael@0 163
michael@0 164 onDataAvailable: function(request, context, stream, offset, count) {
michael@0 165 this._scriptableStream.init(stream);
michael@0 166 let data = this._scriptableStream.read(count);
michael@0 167 if (!data) {
michael@0 168 return;
michael@0 169 }
michael@0 170
michael@0 171 let msg = JSON.parse(data);
michael@0 172 if (this._status === msg._s) {
michael@0 173 return;
michael@0 174 }
michael@0 175
michael@0 176 this._status = msg._s;
michael@0 177
michael@0 178 if (this._listener) {
michael@0 179 // Check to see if we are getting the initial "connected" message
michael@0 180 if (this._status == "connected" && "onRemoteMediaStart" in this._listener) {
michael@0 181 this._listener.onRemoteMediaStart(this);
michael@0 182 }
michael@0 183
michael@0 184 if ("onRemoteMediaStatus" in this._listener) {
michael@0 185 this._listener.onRemoteMediaStatus(this);
michael@0 186 }
michael@0 187 }
michael@0 188 },
michael@0 189
michael@0 190 onStopRequest: function(request, context, result) {
michael@0 191 if (this._listener && "onRemoteMediaStop" in this._listener)
michael@0 192 this._listener.onRemoteMediaStop(this);
michael@0 193 },
michael@0 194
michael@0 195 _sendMsg: function _sendMsg(data) {
michael@0 196 if (!data)
michael@0 197 return;
michael@0 198
michael@0 199 // Add the protocol version
michael@0 200 data["_v"] = PROTOCOL_VERSION;
michael@0 201
michael@0 202 let raw = JSON.stringify(data);
michael@0 203 this._outputStream.write(raw, raw.length);
michael@0 204 },
michael@0 205
michael@0 206 shutdown: function shutdown() {
michael@0 207 this._outputStream.close();
michael@0 208 this._inputStream.close();
michael@0 209 },
michael@0 210
michael@0 211 get active() {
michael@0 212 return (this._socket && this._socket.isAlive());
michael@0 213 },
michael@0 214
michael@0 215 play: function play() {
michael@0 216 // TODO: add position support
michael@0 217 this._sendMsg({ type: "PLAY" });
michael@0 218 },
michael@0 219
michael@0 220 pause: function pause() {
michael@0 221 this._sendMsg({ type: "STOP" });
michael@0 222 },
michael@0 223
michael@0 224 load: function load(aData) {
michael@0 225 this._sendMsg({ type: "LOAD", title: aData.title, source: aData.source, poster: aData.poster });
michael@0 226 },
michael@0 227
michael@0 228 get status() {
michael@0 229 return this._status;
michael@0 230 }
michael@0 231 }

mercurial