keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +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/exported_object.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/memory/ref_counted.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 12 | #include "base/threading/thread_restrictions.h" |
avi@chromium.org | 78a7e7b | 2013-06-29 00:20:02 +0900 | [diff] [blame] | 13 | #include "base/time/time.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 14 | #include "dbus/bus.h" |
| 15 | #include "dbus/message.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 16 | #include "dbus/object_path.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 17 | #include "dbus/scoped_dbus_error.h" |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 18 | #include "dbus/util.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 19 | |
| 20 | namespace dbus { |
| 21 | |
| 22 | namespace { |
| 23 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 24 | // Used for success ratio histograms. 1 for success, 0 for failure. |
| 25 | const int kSuccessRatioHistogramMaxValue = 2; |
| 26 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 27 | } // namespace |
| 28 | |
| 29 | ExportedObject::ExportedObject(Bus* bus, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 30 | const ObjectPath& object_path) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 31 | : bus_(bus), |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 32 | object_path_(object_path), |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 33 | object_is_registered_(false) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ExportedObject::~ExportedObject() { |
| 37 | DCHECK(!object_is_registered_); |
| 38 | } |
| 39 | |
| 40 | bool ExportedObject::ExportMethodAndBlock( |
| 41 | const std::string& interface_name, |
| 42 | const std::string& method_name, |
| 43 | MethodCallCallback method_call_callback) { |
| 44 | bus_->AssertOnDBusThread(); |
| 45 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 46 | // Check if the method is already exported. |
| 47 | const std::string absolute_method_name = |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 48 | GetAbsoluteMemberName(interface_name, method_name); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 49 | if (method_table_.find(absolute_method_name) != method_table_.end()) { |
| 50 | LOG(ERROR) << absolute_method_name << " is already exported"; |
| 51 | return false; |
| 52 | } |
| 53 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 54 | if (!bus_->Connect()) |
| 55 | return false; |
| 56 | if (!bus_->SetUpAsyncOperations()) |
| 57 | return false; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 58 | if (!Register()) |
| 59 | return false; |
| 60 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 61 | // Add the method callback to the method table. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 62 | method_table_[absolute_method_name] = method_call_callback; |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void ExportedObject::ExportMethod(const std::string& interface_name, |
| 68 | const std::string& method_name, |
| 69 | MethodCallCallback method_call_callback, |
| 70 | OnExportedCallback on_exported_calback) { |
| 71 | bus_->AssertOnOriginThread(); |
| 72 | |
| 73 | base::Closure task = base::Bind(&ExportedObject::ExportMethodInternal, |
| 74 | this, |
| 75 | interface_name, |
| 76 | method_name, |
| 77 | method_call_callback, |
| 78 | on_exported_calback); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 79 | bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 80 | } |
| 81 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 82 | void ExportedObject::SendSignal(Signal* signal) { |
| 83 | // For signals, the object path should be set to the path to the sender |
| 84 | // object, which is this exported object here. |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 85 | CHECK(signal->SetPath(object_path_)); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 86 | |
| 87 | // Increment the reference count so we can safely reference the |
| 88 | // underlying signal message until the signal sending is complete. This |
| 89 | // will be unref'ed in SendSignalInternal(). |
| 90 | DBusMessage* signal_message = signal->raw_message(); |
| 91 | dbus_message_ref(signal_message); |
| 92 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 93 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 94 | bus_->GetDBusTaskRunner()->PostTask( |
| 95 | FROM_HERE, |
| 96 | base::Bind(&ExportedObject::SendSignalInternal, |
| 97 | this, |
| 98 | start_time, |
| 99 | signal_message)); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 100 | } |
| 101 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 102 | void ExportedObject::Unregister() { |
| 103 | bus_->AssertOnDBusThread(); |
| 104 | |
| 105 | if (!object_is_registered_) |
| 106 | return; |
| 107 | |
| 108 | bus_->UnregisterObjectPath(object_path_); |
| 109 | object_is_registered_ = false; |
| 110 | } |
| 111 | |
| 112 | void ExportedObject::ExportMethodInternal( |
| 113 | const std::string& interface_name, |
| 114 | const std::string& method_name, |
| 115 | MethodCallCallback method_call_callback, |
| 116 | OnExportedCallback on_exported_calback) { |
| 117 | bus_->AssertOnDBusThread(); |
| 118 | |
| 119 | const bool success = ExportMethodAndBlock(interface_name, |
| 120 | method_name, |
| 121 | method_call_callback); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 122 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 123 | base::Bind(&ExportedObject::OnExported, |
| 124 | this, |
| 125 | on_exported_calback, |
| 126 | interface_name, |
| 127 | method_name, |
| 128 | success)); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void ExportedObject::OnExported(OnExportedCallback on_exported_callback, |
| 132 | const std::string& interface_name, |
| 133 | const std::string& method_name, |
| 134 | bool success) { |
| 135 | bus_->AssertOnOriginThread(); |
| 136 | |
| 137 | on_exported_callback.Run(interface_name, method_name, success); |
| 138 | } |
| 139 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 140 | void ExportedObject::SendSignalInternal(base::TimeTicks start_time, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 141 | DBusMessage* signal_message) { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 142 | uint32 serial = 0; |
| 143 | bus_->Send(signal_message, &serial); |
| 144 | dbus_message_unref(signal_message); |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 145 | // Record time spent to send the the signal. This is not accurate as the |
| 146 | // signal will actually be sent from the next run of the message loop, |
| 147 | // but we can at least tell the number of signals sent. |
| 148 | UMA_HISTOGRAM_TIMES("DBus.SignalSendTime", |
| 149 | base::TimeTicks::Now() - start_time); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 150 | } |
| 151 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 152 | bool ExportedObject::Register() { |
| 153 | bus_->AssertOnDBusThread(); |
| 154 | |
| 155 | if (object_is_registered_) |
| 156 | return true; |
| 157 | |
| 158 | ScopedDBusError error; |
| 159 | |
| 160 | DBusObjectPathVTable vtable = {}; |
| 161 | vtable.message_function = &ExportedObject::HandleMessageThunk; |
| 162 | vtable.unregister_function = &ExportedObject::OnUnregisteredThunk; |
| 163 | const bool success = bus_->TryRegisterObjectPath(object_path_, |
| 164 | &vtable, |
| 165 | this, |
| 166 | error.get()); |
| 167 | if (!success) { |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 168 | LOG(ERROR) << "Failed to register the object: " << object_path_.value() |
| 169 | << ": " << (error.is_set() ? error.message() : ""); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
| 173 | object_is_registered_ = true; |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | DBusHandlerResult ExportedObject::HandleMessage( |
| 178 | DBusConnection* connection, |
| 179 | DBusMessage* raw_message) { |
| 180 | bus_->AssertOnDBusThread(); |
| 181 | DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message)); |
| 182 | |
| 183 | // raw_message will be unrefed on exit of the function. Increment the |
| 184 | // reference so we can use it in MethodCall. |
| 185 | dbus_message_ref(raw_message); |
| 186 | scoped_ptr<MethodCall> method_call( |
| 187 | MethodCall::FromRawMessage(raw_message)); |
| 188 | const std::string interface = method_call->GetInterface(); |
| 189 | const std::string member = method_call->GetMember(); |
| 190 | |
| 191 | if (interface.empty()) { |
| 192 | // We don't support method calls without interface. |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 193 | LOG(WARNING) << "Interface is missing: " << method_call->ToString(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 194 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 195 | } |
| 196 | |
| 197 | // Check if we know about the method. |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 198 | const std::string absolute_method_name = GetAbsoluteMemberName( |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 199 | interface, member); |
| 200 | MethodTable::const_iterator iter = method_table_.find(absolute_method_name); |
| 201 | if (iter == method_table_.end()) { |
| 202 | // Don't know about the method. |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 203 | LOG(WARNING) << "Unknown method: " << method_call->ToString(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 204 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 205 | } |
| 206 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 207 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 208 | if (bus_->HasDBusThread()) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 209 | // Post a task to run the method in the origin thread. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 210 | bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 211 | base::Bind(&ExportedObject::RunMethod, |
| 212 | this, |
| 213 | iter->second, |
| 214 | base::Passed(&method_call), |
| 215 | start_time)); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 216 | } else { |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 217 | // If the D-Bus thread is not used, just call the method directly. |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 218 | MethodCall* method = method_call.get(); |
| 219 | iter->second.Run(method, |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 220 | base::Bind(&ExportedObject::SendResponse, |
| 221 | this, |
| 222 | start_time, |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 223 | base::Passed(&method_call))); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 224 | } |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 225 | |
| 226 | // It's valid to say HANDLED here, and send a method response at a later |
| 227 | // time from OnMethodCompleted() asynchronously. |
| 228 | return DBUS_HANDLER_RESULT_HANDLED; |
| 229 | } |
| 230 | |
| 231 | void ExportedObject::RunMethod(MethodCallCallback method_call_callback, |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 232 | scoped_ptr<MethodCall> method_call, |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 233 | base::TimeTicks start_time) { |
| 234 | bus_->AssertOnOriginThread(); |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 235 | MethodCall* method = method_call.get(); |
| 236 | method_call_callback.Run(method, |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 237 | base::Bind(&ExportedObject::SendResponse, |
| 238 | this, |
| 239 | start_time, |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 240 | base::Passed(&method_call))); |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 241 | } |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 242 | |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 243 | void ExportedObject::SendResponse(base::TimeTicks start_time, |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 244 | scoped_ptr<MethodCall> method_call, |
| 245 | scoped_ptr<Response> response) { |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 246 | DCHECK(method_call); |
| 247 | if (bus_->HasDBusThread()) { |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 248 | bus_->GetDBusTaskRunner()->PostTask( |
| 249 | FROM_HERE, |
| 250 | base::Bind(&ExportedObject::OnMethodCompleted, |
| 251 | this, |
| 252 | base::Passed(&method_call), |
| 253 | base::Passed(&response), |
| 254 | start_time)); |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 255 | } else { |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 256 | OnMethodCompleted(method_call.Pass(), response.Pass(), start_time); |
vlaviano@chromium.org | 241215d | 2011-11-30 13:57:42 +0900 | [diff] [blame] | 257 | } |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 258 | } |
| 259 | |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 260 | void ExportedObject::OnMethodCompleted(scoped_ptr<MethodCall> method_call, |
| 261 | scoped_ptr<Response> response, |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 262 | base::TimeTicks start_time) { |
| 263 | bus_->AssertOnDBusThread(); |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 264 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 265 | // Record if the method call is successful, or not. 1 if successful. |
| 266 | UMA_HISTOGRAM_ENUMERATION("DBus.ExportedMethodHandleSuccess", |
| 267 | response ? 1 : 0, |
| 268 | kSuccessRatioHistogramMaxValue); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 269 | |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 270 | // Check if the bus is still connected. If the method takes long to |
| 271 | // complete, the bus may be shut down meanwhile. |
| 272 | if (!bus_->is_connected()) |
| 273 | return; |
| 274 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 275 | if (!response) { |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 276 | // Something bad happened in the method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 277 | scoped_ptr<ErrorResponse> error_response( |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 278 | ErrorResponse::FromMethodCall( |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 279 | method_call.get(), |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 280 | DBUS_ERROR_FAILED, |
| 281 | "error occurred in " + method_call->GetMember())); |
| 282 | bus_->Send(error_response->raw_message(), NULL); |
| 283 | return; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // The method call was successful. |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 287 | bus_->Send(response->raw_message(), NULL); |
| 288 | |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 289 | // Record time spent to handle the the method call. Don't include failures. |
| 290 | UMA_HISTOGRAM_TIMES("DBus.ExportedMethodHandleTime", |
| 291 | base::TimeTicks::Now() - start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void ExportedObject::OnUnregistered(DBusConnection* connection) { |
| 295 | } |
| 296 | |
| 297 | DBusHandlerResult ExportedObject::HandleMessageThunk( |
| 298 | DBusConnection* connection, |
| 299 | DBusMessage* raw_message, |
| 300 | void* user_data) { |
| 301 | ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data); |
| 302 | return self->HandleMessage(connection, raw_message); |
| 303 | } |
| 304 | |
| 305 | void ExportedObject::OnUnregisteredThunk(DBusConnection *connection, |
| 306 | void* user_data) { |
| 307 | ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data); |
| 308 | return self->OnUnregistered(connection); |
| 309 | } |
| 310 | |
| 311 | } // namespace dbus |