blob: 2047f3fe6f935c62ac9e8003bc93eee762671dfd [file] [log] [blame]
[email protected]216ed0b2012-02-14 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// 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/exported_object.h"
6
avi22437c692015-12-22 18:12:457#include <stdint.h>
dchenge48600452015-12-28 02:24:508#include <utility>
avi22437c692015-12-22 18:12:459
[email protected]a51076112011-08-17 20:58:1210#include "base/bind.h"
11#include "base/logging.h"
12#include "base/memory/ref_counted.h"
[email protected]2a9ec0e2013-07-17 23:00:3013#include "base/message_loop/message_loop.h"
asvitkine12d9cda2017-01-19 15:17:3814#include "base/metrics/histogram_macros.h"
[email protected]a51076112011-08-17 20:58:1215#include "base/threading/thread_restrictions.h"
[email protected]b43e5562013-06-28 15:20:0216#include "base/time/time.h"
[email protected]a51076112011-08-17 20:58:1217#include "dbus/bus.h"
18#include "dbus/message.h"
[email protected]216ed0b2012-02-14 21:29:0619#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1220#include "dbus/scoped_dbus_error.h"
armansitoebff093d2014-09-05 17:49:3421#include "dbus/util.h"
[email protected]a51076112011-08-17 20:58:1222
23namespace dbus {
24
25namespace {
26
[email protected]a73389892011-09-06 20:53:3027// Used for success ratio histograms. 1 for success, 0 for failure.
28const int kSuccessRatioHistogramMaxValue = 2;
29
[email protected]a51076112011-08-17 20:58:1230} // namespace
31
32ExportedObject::ExportedObject(Bus* bus,
[email protected]216ed0b2012-02-14 21:29:0633 const ObjectPath& object_path)
[email protected]a51076112011-08-17 20:58:1234 : bus_(bus),
[email protected]a51076112011-08-17 20:58:1235 object_path_(object_path),
[email protected]e184cce2011-10-07 16:26:3036 object_is_registered_(false) {
[email protected]a51076112011-08-17 20:58:1237}
38
39ExportedObject::~ExportedObject() {
40 DCHECK(!object_is_registered_);
41}
42
43bool ExportedObject::ExportMethodAndBlock(
44 const std::string& interface_name,
45 const std::string& method_name,
46 MethodCallCallback method_call_callback) {
47 bus_->AssertOnDBusThread();
48
[email protected]3beaaa4e2011-08-23 07:29:2149 // Check if the method is already exported.
50 const std::string absolute_method_name =
armansitoebff093d2014-09-05 17:49:3451 GetAbsoluteMemberName(interface_name, method_name);
[email protected]3beaaa4e2011-08-23 07:29:2152 if (method_table_.find(absolute_method_name) != method_table_.end()) {
53 LOG(ERROR) << absolute_method_name << " is already exported";
54 return false;
55 }
56
[email protected]a51076112011-08-17 20:58:1257 if (!bus_->Connect())
58 return false;
59 if (!bus_->SetUpAsyncOperations())
60 return false;
[email protected]a51076112011-08-17 20:58:1261 if (!Register())
62 return false;
63
[email protected]3beaaa4e2011-08-23 07:29:2164 // Add the method callback to the method table.
[email protected]a51076112011-08-17 20:58:1265 method_table_[absolute_method_name] = method_call_callback;
66
67 return true;
68}
69
70void ExportedObject::ExportMethod(const std::string& interface_name,
71 const std::string& method_name,
72 MethodCallCallback method_call_callback,
73 OnExportedCallback on_exported_calback) {
74 bus_->AssertOnOriginThread();
75
76 base::Closure task = base::Bind(&ExportedObject::ExportMethodInternal,
77 this,
78 interface_name,
79 method_name,
80 method_call_callback,
81 on_exported_calback);
[email protected]8609be262013-09-26 04:32:2982 bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task);
[email protected]a51076112011-08-17 20:58:1283}
84
[email protected]3beaaa4e2011-08-23 07:29:2185void ExportedObject::SendSignal(Signal* signal) {
86 // For signals, the object path should be set to the path to the sender
87 // object, which is this exported object here.
[email protected]ca72ff22012-05-23 06:55:2288 CHECK(signal->SetPath(object_path_));
[email protected]3beaaa4e2011-08-23 07:29:2189
90 // Increment the reference count so we can safely reference the
91 // underlying signal message until the signal sending is complete. This
92 // will be unref'ed in SendSignalInternal().
93 DBusMessage* signal_message = signal->raw_message();
94 dbus_message_ref(signal_message);
95
[email protected]a73389892011-09-06 20:53:3096 const base::TimeTicks start_time = base::TimeTicks::Now();
peary26739a6932017-05-19 00:38:5297 if (bus_->GetDBusTaskRunner()->RunsTasksInCurrentSequence()) {
chirantan722c9772015-04-09 19:36:0198 // The Chrome OS power manager doesn't use a dedicated TaskRunner for
99 // sending DBus messages. Sending signals asynchronously can cause an
100 // inversion in the message order if the power manager calls
101 // ObjectProxy::CallMethodAndBlock() before going back to the top level of
102 // the MessageLoop: crbug.com/472361.
103 SendSignalInternal(start_time, signal_message);
104 } else {
105 bus_->GetDBusTaskRunner()->PostTask(
106 FROM_HERE,
107 base::Bind(&ExportedObject::SendSignalInternal,
108 this,
109 start_time,
110 signal_message));
111 }
[email protected]3beaaa4e2011-08-23 07:29:21112}
113
[email protected]a51076112011-08-17 20:58:12114void ExportedObject::Unregister() {
115 bus_->AssertOnDBusThread();
116
117 if (!object_is_registered_)
118 return;
119
120 bus_->UnregisterObjectPath(object_path_);
121 object_is_registered_ = false;
122}
123
124void ExportedObject::ExportMethodInternal(
125 const std::string& interface_name,
126 const std::string& method_name,
127 MethodCallCallback method_call_callback,
128 OnExportedCallback on_exported_calback) {
129 bus_->AssertOnDBusThread();
130
131 const bool success = ExportMethodAndBlock(interface_name,
132 method_name,
133 method_call_callback);
[email protected]8609be262013-09-26 04:32:29134 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE,
135 base::Bind(&ExportedObject::OnExported,
136 this,
137 on_exported_calback,
138 interface_name,
139 method_name,
140 success));
[email protected]a51076112011-08-17 20:58:12141}
142
143void ExportedObject::OnExported(OnExportedCallback on_exported_callback,
144 const std::string& interface_name,
145 const std::string& method_name,
146 bool success) {
147 bus_->AssertOnOriginThread();
148
149 on_exported_callback.Run(interface_name, method_name, success);
150}
151
[email protected]a73389892011-09-06 20:53:30152void ExportedObject::SendSignalInternal(base::TimeTicks start_time,
[email protected]1d887b272011-10-04 13:47:21153 DBusMessage* signal_message) {
avi22437c692015-12-22 18:12:45154 uint32_t serial = 0;
[email protected]3beaaa4e2011-08-23 07:29:21155 bus_->Send(signal_message, &serial);
156 dbus_message_unref(signal_message);
[email protected]a73389892011-09-06 20:53:30157 // Record time spent to send the the signal. This is not accurate as the
158 // signal will actually be sent from the next run of the message loop,
159 // but we can at least tell the number of signals sent.
160 UMA_HISTOGRAM_TIMES("DBus.SignalSendTime",
161 base::TimeTicks::Now() - start_time);
[email protected]3beaaa4e2011-08-23 07:29:21162}
163
[email protected]a51076112011-08-17 20:58:12164bool ExportedObject::Register() {
165 bus_->AssertOnDBusThread();
166
167 if (object_is_registered_)
168 return true;
169
170 ScopedDBusError error;
171
172 DBusObjectPathVTable vtable = {};
173 vtable.message_function = &ExportedObject::HandleMessageThunk;
174 vtable.unregister_function = &ExportedObject::OnUnregisteredThunk;
175 const bool success = bus_->TryRegisterObjectPath(object_path_,
176 &vtable,
177 this,
178 error.get());
179 if (!success) {
[email protected]216ed0b2012-02-14 21:29:06180 LOG(ERROR) << "Failed to register the object: " << object_path_.value()
181 << ": " << (error.is_set() ? error.message() : "");
[email protected]a51076112011-08-17 20:58:12182 return false;
183 }
184
185 object_is_registered_ = true;
186 return true;
187}
188
189DBusHandlerResult ExportedObject::HandleMessage(
190 DBusConnection* connection,
191 DBusMessage* raw_message) {
192 bus_->AssertOnDBusThread();
193 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message));
194
195 // raw_message will be unrefed on exit of the function. Increment the
196 // reference so we can use it in MethodCall.
197 dbus_message_ref(raw_message);
dcheng2a193282016-04-08 22:55:04198 std::unique_ptr<MethodCall> method_call(
[email protected]a51076112011-08-17 20:58:12199 MethodCall::FromRawMessage(raw_message));
200 const std::string interface = method_call->GetInterface();
201 const std::string member = method_call->GetMember();
202
203 if (interface.empty()) {
204 // We don't support method calls without interface.
[email protected]a73389892011-09-06 20:53:30205 LOG(WARNING) << "Interface is missing: " << method_call->ToString();
[email protected]a51076112011-08-17 20:58:12206 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
207 }
208
209 // Check if we know about the method.
armansitoebff093d2014-09-05 17:49:34210 const std::string absolute_method_name = GetAbsoluteMemberName(
[email protected]a51076112011-08-17 20:58:12211 interface, member);
212 MethodTable::const_iterator iter = method_table_.find(absolute_method_name);
213 if (iter == method_table_.end()) {
214 // Don't know about the method.
[email protected]a73389892011-09-06 20:53:30215 LOG(WARNING) << "Unknown method: " << method_call->ToString();
[email protected]a51076112011-08-17 20:58:12216 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
217 }
218
[email protected]a73389892011-09-06 20:53:30219 const base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]a51076112011-08-17 20:58:12220 if (bus_->HasDBusThread()) {
[email protected]a51076112011-08-17 20:58:12221 // Post a task to run the method in the origin thread.
[email protected]8609be262013-09-26 04:32:29222 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE,
223 base::Bind(&ExportedObject::RunMethod,
224 this,
225 iter->second,
226 base::Passed(&method_call),
227 start_time));
[email protected]a51076112011-08-17 20:58:12228 } else {
[email protected]9aa74cc2011-11-30 04:57:42229 // If the D-Bus thread is not used, just call the method directly.
[email protected]9b25d452013-02-07 09:46:24230 MethodCall* method = method_call.get();
231 iter->second.Run(method,
[email protected]9aa74cc2011-11-30 04:57:42232 base::Bind(&ExportedObject::SendResponse,
233 this,
234 start_time,
[email protected]9b25d452013-02-07 09:46:24235 base::Passed(&method_call)));
[email protected]a51076112011-08-17 20:58:12236 }
[email protected]e184cce2011-10-07 16:26:30237
238 // It's valid to say HANDLED here, and send a method response at a later
239 // time from OnMethodCompleted() asynchronously.
240 return DBUS_HANDLER_RESULT_HANDLED;
241}
242
243void ExportedObject::RunMethod(MethodCallCallback method_call_callback,
dcheng2a193282016-04-08 22:55:04244 std::unique_ptr<MethodCall> method_call,
[email protected]e184cce2011-10-07 16:26:30245 base::TimeTicks start_time) {
246 bus_->AssertOnOriginThread();
[email protected]9b25d452013-02-07 09:46:24247 MethodCall* method = method_call.get();
248 method_call_callback.Run(method,
[email protected]9aa74cc2011-11-30 04:57:42249 base::Bind(&ExportedObject::SendResponse,
250 this,
251 start_time,
[email protected]9b25d452013-02-07 09:46:24252 base::Passed(&method_call)));
[email protected]9aa74cc2011-11-30 04:57:42253}
[email protected]e184cce2011-10-07 16:26:30254
[email protected]9aa74cc2011-11-30 04:57:42255void ExportedObject::SendResponse(base::TimeTicks start_time,
dcheng2a193282016-04-08 22:55:04256 std::unique_ptr<MethodCall> method_call,
257 std::unique_ptr<Response> response) {
[email protected]9aa74cc2011-11-30 04:57:42258 DCHECK(method_call);
259 if (bus_->HasDBusThread()) {
[email protected]8609be262013-09-26 04:32:29260 bus_->GetDBusTaskRunner()->PostTask(
261 FROM_HERE,
262 base::Bind(&ExportedObject::OnMethodCompleted,
263 this,
264 base::Passed(&method_call),
265 base::Passed(&response),
266 start_time));
[email protected]9aa74cc2011-11-30 04:57:42267 } else {
dchenge48600452015-12-28 02:24:50268 OnMethodCompleted(std::move(method_call), std::move(response), start_time);
[email protected]9aa74cc2011-11-30 04:57:42269 }
[email protected]e184cce2011-10-07 16:26:30270}
271
dcheng2a193282016-04-08 22:55:04272void ExportedObject::OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
273 std::unique_ptr<Response> response,
[email protected]e184cce2011-10-07 16:26:30274 base::TimeTicks start_time) {
275 bus_->AssertOnDBusThread();
[email protected]e184cce2011-10-07 16:26:30276
[email protected]a73389892011-09-06 20:53:30277 // Record if the method call is successful, or not. 1 if successful.
278 UMA_HISTOGRAM_ENUMERATION("DBus.ExportedMethodHandleSuccess",
279 response ? 1 : 0,
280 kSuccessRatioHistogramMaxValue);
[email protected]a51076112011-08-17 20:58:12281
[email protected]e184cce2011-10-07 16:26:30282 // Check if the bus is still connected. If the method takes long to
283 // complete, the bus may be shut down meanwhile.
284 if (!bus_->is_connected())
285 return;
286
[email protected]a51076112011-08-17 20:58:12287 if (!response) {
[email protected]829f0e4c2011-08-31 18:02:43288 // Something bad happened in the method call.
dcheng2a193282016-04-08 22:55:04289 std::unique_ptr<ErrorResponse> error_response(ErrorResponse::FromMethodCall(
290 method_call.get(), DBUS_ERROR_FAILED,
291 "error occurred in " + method_call->GetMember()));
[email protected]e184cce2011-10-07 16:26:30292 bus_->Send(error_response->raw_message(), NULL);
293 return;
[email protected]a51076112011-08-17 20:58:12294 }
295
296 // The method call was successful.
[email protected]e184cce2011-10-07 16:26:30297 bus_->Send(response->raw_message(), NULL);
298
[email protected]a73389892011-09-06 20:53:30299 // Record time spent to handle the the method call. Don't include failures.
300 UMA_HISTOGRAM_TIMES("DBus.ExportedMethodHandleTime",
301 base::TimeTicks::Now() - start_time);
[email protected]a51076112011-08-17 20:58:12302}
303
304void ExportedObject::OnUnregistered(DBusConnection* connection) {
305}
306
307DBusHandlerResult ExportedObject::HandleMessageThunk(
308 DBusConnection* connection,
309 DBusMessage* raw_message,
310 void* user_data) {
311 ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data);
312 return self->HandleMessage(connection, raw_message);
313}
314
315void ExportedObject::OnUnregisteredThunk(DBusConnection *connection,
316 void* user_data) {
317 ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data);
318 return self->OnUnregistered(connection);
319}
320
321} // namespace dbus