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.ProxyObject = function(session, busConnection, busName, objectPath) {
139 this.wampSession = session;
140 this.busConnection = busConnection;
141 this.busName = busName;
142 this.objectPath = objectPath;
143 this.interfaceProxies = {};
148 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
149 return this.interfaceProxies[ifName];
153 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
157 function getAllPropertiesSuccessCB(props) {
158 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
159 for (var prop in props)
160 ifProxy[prop] = self[prop] = props[prop];
161 getAllPropertiesNextInterfaceCB();
164 function getAllPropertiesNextInterfaceCB() {
165 self.propInterfaces.pop();
166 if (self.propInterfaces.length > 0)
167 self.callMethod("org.freedesktop.DBus.Properties",
169 [self.propInterfaces[self.propInterfaces.length-1]],
170 getAllPropertiesSuccessCB,
171 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
173 self.propInterfaces = null;
179 function introspectSuccessCB(str) {
180 var parser = new DOMParser();
181 var xmlDoc = parser.parseFromString(str, "text/xml");
182 var interfaces = xmlDoc.getElementsByTagName("interface");
183 self.propInterfaces = [];
184 var supportDBusProperties = false;
185 for (var i=0; i < interfaces.length; i++) {
186 var ifName = interfaces[i].attributes.getNamedItem("name").value;
187 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
188 if (ifName == "org.freedesktop.DBus.Properties")
189 supportDBusProperties = true;
190 var hasProperties = false;
191 var ifChild = interfaces[i].firstChild;
193 if (ifChild.nodeName == "method") {
195 var metChild = ifChild.firstChild;
197 if (metChild.nodeName == "arg" &&
198 metChild.attributes.getNamedItem("direction").value == "in")
200 metChild = metChild.nextSibling;
202 var metName = ifChild.attributes.getNamedItem("name").value;
204 self._addMethod(ifName, metName, nArgs);
205 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
207 else if (ifChild.nodeName == "property") {
209 self.propInterfaces.push(ifName);
210 hasProperties = true;
212 ifChild = ifChild.nextSibling;
215 if (supportDBusProperties && self.propInterfaces.length > 0) {
216 self.callMethod("org.freedesktop.DBus.Properties",
218 [self.propInterfaces[self.propInterfaces.length-1]],
219 getAllPropertiesSuccessCB,
220 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
223 self.propInterfaces = null;
229 // call Introspect on self
230 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
234 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
238 self[method] = function() {
239 if (arguments.length < nArgs || arguments.length > nArgs + 2)
240 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
242 var successCB = null;
244 for (var i=0; i < nArgs; i++ )
245 args.push(arguments[i]);
246 if (arguments.length > nArgs)
247 successCB = arguments[nArgs];
248 if (arguments.length > nArgs + 1)
249 errorCB = arguments[nArgs + 1];
250 self.callMethod(ifName, method, args, successCB, errorCB);
256 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
260 function callMethodSuccessCB(str) {
262 try { // calling dbus hook object function for un-translated types
263 successCB.apply(self, eval(str));
266 cloudeebus.log("Method callback exception: " + e);
273 function callMethodErrorCB(error) {
274 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
280 self.busConnection.name,
288 // call dbusSend with bus type, destination, object, message and arguments
289 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
293 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
297 function signalHandler(id, data) {
299 try { // calling dbus hook object function for un-translated types
300 successCB.apply(self, eval(data));
303 cloudeebus.log("Signal handler exception: " + e);
310 function connectToSignalSuccessCB(str) {
312 self.wampSession.subscribe(str, signalHandler);
315 cloudeebus.log("Subscribe error: " + e);
319 function connectToSignalErrorCB(error) {
320 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
326 self.busConnection.name,
333 // call dbusSend with bus type, destination, object, message and arguments
334 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
338 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
340 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
343 cloudeebus.log("Unsubscribe error: " + e);