Skip to content

Commit

Permalink
Support D-Bus Object Manager
Browse files Browse the repository at this point in the history
Object Manager is a new standard D-Bus interface, closely related to
the Properties interface. It is used by BlueZ 5.x thus the need to
implement it now.

The intended use is that Chrome D-Bus Client singletons set up a link
to an object manager in their constructor and register themselves to
handle their particular interface.

BUG=220951
TEST=dbus_unittests

Review URL: https://codereview.chromium.org/12491014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@190440 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
keybuk@chromium.org committed Mar 25, 2013
1 parent dbc220a commit 9cc40cb
Show file tree
Hide file tree
Showing 13 changed files with 1,314 additions and 54 deletions.
4 changes: 4 additions & 0 deletions chromeos/dbus/dbus_thread_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class DBusThreadManagerImpl : public DBusThreadManager {
// initialize it after the main list of clients.
power_policy_controller_.reset(
new PowerPolicyController(this, power_manager_client_.get()));

// This must be called after the list of clients so they've each had a
// chance to register with their object managers.
system_bus_->GetManagedObjects();
}

virtual ~DBusThreadManagerImpl() {
Expand Down
40 changes: 40 additions & 0 deletions dbus/bus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "base/threading/thread_restrictions.h"
#include "base/time.h"
#include "dbus/exported_object.h"
#include "dbus/object_manager.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "dbus/scoped_dbus_error.h"

Expand Down Expand Up @@ -322,6 +324,44 @@ void Bus::UnregisterExportedObjectInternal(
exported_object->Unregister();
}

ObjectManager* Bus::GetObjectManager(const std::string& service_name,
const ObjectPath& object_path) {
AssertOnOriginThread();

// Check if we already have the requested object manager.
const ObjectManagerTable::key_type key(service_name + object_path.value());
ObjectManagerTable::iterator iter = object_manager_table_.find(key);
if (iter != object_manager_table_.end()) {
return iter->second;
}

scoped_refptr<ObjectManager> object_manager =
new ObjectManager(this, service_name, object_path);
object_manager_table_[key] = object_manager;

return object_manager.get();
}

void Bus::RemoveObjectManager(const std::string& service_name,
const ObjectPath& object_path) {
AssertOnOriginThread();

const ObjectManagerTable::key_type key(service_name + object_path.value());
ObjectManagerTable::iterator iter = object_manager_table_.find(key);
if (iter == object_manager_table_.end())
return;

scoped_refptr<ObjectManager> object_manager = iter->second;
object_manager_table_.erase(iter);
}

void Bus::GetManagedObjects() {
for (ObjectManagerTable::iterator iter = object_manager_table_.begin();
iter != object_manager_table_.end(); ++iter) {
iter->second->GetManagedObjects();
}
}

bool Bus::Connect() {
// dbus_bus_get_private() and dbus_bus_get() are blocking calls.
AssertOnDBusThread();
Expand Down
39 changes: 39 additions & 0 deletions dbus/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Location;
namespace dbus {

class ExportedObject;
class ObjectManager;
class ObjectProxy;

// Bus is used to establish a connection with D-Bus, create object
Expand Down Expand Up @@ -302,6 +303,37 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
// Must be called in the origin thread.
virtual void UnregisterExportedObject(const ObjectPath& object_path);


// Gets an object manager for the given remote object path |object_path|
// exported by the service |service_name|.
//
// Returns an existing object manager if the bus object already owns a
// matching object manager, never returns NULL.
//
// The caller must not delete the returned object, the bus retains ownership
// of all object managers.
//
// Must be called in the origin thread.
virtual ObjectManager* GetObjectManager(const std::string& service_name,
const ObjectPath& object_path);

// Unregisters the object manager for the given remote object path
// |object_path| exported by the srevice |service_name|.
//
// Getting an object manager for the same remote object after this call
// will return a new object, method calls on any remaining copies of the
// previous object are not permitted.
//
// Must be called in the origin thread.
virtual void RemoveObjectManager(const std::string& service_name,
const ObjectPath& object_path);

// Instructs all registered object managers to retrieve their set of managed
// objects from their respective remote objects. There is no need to call this
// manually, this is called automatically by the D-Bus thread manager once
// implementation classes are registered.
virtual void GetManagedObjects();

// Shuts down the bus and blocks until it's done. More specifically, this
// function does the following:
//
Expand Down Expand Up @@ -608,6 +640,13 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
ExportedObjectTable exported_object_table_;

// ObjectManagerTable is used to hold the object managers 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,
scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
ObjectManagerTable object_manager_table_;

bool async_operations_set_up_;
bool shutdown_completed_;

Expand Down
5 changes: 5 additions & 0 deletions dbus/dbus.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
'file_descriptor.h',
'message.cc',
'message.h',
'object_manager.cc',
'object_manager.h',
'object_path.cc',
'object_path.h',
'object_proxy.cc',
Expand Down Expand Up @@ -71,6 +73,8 @@
'mock_bus.h',
'mock_exported_object.cc',
'mock_exported_object.h',
'mock_object_manager.cc',
'mock_object_manager.h',
'mock_object_proxy.cc',
'mock_object_proxy.h',
],
Expand Down Expand Up @@ -98,6 +102,7 @@
'end_to_end_sync_unittest.cc',
'message_unittest.cc',
'mock_unittest.cc',
'object_manager_unittest.cc',
'property_unittest.cc',
'signal_sender_verification_unittest.cc',
'string_util_unittest.cc',
Expand Down
2 changes: 2 additions & 0 deletions dbus/mock_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class MockBus : public Bus {
int options));
MOCK_METHOD1(GetExportedObject, ExportedObject*(
const ObjectPath& object_path));
MOCK_METHOD2(GetObjectManager, ObjectManager*(const std::string&,
const ObjectPath&));
MOCK_METHOD0(ShutdownAndBlock, void());
MOCK_METHOD0(ShutdownOnDBusThreadAndBlock, void());
MOCK_METHOD0(Connect, bool());
Expand Down
18 changes: 18 additions & 0 deletions dbus/mock_object_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2013 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.

#include "dbus/mock_object_manager.h"

namespace dbus {

MockObjectManager::MockObjectManager(Bus* bus,
const std::string& service_name,
const ObjectPath& object_path)
: ObjectManager(bus, service_name, object_path) {
}

MockObjectManager::~MockObjectManager() {
}

} // namespace dbus
42 changes: 42 additions & 0 deletions dbus/mock_object_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2013 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.

#ifndef DBUS_MOCK_OBJECT_MANAGER_H_
#define DBUS_MOCK_OBJECT_MANAGER_H_

#include <string>

#include "dbus/message.h"
#include "dbus/object_manager.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace dbus {

// Mock for ObjectManager.
class MockObjectManager : public ObjectManager {
public:
MockObjectManager(Bus* bus,
const std::string& service_name,
const ObjectPath& object_path);

MOCK_METHOD2(RegisterInterface, void(const std::string&,
Interface*));
MOCK_METHOD1(UnregisterInterface, void(const std::string&));
MOCK_METHOD0(GetObjects, std::vector<ObjectPath>());
MOCK_METHOD1(GetObjectsWithInterface,
std::vector<ObjectPath>(const std::string&));
MOCK_METHOD1(GetObjectProxy, ObjectProxy*(const ObjectPath&));
MOCK_METHOD2(GetProperties, PropertySet*(const ObjectPath&,
const std::string&));
MOCK_METHOD0(GetManagedObjects, void());

protected:
virtual ~MockObjectManager();
};

} // namespace dbus

#endif // DBUS_MOCK_OBJECT_MANAGER_H_
Loading

0 comments on commit 9cc40cb

Please sign in to comment.