[xpcom] Implement DBus.emitSignal() in the D-Bus XPCOM service
[browser-dbus-bridge.git] / html / DBus.js
1 /* 
2  * DBus.js, a JS wrapper for the Browser D-Bus Bridge
3  * Copyright © 2008 Movial Creative Technologies Inc
4  *
5  * Contact: Movial Creative Technologies Inc, <info@movial.com>
6  * Authors: Lauri Mylläri, <lauri.myllari@movial.fi>
7  *          Kalle Vahlman, <kalle.vahlman@movial.com>
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  * 
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  * 
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE. 
29  */
30  
31 if (!window.DBus)
32 {
33 function unWrap(method, user_data) {
34   return function() {
35     if (method)
36       method.apply(user_data, arguments[0]);
37   }
38 }
39
40 function makeVariantMaker(variant_type) {
41   var ret = function(val) {
42     dump("make " + variant_type + " from " + val + "\n");
43     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
44     var variant = Components.classes["@mozilla.org/variant;1"].createInstance(Components.interfaces.nsIWritableVariant);
45     variant['setAs' + variant_type](val);
46     dump("made " + variant.QueryInterface(Components.interfaces.nsIVariant) + " from " + variant_type + " " + val + "\n");
47     return variant;
48   };
49   return ret;
50 }
51
52 function DBusWrapper() {
53   dump("DBusWrapper() enter\n");
54
55   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
56   this.dbus = Components.classes["@movial.fi/dbus/service;1"].getService();
57   this.dbus = this.dbus.QueryInterface(Components.interfaces.IDBusService);
58   dump("this.dbus: " + this.dbus + "\n");
59
60   this.SYSTEM = this.dbus.SYSTEM;
61   this.SESSION = this.dbus.SESSION;
62
63   var types = {
64     Int32:  "Int32",
65     UInt32: "Uint32",
66     Int16:  "Int16",
67     UInt16: "Uint16",
68     Int64:  "Int64",
69     UInt64: "Uint64",
70     Double: "Double",
71     Byte:   "Int8"
72   };
73
74   /* TODO: These need their own XPCOM types/handling:
75        - ObjectPath
76        - Signature
77        - Variant?
78        - Struct?
79    */
80
81   for (var type in types) {
82     this[type] = makeVariantMaker(types[type]);
83   }
84
85   this.getMethod = function(bustype,
86                             destination,
87                             object_path,
88                             method_name,
89                             interface,
90                             signature,
91                             user_data) {
92     dump("DBusWrapper.getMethod() enter\n");
93     dump("  bustype: " + bustype + "\n");
94     dump("  destination: " + destination + "\n");
95     dump("  object_path: " + object_path + "\n");
96     dump("  method_name: " + method_name + "\n");
97     dump("  interface: " + interface + "\n");
98     dump("  signature: " + signature + "\n");
99     var method = function() {
100       dump("DBusMethod.call()\n");
101       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
102       method.dbusmethod.doCall(Array.prototype.slice.call(arguments),
103                                arguments.length);
104     }
105     method.user_data = user_data;
106     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
107     method.dbusmethod = this.dbus.getMethod(bustype,
108                                             destination,
109                                             object_path,
110                                             method_name,
111                                             interface,
112                                             signature);
113     method.dbusmethod.onReply = unWrap(method.onreply, method.user_data);
114     method.dbusmethod.onError = unWrap(method.onerror, method.user_data);
115
116     method.watch("onreply", function(id, oldval, newval) {
117                               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
118                               method.dbusmethod.onReply = unWrap(newval, method.user_data);
119                             });
120     method.watch("onerror", function(id, oldval, newval) {
121                               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
122                               method.dbusmethod.onError = unWrap(newval, method.user_data);
123                             });
124
125     method.async = true;
126     method.watch("async", function(id, oldval, newval) {
127                             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
128                             method.dbusmethod.async = newval;
129                           });
130     dump("DBusWrapper.getMethod() leave\n");
131     return method;
132   }
133
134   this.getSignal = function(bustype,
135                             interface,
136                             signal_name,
137                             sender,
138                             object_path,
139                             user_data) {
140     dump("DBusWrapper.getSignal() enter\n");
141     dump("  bustype: " + bustype + "\n");
142     dump("  interface: " + interface + "\n");
143     dump("  signal_name: " + signal_name + "\n");
144     dump("  sender: " + sender + "\n");
145     dump("  object_path: " + object_path + "\n");
146
147     var signal = {};
148
149     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
150     signal.dbussignal = this.dbus.getSignal(bustype,
151                                             interface,
152                                             signal_name,
153                                             sender,
154                                             object_path);
155     signal.user_data = user_data;
156
157     signal.dbussignal.onEmit = unWrap(signal.onemit, signal.user_data);
158     signal.watch("onemit", function(id, oldval, newval) {
159                              netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
160                              signal.dbussignal.onEmit = unWrap(newval, signal.user_data);
161                            });
162
163     signal.enabled = false;
164     signal.watch("enabled", function(id, oldval, newval) {
165                               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
166                               signal.dbussignal.enabled = newval;
167                             });
168
169     dump("DBusWrapper.getSignal() leave\n");
170     return signal;
171   }
172
173   this.emitSignal = function() {
174     var args = new Array();
175     for (var i = 0; i < arguments.length; i++)
176     {
177       if (i < 5)
178         continue;
179       args[i - 5] = arguments[i];
180     }
181     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
182     this.dbus.emitSignal(arguments[0], arguments[1],
183                          arguments[2], arguments[3],
184                          arguments[4], args, args.length);
185  };
186
187   dump("DBusWrapper() leave\n");
188 }
189
190 window.DBus = new DBusWrapper();
191
192 }xa