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, manifest, successCB, errorCB) {
39 function onWAMPSessionAuthenticatedCB(permissions) {
40 cloudeebus.log("Authenticated: " + JSON.stringify(permissions));
41 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
42 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
47 function onWAMPSessionChallengedCB(challenge) {
48 cloudeebus.log("Challenged: " + challenge);
49 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
50 cloudeebus.log("Signature: " + signature);
51 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, errorCB);
54 function onWAMPSessionConnectedCB(session) {
55 cloudeebus.log("Connected to " + cloudeebus.uri);
56 cloudeebus.wampSession = session;
57 cloudeebus.wampSession.authreq(
59 {permissions: JSON.stringify(manifest.permissions)}
60 ).then(onWAMPSessionChallengedCB, errorCB);
63 function onWAMPSessionErrorCB(code, reason) {
64 if (code == ab.CONNECTION_UNSUPPORTED) {
65 cloudeebus.log("Browser is not supported");
68 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
74 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
78 cloudeebus.SessionBus = function() {
79 return cloudeebus.sessionBus;
83 cloudeebus.SystemBus = function() {
84 return cloudeebus.systemBus;
89 /*****************************************************************************/
91 cloudeebus.BusConnection = function(name, session) {
93 this.wampSession = session;
98 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
99 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
101 proxy._introspect(introspectCB, errorCB);
107 /*****************************************************************************/
109 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
110 this.wampSession = session;
111 this.busConnection = busConnection;
112 this.busName = busName;
113 this.objectPath = objectPath;
118 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
122 function getAllPropertiesSuccessCB(props) {
123 for (var prop in props)
124 self[prop] = props[prop];
125 if (self.propInterfaces.length > 0)
126 self.callMethod("org.freedesktop.DBus.Properties",
128 [self.propInterfaces.pop()],
129 getAllPropertiesSuccessCB,
132 self.propInterfaces = null;
138 function introspectSuccessCB(str) {
139 var parser = new DOMParser();
140 var xmlDoc = parser.parseFromString(str, "text/xml");
141 var interfaces = xmlDoc.getElementsByTagName("interface");
142 self.propInterfaces = [];
143 var supportDBusProperties = false;
144 for (var i=0; i < interfaces.length; i++) {
145 var ifName = interfaces[i].attributes.getNamedItem("name").value;
146 if (ifName == "org.freedesktop.DBus.Properties")
147 supportDBusProperties = true;
148 var hasProperties = false;
149 var ifChild = interfaces[i].firstChild;
151 if (ifChild.nodeName == "method") {
153 var metChild = ifChild.firstChild;
155 if (metChild.nodeName == "arg" &&
156 metChild.attributes.getNamedItem("direction").value == "in")
158 metChild = metChild.nextSibling;
160 self._addMethod(ifName,
161 ifChild.attributes.getNamedItem("name").value,
164 else if (ifChild.nodeName == "property") {
166 self.propInterfaces.push(ifName);
167 hasProperties = true;
169 ifChild = ifChild.nextSibling;
172 if (supportDBusProperties && self.propInterfaces.length > 0) {
173 self.callMethod("org.freedesktop.DBus.Properties",
175 [self.propInterfaces.pop()],
176 getAllPropertiesSuccessCB,
180 self.propInterfaces = null;
186 // call Introspect on self
187 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
191 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
195 self[method] = function() {
196 if (arguments.length < nArgs || arguments.length > nArgs + 2)
197 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
199 var successCB = null;
201 for (var i=0; i < nArgs; i++ )
202 args.push(arguments[i]);
203 if (arguments.length > nArgs)
204 successCB = arguments[nArgs];
205 if (arguments.length > nArgs + 1)
206 errorCB = arguments[nArgs + 1];
207 self.callMethod(ifName, method, args, successCB, errorCB);
213 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
217 function callMethodSuccessCB(str) {
219 successCB.apply(self, JSON.parse(str));
222 function callMethodErrorCB(error) {
223 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
229 self.busConnection.name,
237 // call dbusSend with bus type, destination, object, message and arguments
238 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
242 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
246 function signalHandler(id, data) {
248 successCB.apply(self, JSON.parse(data));
251 function connectToSignalSuccessCB(str) {
253 self.wampSession.subscribe(str, signalHandler);
256 cloudeebus.log("Subscribe error: " + e);
260 function connectToSignalErrorCB(error) {
261 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
267 self.busConnection.name,
274 // call dbusSend with bus type, destination, object, message and arguments
275 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
279 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
281 this.wampSession.unsubscribe(this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
284 cloudeebus.log("Unsubscribe error: " + e);