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 cloudeebus = window.cloudeebus = {};
23 cloudeebus.reset = function() {
24 cloudeebus.sessionBus = null;
25 cloudeebus.systemBus = null;
26 cloudeebus.wampSession = null;
27 cloudeebus.uri = null;
31 cloudeebus.log = function(msg) {
35 cloudeebus.connect = function(uri, successCB, errorCB) {
39 function onWAMPSessionConnectedCB(session) {
40 cloudeebus.wampSession = session;
41 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
42 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
43 cloudeebus.log("Connected to " + cloudeebus.uri);
48 function onWAMPSessionErrorCB(code, reason) {
49 if (code == ab.CONNECTION_UNSUPPORTED) {
50 cloudeebus.log("Browser is not supported");
53 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
59 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
63 cloudeebus.SessionBus = function() {
64 return cloudeebus.sessionBus;
68 cloudeebus.SystemBus = function() {
69 return cloudeebus.systemBus;
74 /*****************************************************************************/
76 cloudeebus.BusConnection = function(name, session) {
78 this.wampSession = session;
83 cloudeebus.BusConnection.prototype.listNames = function(successCB, errorCB) {
87 function listNamesSuccessCB(str) {
89 successCB(JSON.parse(str));
92 function listNamesErrorCB(error) {
93 cloudeebus.log("Failed to list names for bus: " + self.name);
94 cloudeebus.log(error.desc);
99 // call listNames with bus name
100 self.wampSession.call("listNames", [self.name]).then(listNamesSuccessCB, listNamesErrorCB);
104 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
105 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
107 proxy._introspect(introspectCB, errorCB);
113 /*****************************************************************************/
115 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
116 this.wampSession = session;
117 this.busConnection = busConnection;
118 this.busName = busName;
119 this.objectPath = objectPath;
124 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
128 function getAllPropertiesSuccessCB(props) {
129 for (var prop in props)
130 self[prop] = props[prop];
131 if (self.propInterfaces.length > 0)
132 self.callMethod("org.freedesktop.DBus.Properties",
134 [self.propInterfaces.pop()],
135 getAllPropertiesSuccessCB,
138 self.propInterfaces = null;
144 function introspectSuccessCB(str) {
145 var parser = new DOMParser();
146 var xmlDoc = parser.parseFromString(str, "text/xml");
147 var interfaces = xmlDoc.getElementsByTagName("interface");
148 self.propInterfaces = [];
149 var supportDBusProperties = false;
150 for (var i=0; i < interfaces.length; i++) {
151 var ifName = interfaces[i].attributes.getNamedItem("name").value;
152 if (ifName == "org.freedesktop.DBus.Properties")
153 supportDBusProperties = true;
154 var hasProperties = false;
155 var ifChild = interfaces[i].firstChild;
157 if (ifChild.nodeName == "method") {
159 var metChild = ifChild.firstChild;
161 if (metChild.nodeName == "arg" &&
162 metChild.attributes.getNamedItem("direction").value == "in")
164 metChild = metChild.nextSibling;
166 self._addMethod(ifName,
167 ifChild.attributes.getNamedItem("name").value,
170 else if (ifChild.nodeName == "property") {
172 self.propInterfaces.push(ifName);
173 hasProperties = true;
175 ifChild = ifChild.nextSibling;
178 if (supportDBusProperties && self.propInterfaces.length > 0) {
179 self.callMethod("org.freedesktop.DBus.Properties",
181 [self.propInterfaces.pop()],
182 getAllPropertiesSuccessCB,
186 self.propInterfaces = null;
192 // call Introspect on self
193 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
197 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
201 self[method] = function() {
202 if (arguments.length < nArgs || arguments.length > nArgs + 2)
203 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
205 var successCB = null;
207 for (var i=0; i < nArgs; i++ )
208 args.push(arguments[i]);
209 if (arguments.length > nArgs)
210 successCB = arguments[nArgs];
211 if (arguments.length > nArgs + 1)
212 errorCB = arguments[nArgs + 1];
213 self.callMethod(ifName, method, args, successCB, errorCB);
219 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
223 function callMethodSuccessCB(str) {
225 successCB.apply(self, JSON.parse(str));
228 function callMethodErrorCB(error) {
229 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath);
230 cloudeebus.log(error.desc);
236 self.busConnection.name,
244 // call dbusSend with bus type, destination, object, message and arguments
245 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
249 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
253 function signalHandler(id, data) {
254 cloudeebus.log("Object: " + self.objectPath + " received signal: " + signal + " id: " + id);
256 successCB.apply(self, JSON.parse(data));
259 function connectToSignalSuccessCB(str) {
260 cloudeebus.log("Object: " + self.objectPath + " subscribing to signal: " + str);
261 self.wampSession.subscribe(str, signalHandler);
264 function connectToSignalErrorCB(error) {
265 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath);
266 cloudeebus.log(error.desc);
272 self.busConnection.name,
279 // call dbusSend with bus type, destination, object, message and arguments
280 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);