Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: dbus/object_proxy.cc

Issue 9373039: Allow dbus clients to silence logging when a service is unavailable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use pair, factor out error logging logic Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dbus/object_proxy.h ('k') | dbus/scoped_dbus_error.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bus.h" 5 #include "dbus/bus.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/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/string_piece.h"
11 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
12 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
14 #include "dbus/message.h" 15 #include "dbus/message.h"
15 #include "dbus/object_proxy.h" 16 #include "dbus/object_proxy.h"
16 #include "dbus/scoped_dbus_error.h" 17 #include "dbus/scoped_dbus_error.h"
17 18
18 namespace { 19 namespace {
19 20
21 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown";
22
20 // Used for success ratio histograms. 1 for success, 0 for failure. 23 // Used for success ratio histograms. 1 for success, 0 for failure.
21 const int kSuccessRatioHistogramMaxValue = 2; 24 const int kSuccessRatioHistogramMaxValue = 2;
22 25
23 // Gets the absolute signal name by concatenating the interface name and 26 // Gets the absolute signal name by concatenating the interface name and
24 // the signal name. Used for building keys for method_table_ in 27 // the signal name. Used for building keys for method_table_ in
25 // ObjectProxy. 28 // ObjectProxy.
26 std::string GetAbsoluteSignalName( 29 std::string GetAbsoluteSignalName(
27 const std::string& interface_name, 30 const std::string& interface_name,
28 const std::string& signal_name) { 31 const std::string& signal_name) {
29 return interface_name + "." + signal_name; 32 return interface_name + "." + signal_name;
30 } 33 }
31 34
32 // An empty function used for ObjectProxy::EmptyResponseCallback(). 35 // An empty function used for ObjectProxy::EmptyResponseCallback().
33 void EmptyResponseCallbackBody(dbus::Response* unused_response) { 36 void EmptyResponseCallbackBody(dbus::Response* unused_response) {
34 } 37 }
35 38
36 } // namespace 39 } // namespace
37 40
38 namespace dbus { 41 namespace dbus {
39 42
40 ObjectProxy::ObjectProxy(Bus* bus, 43 ObjectProxy::ObjectProxy(Bus* bus,
41 const std::string& service_name, 44 const std::string& service_name,
42 const std::string& object_path) 45 const std::string& object_path,
46 int options)
43 : bus_(bus), 47 : bus_(bus),
44 service_name_(service_name), 48 service_name_(service_name),
45 object_path_(object_path), 49 object_path_(object_path),
46 filter_added_(false) { 50 filter_added_(false),
51 ignore_service_unknown_errors_(
52 options & IGNORE_SERVICE_UNKNOWN_ERRORS) {
47 } 53 }
48 54
49 ObjectProxy::~ObjectProxy() { 55 ObjectProxy::~ObjectProxy() {
50 } 56 }
51 57
52 // Originally we tried to make |method_call| a const reference, but we 58 // 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 59 // gave up as dbus_connection_send_with_reply_and_block() takes a
54 // non-const pointer of DBusMessage as the second parameter. 60 // non-const pointer of DBusMessage as the second parameter.
55 Response* ObjectProxy::CallMethodAndBlock(MethodCall* method_call, 61 Response* ObjectProxy::CallMethodAndBlock(MethodCall* method_call,
56 int timeout_ms) { 62 int timeout_ms) {
(...skipping 11 matching lines...) Expand all
68 // Send the message synchronously. 74 // Send the message synchronously.
69 const base::TimeTicks start_time = base::TimeTicks::Now(); 75 const base::TimeTicks start_time = base::TimeTicks::Now();
70 DBusMessage* response_message = 76 DBusMessage* response_message =
71 bus_->SendWithReplyAndBlock(request_message, timeout_ms, error.get()); 77 bus_->SendWithReplyAndBlock(request_message, timeout_ms, error.get());
72 // Record if the method call is successful, or not. 1 if successful. 78 // Record if the method call is successful, or not. 1 if successful.
73 UMA_HISTOGRAM_ENUMERATION("DBus.SyncMethodCallSuccess", 79 UMA_HISTOGRAM_ENUMERATION("DBus.SyncMethodCallSuccess",
74 response_message ? 1 : 0, 80 response_message ? 1 : 0,
75 kSuccessRatioHistogramMaxValue); 81 kSuccessRatioHistogramMaxValue);
76 82
77 if (!response_message) { 83 if (!response_message) {
78 LOG(ERROR) << "Failed to call method: " 84 LogMethodCallFailure(error.is_set() ? error.name() : "unknown error type",
79 << (error.is_set() ? error.message() : ""); 85 error.is_set() ? error.message() : "");
80 return NULL; 86 return NULL;
81 } 87 }
82 // Record time spent for the method call. Don't include failures. 88 // Record time spent for the method call. Don't include failures.
83 UMA_HISTOGRAM_TIMES("DBus.SyncMethodCallTime", 89 UMA_HISTOGRAM_TIMES("DBus.SyncMethodCallTime",
84 base::TimeTicks::Now() - start_time); 90 base::TimeTicks::Now() - start_time);
85 91
86 return Response::FromRawMessage(response_message); 92 return Response::FromRawMessage(response_message);
87 } 93 }
88 94
89 void ObjectProxy::CallMethod(MethodCall* method_call, 95 void ObjectProxy::CallMethod(MethodCall* method_call,
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 response_callback.Run(NULL); 235 response_callback.Run(NULL);
230 } else if (dbus_message_get_type(response_message) == 236 } else if (dbus_message_get_type(response_message) ==
231 DBUS_MESSAGE_TYPE_ERROR) { 237 DBUS_MESSAGE_TYPE_ERROR) {
232 // This will take |response_message| and release (unref) it. 238 // This will take |response_message| and release (unref) it.
233 scoped_ptr<dbus::ErrorResponse> error_response( 239 scoped_ptr<dbus::ErrorResponse> error_response(
234 dbus::ErrorResponse::FromRawMessage(response_message)); 240 dbus::ErrorResponse::FromRawMessage(response_message));
235 // Error message may contain the error message as string. 241 // Error message may contain the error message as string.
236 dbus::MessageReader reader(error_response.get()); 242 dbus::MessageReader reader(error_response.get());
237 std::string error_message; 243 std::string error_message;
238 reader.PopString(&error_message); 244 reader.PopString(&error_message);
239 LOG(ERROR) << "Failed to call method: " << error_response->GetErrorName() 245 LogMethodCallFailure(error_response->GetErrorName(), error_message);
240 << ": " << error_message;
241 // We don't give the error message to the callback. 246 // We don't give the error message to the callback.
242 response_callback.Run(NULL); 247 response_callback.Run(NULL);
243 } else { 248 } else {
244 // This will take |response_message| and release (unref) it. 249 // This will take |response_message| and release (unref) it.
245 scoped_ptr<dbus::Response> response( 250 scoped_ptr<dbus::Response> response(
246 dbus::Response::FromRawMessage(response_message)); 251 dbus::Response::FromRawMessage(response_message));
247 // The response is successfully received. 252 // The response is successfully received.
248 response_callback.Run(response.get()); 253 response_callback.Run(response.get());
249 method_call_successful = true; 254 method_call_successful = true;
250 // Record time spent for the method call. Don't include failures. 255 // Record time spent for the method call. Don't include failures.
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 414 }
410 415
411 DBusHandlerResult ObjectProxy::HandleMessageThunk( 416 DBusHandlerResult ObjectProxy::HandleMessageThunk(
412 DBusConnection* connection, 417 DBusConnection* connection,
413 DBusMessage* raw_message, 418 DBusMessage* raw_message,
414 void* user_data) { 419 void* user_data) {
415 ObjectProxy* self = reinterpret_cast<ObjectProxy*>(user_data); 420 ObjectProxy* self = reinterpret_cast<ObjectProxy*>(user_data);
416 return self->HandleMessage(connection, raw_message); 421 return self->HandleMessage(connection, raw_message);
417 } 422 }
418 423
424 void ObjectProxy::LogMethodCallFailure(
425 const base::StringPiece& error_name,
426 const base::StringPiece& error_message) const {
427 if (ignore_service_unknown_errors_ && error_name == kErrorServiceUnknown)
428 return;
satorux1 2012/02/10 20:00:11 Nice. it's now easier to understand and cleaner!
429 LOG(ERROR) << "Failed to call method: " << error_name
430 << ": " << error_message;
431 }
432
419 } // namespace dbus 433 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/object_proxy.h ('k') | dbus/scoped_dbus_error.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698