From f63bfe5e62af1e37616239b8ff7754ec8e836cc1 Mon Sep 17 00:00:00 2001 From: zqiu Date: Tue, 7 Jul 2015 19:08:30 -0700 Subject: dbus: expose bus connection unique name Add an API to Bus class to expose the unique name of the bus connection. The function will return an empty string if bus connection is not established. BUG=chromium:507718 TEST=unittest Review URL: https://codereview.chromium.org/1222073003 Cr-Commit-Position: refs/heads/master@{#337732} --- dbus/bus.cc | 6 ++++++ dbus/bus.h | 4 ++++ dbus/bus_unittest.cc | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) (limited to 'dbus') diff --git a/dbus/bus.cc b/dbus/bus.cc index 9fbd821..3a4fe21 100644 --- a/dbus/bus.cc +++ b/dbus/bus.cc @@ -1020,6 +1020,12 @@ void Bus::UnlistenForServiceOwnerChangeInternal( RemoveFilterFunction(Bus::OnServiceOwnerChangedFilter, this); } +std::string Bus::GetConnectionName() { + if (!connection_) + return ""; + return dbus_bus_get_unique_name(connection_); +} + dbus_bool_t Bus::OnAddWatch(DBusWatch* raw_watch) { AssertOnDBusThread(); diff --git a/dbus/bus.h b/dbus/bus.h index f9f0dfd..27d149c 100644 --- a/dbus/bus.h +++ b/dbus/bus.h @@ -586,6 +586,10 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe { const std::string& service_name, const GetServiceOwnerCallback& callback); + // Return the unique name of the bus connnection if it is connected to + // D-BUS. Otherwise, return an empty string. + std::string GetConnectionName(); + // Returns true if the bus is connected to D-Bus. bool is_connected() { return connection_ != NULL; } diff --git a/dbus/bus_unittest.cc b/dbus/bus_unittest.cc index 1e84fa5..27d9bb2 100644 --- a/dbus/bus_unittest.cc +++ b/dbus/bus_unittest.cc @@ -394,4 +394,24 @@ TEST(BusTest, ListenForServiceOwnerChange) { EXPECT_TRUE(bus->shutdown_completed()); } +TEST(BusTest, GetConnectionName) { + Bus::Options options; + scoped_refptr bus = new Bus(options); + + // Connection name is empty since bus is not connected. + EXPECT_FALSE(bus->is_connected()); + EXPECT_TRUE(bus->GetConnectionName().empty()); + + // Connect bus to D-Bus. + bus->Connect(); + + // Connection name is not empty after connection is established. + EXPECT_TRUE(bus->is_connected()); + EXPECT_FALSE(bus->GetConnectionName().empty()); + + // Shut down synchronously. + bus->ShutdownAndBlock(); + EXPECT_TRUE(bus->shutdown_completed()); +} + } // namespace dbus -- cgit v1.1