dbus service : Invoking a JS method from DBus, Bug Fix in dynamic code generated
[contrib/cloudeebus.git] / cloudeebus / cloudeebus.js
1 /******************************************************************************
2  * Copyright 2012 Intel Corporation.
3  * 
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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  *****************************************************************************/
16
17
18
19 /*****************************************************************************/
20
21 var dbus = { // hook object for dbus types not translated by python-json
22                 Double: function(value, level) {
23                         return value;
24                 }
25 };
26
27
28
29 /*****************************************************************************/
30
31 var cloudeebus = window.cloudeebus = {version: "0.2.1"};
32
33 cloudeebus.reset = function() {
34         cloudeebus.sessionBus = null;
35         cloudeebus.systemBus = null;
36         cloudeebus.wampSession = null;
37         cloudeebus.uri = null;
38 };
39
40
41 cloudeebus.log = function(msg) { 
42 };
43
44
45 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
46         cloudeebus.reset();
47         cloudeebus.uri = uri;
48         
49         function onCloudeebusVersionCheckCB(version) {
50                 if (cloudeebus.version == version) {
51                         cloudeebus.log("Connected to " + cloudeebus.uri);
52                         if (successCB)
53                                 successCB();
54                 } else {
55                         var errorMsg = "Cloudeebus server version " + version + " and client version " + cloudeebus.version + " mismatch";
56                         cloudeebus.log(errorMsg);
57                         if (errorCB)
58                                 errorCB(errorMsg);
59                 }
60         }
61         
62         function onWAMPSessionAuthErrorCB(error) {
63                 cloudeebus.log("Authentication error: " + error.desc);
64                 if (errorCB)
65                         errorCB(error.desc);
66         }
67         
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);
72         }
73         
74         function onWAMPSessionChallengedCB(challenge) {
75                 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
76                 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
77         }
78         
79         function onWAMPSessionConnectedCB(session) {
80                 cloudeebus.wampSession = session;
81                 if (manifest)
82                         cloudeebus.wampSession.authreq(
83                                         manifest.name, 
84                                         {permissions: JSON.stringify(manifest.permissions)}
85                                 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
86                 else
87                         cloudeebus.wampSession.authreq().then(function() {
88                                 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
89                                 }, onWAMPSessionAuthErrorCB);
90         }
91
92         function onWAMPSessionErrorCB(code, reason) {
93                 if (code == ab.CONNECTION_UNSUPPORTED) {
94                         cloudeebus.log("Browser is not supported");
95                 }
96                 else {
97                         cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
98                 }
99                 if (errorCB)
100                         errorCB(reason);
101         }
102
103         return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
104 };
105
106
107 cloudeebus.SessionBus = function() {
108         return cloudeebus.sessionBus;
109 };
110
111
112 cloudeebus.SystemBus = function() {
113         return cloudeebus.systemBus;
114 };
115
116
117
118 /*****************************************************************************/
119
120 cloudeebus.BusConnection = function(name, session) {
121         this.name = name;
122         this.wampSession = session;
123         return this;
124 };
125
126
127 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
128         var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
129         if (introspectCB)
130                 proxy._introspect(introspectCB, errorCB);
131         return proxy;
132 };
133
134
135 cloudeebus.BusConnection.prototype.addService = function(serviceName, successCB, errorCB) {
136         self = this;
137         
138         cloudeebusService = new cloudeebus.Service(this.wampSession, this, serviceName);
139         
140         function busServiceAddedSuccessCB() {
141                 if (successCB)
142                         successCB(cloudeebusService);
143         }
144         
145         function busServiceAddedErrorCB(error) {
146                 if (errorCB)
147                         errorCB();
148         }
149         
150         cloudeebusService.add(busServiceAddedSuccessCB, busServiceAddedErrorCB);
151         
152         return cloudeebusService;
153 };
154
155
156 /*****************************************************************************/
157
158 cloudeebus.Service = function(session, busConnection, name) {
159         this.wampSession = session;
160         this.busConnection = busConnection; 
161         this.name = name;
162         this.isCreated = false;
163         return this;
164 };
165
166 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
167         self = this;
168         
169         function addServiceSuccessCB(dbusService) {
170                 if (successCB) {
171                         try { // calling dbus hook object function for un-translated types
172                                 successCB(dbusService);
173                         }
174                         catch (e) {
175                                 alert("Method callback exception: " + e);
176                         }
177                 }
178         }
179         
180         function addServiceErrorCB(error) {
181                 if (errorCB)
182                         errorCB(error.desc);
183         }
184
185         var arglist = [
186             this.busConnection,
187             this.name
188             ];
189
190         // call dbusSend with bus type, destination, object, message and arguments
191         self.wampSession.call("serviceAdd", arglist).then(addServiceSuccessCB, addServiceErrorCB);
192 };
193
194 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
195         self = this;
196         
197         function addAgentSuccessCB(dbusService) {
198                 if (successCB) {
199                         try { // calling dbus hook object function for un-translated types
200                                 successCB(dbusService);
201                         }
202                         catch (e) {
203                                 alert("Method callback exception: " + e);
204                         }
205                 }
206         }
207         
208         function addAgentErrorCB(error) {
209                 if (errorCB)
210                         errorCB(error.desc);
211         }
212
213         var arglist = [
214             objectPath,
215             xmlTemplate
216             ];
217
218         // call dbusSend with bus type, destination, object, message and arguments
219         self.wampSession.call("serviceAddAgent", arglist).then(addAgentSuccessCB, addAgentErrorCB);
220 };
221
222 cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
223         self.wampSession.subscribe(methodId, methodHandler);
224 };
225
226
227 /*****************************************************************************/
228
229 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
230         this.wampSession = session; 
231         this.busConnection = busConnection; 
232         this.busName = busName; 
233         this.objectPath = objectPath; 
234         return this;
235 };
236
237
238 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
239         
240         var self = this; 
241
242         function getAllPropertiesSuccessCB(props) {
243                 for (var prop in props)
244                         self[prop] = props[prop];
245                 getAllPropertiesNextInterfaceCB();
246         }
247         
248         function getAllPropertiesNextInterfaceCB() {
249                 if (self.propInterfaces.length > 0) 
250                         self.callMethod("org.freedesktop.DBus.Properties", 
251                                 "GetAll", 
252                                 [self.propInterfaces.pop()], 
253                                 getAllPropertiesSuccessCB, 
254                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
255                 else {
256                         self.propInterfaces = null;
257                         if (successCB)
258                                 successCB(self);
259                 }
260         }
261         
262         function introspectSuccessCB(str) {
263                 var parser = new DOMParser();
264                 var xmlDoc = parser.parseFromString(str, "text/xml");
265                 var interfaces = xmlDoc.getElementsByTagName("interface");
266                 self.propInterfaces = [];
267                 var supportDBusProperties = false;
268                 for (var i=0; i < interfaces.length; i++) {
269                         var ifName = interfaces[i].attributes.getNamedItem("name").value;
270                         if (ifName == "org.freedesktop.DBus.Properties")
271                                 supportDBusProperties = true;
272                         var hasProperties = false;
273                         var ifChild = interfaces[i].firstChild;
274                         while (ifChild) {
275                                 if (ifChild.nodeName == "method") {
276                                         var nArgs = 0;
277                                         var metChild = ifChild.firstChild;
278                                         while (metChild) {
279                                                 if (metChild.nodeName == "arg" &&
280                                                         metChild.attributes.getNamedItem("direction").value == "in")
281                                                                 nArgs++;
282                                                 metChild = metChild.nextSibling;
283                                         }
284                                         self._addMethod(ifName, 
285                                                         ifChild.attributes.getNamedItem("name").value, 
286                                                         nArgs);
287                                 }
288                                 else if (ifChild.nodeName == "property") {
289                                         if (!hasProperties)
290                                                 self.propInterfaces.push(ifName);
291                                         hasProperties = true;
292                                 }
293                                 ifChild = ifChild.nextSibling;
294                         }
295                 }
296                 if (supportDBusProperties && self.propInterfaces.length > 0) {
297                         self.callMethod("org.freedesktop.DBus.Properties", 
298                                 "GetAll", 
299                                 [self.propInterfaces.pop()], 
300                                 getAllPropertiesSuccessCB, 
301                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
302                 }
303                 else {
304                         self.propInterfaces = null;
305                         if (successCB)
306                                 successCB(self);
307                 }
308         }
309
310         // call Introspect on self
311         self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
312 };
313
314
315 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
316
317         var self = this;
318         
319         self[method] = function() {
320                 if (arguments.length < nArgs || arguments.length > nArgs + 2)
321                         throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
322                 var args = [];
323                 var successCB = null;
324                 var errorCB = null;
325                 for (var i=0; i < nArgs; i++ )
326                         args.push(arguments[i]);
327                 if (arguments.length > nArgs)
328                         successCB = arguments[nArgs];
329                 if (arguments.length > nArgs + 1)
330                         errorCB = arguments[nArgs + 1];
331                 self.callMethod(ifName, method, args, successCB, errorCB);
332         };
333         
334 };
335
336
337 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
338         
339         var self = this; 
340
341         function callMethodSuccessCB(str) {
342                 if (successCB) {
343                         try { // calling dbus hook object function for un-translated types
344                                 successCB.apply(self, eval(str));
345                         }
346                         catch (e) {
347                                 cloudeebus.log("Method callback exception: " + e);
348                                 if (errorCB)
349                                         errorCB(e);
350                         }
351                 }
352         }
353
354         function callMethodErrorCB(error) {
355                 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
356                 if (errorCB)
357                         errorCB(error.desc);
358         }
359
360         var arglist = [
361                 self.busConnection.name,
362                 self.busName,
363                 self.objectPath,
364                 ifName,
365                 method,
366                 JSON.stringify(args)
367         ];
368
369         // call dbusSend with bus type, destination, object, message and arguments
370         self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
371 };
372
373
374 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
375         
376         var self = this; 
377
378         function signalHandler(id, data) {
379                 if (successCB) {
380                         try { // calling dbus hook object function for un-translated types
381                                 successCB.apply(self, eval(data));
382                         }
383                         catch (e) {
384                                 cloudeebus.log("Signal handler exception: " + e);
385                                 if (errorCB)
386                                         errorCB(e);
387                         }
388                 }
389         }
390         
391         function connectToSignalSuccessCB(str) {
392                 try {
393                         self.wampSession.subscribe(str, signalHandler);
394                 }
395                 catch (e) {
396                         cloudeebus.log("Subscribe error: " + e);
397                 }
398         }
399
400         function connectToSignalErrorCB(error) {
401                 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
402                 if (errorCB)
403                         errorCB(error.desc);
404         }
405
406         var arglist = [
407                 self.busConnection.name,
408                 self.busName,
409                 self.objectPath,
410                 ifName,
411                 signal
412         ];
413
414         // call dbusSend with bus type, destination, object, message and arguments
415         self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
416 };
417
418
419 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
420         try {
421                 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
422         }
423         catch (e) {
424                 cloudeebus.log("Unsubscribe error: " + e);
425         }
426 };