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 <string.h> | |
6 | |
5 #include "dbus/bus.h" | 7 #include "dbus/bus.h" |
6 | 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
10 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
11 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
12 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
13 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
14 #include "dbus/message.h" | 16 #include "dbus/message.h" |
15 #include "dbus/object_proxy.h" | 17 #include "dbus/object_proxy.h" |
16 #include "dbus/scoped_dbus_error.h" | 18 #include "dbus/scoped_dbus_error.h" |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
22 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; | |
23 | |
20 // Used for success ratio histograms. 1 for success, 0 for failure. | 24 // Used for success ratio histograms. 1 for success, 0 for failure. |
21 const int kSuccessRatioHistogramMaxValue = 2; | 25 const int kSuccessRatioHistogramMaxValue = 2; |
22 | 26 |
23 // Gets the absolute signal name by concatenating the interface name and | 27 // Gets the absolute signal name by concatenating the interface name and |
24 // the signal name. Used for building keys for method_table_ in | 28 // the signal name. Used for building keys for method_table_ in |
25 // ObjectProxy. | 29 // ObjectProxy. |
26 std::string GetAbsoluteSignalName( | 30 std::string GetAbsoluteSignalName( |
27 const std::string& interface_name, | 31 const std::string& interface_name, |
28 const std::string& signal_name) { | 32 const std::string& signal_name) { |
29 return interface_name + "." + signal_name; | 33 return interface_name + "." + signal_name; |
30 } | 34 } |
31 | 35 |
32 // An empty function used for ObjectProxy::EmptyResponseCallback(). | 36 // An empty function used for ObjectProxy::EmptyResponseCallback(). |
33 void EmptyResponseCallbackBody(dbus::Response* unused_response) { | 37 void EmptyResponseCallbackBody(dbus::Response* unused_response) { |
34 } | 38 } |
35 | 39 |
36 } // namespace | 40 } // namespace |
37 | 41 |
38 namespace dbus { | 42 namespace dbus { |
39 | 43 |
40 ObjectProxy::ObjectProxy(Bus* bus, | 44 ObjectProxy::ObjectProxy(Bus* bus, |
41 const std::string& service_name, | 45 const std::string& service_name, |
42 const std::string& object_path) | 46 const std::string& object_path, |
47 ServiceUnknownBehavior service_unknown_behavior) | |
43 : bus_(bus), | 48 : bus_(bus), |
44 service_name_(service_name), | 49 service_name_(service_name), |
45 object_path_(object_path), | 50 object_path_(object_path), |
46 filter_added_(false) { | 51 filter_added_(false), |
52 log_unknown_service_errors_( | |
53 service_unknown_behavior == LOG_SERVICE_UNKNOWN_ERRORS) { | |
47 } | 54 } |
48 | 55 |
49 ObjectProxy::~ObjectProxy() { | 56 ObjectProxy::~ObjectProxy() { |
50 } | 57 } |
51 | 58 |
52 // Originally we tried to make |method_call| a const reference, but we | 59 // Originally we tried to make |method_call| a const reference, but we |
53 // gave up as dbus_connection_send_with_reply_and_block() takes a | 60 // gave up as dbus_connection_send_with_reply_and_block() takes a |
54 // non-const pointer of DBusMessage as the second parameter. | 61 // non-const pointer of DBusMessage as the second parameter. |
55 Response* ObjectProxy::CallMethodAndBlock(MethodCall* method_call, | 62 Response* ObjectProxy::CallMethodAndBlock(MethodCall* method_call, |
56 int timeout_ms) { | 63 int timeout_ms) { |
(...skipping 11 matching lines...) Expand all Loading... | |
68 // Send the message synchronously. | 75 // Send the message synchronously. |
69 const base::TimeTicks start_time = base::TimeTicks::Now(); | 76 const base::TimeTicks start_time = base::TimeTicks::Now(); |
70 DBusMessage* response_message = | 77 DBusMessage* response_message = |
71 bus_->SendWithReplyAndBlock(request_message, timeout_ms, error.get()); | 78 bus_->SendWithReplyAndBlock(request_message, timeout_ms, error.get()); |
72 // Record if the method call is successful, or not. 1 if successful. | 79 // Record if the method call is successful, or not. 1 if successful. |
73 UMA_HISTOGRAM_ENUMERATION("DBus.SyncMethodCallSuccess", | 80 UMA_HISTOGRAM_ENUMERATION("DBus.SyncMethodCallSuccess", |
74 response_message ? 1 : 0, | 81 response_message ? 1 : 0, |
75 kSuccessRatioHistogramMaxValue); | 82 kSuccessRatioHistogramMaxValue); |
76 | 83 |
77 if (!response_message) { | 84 if (!response_message) { |
78 LOG(ERROR) << "Failed to call method: " | 85 const bool error_is_set = error.is_set(); |
satorux1
2012/02/10 08:12:52
The caching is unnecessary. You'll only save two b
adamk
2012/02/10 17:57:16
It wasn't obvious to me that error.is_set() was ch
| |
79 << (error.is_set() ? error.message() : ""); | 86 const char* error_name = error_is_set ? error.name() : ""; |
87 if (log_unknown_service_errors_ || | |
88 strcmp(error_name, kErrorServiceUnknown)) { | |
satorux1
2012/02/10 08:12:52
strcmp() is hard to read. Please make it something
adamk
2012/02/10 17:57:16
Changed error_name to be a base::StringPiece.
| |
89 LOG(ERROR) << "Failed to call method: " | |
90 << (error_is_set ? error.message() : ""); | |
91 } | |
80 return NULL; | 92 return NULL; |
81 } | 93 } |
82 // Record time spent for the method call. Don't include failures. | 94 // Record time spent for the method call. Don't include failures. |
83 UMA_HISTOGRAM_TIMES("DBus.SyncMethodCallTime", | 95 UMA_HISTOGRAM_TIMES("DBus.SyncMethodCallTime", |
84 base::TimeTicks::Now() - start_time); | 96 base::TimeTicks::Now() - start_time); |
85 | 97 |
86 return Response::FromRawMessage(response_message); | 98 return Response::FromRawMessage(response_message); |
87 } | 99 } |
88 | 100 |
89 void ObjectProxy::CallMethod(MethodCall* method_call, | 101 void ObjectProxy::CallMethod(MethodCall* method_call, |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 if (!response_message) { | 239 if (!response_message) { |
228 // The response is not received. | 240 // The response is not received. |
229 response_callback.Run(NULL); | 241 response_callback.Run(NULL); |
230 } else if (dbus_message_get_type(response_message) == | 242 } else if (dbus_message_get_type(response_message) == |
231 DBUS_MESSAGE_TYPE_ERROR) { | 243 DBUS_MESSAGE_TYPE_ERROR) { |
232 // This will take |response_message| and release (unref) it. | 244 // This will take |response_message| and release (unref) it. |
233 scoped_ptr<dbus::ErrorResponse> error_response( | 245 scoped_ptr<dbus::ErrorResponse> error_response( |
234 dbus::ErrorResponse::FromRawMessage(response_message)); | 246 dbus::ErrorResponse::FromRawMessage(response_message)); |
235 // Error message may contain the error message as string. | 247 // Error message may contain the error message as string. |
236 dbus::MessageReader reader(error_response.get()); | 248 dbus::MessageReader reader(error_response.get()); |
237 std::string error_message; | 249 std::string error_name = error_response->GetErrorName(); |
238 reader.PopString(&error_message); | 250 if (log_unknown_service_errors_ || error_name != kErrorServiceUnknown) { |
239 LOG(ERROR) << "Failed to call method: " << error_response->GetErrorName() | 251 std::string error_message; |
240 << ": " << error_message; | 252 reader.PopString(&error_message); |
253 LOG(ERROR) << "Failed to call method: " << error_name | |
254 << ": " << error_message; | |
255 } | |
241 // We don't give the error message to the callback. | 256 // We don't give the error message to the callback. |
242 response_callback.Run(NULL); | 257 response_callback.Run(NULL); |
243 } else { | 258 } else { |
244 // This will take |response_message| and release (unref) it. | 259 // This will take |response_message| and release (unref) it. |
245 scoped_ptr<dbus::Response> response( | 260 scoped_ptr<dbus::Response> response( |
246 dbus::Response::FromRawMessage(response_message)); | 261 dbus::Response::FromRawMessage(response_message)); |
247 // The response is successfully received. | 262 // The response is successfully received. |
248 response_callback.Run(response.get()); | 263 response_callback.Run(response.get()); |
249 method_call_successful = true; | 264 method_call_successful = true; |
250 // Record time spent for the method call. Don't include failures. | 265 // Record time spent for the method call. Don't include failures. |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 | 425 |
411 DBusHandlerResult ObjectProxy::HandleMessageThunk( | 426 DBusHandlerResult ObjectProxy::HandleMessageThunk( |
412 DBusConnection* connection, | 427 DBusConnection* connection, |
413 DBusMessage* raw_message, | 428 DBusMessage* raw_message, |
414 void* user_data) { | 429 void* user_data) { |
415 ObjectProxy* self = reinterpret_cast<ObjectProxy*>(user_data); | 430 ObjectProxy* self = reinterpret_cast<ObjectProxy*>(user_data); |
416 return self->HandleMessage(connection, raw_message); | 431 return self->HandleMessage(connection, raw_message); |
417 } | 432 } |
418 | 433 |
419 } // namespace dbus | 434 } // namespace dbus |
OLD | NEW |