Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "dbus/exported_object.h" | 5 #include "dbus/exported_object.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | |
| 11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
| 14 #include "dbus/message.h" | 15 #include "dbus/message.h" |
| 15 #include "dbus/scoped_dbus_error.h" | 16 #include "dbus/scoped_dbus_error.h" |
| 16 | 17 |
| 17 namespace dbus { | 18 namespace dbus { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 // For signals, the object path should be set to the path to the sender | 94 // For signals, the object path should be set to the path to the sender |
| 94 // object, which is this exported object here. | 95 // object, which is this exported object here. |
| 95 signal->SetPath(object_path_); | 96 signal->SetPath(object_path_); |
| 96 | 97 |
| 97 // Increment the reference count so we can safely reference the | 98 // Increment the reference count so we can safely reference the |
| 98 // underlying signal message until the signal sending is complete. This | 99 // underlying signal message until the signal sending is complete. This |
| 99 // will be unref'ed in SendSignalInternal(). | 100 // will be unref'ed in SendSignalInternal(). |
| 100 DBusMessage* signal_message = signal->raw_message(); | 101 DBusMessage* signal_message = signal->raw_message(); |
| 101 dbus_message_ref(signal_message); | 102 dbus_message_ref(signal_message); |
| 102 | 103 |
| 104 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 103 // Bind() won't compile if we pass signal_message. See the comment at | 105 // Bind() won't compile if we pass signal_message. See the comment at |
| 104 // ObjectProxy::CallMethod() for details. | 106 // ObjectProxy::CallMethod() for details. |
| 105 bus_->PostTaskToDBusThread(FROM_HERE, | 107 bus_->PostTaskToDBusThread(FROM_HERE, |
| 106 base::Bind(&ExportedObject::SendSignalInternal, | 108 base::Bind(&ExportedObject::SendSignalInternal, |
| 107 this, | 109 this, |
| 110 start_time, | |
| 108 static_cast<void*>(signal_message))); | 111 static_cast<void*>(signal_message))); |
| 109 } | 112 } |
| 110 | 113 |
| 111 void ExportedObject::Unregister() { | 114 void ExportedObject::Unregister() { |
| 112 bus_->AssertOnDBusThread(); | 115 bus_->AssertOnDBusThread(); |
| 113 | 116 |
| 114 if (!object_is_registered_) | 117 if (!object_is_registered_) |
| 115 return; | 118 return; |
| 116 | 119 |
| 117 bus_->UnregisterObjectPath(object_path_); | 120 bus_->UnregisterObjectPath(object_path_); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 139 | 142 |
| 140 void ExportedObject::OnExported(OnExportedCallback on_exported_callback, | 143 void ExportedObject::OnExported(OnExportedCallback on_exported_callback, |
| 141 const std::string& interface_name, | 144 const std::string& interface_name, |
| 142 const std::string& method_name, | 145 const std::string& method_name, |
| 143 bool success) { | 146 bool success) { |
| 144 bus_->AssertOnOriginThread(); | 147 bus_->AssertOnOriginThread(); |
| 145 | 148 |
| 146 on_exported_callback.Run(interface_name, method_name, success); | 149 on_exported_callback.Run(interface_name, method_name, success); |
| 147 } | 150 } |
| 148 | 151 |
| 149 void ExportedObject::SendSignalInternal(void* in_signal_message) { | 152 void ExportedObject::SendSignalInternal(base::TimeTicks start_time, |
| 153 void* in_signal_message) { | |
| 150 DBusMessage* signal_message = | 154 DBusMessage* signal_message = |
| 151 static_cast<DBusMessage*>(in_signal_message); | 155 static_cast<DBusMessage*>(in_signal_message); |
| 152 uint32 serial = 0; | 156 uint32 serial = 0; |
| 153 bus_->Send(signal_message, &serial); | 157 bus_->Send(signal_message, &serial); |
| 154 dbus_message_unref(signal_message); | 158 dbus_message_unref(signal_message); |
| 159 // Record time spent to send the the signal. This is not accurate as the | |
| 160 // signal will actually be sent from the next run of the message loop, | |
| 161 // but we can at least tell the number of signals sent. | |
| 162 UMA_HISTOGRAM_TIMES("DBus.SignalSendTime", | |
| 163 base::TimeTicks::Now() - start_time); | |
| 155 } | 164 } |
| 156 | 165 |
| 157 bool ExportedObject::Register() { | 166 bool ExportedObject::Register() { |
| 158 bus_->AssertOnDBusThread(); | 167 bus_->AssertOnDBusThread(); |
| 159 | 168 |
| 160 if (object_is_registered_) | 169 if (object_is_registered_) |
| 161 return true; | 170 return true; |
| 162 | 171 |
| 163 ScopedDBusError error; | 172 ScopedDBusError error; |
| 164 | 173 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 188 // raw_message will be unrefed on exit of the function. Increment the | 197 // raw_message will be unrefed on exit of the function. Increment the |
| 189 // reference so we can use it in MethodCall. | 198 // reference so we can use it in MethodCall. |
| 190 dbus_message_ref(raw_message); | 199 dbus_message_ref(raw_message); |
| 191 scoped_ptr<MethodCall> method_call( | 200 scoped_ptr<MethodCall> method_call( |
| 192 MethodCall::FromRawMessage(raw_message)); | 201 MethodCall::FromRawMessage(raw_message)); |
| 193 const std::string interface = method_call->GetInterface(); | 202 const std::string interface = method_call->GetInterface(); |
| 194 const std::string member = method_call->GetMember(); | 203 const std::string member = method_call->GetMember(); |
| 195 | 204 |
| 196 if (interface.empty()) { | 205 if (interface.empty()) { |
| 197 // We don't support method calls without interface. | 206 // We don't support method calls without interface. |
| 207 LOG(WARNING) << "Interface is missing: " << method_call->ToString(); | |
| 198 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 208 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 199 } | 209 } |
| 200 | 210 |
| 201 // Check if we know about the method. | 211 // Check if we know about the method. |
| 202 const std::string absolute_method_name = GetAbsoluteMethodName( | 212 const std::string absolute_method_name = GetAbsoluteMethodName( |
| 203 interface, member); | 213 interface, member); |
| 204 MethodTable::const_iterator iter = method_table_.find(absolute_method_name); | 214 MethodTable::const_iterator iter = method_table_.find(absolute_method_name); |
| 205 if (iter == method_table_.end()) { | 215 if (iter == method_table_.end()) { |
| 206 // Don't know about the method. | 216 // Don't know about the method. |
| 217 LOG(WARNING) << "Unknown method: " << method_call->ToString(); | |
| 207 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 218 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 208 } | 219 } |
| 209 | 220 |
| 221 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 210 Response* response = NULL; | 222 Response* response = NULL; |
| 211 if (bus_->HasDBusThread()) { | 223 if (bus_->HasDBusThread()) { |
| 212 response_from_method_ = NULL; | 224 response_from_method_ = NULL; |
| 213 // Post a task to run the method in the origin thread. | 225 // Post a task to run the method in the origin thread. |
| 214 bus_->PostTaskToOriginThread(FROM_HERE, | 226 bus_->PostTaskToOriginThread(FROM_HERE, |
| 215 base::Bind(&ExportedObject::RunMethod, | 227 base::Bind(&ExportedObject::RunMethod, |
| 216 this, | 228 this, |
| 217 iter->second, | 229 iter->second, |
| 218 method_call.get())); | 230 method_call.get())); |
| 219 // Wait until the method call is done. Blocking is not desirable but we | 231 // Wait until the method call is done. Blocking is not desirable but we |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 230 // infinitely in the origin thread. No way to stop it from here. | 242 // infinitely in the origin thread. No way to stop it from here. |
| 231 CHECK(signaled) << "Method " << absolute_method_name << " not called"; | 243 CHECK(signaled) << "Method " << absolute_method_name << " not called"; |
| 232 } | 244 } |
| 233 response = response_from_method_; | 245 response = response_from_method_; |
| 234 } else { | 246 } else { |
| 235 // If the D-Bus thread is not used, just call the method directly. We | 247 // If the D-Bus thread is not used, just call the method directly. We |
| 236 // don't need the complicated logic to wait for the method call to be | 248 // don't need the complicated logic to wait for the method call to be |
| 237 // complete. | 249 // complete. |
| 238 response = iter->second.Run(method_call.get()); | 250 response = iter->second.Run(method_call.get()); |
| 239 } | 251 } |
| 252 // Record if the method call is successful, or not. 1 if successful. | |
| 253 UMA_HISTOGRAM_ENUMERATION("DBus.ExportedMethodHandleSuccess", | |
| 254 response ? 1 : 0, 2); | |
|
stevenjb
2011/09/02 20:43:26
nit: What does the '2' refer to?
satorux1
2011/09/02 21:57:11
2 means this enumeration histogram takes values le
stevenjb
2011/09/06 16:51:04
const int histogram_max_value = 2;
On the other ha
satorux1
2011/09/06 17:32:04
You were right that 2 was cryptic. Introduced kSuc
| |
| 240 | 255 |
| 241 if (!response) { | 256 if (!response) { |
| 242 // Something bad happened in the method call. | 257 // Something bad happened in the method call. |
| 243 scoped_ptr<dbus::ErrorResponse> error_response( | 258 scoped_ptr<dbus::ErrorResponse> error_response( |
| 244 ErrorResponse::FromMethodCall(method_call.get(), | 259 ErrorResponse::FromMethodCall(method_call.get(), |
| 245 DBUS_ERROR_FAILED, | 260 DBUS_ERROR_FAILED, |
| 246 "error occurred in " + member)); | 261 "error occurred in " + member)); |
| 247 dbus_connection_send(connection, error_response->raw_message(), NULL); | 262 dbus_connection_send(connection, error_response->raw_message(), NULL); |
| 248 return DBUS_HANDLER_RESULT_HANDLED; | 263 return DBUS_HANDLER_RESULT_HANDLED; |
| 249 } | 264 } |
| 250 | 265 |
| 251 // The method call was successful. | 266 // The method call was successful. |
| 252 dbus_connection_send(connection, response->raw_message(), NULL); | 267 dbus_connection_send(connection, response->raw_message(), NULL); |
| 253 delete response; | 268 delete response; |
| 269 // Record time spent to handle the the method call. Don't include failures. | |
| 270 UMA_HISTOGRAM_TIMES("DBus.ExportedMethodHandleTime", | |
| 271 base::TimeTicks::Now() - start_time); | |
| 254 | 272 |
| 255 return DBUS_HANDLER_RESULT_HANDLED; | 273 return DBUS_HANDLER_RESULT_HANDLED; |
| 256 } | 274 } |
| 257 | 275 |
| 258 void ExportedObject::RunMethod(MethodCallCallback method_call_callback, | 276 void ExportedObject::RunMethod(MethodCallCallback method_call_callback, |
| 259 MethodCall* method_call) { | 277 MethodCall* method_call) { |
| 260 bus_->AssertOnOriginThread(); | 278 bus_->AssertOnOriginThread(); |
| 261 | 279 |
| 262 response_from_method_ = method_call_callback.Run(method_call); | 280 response_from_method_ = method_call_callback.Run(method_call); |
| 263 on_method_is_called_.Signal(); | 281 on_method_is_called_.Signal(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 274 return self->HandleMessage(connection, raw_message); | 292 return self->HandleMessage(connection, raw_message); |
| 275 } | 293 } |
| 276 | 294 |
| 277 void ExportedObject::OnUnregisteredThunk(DBusConnection *connection, | 295 void ExportedObject::OnUnregisteredThunk(DBusConnection *connection, |
| 278 void* user_data) { | 296 void* user_data) { |
| 279 ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data); | 297 ExportedObject* self = reinterpret_cast<ExportedObject*>(user_data); |
| 280 return self->OnUnregistered(connection); | 298 return self->OnUnregistered(connection); |
| 281 } | 299 } |
| 282 | 300 |
| 283 } // namespace dbus | 301 } // namespace dbus |
| OLD | NEW |