1 /******************************************************************************
2 * Copyright 2012 Intel Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
19 /*****************************************************************************/
21 var dbus = { // hook object for dbus types not translated by python-json
22 Double: function(value, level) {
29 /*****************************************************************************/
31 var cloudeebus = window.cloudeebus = {
36 cloudeebus.reset = function() {
37 cloudeebus.sessionBus = null;
38 cloudeebus.systemBus = null;
39 cloudeebus.wampSession = null;
40 cloudeebus.uri = null;
44 cloudeebus.log = function(msg) {
48 cloudeebus.versionCheck = function(version) {
49 var ver = version.split(".");
50 var min = cloudeebus.minVersion.split(".");
51 for (var i=0; i<ver.length; i++) {
52 if (Number(ver[i]) > Number(min[i]))
54 if (Number(ver[i]) < Number(min[i]))
61 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
65 function onCloudeebusVersionCheckCB(version) {
66 if (cloudeebus.versionCheck(version)) {
67 cloudeebus.log("Connected to " + cloudeebus.uri);
71 var errorMsg = "Cloudeebus server version " + version + " unsupported, need version " + cloudeebus.minVersion + " or superior";
72 cloudeebus.log(errorMsg);
78 function onWAMPSessionAuthErrorCB(error) {
79 cloudeebus.log("Authentication error: " + error.desc);
84 function onWAMPSessionAuthenticatedCB(permissions) {
85 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
86 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
87 cloudeebus.wampSession.call("getVersion").then(onCloudeebusVersionCheckCB, errorCB);
90 function onWAMPSessionChallengedCB(challenge) {
91 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
92 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
95 function onWAMPSessionConnectedCB(session) {
96 cloudeebus.wampSession = session;
98 cloudeebus.wampSession.authreq(
100 {permissions: manifest.permissions}
101 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
103 cloudeebus.wampSession.authreq().then(function() {
104 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
105 }, onWAMPSessionAuthErrorCB);
108 function onWAMPSessionErrorCB(code, reason) {
109 if (code == ab.CONNECTION_UNSUPPORTED) {
110 cloudeebus.log("Browser is not supported");
113 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
119 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
123 cloudeebus.SessionBus = function() {
124 return cloudeebus.sessionBus;
128 cloudeebus.SystemBus = function() {
129 return cloudeebus.systemBus;
134 /*****************************************************************************/
136 cloudeebus.BusConnection = function(name, session) {
138 this.wampSession = session;
143 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
144 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
146 proxy._introspect(introspectCB, errorCB);
152 /*****************************************************************************/
154 cloudeebus.Request = function(proxy, onsuccess, onerror) {
156 this.readyState = "pending";
159 this.onsuccess = onsuccess;
160 this.onerror = onerror;
164 cloudeebus.Request.prototype.then = function(onsuccess, onerror) {
165 this.onsuccess = onsuccess;
166 this.onerror = onerror;
172 /*****************************************************************************/
174 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
175 this.wampSession = session;
176 this.busConnection = busConnection;
177 this.busName = busName;
178 this.objectPath = objectPath;
179 this.interfaceProxies = {};
184 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
185 return this.interfaceProxies[ifName];
189 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
193 function getAllPropertiesSuccessCB(props) {
194 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
195 for (var prop in props)
196 ifProxy[prop] = self[prop] = props[prop];
197 getAllPropertiesNextInterfaceCB();
200 function getAllPropertiesNextInterfaceCB() {
201 self.propInterfaces.pop();
202 if (self.propInterfaces.length > 0)
203 self.callMethod("org.freedesktop.DBus.Properties",
205 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
206 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
208 self.propInterfaces = null;
214 function introspectSuccessCB(str) {
215 var parser = new DOMParser();
216 var xmlDoc = parser.parseFromString(str, "text/xml");
217 var interfaces = xmlDoc.getElementsByTagName("interface");
218 self.propInterfaces = [];
219 var supportDBusProperties = false;
220 for (var i=0; i < interfaces.length; i++) {
221 var ifName = interfaces[i].attributes.getNamedItem("name").value;
222 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
223 if (ifName == "org.freedesktop.DBus.Properties")
224 supportDBusProperties = true;
225 var hasProperties = false;
226 var ifChild = interfaces[i].firstChild;
228 if (ifChild.nodeName == "method") {
231 var metChild = ifChild.firstChild;
233 if (metChild.nodeName == "arg" &&
234 metChild.attributes.getNamedItem("direction").value == "in") {
235 signature += metChild.attributes.getNamedItem("type").value;
238 metChild = metChild.nextSibling;
240 var metName = ifChild.attributes.getNamedItem("name").value;
242 self._addMethod(ifName, metName, nArgs, signature);
243 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs, signature);
245 else if (ifChild.nodeName == "property") {
247 self.propInterfaces.push(ifName);
248 hasProperties = true;
250 ifChild = ifChild.nextSibling;
253 if (supportDBusProperties && self.propInterfaces.length > 0) {
254 self.callMethod("org.freedesktop.DBus.Properties",
256 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
257 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
260 self.propInterfaces = null;
266 // call Introspect on self
267 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
271 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs, signature) {
275 self[method] = function() {
277 for (var i=0; i < nArgs; i++ )
278 args.push(arguments[i]);
279 return self.callMethod(ifName, method, args, signature);
283 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) {
286 var request = new cloudeebus.Request(this);
288 function callMethodSuccessCB(str) {
289 request.readyState = "done";
290 try { // calling dbus hook object function for un-translated types
291 request.result = eval(str);
292 if (request.onsuccess)
293 request.onsuccess.apply(request, request.result);
296 cloudeebus.log("Method callback exception: " + e);
299 request.onerror.apply(request, [request.error]);
303 function callMethodErrorCB(error) {
304 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
305 request.readyState = "done";
306 request.error = error.desc;
308 request.onerror.apply(request, [request.error]);
312 self.busConnection.name,
320 // call dbusSend with bus type, destination, object, message and arguments
321 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
326 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
330 function signalHandler(id, data) {
332 try { // calling dbus hook object function for un-translated types
333 successCB.apply(self, eval(data));
336 cloudeebus.log("Signal handler exception: " + e);
343 function connectToSignalSuccessCB(str) {
345 self.wampSession.subscribe(str, signalHandler);
348 cloudeebus.log("Subscribe error: " + e);
352 function connectToSignalErrorCB(error) {
353 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
359 self.busConnection.name,
366 // call dbusSend with bus type, destination, object, message and arguments
367 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
371 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
373 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
376 cloudeebus.log("Unsubscribe error: " + e);