adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 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 "dbus/bus.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
tfarina@chromium.org | 0eed10b | 2013-04-18 06:42:40 +0900 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
avi@chromium.org | ffcdb95 | 2013-06-11 16:27:01 +0900 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 13 | #include "base/task_runner_util.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
| 15 | #include "base/threading/thread_restrictions.h" |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 16 | #include "dbus/dbus_statistics.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 17 | #include "dbus/message.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 18 | #include "dbus/object_path.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 19 | #include "dbus/object_proxy.h" |
| 20 | #include "dbus/scoped_dbus_error.h" |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 21 | #include "dbus/util.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 22 | |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 23 | namespace dbus { |
| 24 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 25 | namespace { |
| 26 | |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 27 | const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; |
stevenjb@chromium.org | c415c48 | 2014-08-09 06:57:19 +0900 | [diff] [blame] | 28 | const char kErrorObjectUnknown[] = "org.freedesktop.DBus.Error.UnknownObject"; |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 29 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 30 | // Used for success ratio histograms. 1 for success, 0 for failure. |
| 31 | const int kSuccessRatioHistogramMaxValue = 2; |
| 32 | |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 33 | // The path of D-Bus Object sending NameOwnerChanged signal. |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 34 | const char kDBusSystemObjectPath[] = "/org/freedesktop/DBus"; |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 35 | |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 36 | // The D-Bus Object interface. |
| 37 | const char kDBusSystemObjectInterface[] = "org.freedesktop.DBus"; |
| 38 | |
| 39 | // The D-Bus Object address. |
| 40 | const char kDBusSystemObjectAddress[] = "org.freedesktop.DBus"; |
| 41 | |
| 42 | // The NameOwnerChanged member in |kDBusSystemObjectInterface|. |
| 43 | const char kNameOwnerChangedMember[] = "NameOwnerChanged"; |
| 44 | |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 45 | // An empty function used for ObjectProxy::EmptyResponseCallback(). |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 46 | void EmptyResponseCallbackBody(Response* /*response*/) { |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 47 | } |
| 48 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 49 | } // namespace |
| 50 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 51 | ObjectProxy::ObjectProxy(Bus* bus, |
| 52 | const std::string& service_name, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 53 | const ObjectPath& object_path, |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 54 | int options) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 55 | : bus_(bus), |
| 56 | service_name_(service_name), |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 57 | object_path_(object_path), |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 58 | ignore_service_unknown_errors_( |
| 59 | options & IGNORE_SERVICE_UNKNOWN_ERRORS) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ObjectProxy::~ObjectProxy() { |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 63 | DCHECK(pending_calls_.empty()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Originally we tried to make |method_call| a const reference, but we |
| 67 | // gave up as dbus_connection_send_with_reply_and_block() takes a |
| 68 | // non-const pointer of DBusMessage as the second parameter. |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 69 | scoped_ptr<Response> ObjectProxy::CallMethodAndBlockWithErrorDetails( |
| 70 | MethodCall* method_call, int timeout_ms, ScopedDBusError* error) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 71 | bus_->AssertOnDBusThread(); |
| 72 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 73 | if (!bus_->Connect() || |
| 74 | !method_call->SetDestination(service_name_) || |
| 75 | !method_call->SetPath(object_path_)) |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 76 | return scoped_ptr<Response>(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 77 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 78 | DBusMessage* request_message = method_call->raw_message(); |
| 79 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 80 | // Send the message synchronously. |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 81 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 82 | DBusMessage* response_message = |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 83 | bus_->SendWithReplyAndBlock(request_message, timeout_ms, error->get()); |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 84 | // Record if the method call is successful, or not. 1 if successful. |
| 85 | UMA_HISTOGRAM_ENUMERATION("DBus.SyncMethodCallSuccess", |
| 86 | response_message ? 1 : 0, |
| 87 | kSuccessRatioHistogramMaxValue); |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 88 | statistics::AddBlockingSentMethodCall(service_name_, |
| 89 | method_call->GetInterface(), |
| 90 | method_call->GetMember()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 91 | |
| 92 | if (!response_message) { |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 93 | LogMethodCallFailure(method_call->GetInterface(), |
| 94 | method_call->GetMember(), |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 95 | error->is_set() ? error->name() : "unknown error type", |
| 96 | error->is_set() ? error->message() : ""); |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 97 | return scoped_ptr<Response>(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 98 | } |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 99 | // Record time spent for the method call. Don't include failures. |
| 100 | UMA_HISTOGRAM_TIMES("DBus.SyncMethodCallTime", |
| 101 | base::TimeTicks::Now() - start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 102 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 103 | return Response::FromRawMessage(response_message); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 104 | } |
| 105 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 106 | scoped_ptr<Response> ObjectProxy::CallMethodAndBlock(MethodCall* method_call, |
| 107 | int timeout_ms) { |
| 108 | ScopedDBusError error; |
| 109 | return CallMethodAndBlockWithErrorDetails(method_call, timeout_ms, &error); |
| 110 | } |
| 111 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 112 | void ObjectProxy::CallMethod(MethodCall* method_call, |
| 113 | int timeout_ms, |
| 114 | ResponseCallback callback) { |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 115 | CallMethodWithErrorCallback(method_call, timeout_ms, callback, |
| 116 | base::Bind(&ObjectProxy::OnCallMethodError, |
| 117 | this, |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 118 | method_call->GetInterface(), |
| 119 | method_call->GetMember(), |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 120 | callback)); |
| 121 | } |
| 122 | |
| 123 | void ObjectProxy::CallMethodWithErrorCallback(MethodCall* method_call, |
| 124 | int timeout_ms, |
| 125 | ResponseCallback callback, |
| 126 | ErrorCallback error_callback) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 127 | bus_->AssertOnOriginThread(); |
| 128 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 129 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 130 | |
| 131 | if (!method_call->SetDestination(service_name_) || |
| 132 | !method_call->SetPath(object_path_)) { |
| 133 | // In case of a failure, run the error callback with NULL. |
| 134 | DBusMessage* response_message = NULL; |
| 135 | base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
| 136 | this, |
| 137 | callback, |
| 138 | error_callback, |
| 139 | start_time, |
| 140 | response_message); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 141 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 145 | // Increment the reference count so we can safely reference the |
| 146 | // underlying request message until the method call is complete. This |
| 147 | // will be unref'ed in StartAsyncMethodCall(). |
| 148 | DBusMessage* request_message = method_call->raw_message(); |
| 149 | dbus_message_ref(request_message); |
| 150 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 151 | base::Closure task = base::Bind(&ObjectProxy::StartAsyncMethodCall, |
| 152 | this, |
| 153 | timeout_ms, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 154 | request_message, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 155 | callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 156 | error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 157 | start_time); |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 158 | statistics::AddSentMethodCall(service_name_, |
| 159 | method_call->GetInterface(), |
| 160 | method_call->GetMember()); |
| 161 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 162 | // Wait for the response in the D-Bus thread. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 163 | bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 164 | } |
| 165 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 166 | void ObjectProxy::ConnectToSignal(const std::string& interface_name, |
| 167 | const std::string& signal_name, |
| 168 | SignalCallback signal_callback, |
| 169 | OnConnectedCallback on_connected_callback) { |
| 170 | bus_->AssertOnOriginThread(); |
| 171 | |
dtapuska | d998f17 | 2015-01-23 23:41:21 +0900 | [diff] [blame] | 172 | if (bus_->HasDBusThread()) { |
| 173 | base::PostTaskAndReplyWithResult( |
| 174 | bus_->GetDBusTaskRunner(), FROM_HERE, |
| 175 | base::Bind(&ObjectProxy::ConnectToSignalInternal, this, interface_name, |
| 176 | signal_name, signal_callback), |
| 177 | base::Bind(on_connected_callback, interface_name, signal_name)); |
| 178 | } else { |
| 179 | // If the bus doesn't have a dedicated dbus thread we need to call |
| 180 | // ConnectToSignalInternal directly otherwise we might miss a signal |
| 181 | // that is currently queued if we do a PostTask. |
| 182 | const bool success = |
| 183 | ConnectToSignalInternal(interface_name, signal_name, signal_callback); |
| 184 | on_connected_callback.Run(interface_name, signal_name, success); |
| 185 | } |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 186 | } |
| 187 | |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 188 | void ObjectProxy::SetNameOwnerChangedCallback( |
| 189 | NameOwnerChangedCallback callback) { |
| 190 | bus_->AssertOnOriginThread(); |
| 191 | |
| 192 | name_owner_changed_callback_ = callback; |
| 193 | } |
| 194 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 195 | void ObjectProxy::WaitForServiceToBeAvailable( |
| 196 | WaitForServiceToBeAvailableCallback callback) { |
| 197 | bus_->AssertOnOriginThread(); |
| 198 | |
| 199 | wait_for_service_to_be_available_callbacks_.push_back(callback); |
| 200 | bus_->GetDBusTaskRunner()->PostTask( |
| 201 | FROM_HERE, |
| 202 | base::Bind(&ObjectProxy::WaitForServiceToBeAvailableInternal, this)); |
| 203 | } |
| 204 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 205 | void ObjectProxy::Detach() { |
| 206 | bus_->AssertOnDBusThread(); |
| 207 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 208 | if (bus_->is_connected()) |
| 209 | bus_->RemoveFilterFunction(&ObjectProxy::HandleMessageThunk, this); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 210 | |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 211 | for (const auto& match_rule : match_rules_) { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 212 | ScopedDBusError error; |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 213 | bus_->RemoveMatch(match_rule, error.get()); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 214 | if (error.is_set()) { |
| 215 | // There is nothing we can do to recover, so just print the error. |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 216 | LOG(ERROR) << "Failed to remove match rule: " << match_rule; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 217 | } |
| 218 | } |
satorux@chromium.org | 5b3e496 | 2011-11-24 07:08:38 +0900 | [diff] [blame] | 219 | match_rules_.clear(); |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 220 | |
| 221 | for (auto* pending_call : pending_calls_) { |
| 222 | dbus_pending_call_cancel(pending_call); |
| 223 | dbus_pending_call_unref(pending_call); |
| 224 | } |
| 225 | pending_calls_.clear(); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 226 | } |
| 227 | |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 228 | // static |
| 229 | ObjectProxy::ResponseCallback ObjectProxy::EmptyResponseCallback() { |
| 230 | return base::Bind(&EmptyResponseCallbackBody); |
| 231 | } |
| 232 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 233 | ObjectProxy::OnPendingCallIsCompleteData::OnPendingCallIsCompleteData( |
| 234 | ObjectProxy* in_object_proxy, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 235 | ResponseCallback in_response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 236 | ErrorCallback in_error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 237 | base::TimeTicks in_start_time) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 238 | : object_proxy(in_object_proxy), |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 239 | response_callback(in_response_callback), |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 240 | error_callback(in_error_callback), |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 241 | start_time(in_start_time) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | ObjectProxy::OnPendingCallIsCompleteData::~OnPendingCallIsCompleteData() { |
| 245 | } |
| 246 | |
| 247 | void ObjectProxy::StartAsyncMethodCall(int timeout_ms, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 248 | DBusMessage* request_message, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 249 | ResponseCallback response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 250 | ErrorCallback error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 251 | base::TimeTicks start_time) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 252 | bus_->AssertOnDBusThread(); |
| 253 | |
| 254 | if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) { |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 255 | // In case of a failure, run the error callback with NULL. |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 256 | DBusMessage* response_message = NULL; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 257 | base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
| 258 | this, |
| 259 | response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 260 | error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 261 | start_time, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 262 | response_message); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 263 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 264 | |
| 265 | dbus_message_unref(request_message); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 269 | DBusPendingCall* pending_call = NULL; |
| 270 | |
| 271 | bus_->SendWithReply(request_message, &pending_call, timeout_ms); |
| 272 | |
| 273 | // Prepare the data we'll be passing to OnPendingCallIsCompleteThunk(). |
| 274 | // The data will be deleted in OnPendingCallIsCompleteThunk(). |
| 275 | OnPendingCallIsCompleteData* data = |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 276 | new OnPendingCallIsCompleteData(this, response_callback, error_callback, |
| 277 | start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 278 | |
| 279 | // This returns false only when unable to allocate memory. |
| 280 | const bool success = dbus_pending_call_set_notify( |
| 281 | pending_call, |
| 282 | &ObjectProxy::OnPendingCallIsCompleteThunk, |
| 283 | data, |
hashimoto | b6084f5 | 2015-03-30 15:52:21 +0900 | [diff] [blame] | 284 | &DeleteVoidPointer<OnPendingCallIsCompleteData>); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 285 | CHECK(success) << "Unable to allocate memory"; |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 286 | pending_calls_.insert(pending_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 287 | |
| 288 | // It's now safe to unref the request message. |
| 289 | dbus_message_unref(request_message); |
| 290 | } |
| 291 | |
| 292 | void ObjectProxy::OnPendingCallIsComplete(DBusPendingCall* pending_call, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 293 | ResponseCallback response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 294 | ErrorCallback error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 295 | base::TimeTicks start_time) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 296 | bus_->AssertOnDBusThread(); |
| 297 | |
| 298 | DBusMessage* response_message = dbus_pending_call_steal_reply(pending_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 299 | base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
| 300 | this, |
| 301 | response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 302 | error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 303 | start_time, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 304 | response_message); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 305 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
hashimoto | 14eb150 | 2015-03-30 16:01:39 +0900 | [diff] [blame] | 306 | |
| 307 | // Remove the pending call from the set. |
| 308 | pending_calls_.erase(pending_call); |
| 309 | dbus_pending_call_unref(pending_call); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 313 | ErrorCallback error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 314 | base::TimeTicks start_time, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 315 | DBusMessage* response_message) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 316 | bus_->AssertOnOriginThread(); |
| 317 | |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 318 | bool method_call_successful = false; |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 319 | if (!response_message) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 320 | // The response is not received. |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 321 | error_callback.Run(NULL); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 322 | } else if (dbus_message_get_type(response_message) == |
| 323 | DBUS_MESSAGE_TYPE_ERROR) { |
| 324 | // This will take |response_message| and release (unref) it. |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 325 | scoped_ptr<ErrorResponse> error_response( |
| 326 | ErrorResponse::FromRawMessage(response_message)); |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 327 | error_callback.Run(error_response.get()); |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 328 | // Delete the message on the D-Bus thread. See below for why. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 329 | bus_->GetDBusTaskRunner()->PostTask( |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 330 | FROM_HERE, |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 331 | base::Bind(&base::DeletePointer<ErrorResponse>, |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 332 | error_response.release())); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 333 | } else { |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 334 | // This will take |response_message| and release (unref) it. |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 335 | scoped_ptr<Response> response(Response::FromRawMessage(response_message)); |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 336 | // The response is successfully received. |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 337 | response_callback.Run(response.get()); |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 338 | // The message should be deleted on the D-Bus thread for a complicated |
| 339 | // reason: |
| 340 | // |
| 341 | // libdbus keeps track of the number of bytes in the incoming message |
| 342 | // queue to ensure that the data size in the queue is manageable. The |
| 343 | // bookkeeping is partly done via dbus_message_unref(), and immediately |
| 344 | // asks the client code (Chrome) to stop monitoring the underlying |
| 345 | // socket, if the number of bytes exceeds a certian number, which is set |
| 346 | // to 63MB, per dbus-transport.cc: |
| 347 | // |
| 348 | // /* Try to default to something that won't totally hose the system, |
| 349 | // * but doesn't impose too much of a limitation. |
| 350 | // */ |
| 351 | // transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63; |
| 352 | // |
| 353 | // The monitoring of the socket is done on the D-Bus thread (see Watch |
| 354 | // class in bus.cc), hence we should stop the monitoring from D-Bus |
| 355 | // thread, not from the current thread here, which is likely UI thread. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 356 | bus_->GetDBusTaskRunner()->PostTask( |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 357 | FROM_HERE, |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 358 | base::Bind(&base::DeletePointer<Response>, response.release())); |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 359 | |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 360 | method_call_successful = true; |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 361 | // Record time spent for the method call. Don't include failures. |
| 362 | UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime", |
| 363 | base::TimeTicks::Now() - start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 364 | } |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 365 | // Record if the method call is successful, or not. 1 if successful. |
| 366 | UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess", |
satorux@chromium.org | b175315 | 2011-11-11 17:36:22 +0900 | [diff] [blame] | 367 | method_call_successful, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 368 | kSuccessRatioHistogramMaxValue); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void ObjectProxy::OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call, |
| 372 | void* user_data) { |
| 373 | OnPendingCallIsCompleteData* data = |
| 374 | reinterpret_cast<OnPendingCallIsCompleteData*>(user_data); |
| 375 | ObjectProxy* self = data->object_proxy; |
| 376 | self->OnPendingCallIsComplete(pending_call, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 377 | data->response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 378 | data->error_callback, |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 379 | data->start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 380 | } |
| 381 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 382 | bool ObjectProxy::ConnectToNameOwnerChangedSignal() { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 383 | bus_->AssertOnDBusThread(); |
| 384 | |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 385 | if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) |
| 386 | return false; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 387 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 388 | bus_->AddFilterFunction(&ObjectProxy::HandleMessageThunk, this); |
| 389 | |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 390 | // Add a match_rule listening NameOwnerChanged for the well-known name |
| 391 | // |service_name_|. |
| 392 | const std::string name_owner_changed_match_rule = |
| 393 | base::StringPrintf( |
| 394 | "type='signal',interface='org.freedesktop.DBus'," |
| 395 | "member='NameOwnerChanged',path='/org/freedesktop/DBus'," |
| 396 | "sender='org.freedesktop.DBus',arg0='%s'", |
| 397 | service_name_.c_str()); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 398 | |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 399 | const bool success = |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 400 | AddMatchRuleWithoutCallback(name_owner_changed_match_rule, |
| 401 | "org.freedesktop.DBus.NameOwnerChanged"); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 402 | |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 403 | // Try getting the current name owner. It's not guaranteed that we can get |
| 404 | // the name owner at this moment, as the service may not yet be started. If |
| 405 | // that's the case, we'll get the name owner via NameOwnerChanged signal, |
| 406 | // as soon as the service is started. |
| 407 | UpdateNameOwnerAndBlock(); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 408 | |
hashimoto@chromium.org | ce5c615 | 2013-09-26 15:40:04 +0900 | [diff] [blame] | 409 | return success; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 410 | } |
| 411 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 412 | bool ObjectProxy::ConnectToSignalInternal(const std::string& interface_name, |
| 413 | const std::string& signal_name, |
| 414 | SignalCallback signal_callback) { |
| 415 | bus_->AssertOnDBusThread(); |
| 416 | |
| 417 | if (!ConnectToNameOwnerChangedSignal()) |
| 418 | return false; |
| 419 | |
| 420 | const std::string absolute_signal_name = |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 421 | GetAbsoluteMemberName(interface_name, signal_name); |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 422 | |
| 423 | // Add a match rule so the signal goes through HandleMessage(). |
| 424 | const std::string match_rule = |
| 425 | base::StringPrintf("type='signal', interface='%s', path='%s'", |
| 426 | interface_name.c_str(), |
| 427 | object_path_.value().c_str()); |
| 428 | return AddMatchRuleWithCallback(match_rule, |
| 429 | absolute_signal_name, |
| 430 | signal_callback); |
| 431 | } |
| 432 | |
| 433 | void ObjectProxy::WaitForServiceToBeAvailableInternal() { |
| 434 | bus_->AssertOnDBusThread(); |
| 435 | |
| 436 | if (!ConnectToNameOwnerChangedSignal()) { // Failed to connect to the signal. |
| 437 | const bool service_is_ready = false; |
| 438 | bus_->GetOriginTaskRunner()->PostTask( |
| 439 | FROM_HERE, |
| 440 | base::Bind(&ObjectProxy::RunWaitForServiceToBeAvailableCallbacks, |
| 441 | this, service_is_ready)); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | const bool service_is_available = !service_name_owner_.empty(); |
| 446 | if (service_is_available) { // Service is already available. |
| 447 | bus_->GetOriginTaskRunner()->PostTask( |
| 448 | FROM_HERE, |
| 449 | base::Bind(&ObjectProxy::RunWaitForServiceToBeAvailableCallbacks, |
| 450 | this, service_is_available)); |
| 451 | return; |
| 452 | } |
| 453 | } |
| 454 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 455 | DBusHandlerResult ObjectProxy::HandleMessage( |
| 456 | DBusConnection* connection, |
| 457 | DBusMessage* raw_message) { |
| 458 | bus_->AssertOnDBusThread(); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 459 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 460 | if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) |
| 461 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 462 | |
| 463 | // raw_message will be unrefed on exit of the function. Increment the |
| 464 | // reference so we can use it in Signal. |
| 465 | dbus_message_ref(raw_message); |
| 466 | scoped_ptr<Signal> signal( |
| 467 | Signal::FromRawMessage(raw_message)); |
| 468 | |
keybuk@chromium.org | b929d75 | 2012-03-01 13:01:05 +0900 | [diff] [blame] | 469 | // Verify the signal comes from the object we're proxying for, this is |
| 470 | // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and |
| 471 | // allow other object proxies to handle instead. |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 472 | const ObjectPath path = signal->GetPath(); |
keybuk@chromium.org | b929d75 | 2012-03-01 13:01:05 +0900 | [diff] [blame] | 473 | if (path != object_path_) { |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 474 | if (path.value() == kDBusSystemObjectPath && |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 475 | signal->GetMember() == kNameOwnerChangedMember) { |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 476 | // Handle NameOwnerChanged separately |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 477 | return HandleNameOwnerChanged(signal.Pass()); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 478 | } |
keybuk@chromium.org | b929d75 | 2012-03-01 13:01:05 +0900 | [diff] [blame] | 479 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 480 | } |
| 481 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 482 | const std::string interface = signal->GetInterface(); |
| 483 | const std::string member = signal->GetMember(); |
| 484 | |
stevenjb@chromium.org | 3199bd1 | 2012-11-15 06:03:29 +0900 | [diff] [blame] | 485 | statistics::AddReceivedSignal(service_name_, interface, member); |
| 486 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 487 | // Check if we know about the signal. |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 488 | const std::string absolute_signal_name = GetAbsoluteMemberName( |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 489 | interface, member); |
| 490 | MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); |
| 491 | if (iter == method_table_.end()) { |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 492 | // Don't know about the signal. |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 493 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 494 | } |
satorux@chromium.org | 63c284f | 2011-10-15 07:37:12 +0900 | [diff] [blame] | 495 | VLOG(1) << "Signal received: " << signal->ToString(); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 496 | |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 497 | std::string sender = signal->GetSender(); |
| 498 | if (service_name_owner_ != sender) { |
| 499 | LOG(ERROR) << "Rejecting a message from a wrong sender."; |
| 500 | UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); |
| 501 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 502 | } |
| 503 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 504 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 505 | if (bus_->HasDBusThread()) { |
| 506 | // Post a task to run the method in the origin thread. |
| 507 | // Transfer the ownership of |signal| to RunMethod(). |
| 508 | // |released_signal| will be deleted in RunMethod(). |
| 509 | Signal* released_signal = signal.release(); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 510 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 511 | base::Bind(&ObjectProxy::RunMethod, |
| 512 | this, |
| 513 | start_time, |
| 514 | iter->second, |
| 515 | released_signal)); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 516 | } else { |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 517 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 518 | // If the D-Bus thread is not used, just call the callback on the |
| 519 | // current thread. Transfer the ownership of |signal| to RunMethod(). |
| 520 | Signal* released_signal = signal.release(); |
| 521 | RunMethod(start_time, iter->second, released_signal); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 522 | } |
| 523 | |
hashimoto@chromium.org | fb3930a | 2013-12-17 17:41:57 +0900 | [diff] [blame] | 524 | // We don't return DBUS_HANDLER_RESULT_HANDLED for signals because other |
| 525 | // objects may be interested in them. (e.g. Signals from org.freedesktop.DBus) |
| 526 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 527 | } |
| 528 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 529 | void ObjectProxy::RunMethod(base::TimeTicks start_time, |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 530 | std::vector<SignalCallback> signal_callbacks, |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 531 | Signal* signal) { |
| 532 | bus_->AssertOnOriginThread(); |
| 533 | |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 534 | for (std::vector<SignalCallback>::iterator iter = signal_callbacks.begin(); |
| 535 | iter != signal_callbacks.end(); ++iter) |
| 536 | iter->Run(signal); |
| 537 | |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 538 | // Delete the message on the D-Bus thread. See comments in |
| 539 | // RunResponseCallback(). |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 540 | bus_->GetDBusTaskRunner()->PostTask( |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 541 | FROM_HERE, |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 542 | base::Bind(&base::DeletePointer<Signal>, signal)); |
satorux@chromium.org | 9f10a6e | 2012-06-02 11:53:20 +0900 | [diff] [blame] | 543 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 544 | // Record time spent for handling the signal. |
| 545 | UMA_HISTOGRAM_TIMES("DBus.SignalHandleTime", |
| 546 | base::TimeTicks::Now() - start_time); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | DBusHandlerResult ObjectProxy::HandleMessageThunk( |
| 550 | DBusConnection* connection, |
| 551 | DBusMessage* raw_message, |
| 552 | void* user_data) { |
| 553 | ObjectProxy* self = reinterpret_cast<ObjectProxy*>(user_data); |
| 554 | return self->HandleMessage(connection, raw_message); |
| 555 | } |
| 556 | |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 557 | void ObjectProxy::LogMethodCallFailure( |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 558 | const base::StringPiece& interface_name, |
| 559 | const base::StringPiece& method_name, |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 560 | const base::StringPiece& error_name, |
| 561 | const base::StringPiece& error_message) const { |
stevenjb@chromium.org | c415c48 | 2014-08-09 06:57:19 +0900 | [diff] [blame] | 562 | if (ignore_service_unknown_errors_ && |
| 563 | (error_name == kErrorServiceUnknown || error_name == kErrorObjectUnknown)) |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 564 | return; |
stevenjb@chromium.org | c415c48 | 2014-08-09 06:57:19 +0900 | [diff] [blame] | 565 | logging::LogSeverity severity = logging::LOG_ERROR; |
| 566 | // "UnknownObject" indicates that an object or service is no longer available, |
| 567 | // e.g. a Shill network service has gone out of range. Treat these as warnings |
| 568 | // not errors. |
| 569 | if (error_name == kErrorObjectUnknown) |
| 570 | severity = logging::LOG_WARNING; |
| 571 | std::ostringstream msg; |
| 572 | msg << "Failed to call method: " << interface_name << "." << method_name |
| 573 | << ": object_path= " << object_path_.value() |
| 574 | << ": " << error_name << ": " << error_message; |
| 575 | logging::LogAtLevel(severity, msg.str()); |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 576 | } |
| 577 | |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 578 | void ObjectProxy::OnCallMethodError(const std::string& interface_name, |
| 579 | const std::string& method_name, |
| 580 | ResponseCallback response_callback, |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 581 | ErrorResponse* error_response) { |
| 582 | if (error_response) { |
| 583 | // Error message may contain the error message as string. |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 584 | MessageReader reader(error_response); |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 585 | std::string error_message; |
| 586 | reader.PopString(&error_message); |
satorux@chromium.org | 31bb21e | 2012-05-31 15:12:11 +0900 | [diff] [blame] | 587 | LogMethodCallFailure(interface_name, |
| 588 | method_name, |
| 589 | error_response->GetErrorName(), |
| 590 | error_message); |
hashimoto@chromium.org | 0d2477e | 2012-04-20 12:18:27 +0900 | [diff] [blame] | 591 | } |
| 592 | response_callback.Run(NULL); |
| 593 | } |
| 594 | |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 595 | bool ObjectProxy::AddMatchRuleWithCallback( |
| 596 | const std::string& match_rule, |
| 597 | const std::string& absolute_signal_name, |
| 598 | SignalCallback signal_callback) { |
| 599 | DCHECK(!match_rule.empty()); |
| 600 | DCHECK(!absolute_signal_name.empty()); |
| 601 | bus_->AssertOnDBusThread(); |
| 602 | |
| 603 | if (match_rules_.find(match_rule) == match_rules_.end()) { |
| 604 | ScopedDBusError error; |
| 605 | bus_->AddMatch(match_rule, error.get()); |
| 606 | if (error.is_set()) { |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 607 | LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " |
| 608 | << error.name() << ": " << error.message(); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 609 | return false; |
| 610 | } else { |
| 611 | // Store the match rule, so that we can remove this in Detach(). |
| 612 | match_rules_.insert(match_rule); |
| 613 | // Add the signal callback to the method table. |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 614 | method_table_[absolute_signal_name].push_back(signal_callback); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 615 | return true; |
| 616 | } |
| 617 | } else { |
| 618 | // We already have the match rule. |
keybuk@chromium.org | 2594a71 | 2013-04-24 09:12:35 +0900 | [diff] [blame] | 619 | method_table_[absolute_signal_name].push_back(signal_callback); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 620 | return true; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | bool ObjectProxy::AddMatchRuleWithoutCallback( |
| 625 | const std::string& match_rule, |
| 626 | const std::string& absolute_signal_name) { |
| 627 | DCHECK(!match_rule.empty()); |
| 628 | DCHECK(!absolute_signal_name.empty()); |
| 629 | bus_->AssertOnDBusThread(); |
| 630 | |
| 631 | if (match_rules_.find(match_rule) != match_rules_.end()) |
| 632 | return true; |
| 633 | |
| 634 | ScopedDBusError error; |
| 635 | bus_->AddMatch(match_rule, error.get()); |
| 636 | if (error.is_set()) { |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 637 | LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " |
| 638 | << error.name() << ": " << error.message(); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | // Store the match rule, so that we can remove this in Detach(). |
| 642 | match_rules_.insert(match_rule); |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | void ObjectProxy::UpdateNameOwnerAndBlock() { |
| 647 | bus_->AssertOnDBusThread(); |
satorux@chromium.org | 9b1f85d | 2013-08-16 18:16:42 +0900 | [diff] [blame] | 648 | // Errors should be suppressed here, as the service may not be yet running |
| 649 | // when connecting to signals of the service, which is just fine. |
| 650 | // The ObjectProxy will be notified when the service is launched via |
| 651 | // NameOwnerChanged signal. See also comments in ConnectToSignalInternal(). |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 652 | service_name_owner_ = |
satorux@chromium.org | 9b1f85d | 2013-08-16 18:16:42 +0900 | [diff] [blame] | 653 | bus_->GetServiceOwnerAndBlock(service_name_, Bus::SUPPRESS_ERRORS); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 654 | } |
| 655 | |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 656 | DBusHandlerResult ObjectProxy::HandleNameOwnerChanged( |
| 657 | scoped_ptr<Signal> signal) { |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 658 | DCHECK(signal); |
| 659 | bus_->AssertOnDBusThread(); |
| 660 | |
| 661 | // Confirm the validity of the NameOwnerChanged signal. |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 662 | if (signal->GetMember() == kNameOwnerChangedMember && |
| 663 | signal->GetInterface() == kDBusSystemObjectInterface && |
| 664 | signal->GetSender() == kDBusSystemObjectAddress) { |
haruki@chromium.org | c8d231a | 2012-11-14 20:02:59 +0900 | [diff] [blame] | 665 | MessageReader reader(signal.get()); |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 666 | std::string name, old_owner, new_owner; |
| 667 | if (reader.PopString(&name) && |
| 668 | reader.PopString(&old_owner) && |
| 669 | reader.PopString(&new_owner) && |
| 670 | name == service_name_) { |
| 671 | service_name_owner_ = new_owner; |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 672 | bus_->GetOriginTaskRunner()->PostTask( |
| 673 | FROM_HERE, |
| 674 | base::Bind(&ObjectProxy::RunNameOwnerChangedCallback, |
| 675 | this, old_owner, new_owner)); |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 676 | |
| 677 | const bool service_is_available = !service_name_owner_.empty(); |
| 678 | if (service_is_available) { |
| 679 | bus_->GetOriginTaskRunner()->PostTask( |
| 680 | FROM_HERE, |
| 681 | base::Bind(&ObjectProxy::RunWaitForServiceToBeAvailableCallbacks, |
| 682 | this, service_is_available)); |
| 683 | } |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
keybuk@chromium.org | 359c9b6 | 2012-11-27 09:23:25 +0900 | [diff] [blame] | 687 | // Always return unhandled to let other object proxies handle the same |
| 688 | // signal. |
haruki@chromium.org | c5623ec | 2012-10-29 15:27:33 +0900 | [diff] [blame] | 689 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 690 | } |
| 691 | |
hashimoto@chromium.org | 4f3851c | 2013-09-27 16:12:03 +0900 | [diff] [blame] | 692 | void ObjectProxy::RunNameOwnerChangedCallback(const std::string& old_owner, |
| 693 | const std::string& new_owner) { |
| 694 | bus_->AssertOnOriginThread(); |
| 695 | if (!name_owner_changed_callback_.is_null()) |
| 696 | name_owner_changed_callback_.Run(old_owner, new_owner); |
| 697 | } |
| 698 | |
hashimoto@chromium.org | ed26809 | 2013-10-02 16:53:09 +0900 | [diff] [blame] | 699 | void ObjectProxy::RunWaitForServiceToBeAvailableCallbacks( |
| 700 | bool service_is_available) { |
| 701 | bus_->AssertOnOriginThread(); |
| 702 | |
| 703 | std::vector<WaitForServiceToBeAvailableCallback> callbacks; |
| 704 | callbacks.swap(wait_for_service_to_be_available_callbacks_); |
| 705 | for (size_t i = 0; i < callbacks.size(); ++i) |
| 706 | callbacks[i].Run(service_is_available); |
| 707 | } |
| 708 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 709 | } // namespace dbus |