blob: 19d17150ea4189cb911f681ffa0c3abd8c170864 [file] [log] [blame]
keybuk@google.combf4649a2012-02-15 06:29:06 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09002// 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
avi0ad0ce02015-12-23 03:12:45 +09007#include <stdint.h>
dcheng6c1ffcf2015-12-28 11:24:50 +09008#include <utility>
avi0ad0ce02015-12-23 03:12:45 +09009
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090010#include "base/bind.h"
11#include "base/logging.h"
12#include "base/memory/ref_counted.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090013#include "base/message_loop/message_loop.h"
satorux@chromium.org5a92cf32011-09-07 05:53:30 +090014#include "base/metrics/histogram.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090015#include "base/threading/thread_restrictions.h"
avi@chromium.org78a7e7b2013-06-29 00:20:02 +090016#include "base/time/time.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090017#include "dbus/bus.h"
18#include "dbus/message.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090019#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090020#include "dbus/scoped_dbus_error.h"
armansitof4364642014-09-06 02:49:34 +090021#include "dbus/util.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090022
23namespace dbus {
24
25namespace {
26
satorux@chromium.org5a92cf32011-09-07 05:53:30 +090027// Used for success ratio histograms. 1 for success, 0 for failure.
28const int kSuccessRatioHistogramMaxValue = 2;
29
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090030} // namespace
31
32ExportedObject::ExportedObject(Bus* bus,
keybuk@google.combf4649a2012-02-15 06:29:06 +090033 const ObjectPath& object_path)
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090034 : bus_(bus),
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090035 object_path_(object_path),
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +090036 object_is_registered_(false) {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090037}
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
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090049 // Check if the method is already exported.
50 const std::string absolute_method_name =
armansitof4364642014-09-06 02:49:34 +090051 GetAbsoluteMemberName(interface_name, method_name);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090052 if (method_table_.find(absolute_method_name) != method_table_.end()) {
53 LOG(ERROR) << absolute_method_name << " is already exported";
54 return false;
55 }
56
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090057 if (!bus_->Connect())
58 return false;
59 if (!bus_->SetUpAsyncOperations())
60 return false;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090061 if (!Register())
62 return false;
63
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090064 // Add the method callback to the method table.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090065 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);
hashimoto@chromium.org955f6482013-09-26 13:32:29 +090082 bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090083}
84
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090085void 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.
hashimoto@chromium.orgb0305512012-05-23 15:55:22 +090088 CHECK(signal->SetPath(object_path_));
satorux@chromium.org7f0c4512011-08-23 16:29:21 +090089
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
satorux@chromium.org5a92cf32011-09-07 05:53:30 +090096 const base::TimeTicks start_time = base::TimeTicks::Now();
chirantand796bf72015-04-10 04:36:01 +090097 if (bus_->GetDBusTaskRunner()->RunsTasksOnCurrentThread()) {
98 // 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 }
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900112}
113
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900114void 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);
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900134 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE,
135 base::Bind(&ExportedObject::OnExported,
136 this,
137 on_exported_calback,
138 interface_name,
139 method_name,
140 success));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900141}
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
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900152void ExportedObject::SendSignalInternal(base::TimeTicks start_time,
satorux@chromium.org47d706b2011-10-04 22:47:21 +0900153 DBusMessage* signal_message) {
avi0ad0ce02015-12-23 03:12:45 +0900154 uint32_t serial = 0;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900155 bus_->Send(signal_message, &serial);
156 dbus_message_unref(signal_message);
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900157 // 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);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900162}
163
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900164bool 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) {
keybuk@google.combf4649a2012-02-15 06:29:06 +0900180 LOG(ERROR) << "Failed to register the object: " << object_path_.value()
181 << ": " << (error.is_set() ? error.message() : "");
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900182 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);
dcheng30c5a172016-04-09 07:55:04 +0900198 std::unique_ptr<MethodCall> method_call(
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900199 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.
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900205 LOG(WARNING) << "Interface is missing: " << method_call->ToString();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900206 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
207 }
208
209 // Check if we know about the method.
armansitof4364642014-09-06 02:49:34 +0900210 const std::string absolute_method_name = GetAbsoluteMemberName(
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900211 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.
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900215 LOG(WARNING) << "Unknown method: " << method_call->ToString();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900216 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
217 }
218
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900219 const base::TimeTicks start_time = base::TimeTicks::Now();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900220 if (bus_->HasDBusThread()) {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900221 // Post a task to run the method in the origin thread.
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900222 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE,
223 base::Bind(&ExportedObject::RunMethod,
224 this,
225 iter->second,
226 base::Passed(&method_call),
227 start_time));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900228 } else {
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900229 // If the D-Bus thread is not used, just call the method directly.
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900230 MethodCall* method = method_call.get();
231 iter->second.Run(method,
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900232 base::Bind(&ExportedObject::SendResponse,
233 this,
234 start_time,
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900235 base::Passed(&method_call)));
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900236 }
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900237
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,
dcheng30c5a172016-04-09 07:55:04 +0900244 std::unique_ptr<MethodCall> method_call,
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900245 base::TimeTicks start_time) {
246 bus_->AssertOnOriginThread();
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900247 MethodCall* method = method_call.get();
248 method_call_callback.Run(method,
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900249 base::Bind(&ExportedObject::SendResponse,
250 this,
251 start_time,
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900252 base::Passed(&method_call)));
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900253}
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900254
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900255void ExportedObject::SendResponse(base::TimeTicks start_time,
dcheng30c5a172016-04-09 07:55:04 +0900256 std::unique_ptr<MethodCall> method_call,
257 std::unique_ptr<Response> response) {
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900258 DCHECK(method_call);
259 if (bus_->HasDBusThread()) {
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900260 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));
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900267 } else {
dcheng6c1ffcf2015-12-28 11:24:50 +0900268 OnMethodCompleted(std::move(method_call), std::move(response), start_time);
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900269 }
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900270}
271
dcheng30c5a172016-04-09 07:55:04 +0900272void ExportedObject::OnMethodCompleted(std::unique_ptr<MethodCall> method_call,
273 std::unique_ptr<Response> response,
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900274 base::TimeTicks start_time) {
275 bus_->AssertOnDBusThread();
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900276
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900277 // Record if the method call is successful, or not. 1 if successful.
278 UMA_HISTOGRAM_ENUMERATION("DBus.ExportedMethodHandleSuccess",
279 response ? 1 : 0,
280 kSuccessRatioHistogramMaxValue);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900281
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900282 // 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
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900287 if (!response) {
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +0900288 // Something bad happened in the method call.
dcheng30c5a172016-04-09 07:55:04 +0900289 std::unique_ptr<ErrorResponse> error_response(ErrorResponse::FromMethodCall(
290 method_call.get(), DBUS_ERROR_FAILED,
291 "error occurred in " + method_call->GetMember()));
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900292 bus_->Send(error_response->raw_message(), NULL);
293 return;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900294 }
295
296 // The method call was successful.
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900297 bus_->Send(response->raw_message(), NULL);
298
satorux@chromium.org5a92cf32011-09-07 05:53:30 +0900299 // 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);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900302}
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