blob: fb9b8963c9ecde7ab4e162a306b2701d28755b1f [file] [log] [blame]
Darin Petkov18fb2f72012-06-14 09:09:34 +02001// 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 Wileyb691efd2012-08-09 13:51:51 -070010#include "shill/logging.h"
Darin Petkov18fb2f72012-06-14 09:09:34 +020011
12using std::string;
13
14namespace shill {
15
16DBusServiceProxy::DBusServiceProxy(DBus::Connection *connection)
17 : proxy_(connection) {}
18
19DBusServiceProxy::~DBusServiceProxy() {}
20
21void 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
35void DBusServiceProxy::set_name_owner_changed_callback(
36 const NameOwnerChangedCallback &callback) {
37 proxy_.set_name_owner_changed_callback(callback);
38}
39
40// static
41void 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
53DBusServiceProxy::Proxy::Proxy(DBus::Connection *connection)
54 : DBus::ObjectProxy(*connection,
Darin Petkov002c58e2012-06-19 02:56:05 +020055 dbus::kDBusServicePath, dbus::kDBusServiceName) {}
Darin Petkov18fb2f72012-06-14 09:09:34 +020056
57DBusServiceProxy::Proxy::~Proxy() {}
58
59void DBusServiceProxy::Proxy::set_name_owner_changed_callback(
60 const NameOwnerChangedCallback &callback) {
61 name_owner_changed_callback_ = callback;
62}
63
64void 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
72void 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