1
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is ubufox.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2011
20
* the Initial Developer. All Rights Reserved.
23
* Chris Coulson <chris.coulson@canonical.com>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
var EXPORTED_SYMBOLS = [ "gio" ];
41
const GIO_LIBNAME = "gio-2.0";
42
const GIO_ABIS = [ 0 ];
44
const Cu = Components.utils;
46
Cu.import("resource://gre/modules/ctypes.jsm");
47
Cu.import("resource://ubufox/libs/ctypes-utils.jsm");
48
Cu.import("resource://ubufox/libs/gobject.jsm");
49
Cu.import("resource://ubufox/libs/glib.jsm");
51
["LOG", "WARN", "ERROR"].forEach(function(aName) {
52
this.__defineGetter__(aName, function() {
53
Components.utils.import("resource://gre/modules/AddonLogging.jsm");
55
LogManager.getLogger("ubufox.gio", this);
60
var asyncCbHandlers = {};
61
var asyncCbIdSerial = 0;
63
function AsyncCbData(cb, ccb) {
68
function newAsyncCbId() {
69
while (asyncCbHandlers[++asyncCbIdSerial]) {}
70
return asyncCbIdSerial;
73
function gio_defines(lib) {
76
this.GBusType = ctypes.int;
77
this.G_BUS_TYPE_STARTER = -1;
78
this.G_BUS_TYPE_NONE = 0;
79
this.G_BUS_TYPE_SYSTEM = 1;
80
this.G_BUS_TYPE_SESSION = 2;
83
this.GDBusProxyFlags = ctypes.int;
84
this.G_DBUS_PROXY_FLAGS_NONE = 0;
85
this.G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES = (1<<0);
86
this.G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS = (1<<1);
87
this.G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START = (1<<2);
90
this.GDBusCallFlags = ctypes.int;
91
this.G_DBUS_CALL_FLAGS_NONE = 0;
92
this.G_DBUS_CALL_FLAGS_NO_AUTO_START = (1<<0);
95
this.G_IO_ERROR_DBUS_ERROR = 36;
98
this.GDBusInterfaceInfo = ctypes.StructType("GDBusInterfaceInfo");
99
this.GCancellable = ctypes.StructType("GCancellable");
100
this.GAsyncResult = ctypes.StructType("GAsyncResult");
101
this.GDBusProxy = ctypes.StructType("GDBusProxy");
104
this.GAsyncReadyCallback = ctypes.FunctionType(ctypes.default_abi,
106
[gobject.GObject.ptr,
107
this.GAsyncResult.ptr,
111
lib.lazy_bind("g_dbus_proxy_new_for_bus", ctypes.void_t, this.GBusType,
112
this.GDBusProxyFlags, this.GDBusInterfaceInfo.ptr,
113
glib.gchar.ptr, glib.gchar.ptr, glib.gchar.ptr,
114
this.GCancellable.ptr, this.GAsyncReadyCallback, glib.gpointer);
115
lib.lazy_bind("g_dbus_proxy_new_for_bus_finish", this.GDBusProxy.ptr,
116
this.GAsyncResult.ptr, glib.GError.ptr.ptr);
117
lib.lazy_bind("g_dbus_proxy_call", ctypes.void_t, this.GDBusProxy.ptr,
118
glib.gchar.ptr, glib.GVariant.ptr, this.GDBusCallFlags,
119
glib.gint, this.GCancellable.ptr, this.GAsyncReadyCallback,
121
lib.lazy_bind("g_dbus_proxy_call_finish", glib.GVariant.ptr,
122
this.GDBusProxy.ptr, this.GAsyncResult.ptr, glib.GError.ptr.ptr);
123
lib.lazy_bind("g_io_error_quark", glib.GQuark);
124
lib.lazy_bind("g_dbus_error_get_remote_error", glib.gchar.ptr, glib.GError.ptr);
125
lib.lazy_bind("g_dbus_proxy_get_name_owner", glib.gchar.ptr, this.GDBusProxy.ptr);
127
// Helpers to work around jsctypes limitations
128
this.GDbusProxyNewForBus = function(aType, aFlags, aInfo, aName,
129
aPath, aInterface, aCancellable,
131
let cb = function(aObject, aResult, aData) {
133
aCallback(aObject, aResult);
137
delete asyncCbHandlers[id];
139
let ccb = gio.GAsyncReadyCallback(cb);
141
var id = newAsyncCbId();
142
asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
144
gio.g_dbus_proxy_new_for_bus(aType, aFlags, aInfo, aName, aPath,
145
aInterface, aCancellable, ccb, null);
148
this.GDbusProxyCall = function(aProxy, aMethod, aParams, aFlags, aTimeout,
149
aCancellable, aCallback) {
150
let cb = function(aObject, aResult, aData) {
152
aCallback(aObject, aResult);
156
delete asyncCbHandlers[id];
158
let ccb = gio.GAsyncReadyCallback(cb);
160
var id = newAsyncCbId();
161
asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
163
gio.g_dbus_proxy_call(aProxy, aMethod, aParams, aFlags, aTimeout,
164
aCancellable, ccb, null);
167
this.G_IO_ERROR = this.g_io_error_quark();
170
new ctypes_library(GIO_LIBNAME, GIO_ABIS, gio_defines, this);