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 function _processWrappers(wrappers, value) {
155 for (var i=0; i<wrappers.length; i++)
160 function _processWrappersAsync(wrappers, value) {
162 function processAsyncOnce() {
163 _processWrappers(wrappers, value);
164 clearInterval(taskid);
166 taskid = setInterval(processAsyncOnce, 200);
171 /*****************************************************************************/
173 cloudeebus.FutureResolver = function(future) {
174 this.future = future;
175 this.resolved = null;
180 cloudeebus.FutureResolver.prototype.resolve = function(value, sync) {
184 var then = (value && value.then && value.then.apply) ? value.then : null;
187 var acceptCallback = function(arg) {
188 self.accept(arg, true);
190 var rejectCallback = function(arg) {
191 self.reject(arg, true);
194 then.apply(value, [acceptCallback, rejectCallback]);
197 this.reject(e, true);
201 this.accept(value, sync);
205 cloudeebus.FutureResolver.prototype.accept = function(value, sync) {
209 var future = this.future;
210 future.state = "accepted";
211 future.result = value;
213 this.resolved = true;
215 _processWrappers(future._acceptWrappers, value);
217 _processWrappersAsync(future._acceptWrappers, value);
221 cloudeebus.FutureResolver.prototype.reject = function(value, sync) {
225 var future = this.future;
226 future.state = "rejected";
227 future.result = value;
229 this.resolved = true;
231 _processWrappers(future._rejectWrappers, value);
233 _processWrappersAsync(future._rejectWrappers, value);
238 /*****************************************************************************/
240 cloudeebus.Future = function(init) {
241 this.state = "pending";
243 this._acceptWrappers = [];
244 this._rejectWrappers = [];
245 this.resolver = new cloudeebus.FutureResolver(this);
248 init.apply(this, [this.resolver]);
251 this.resolver.reject(e, true);
258 cloudeebus.Future.prototype.appendWrappers = function(acceptWrapper, rejectWrapper) {
259 this._acceptWrappers.push(acceptWrapper);
260 this._rejectWrappers.push(rejectWrapper);
261 if (this.state == "accepted")
262 _processWrappersAsync(this._acceptWrappers, this.result);
263 if (this.state == "rejected")
264 _processWrappersAsync(this._rejectWrappers, this.result);
268 cloudeebus.Future.prototype.then = function(acceptCB, rejectCB) {
269 var future = new cloudeebus.Future();
270 var resolver = future.resolver;
271 var acceptWrapper, rejectWrapper;
274 acceptWrapper = function(arg) {
276 var value = acceptCB.apply(future, [arg]);
277 resolver.resolve(value, true);
280 resolver.reject(e, true);
284 acceptWrapper = function(arg) {
285 resolver.accept(arg, true);
289 rejectWrapper = function(arg) {
291 var value = rejectCB.apply(future, [arg]);
292 resolver.resolve(value, true);
295 resolver.reject(e, true);
299 rejectWrapper = function(arg) {
300 resolver.reject(arg, true);
303 this.appendWrappers(acceptWrapper,rejectWrapper);
309 /*****************************************************************************/
311 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
312 this.wampSession = session;
313 this.busConnection = busConnection;
314 this.busName = busName;
315 this.objectPath = objectPath;
316 this.interfaceProxies = {};
321 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
322 return this.interfaceProxies[ifName];
326 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
330 function getAllPropertiesSuccessCB(props) {
331 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
332 for (var prop in props)
333 ifProxy[prop] = self[prop] = props[prop];
334 getAllPropertiesNextInterfaceCB();
337 function getAllPropertiesNextInterfaceCB() {
338 self.propInterfaces.pop();
339 if (self.propInterfaces.length > 0)
340 self.callMethod("org.freedesktop.DBus.Properties",
342 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
343 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
345 self.propInterfaces = null;
351 function introspectSuccessCB(str) {
352 var parser = new DOMParser();
353 var xmlDoc = parser.parseFromString(str, "text/xml");
354 var interfaces = xmlDoc.getElementsByTagName("interface");
355 self.propInterfaces = [];
356 var supportDBusProperties = false;
357 for (var i=0; i < interfaces.length; i++) {
358 var ifName = interfaces[i].attributes.getNamedItem("name").value;
359 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
360 if (ifName == "org.freedesktop.DBus.Properties")
361 supportDBusProperties = true;
362 var hasProperties = false;
363 var ifChild = interfaces[i].firstChild;
365 if (ifChild.nodeName == "method") {
368 var metChild = ifChild.firstChild;
370 if (metChild.nodeName == "arg" &&
371 metChild.attributes.getNamedItem("direction").value == "in") {
372 signature += metChild.attributes.getNamedItem("type").value;
375 metChild = metChild.nextSibling;
377 var metName = ifChild.attributes.getNamedItem("name").value;
379 self._addMethod(ifName, metName, nArgs, signature);
380 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs, signature);
382 else if (ifChild.nodeName == "property") {
384 self.propInterfaces.push(ifName);
385 hasProperties = true;
387 ifChild = ifChild.nextSibling;
390 if (supportDBusProperties && self.propInterfaces.length > 0) {
391 self.callMethod("org.freedesktop.DBus.Properties",
393 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
394 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
397 self.propInterfaces = null;
403 // call Introspect on self
404 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
408 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs, signature) {
412 self[method] = function() {
414 for (var i=0; i < nArgs; i++ )
415 args.push(arguments[i]);
416 return self.callMethod(ifName, method, args, signature);
421 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) {
425 var future = new cloudeebus.Future(function (resolver) {
426 function callMethodSuccessCB(str) {
427 try { // calling dbus hook object function for un-translated types
428 var result = eval(str);
429 resolver.accept(result[0]);
432 cloudeebus.log("Method callback exception: " + e);
437 function callMethodErrorCB(error) {
438 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
439 resolver.reject(error.desc);
443 self.busConnection.name,
451 // call dbusSend with bus type, destination, object, message and arguments
452 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
459 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
463 function signalHandler(id, data) {
465 try { // calling dbus hook object function for un-translated types
466 successCB.apply(self, eval(data));
469 cloudeebus.log("Signal handler exception: " + e);
476 function connectToSignalSuccessCB(str) {
478 self.wampSession.subscribe(str, signalHandler);
481 cloudeebus.log("Subscribe error: " + e);
485 function connectToSignalErrorCB(error) {
486 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
492 self.busConnection.name,
499 // call dbusSend with bus type, destination, object, message and arguments
500 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
504 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
506 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
509 cloudeebus.log("Unsubscribe error: " + e);