Darin Petkov | 18fb2f7 | 2012-06-14 09:09:34 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/dbus_service_proxy.h" |
| 6 | |
| 7 | #include <chromeos/dbus/service_constants.h> |
| 8 | |
| 9 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #include "shill/logging.h" |
Darin Petkov | 18fb2f7 | 2012-06-14 09:09:34 +0200 | [diff] [blame] | 11 | |
| 12 | using std::string; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | DBusServiceProxy::DBusServiceProxy(DBus::Connection *connection) |
| 17 | : proxy_(connection) {} |
| 18 | |
| 19 | DBusServiceProxy::~DBusServiceProxy() {} |
| 20 | |
| 21 | void DBusServiceProxy::GetNameOwner(const string &name, |
| 22 | Error *error, |
| 23 | const StringCallback &callback, |
| 24 | int timeout) { |
| 25 | SLOG(DBus, 2) << __func__ << "(" << name << ")"; |
| 26 | scoped_ptr<StringCallback> cb(new StringCallback(callback)); |
| 27 | try { |
| 28 | proxy_.GetNameOwner(name, cb.get(), timeout); |
| 29 | cb.release(); |
| 30 | } catch (const DBus::Error &e) { |
| 31 | FromDBusError(e, error); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void DBusServiceProxy::set_name_owner_changed_callback( |
| 36 | const NameOwnerChangedCallback &callback) { |
| 37 | proxy_.set_name_owner_changed_callback(callback); |
| 38 | } |
| 39 | |
| 40 | // static |
| 41 | void DBusServiceProxy::FromDBusError(const DBus::Error &dbus_error, |
| 42 | Error *error) { |
| 43 | if (!error) { |
| 44 | return; |
| 45 | } |
| 46 | if (!dbus_error.is_set()) { |
| 47 | error->Reset(); |
| 48 | return; |
| 49 | } |
| 50 | Error::PopulateAndLog(error, Error::kOperationFailed, dbus_error.what()); |
| 51 | } |
| 52 | |
| 53 | DBusServiceProxy::Proxy::Proxy(DBus::Connection *connection) |
| 54 | : DBus::ObjectProxy(*connection, |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 55 | dbus::kDBusServicePath, dbus::kDBusServiceName) {} |
Darin Petkov | 18fb2f7 | 2012-06-14 09:09:34 +0200 | [diff] [blame] | 56 | |
| 57 | DBusServiceProxy::Proxy::~Proxy() {} |
| 58 | |
| 59 | void DBusServiceProxy::Proxy::set_name_owner_changed_callback( |
| 60 | const NameOwnerChangedCallback &callback) { |
| 61 | name_owner_changed_callback_ = callback; |
| 62 | } |
| 63 | |
| 64 | void DBusServiceProxy::Proxy::NameOwnerChanged(const string &name, |
| 65 | const string &old_owner, |
| 66 | const string &new_owner) { |
| 67 | if (!name_owner_changed_callback_.is_null()) { |
| 68 | name_owner_changed_callback_.Run(name, old_owner, new_owner); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void DBusServiceProxy::Proxy::GetNameOwnerCallback(const string &unique_name, |
| 73 | const DBus::Error &error, |
| 74 | void *data) { |
| 75 | scoped_ptr<StringCallback> callback(reinterpret_cast<StringCallback *>(data)); |
| 76 | Error e; |
| 77 | FromDBusError(error, &e); |
| 78 | callback->Run(unique_name, e); |
| 79 | } |
| 80 | |
| 81 | } // namespace shill |