Skip to content

Commit

Permalink
dbus: Add TryRegisterFallback
Browse files Browse the repository at this point in the history
The TryRegisterFallback works just like TryRegisterObjectPath,
with addition that the registered callbacks also apply to the specified
object path and its sub paths. This is useful to implement a "catch-all"
handler.

Currently this is needed by Bluetooth dispatcher which needs to catch
all method calls to all objects and forward them to another D-Bus
service.

Bug: 862849
Change-Id: I77b1cd924690a673c872b69a35120ee5bf223b0d
Reviewed-on: https://chromium-review.googlesource.com/1134605
Commit-Queue: Sonny Sasaka <sonnysasaka@chromium.org>
Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576565}
  • Loading branch information
Sonny Sasaka authored and Commit Bot committed Jul 19, 2018
1 parent 85c987f commit 564474a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
27 changes: 21 additions & 6 deletions dbus/bus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,25 @@ bool Bus::TryRegisterObjectPath(const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error) {
return TryRegisterObjectPathInternal(
object_path, vtable, user_data, error,
dbus_connection_try_register_object_path);
}

bool Bus::TryRegisterFallback(const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error) {
return TryRegisterObjectPathInternal(object_path, vtable, user_data, error,
dbus_connection_try_register_fallback);
}

bool Bus::TryRegisterObjectPathInternal(
const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error,
TryRegisterObjectPathFunction* register_function) {
DCHECK(connection_);
AssertOnDBusThread();

Expand All @@ -731,12 +750,8 @@ bool Bus::TryRegisterObjectPath(const ObjectPath& object_path,
return false;
}

const bool success = dbus_connection_try_register_object_path(
connection_,
object_path.value().c_str(),
vtable,
user_data,
error);
const bool success = register_function(
connection_, object_path.value().c_str(), vtable, user_data, error);
if (success)
registered_object_paths_.insert(object_path);
return success;
Expand Down
32 changes: 32 additions & 0 deletions dbus/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,24 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
void* user_data,
DBusError* error);

// Tries to register the object path and its sub paths.
// Returns true on success.
// Returns false if the object path is already registered.
//
// |message_function| in |vtable| will be called every time when a new
// message sent to the object path (or hierarchically below) arrives.
//
// The same object path must not be added more than once.
//
// See also documentation of |dbus_connection_try_register_fallback| at
// http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
//
// BLOCKING CALL.
virtual bool TryRegisterFallback(const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error);

// Unregister the object path.
//
// BLOCKING CALL.
Expand Down Expand Up @@ -590,8 +608,22 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
virtual ~Bus();

private:
using TryRegisterObjectPathFunction =
dbus_bool_t(DBusConnection* connection,
const char* object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error);

friend class base::RefCountedThreadSafe<Bus>;

bool TryRegisterObjectPathInternal(
const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error,
TryRegisterObjectPathFunction* register_function);

// Helper function used for RemoveObjectProxy().
void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
const base::Closure& callback);
Expand Down
5 changes: 5 additions & 0 deletions dbus/mock_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class MockBus : public Bus {
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error));
MOCK_METHOD4(TryRegisterFallback,
bool(const ObjectPath& object_path,
const DBusObjectPathVTable* vtable,
void* user_data,
DBusError* error));
MOCK_METHOD1(UnregisterObjectPath, void(const ObjectPath& object_path));
MOCK_METHOD0(GetDBusTaskRunner, base::TaskRunner*());
MOCK_METHOD0(GetOriginTaskRunner, base::TaskRunner*());
Expand Down

0 comments on commit 564474a

Please sign in to comment.