1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/dbus/DBusUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* 1.7 +** Copyright 2006, The Android Open Source Project 1.8 +** 1.9 +** Licensed under the Apache License, Version 2.0 (the "License"); 1.10 +** you may not use this file except in compliance with the License. 1.11 +** You may obtain a copy of the License at 1.12 +** 1.13 +** http://www.apache.org/licenses/LICENSE-2.0 1.14 +** 1.15 +** Unless required by applicable law or agreed to in writing, software 1.16 +** distributed under the License is distributed on an "AS IS" BASIS, 1.17 +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.18 +** See the License for the specific language governing permissions and 1.19 +** limitations under the License. 1.20 +*/ 1.21 + 1.22 +#include <dbus/dbus.h> 1.23 +#include "nsAutoPtr.h" 1.24 +#include "DBusUtils.h" 1.25 + 1.26 +#undef CHROMIUM_LOG 1.27 +#if defined(MOZ_WIDGET_GONK) 1.28 +#include <android/log.h> 1.29 +#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args); 1.30 +#else 1.31 +#define CHROMIUM_LOG(args...) printf(args); 1.32 +#endif 1.33 + 1.34 +namespace mozilla { 1.35 +namespace ipc { 1.36 + 1.37 +// 1.38 +// DBusMessageRefPtr 1.39 +// 1.40 + 1.41 +DBusMessageRefPtr::DBusMessageRefPtr(DBusMessage* aMsg) 1.42 + : mMsg(aMsg) 1.43 +{ 1.44 + if (mMsg) { 1.45 + dbus_message_ref(mMsg); 1.46 + } 1.47 +} 1.48 + 1.49 +DBusMessageRefPtr::~DBusMessageRefPtr() 1.50 +{ 1.51 + if (mMsg) { 1.52 + dbus_message_unref(mMsg); 1.53 + } 1.54 +} 1.55 + 1.56 +// 1.57 +// DBusReplyHandler 1.58 +// 1.59 + 1.60 +void DBusReplyHandler::Callback(DBusMessage* aReply, void* aData) 1.61 +{ 1.62 + MOZ_ASSERT(aData); 1.63 + 1.64 + nsRefPtr<DBusReplyHandler> handler = 1.65 + already_AddRefed<DBusReplyHandler>(static_cast<DBusReplyHandler*>(aData)); 1.66 + 1.67 + handler->Handle(aReply); 1.68 +} 1.69 + 1.70 +// 1.71 +// Utility functions 1.72 +// 1.73 + 1.74 +void 1.75 +log_and_free_dbus_error(DBusError* err, const char* function, DBusMessage* msg) 1.76 +{ 1.77 + if (msg) { 1.78 + CHROMIUM_LOG("%s: D-Bus error in %s: %s (%s)", function, 1.79 + dbus_message_get_member((msg)), (err)->name, (err)->message); 1.80 + } else { 1.81 + CHROMIUM_LOG("%s: D-Bus error: %s (%s)", __FUNCTION__, 1.82 + (err)->name, (err)->message); 1.83 + } 1.84 + dbus_error_free((err)); 1.85 +} 1.86 + 1.87 +int dbus_returns_int32(DBusMessage *reply) 1.88 +{ 1.89 + DBusError err; 1.90 + int32_t ret = -1; 1.91 + 1.92 + dbus_error_init(&err); 1.93 + if (!dbus_message_get_args(reply, &err, 1.94 + DBUS_TYPE_INT32, &ret, 1.95 + DBUS_TYPE_INVALID)) { 1.96 + LOG_AND_FREE_DBUS_ERROR_WITH_MSG(&err, reply); 1.97 + } 1.98 + 1.99 + return ret; 1.100 +} 1.101 + 1.102 +} 1.103 +}