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 = {version: "0.3.1"};
33 cloudeebus.reset = function() {
34 cloudeebus.sessionBus = null;
35 cloudeebus.systemBus = null;
36 cloudeebus.wampSession = null;
37 cloudeebus.uri = null;
41 cloudeebus.log = function(msg) {
45 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
49 function onCloudeebusVersionCheckCB(version) {
50 if (cloudeebus.version == version) {
51 cloudeebus.log("Connected to " + cloudeebus.uri);
55 var errorMsg = "Cloudeebus server version " + version + " and client version " + cloudeebus.version + " mismatch";
56 cloudeebus.log(errorMsg);
62 function onWAMPSessionAuthErrorCB(error) {
63 cloudeebus.log("Authentication error: " + error.desc);
68 function onWAMPSessionAuthenticatedCB(permissions) {
69 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
70 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
71 cloudeebus.wampSession.call("getVersion").then(onCloudeebusVersionCheckCB, errorCB);
74 function onWAMPSessionChallengedCB(challenge) {
75 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
76 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
79 function onWAMPSessionConnectedCB(session) {
80 cloudeebus.wampSession = session;
82 cloudeebus.wampSession.authreq(
84 {permissions: JSON.stringify(manifest.permissions)}
85 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
87 cloudeebus.wampSession.authreq().then(function() {
88 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
89 }, onWAMPSessionAuthErrorCB);
92 function onWAMPSessionErrorCB(code, reason) {
93 if (code == ab.CONNECTION_UNSUPPORTED) {
94 cloudeebus.log("Browser is not supported");
97 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
103 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
107 cloudeebus.SessionBus = function() {
108 return cloudeebus.sessionBus;
112 cloudeebus.SystemBus = function() {
113 return cloudeebus.systemBus;
118 /*****************************************************************************/
120 cloudeebus.BusConnection = function(name, session) {
122 this.wampSession = session;
127 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
128 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
130 proxy._introspect(introspectCB, errorCB);
136 /*****************************************************************************/
138 cloudeebus.Request = function(proxy, onsuccess, onerror) {
142 this.onsuccess = onsuccess;
143 this.onerror = onerror;
147 cloudeebus.Request.prototype.then = function(onsuccess, onerror) {
148 this.onsuccess = onsuccess;
149 this.onerror = onerror;
155 /*****************************************************************************/
157 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
158 this.wampSession = session;
159 this.busConnection = busConnection;
160 this.busName = busName;
161 this.objectPath = objectPath;
162 this.interfaceProxies = {};
167 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
168 return this.interfaceProxies[ifName];
172 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
176 function getAllPropertiesSuccessCB(props) {
177 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
178 for (var prop in props)
179 ifProxy[prop] = self[prop] = props[prop];
180 getAllPropertiesNextInterfaceCB();
183 function getAllPropertiesNextInterfaceCB() {
184 self.propInterfaces.pop();
185 if (self.propInterfaces.length > 0)
186 self.callMethod("org.freedesktop.DBus.Properties",
188 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
189 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
191 self.propInterfaces = null;
197 function introspectSuccessCB(str) {
198 var parser = new DOMParser();
199 var xmlDoc = parser.parseFromString(str, "text/xml");
200 var interfaces = xmlDoc.getElementsByTagName("interface");
201 self.propInterfaces = [];
202 var supportDBusProperties = false;
203 for (var i=0; i < interfaces.length; i++) {
204 var ifName = interfaces[i].attributes.getNamedItem("name").value;
205 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
206 if (ifName == "org.freedesktop.DBus.Properties")
207 supportDBusProperties = true;
208 var hasProperties = false;
209 var ifChild = interfaces[i].firstChild;
211 if (ifChild.nodeName == "method") {
213 var metChild = ifChild.firstChild;
215 if (metChild.nodeName == "arg" &&
216 metChild.attributes.getNamedItem("direction").value == "in")
218 metChild = metChild.nextSibling;
220 var metName = ifChild.attributes.getNamedItem("name").value;
222 self._addMethod(ifName, metName, nArgs);
223 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
225 else if (ifChild.nodeName == "property") {
227 self.propInterfaces.push(ifName);
228 hasProperties = true;
230 ifChild = ifChild.nextSibling;
233 if (supportDBusProperties && self.propInterfaces.length > 0) {
234 self.callMethod("org.freedesktop.DBus.Properties",
236 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
237 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
240 self.propInterfaces = null;
246 // call Introspect on self
247 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
251 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
255 self[method] = function() {
256 if (arguments.length < nArgs || arguments.length > nArgs + 2)
257 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
259 var successCB = null;
261 for (var i=0; i < nArgs; i++ )
262 args.push(arguments[i]);
263 if (arguments.length > nArgs)
264 successCB = arguments[nArgs];
265 if (arguments.length > nArgs + 1)
266 errorCB = arguments[nArgs + 1];
267 self.callMethod(ifName, method, args).then(successCB, errorCB);
272 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args) {
275 var request = new cloudeebus.Request(this, successCB, errorCB);
277 function callMethodSuccessCB(str) {
278 request.result = eval(str);
279 if (request.onsuccess) {
280 try { // calling dbus hook object function for un-translated types
281 request.onsuccess.apply(request, request.result);
284 cloudeebus.log("Method callback exception: " + e);
287 request.onerror.apply(request, e);
292 function callMethodErrorCB(error) {
293 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
294 request.error = error.desc;
296 request.onerror.apply(request, request.error);
300 self.busConnection.name,
308 // call dbusSend with bus type, destination, object, message and arguments
309 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
314 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
318 function signalHandler(id, data) {
320 try { // calling dbus hook object function for un-translated types
321 successCB.apply(self, eval(data));
324 cloudeebus.log("Signal handler exception: " + e);
331 function connectToSignalSuccessCB(str) {
333 self.wampSession.subscribe(str, signalHandler);
336 cloudeebus.log("Subscribe error: " + e);
340 function connectToSignalErrorCB(error) {
341 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
347 self.busConnection.name,
354 // call dbusSend with bus type, destination, object, message and arguments
355 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
359 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
361 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
364 cloudeebus.log("Unsubscribe error: " + e);