summaryrefslogtreecommitdiffstats
path: root/dbus
diff options
context:
space:
mode:
authoradamk@chromium.org <adamk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-10 21:45:23 +0000
committeradamk@chromium.org <adamk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-10 21:45:23 +0000
commit20bed01e980a5853dd87d4c0d65d4912697f22ae (patch)
treeba248e80e0792a777a7c3ab96f319bc98f6ef025 /dbus
parente84efb11971bfbddfaff7b97f1fe8bb21fa82028 (diff)
downloadchromium_src-20bed01e980a5853dd87d4c0d65d4912697f22ae.zip
chromium_src-20bed01e980a5853dd87d4c0d65d4912697f22ae.tar.gz
chromium_src-20bed01e980a5853dd87d4c0d65d4912697f22ae.tar.bz2
Allow dbus clients to silence logging when a service is unavailable.
BUG=109696 Review URL: https://chromiumcodereview.appspot.com/9373039 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121544 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus')
-rw-r--r--dbus/bus.cc13
-rw-r--r--dbus/bus.h17
-rw-r--r--dbus/bus_unittest.cc34
-rw-r--r--dbus/mock_bus.h6
-rw-r--r--dbus/mock_object_proxy.cc4
-rw-r--r--dbus/object_proxy.cc28
-rw-r--r--dbus/object_proxy.h24
-rw-r--r--dbus/scoped_dbus_error.h3
8 files changed, 106 insertions, 23 deletions
diff --git a/dbus/bus.cc b/dbus/bus.cc
index d066217..0c6a421 100644
--- a/dbus/bus.cc
+++ b/dbus/bus.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
@@ -208,17 +208,24 @@ Bus::~Bus() {
ObjectProxy* Bus::GetObjectProxy(const std::string& service_name,
const std::string& object_path) {
+ return GetObjectProxyWithOptions(service_name, object_path,
+ ObjectProxy::DEFAULT_OPTIONS);
+}
+
+ObjectProxy* Bus::GetObjectProxyWithOptions(const std::string& service_name,
+ const std::string& object_path,
+ int options) {
AssertOnOriginThread();
// Check if we already have the requested object proxy.
- const std::string key = service_name + object_path;
+ const ObjectProxyTable::key_type key(service_name + object_path, options);
ObjectProxyTable::iterator iter = object_proxy_table_.find(key);
if (iter != object_proxy_table_.end()) {
return iter->second;
}
scoped_refptr<ObjectProxy> object_proxy =
- new ObjectProxy(this, service_name, object_path);
+ new ObjectProxy(this, service_name, object_path, options);
object_proxy_table_[key] = object_proxy;
return object_proxy.get();
diff --git a/dbus/bus.h b/dbus/bus.h
index 4af07ce..22c2218 100644
--- a/dbus/bus.h
+++ b/dbus/bus.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -196,6 +196,13 @@ class Bus : public base::RefCountedThreadSafe<Bus> {
virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
const std::string& object_path);
+ // Same as above, but also takes a bitfield of ObjectProxy::Options.
+ // See object_proxy.h for available options.
+ virtual ObjectProxy* GetObjectProxyWithOptions(
+ const std::string& service_name,
+ const std::string& object_path,
+ int options);
+
// Gets the exported object for the given service name and the object
// path. The caller must not delete the returned object.
//
@@ -459,9 +466,11 @@ class Bus : public base::RefCountedThreadSafe<Bus> {
filter_functions_added_;
// ObjectProxyTable is used to hold the object proxies created by the
- // bus object. Key is a concatenated string of service name + object path,
- // like "org.chromium.TestService/org/chromium/TestObject".
- typedef std::map<std::string,
+ // bus object. Key is a pair; the first part is a concatenated string of
+ // service name + object path, like
+ // "org.chromium.TestService/org/chromium/TestObject".
+ // The second part is the ObjectProxy::Options for the proxy.
+ typedef std::map<std::pair<std::string, int>,
scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
ObjectProxyTable object_proxy_table_;
diff --git a/dbus/bus_unittest.cc b/dbus/bus_unittest.cc
index bbd2a4c..c21044b 100644
--- a/dbus/bus_unittest.cc
+++ b/dbus/bus_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -50,6 +50,38 @@ TEST(BusTest, GetObjectProxy) {
bus->ShutdownAndBlock();
}
+TEST(BusTest, GetObjectProxyIgnoreUnknownService) {
+ dbus::Bus::Options options;
+ scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
+
+ dbus::ObjectProxy* object_proxy1 =
+ bus->GetObjectProxyWithOptions(
+ "org.chromium.TestService",
+ "/org/chromium/TestObject",
+ dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
+ ASSERT_TRUE(object_proxy1);
+
+ // This should return the same object.
+ dbus::ObjectProxy* object_proxy2 =
+ bus->GetObjectProxyWithOptions(
+ "org.chromium.TestService",
+ "/org/chromium/TestObject",
+ dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
+ ASSERT_TRUE(object_proxy2);
+ EXPECT_EQ(object_proxy1, object_proxy2);
+
+ // This should not.
+ dbus::ObjectProxy* object_proxy3 =
+ bus->GetObjectProxyWithOptions(
+ "org.chromium.TestService",
+ "/org/chromium/DifferentTestObject",
+ dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
+ ASSERT_TRUE(object_proxy3);
+ EXPECT_NE(object_proxy1, object_proxy3);
+
+ bus->ShutdownAndBlock();
+}
+
TEST(BusTest, GetExportedObject) {
dbus::Bus::Options options;
scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
diff --git a/dbus/mock_bus.h b/dbus/mock_bus.h
index 7b949cb..31c1349 100644
--- a/dbus/mock_bus.h
+++ b/dbus/mock_bus.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -21,6 +21,10 @@ class MockBus : public Bus {
MOCK_METHOD2(GetObjectProxy, ObjectProxy*(const std::string& service_name,
const std::string& object_path));
+ MOCK_METHOD3(GetObjectProxyWithOptions,
+ ObjectProxy*(const std::string& service_name,
+ const std::string& object_path,
+ int options));
MOCK_METHOD2(GetExportedObject, ExportedObject*(
const std::string& service_name,
const std::string& object_path));
diff --git a/dbus/mock_object_proxy.cc b/dbus/mock_object_proxy.cc
index d58a604..a7186bb 100644
--- a/dbus/mock_object_proxy.cc
+++ b/dbus/mock_object_proxy.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -9,7 +9,7 @@ namespace dbus {
MockObjectProxy::MockObjectProxy(Bus* bus,
const std::string& service_name,
const std::string& object_path)
- : ObjectProxy(bus, service_name, object_path) {
+ : ObjectProxy(bus, service_name, object_path, DEFAULT_OPTIONS) {
}
MockObjectProxy::~MockObjectProxy() {
diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc
index 5da5901..58d679c 100644
--- a/dbus/object_proxy.cc
+++ b/dbus/object_proxy.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -8,6 +8,7 @@
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
+#include "base/string_piece.h"
#include "base/stringprintf.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
@@ -17,6 +18,8 @@
namespace {
+const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown";
+
// Used for success ratio histograms. 1 for success, 0 for failure.
const int kSuccessRatioHistogramMaxValue = 2;
@@ -39,11 +42,14 @@ namespace dbus {
ObjectProxy::ObjectProxy(Bus* bus,
const std::string& service_name,
- const std::string& object_path)
+ const std::string& object_path,
+ int options)
: bus_(bus),
service_name_(service_name),
object_path_(object_path),
- filter_added_(false) {
+ filter_added_(false),
+ ignore_service_unknown_errors_(
+ options & IGNORE_SERVICE_UNKNOWN_ERRORS) {
}
ObjectProxy::~ObjectProxy() {
@@ -75,8 +81,8 @@ Response* ObjectProxy::CallMethodAndBlock(MethodCall* method_call,
kSuccessRatioHistogramMaxValue);
if (!response_message) {
- LOG(ERROR) << "Failed to call method: "
- << (error.is_set() ? error.message() : "");
+ LogMethodCallFailure(error.is_set() ? error.name() : "unknown error type",
+ error.is_set() ? error.message() : "");
return NULL;
}
// Record time spent for the method call. Don't include failures.
@@ -236,8 +242,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback,
dbus::MessageReader reader(error_response.get());
std::string error_message;
reader.PopString(&error_message);
- LOG(ERROR) << "Failed to call method: " << error_response->GetErrorName()
- << ": " << error_message;
+ LogMethodCallFailure(error_response->GetErrorName(), error_message);
// We don't give the error message to the callback.
response_callback.Run(NULL);
} else {
@@ -416,4 +421,13 @@ DBusHandlerResult ObjectProxy::HandleMessageThunk(
return self->HandleMessage(connection, raw_message);
}
+void ObjectProxy::LogMethodCallFailure(
+ const base::StringPiece& error_name,
+ const base::StringPiece& error_message) const {
+ if (ignore_service_unknown_errors_ && error_name == kErrorServiceUnknown)
+ return;
+ LOG(ERROR) << "Failed to call method: " << error_name
+ << ": " << error_message;
+}
+
} // namespace dbus
diff --git a/dbus/object_proxy.h b/dbus/object_proxy.h
index ef45305..3da4e9b 100644
--- a/dbus/object_proxy.h
+++ b/dbus/object_proxy.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -14,6 +14,7 @@
#include "base/callback.h"
#include "base/memory/ref_counted.h"
+#include "base/string_piece.h"
#include "base/time.h"
namespace dbus {
@@ -30,11 +31,20 @@ class Signal;
// object is is alive when callbacks referencing |this| are called.
class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> {
public:
- // Client code should use Bus::GetObjectProxy() instead of this
- // constructor.
+ // Client code should use Bus::GetObjectProxy() or
+ // Bus::GetObjectProxyWithOptions() instead of this constructor.
ObjectProxy(Bus* bus,
const std::string& service_name,
- const std::string& object_path);
+ const std::string& object_path,
+ int options);
+
+ // Options to be OR-ed together when calling Bus::GetObjectProxyWithOptions().
+ // Set the IGNORE_SERVICE_UNKNOWN_ERRORS option to silence logging of
+ // org.freedesktop.DBus.Error.ServiceUnknown errors.
+ enum Options {
+ DEFAULT_OPTIONS = 0,
+ IGNORE_SERVICE_UNKNOWN_ERRORS = 1 << 0
+ };
// Special timeout constants.
//
@@ -180,6 +190,10 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> {
DBusMessage* raw_message,
void* user_data);
+ // Helper method for logging response errors appropriately.
+ void LogMethodCallFailure(const base::StringPiece& error_name,
+ const base::StringPiece& error_message) const;
+
scoped_refptr<Bus> bus_;
std::string service_name_;
std::string object_path_;
@@ -194,6 +208,8 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> {
std::set<std::string> match_rules_;
+ const bool ignore_service_unknown_errors_;
+
DISALLOW_COPY_AND_ASSIGN(ObjectProxy);
};
diff --git a/dbus/scoped_dbus_error.h b/dbus/scoped_dbus_error.h
index e76e4f7..79296f7 100644
--- a/dbus/scoped_dbus_error.h
+++ b/dbus/scoped_dbus_error.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -23,6 +23,7 @@ class ScopedDBusError {
DBusError* get() { return &error_; }
bool is_set() { return dbus_error_is_set(&error_); }
+ const char* name() { return error_.name; }
const char* message() { return error_.message; }
private: