|
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* |
|
4 ** Copyright 2006, The Android Open Source Project |
|
5 ** |
|
6 ** Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 ** you may not use this file except in compliance with the License. |
|
8 ** You may obtain a copy of the License at |
|
9 ** |
|
10 ** http://www.apache.org/licenses/LICENSE-2.0 |
|
11 ** |
|
12 ** Unless required by applicable law or agreed to in writing, software |
|
13 ** distributed under the License is distributed on an "AS IS" BASIS, |
|
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 ** See the License for the specific language governing permissions and |
|
16 ** limitations under the License. |
|
17 */ |
|
18 |
|
19 #include <dbus/dbus.h> |
|
20 #include "nsAutoPtr.h" |
|
21 #include "DBusUtils.h" |
|
22 |
|
23 #undef CHROMIUM_LOG |
|
24 #if defined(MOZ_WIDGET_GONK) |
|
25 #include <android/log.h> |
|
26 #define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args); |
|
27 #else |
|
28 #define CHROMIUM_LOG(args...) printf(args); |
|
29 #endif |
|
30 |
|
31 namespace mozilla { |
|
32 namespace ipc { |
|
33 |
|
34 // |
|
35 // DBusMessageRefPtr |
|
36 // |
|
37 |
|
38 DBusMessageRefPtr::DBusMessageRefPtr(DBusMessage* aMsg) |
|
39 : mMsg(aMsg) |
|
40 { |
|
41 if (mMsg) { |
|
42 dbus_message_ref(mMsg); |
|
43 } |
|
44 } |
|
45 |
|
46 DBusMessageRefPtr::~DBusMessageRefPtr() |
|
47 { |
|
48 if (mMsg) { |
|
49 dbus_message_unref(mMsg); |
|
50 } |
|
51 } |
|
52 |
|
53 // |
|
54 // DBusReplyHandler |
|
55 // |
|
56 |
|
57 void DBusReplyHandler::Callback(DBusMessage* aReply, void* aData) |
|
58 { |
|
59 MOZ_ASSERT(aData); |
|
60 |
|
61 nsRefPtr<DBusReplyHandler> handler = |
|
62 already_AddRefed<DBusReplyHandler>(static_cast<DBusReplyHandler*>(aData)); |
|
63 |
|
64 handler->Handle(aReply); |
|
65 } |
|
66 |
|
67 // |
|
68 // Utility functions |
|
69 // |
|
70 |
|
71 void |
|
72 log_and_free_dbus_error(DBusError* err, const char* function, DBusMessage* msg) |
|
73 { |
|
74 if (msg) { |
|
75 CHROMIUM_LOG("%s: D-Bus error in %s: %s (%s)", function, |
|
76 dbus_message_get_member((msg)), (err)->name, (err)->message); |
|
77 } else { |
|
78 CHROMIUM_LOG("%s: D-Bus error: %s (%s)", __FUNCTION__, |
|
79 (err)->name, (err)->message); |
|
80 } |
|
81 dbus_error_free((err)); |
|
82 } |
|
83 |
|
84 int dbus_returns_int32(DBusMessage *reply) |
|
85 { |
|
86 DBusError err; |
|
87 int32_t ret = -1; |
|
88 |
|
89 dbus_error_init(&err); |
|
90 if (!dbus_message_get_args(reply, &err, |
|
91 DBUS_TYPE_INT32, &ret, |
|
92 DBUS_TYPE_INVALID)) { |
|
93 LOG_AND_FREE_DBUS_ERROR_WITH_MSG(&err, reply); |
|
94 } |
|
95 |
|
96 return ret; |
|
97 } |
|
98 |
|
99 } |
|
100 } |