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