Commit ef009f18 authored by pneubeck's avatar pneubeck Committed by Commit bot

Fix minor issues about DBusThreadManager.

- Remove the global unstub_client_mask_ and accordingly make DBusThreadManager::IsUsingStub a non-static function. This makes it obvious when it's ok to call this function (and prevents misuse), namely only after the Manager is initialized.

- By moving the mask to the DBusClientBundle, the cyclic dependency between bundle and manager is broken. The Bundle does not know about the Manager anymore.

- Fix the difference between DBusClientTypeMask ("A set of DBusClients") and DBusClientType ("A single DBusClient").

BUG=408617

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

Cr-Commit-Position: refs/heads/master@{#294361}
parent f691be3b
...@@ -50,7 +50,8 @@ class BluetoothAdapterClientImpl ...@@ -50,7 +50,8 @@ class BluetoothAdapterClientImpl
: public BluetoothAdapterClient, : public BluetoothAdapterClient,
public dbus::ObjectManager::Interface { public dbus::ObjectManager::Interface {
public: public:
BluetoothAdapterClientImpl() : weak_ptr_factory_(this) {} BluetoothAdapterClientImpl()
: object_manager_(NULL), weak_ptr_factory_(this) {}
virtual ~BluetoothAdapterClientImpl() { virtual ~BluetoothAdapterClientImpl() {
object_manager_->UnregisterInterface( object_manager_->UnregisterInterface(
......
...@@ -469,7 +469,7 @@ BluetoothAgentServiceProvider* BluetoothAgentServiceProvider::Create( ...@@ -469,7 +469,7 @@ BluetoothAgentServiceProvider* BluetoothAgentServiceProvider::Create(
dbus::Bus* bus, dbus::Bus* bus,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
Delegate* delegate) { Delegate* delegate) {
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::BLUETOOTH)) { if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
return new BluetoothAgentServiceProviderImpl(bus, object_path, delegate); return new BluetoothAgentServiceProviderImpl(bus, object_path, delegate);
} else { } else {
return new FakeBluetoothAgentServiceProvider(object_path, delegate); return new FakeBluetoothAgentServiceProvider(object_path, delegate);
......
...@@ -56,7 +56,8 @@ class BluetoothDeviceClientImpl ...@@ -56,7 +56,8 @@ class BluetoothDeviceClientImpl
: public BluetoothDeviceClient, : public BluetoothDeviceClient,
public dbus::ObjectManager::Interface { public dbus::ObjectManager::Interface {
public: public:
BluetoothDeviceClientImpl() : weak_ptr_factory_(this) {} BluetoothDeviceClientImpl()
: object_manager_(NULL), weak_ptr_factory_(this) {}
virtual ~BluetoothDeviceClientImpl() { virtual ~BluetoothDeviceClientImpl() {
object_manager_->UnregisterInterface( object_manager_->UnregisterInterface(
......
...@@ -464,7 +464,7 @@ BluetoothGattCharacteristicServiceProvider::Create( ...@@ -464,7 +464,7 @@ BluetoothGattCharacteristicServiceProvider::Create(
const std::vector<std::string>& flags, const std::vector<std::string>& flags,
const std::vector<std::string>& permissions, const std::vector<std::string>& permissions,
const dbus::ObjectPath& service_path) { const dbus::ObjectPath& service_path) {
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::BLUETOOTH)) { if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
return new BluetoothGattCharacteristicServiceProviderImpl( return new BluetoothGattCharacteristicServiceProviderImpl(
bus, object_path, delegate, uuid, flags, permissions, service_path); bus, object_path, delegate, uuid, flags, permissions, service_path);
} }
......
...@@ -460,7 +460,7 @@ BluetoothGattDescriptorServiceProvider::Create( ...@@ -460,7 +460,7 @@ BluetoothGattDescriptorServiceProvider::Create(
const std::string& uuid, const std::string& uuid,
const std::vector<std::string>& permissions, const std::vector<std::string>& permissions,
const dbus::ObjectPath& characteristic_path) { const dbus::ObjectPath& characteristic_path) {
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::BLUETOOTH)) { if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
return new BluetoothGattDescriptorServiceProviderImpl( return new BluetoothGattDescriptorServiceProviderImpl(
bus, object_path, delegate, uuid, permissions, characteristic_path); bus, object_path, delegate, uuid, permissions, characteristic_path);
} }
......
...@@ -269,7 +269,7 @@ BluetoothGattServiceServiceProvider::Create( ...@@ -269,7 +269,7 @@ BluetoothGattServiceServiceProvider::Create(
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const std::string& uuid, const std::string& uuid,
const std::vector<dbus::ObjectPath>& includes) { const std::vector<dbus::ObjectPath>& includes) {
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::BLUETOOTH)) { if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
return new BluetoothGattServiceServiceProviderImpl( return new BluetoothGattServiceServiceProviderImpl(
bus, object_path, uuid, includes); bus, object_path, uuid, includes);
} }
......
...@@ -34,7 +34,7 @@ class BluetoothInputClientImpl ...@@ -34,7 +34,7 @@ class BluetoothInputClientImpl
: public BluetoothInputClient, : public BluetoothInputClient,
public dbus::ObjectManager::Interface { public dbus::ObjectManager::Interface {
public: public:
BluetoothInputClientImpl() : weak_ptr_factory_(this) {} BluetoothInputClientImpl() : object_manager_(NULL), weak_ptr_factory_(this) {}
virtual ~BluetoothInputClientImpl() { virtual ~BluetoothInputClientImpl() {
object_manager_->UnregisterInterface( object_manager_->UnregisterInterface(
......
...@@ -252,7 +252,7 @@ BluetoothProfileServiceProvider* BluetoothProfileServiceProvider::Create( ...@@ -252,7 +252,7 @@ BluetoothProfileServiceProvider* BluetoothProfileServiceProvider::Create(
dbus::Bus* bus, dbus::Bus* bus,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
Delegate* delegate) { Delegate* delegate) {
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::BLUETOOTH)) { if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
return new BluetoothProfileServiceProviderImpl(bus, object_path, delegate); return new BluetoothProfileServiceProviderImpl(bus, object_path, delegate);
} else { } else {
return new FakeBluetoothProfileServiceProvider(object_path, delegate); return new FakeBluetoothProfileServiceProvider(object_path, delegate);
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "chromeos/dbus/cras_audio_client_stub_impl.h" #include "chromeos/dbus/cras_audio_client_stub_impl.h"
#include "chromeos/dbus/cros_disks_client.h" #include "chromeos/dbus/cros_disks_client.h"
#include "chromeos/dbus/cryptohome_client.h" #include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon_client.h" #include "chromeos/dbus/debug_daemon_client.h"
#include "chromeos/dbus/easy_unlock_client.h" #include "chromeos/dbus/easy_unlock_client.h"
#include "chromeos/dbus/fake_bluetooth_adapter_client.h" #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
...@@ -107,22 +106,22 @@ const struct { ...@@ -107,22 +106,22 @@ const struct {
{ "update_engine", DBusClientBundle::UPDATE_ENGINE }, { "update_engine", DBusClientBundle::UPDATE_ENGINE },
}; };
// Parses single command line param value for dbus subsystem and returns its // Parses single command line param value for dbus subsystem. If successful,
// enum representation. DBusClientType::UNKWNOWN is returned if |client_type| // returns its enum representation. Otherwise returns NO_CLIENT.
// does not match any known dbus client.
DBusClientBundle::DBusClientType GetDBusClientType( DBusClientBundle::DBusClientType GetDBusClientType(
const std::string& client_type) { const std::string& client_type_name) {
for (size_t i = 0; i < arraysize(client_type_map); i++) { for (size_t i = 0; i < arraysize(client_type_map); i++) {
if (LowerCaseEqualsASCII(client_type, client_type_map[i].param_name)) if (LowerCaseEqualsASCII(client_type_name, client_type_map[i].param_name))
return client_type_map[i].client_type; return client_type_map[i].client_type;
} }
return DBusClientBundle::NO_CLIENTS; return DBusClientBundle::NO_CLIENT;
} }
} // namespace } // namespace
DBusClientBundle::DBusClientBundle() { DBusClientBundle::DBusClientBundle(DBusClientTypeMask unstub_client_mask)
if (!DBusThreadManager::IsUsingStub(BLUETOOTH)) { : unstub_client_mask_(unstub_client_mask) {
if (!IsUsingStub(BLUETOOTH)) {
bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create()); bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create());
bluetooth_agent_manager_client_.reset( bluetooth_agent_manager_client_.reset(
BluetoothAgentManagerClient::Create()); BluetoothAgentManagerClient::Create());
...@@ -153,37 +152,36 @@ DBusClientBundle::DBusClientBundle() { ...@@ -153,37 +152,36 @@ DBusClientBundle::DBusClientBundle() {
bluetooth_gatt_service_client_.reset(new FakeBluetoothGattServiceClient); bluetooth_gatt_service_client_.reset(new FakeBluetoothGattServiceClient);
} }
if (!DBusThreadManager::IsUsingStub(CRAS)) if (!IsUsingStub(CRAS))
cras_audio_client_.reset(CrasAudioClient::Create()); cras_audio_client_.reset(CrasAudioClient::Create());
else else
cras_audio_client_.reset(new CrasAudioClientStubImpl); cras_audio_client_.reset(new CrasAudioClientStubImpl);
cros_disks_client_.reset(CrosDisksClient::Create( cros_disks_client_.reset(CrosDisksClient::Create(
DBusThreadManager::IsUsingStub(CROS_DISKS) ? IsUsingStub(CROS_DISKS) ? STUB_DBUS_CLIENT_IMPLEMENTATION
STUB_DBUS_CLIENT_IMPLEMENTATION : : REAL_DBUS_CLIENT_IMPLEMENTATION));
REAL_DBUS_CLIENT_IMPLEMENTATION));
if (!DBusThreadManager::IsUsingStub(CRYPTOHOME)) if (!IsUsingStub(CRYPTOHOME))
cryptohome_client_.reset(CryptohomeClient::Create()); cryptohome_client_.reset(CryptohomeClient::Create());
else else
cryptohome_client_.reset(new FakeCryptohomeClient); cryptohome_client_.reset(new FakeCryptohomeClient);
if (!DBusThreadManager::IsUsingStub(DEBUG_DAEMON)) if (!IsUsingStub(DEBUG_DAEMON))
debug_daemon_client_.reset(DebugDaemonClient::Create()); debug_daemon_client_.reset(DebugDaemonClient::Create());
else else
debug_daemon_client_.reset(new FakeDebugDaemonClient); debug_daemon_client_.reset(new FakeDebugDaemonClient);
if (!DBusThreadManager::IsUsingStub(EASY_UNLOCK)) if (!IsUsingStub(EASY_UNLOCK))
easy_unlock_client_.reset(EasyUnlockClient::Create()); easy_unlock_client_.reset(EasyUnlockClient::Create());
else else
easy_unlock_client_.reset(new FakeEasyUnlockClient); easy_unlock_client_.reset(new FakeEasyUnlockClient);
if (!DBusThreadManager::IsUsingStub(LORGNETTE_MANAGER)) if (!IsUsingStub(LORGNETTE_MANAGER))
lorgnette_manager_client_.reset(LorgnetteManagerClient::Create()); lorgnette_manager_client_.reset(LorgnetteManagerClient::Create());
else else
lorgnette_manager_client_.reset(new FakeLorgnetteManagerClient); lorgnette_manager_client_.reset(new FakeLorgnetteManagerClient);
if (!DBusThreadManager::IsUsingStub(SHILL)) { if (!IsUsingStub(SHILL)) {
shill_manager_client_.reset(ShillManagerClient::Create()); shill_manager_client_.reset(ShillManagerClient::Create());
shill_device_client_.reset(ShillDeviceClient::Create()); shill_device_client_.reset(ShillDeviceClient::Create());
shill_ipconfig_client_.reset(ShillIPConfigClient::Create()); shill_ipconfig_client_.reset(ShillIPConfigClient::Create());
...@@ -197,7 +195,7 @@ DBusClientBundle::DBusClientBundle() { ...@@ -197,7 +195,7 @@ DBusClientBundle::DBusClientBundle() {
shill_profile_client_.reset(new FakeShillProfileClient); shill_profile_client_.reset(new FakeShillProfileClient);
} }
if (!DBusThreadManager::IsUsingStub(GSM_SMS)) { if (!IsUsingStub(GSM_SMS)) {
gsm_sms_client_.reset(GsmSMSClient::Create()); gsm_sms_client_.reset(GsmSMSClient::Create());
} else { } else {
FakeGsmSMSClient* gsm_sms_client = new FakeGsmSMSClient(); FakeGsmSMSClient* gsm_sms_client = new FakeGsmSMSClient();
...@@ -207,23 +205,23 @@ DBusClientBundle::DBusClientBundle() { ...@@ -207,23 +205,23 @@ DBusClientBundle::DBusClientBundle() {
gsm_sms_client_.reset(gsm_sms_client); gsm_sms_client_.reset(gsm_sms_client);
} }
if (!DBusThreadManager::IsUsingStub(IMAGE_BURNER)) if (!IsUsingStub(IMAGE_BURNER))
image_burner_client_.reset(ImageBurnerClient::Create()); image_burner_client_.reset(ImageBurnerClient::Create());
else else
image_burner_client_.reset(new FakeImageBurnerClient); image_burner_client_.reset(new FakeImageBurnerClient);
if (!DBusThreadManager::IsUsingStub(INTROSPECTABLE)) if (!IsUsingStub(INTROSPECTABLE))
introspectable_client_.reset(IntrospectableClient::Create()); introspectable_client_.reset(IntrospectableClient::Create());
else else
introspectable_client_.reset(new FakeIntrospectableClient); introspectable_client_.reset(new FakeIntrospectableClient);
if (!DBusThreadManager::IsUsingStub(MODEM_MESSAGING)) if (!IsUsingStub(MODEM_MESSAGING))
modem_messaging_client_.reset(ModemMessagingClient::Create()); modem_messaging_client_.reset(ModemMessagingClient::Create());
else else
modem_messaging_client_.reset(new FakeModemMessagingClient); modem_messaging_client_.reset(new FakeModemMessagingClient);
// Create the NFC clients in the correct order based on their dependencies. // Create the NFC clients in the correct order based on their dependencies.
if (!DBusThreadManager::IsUsingStub(NFC)) { if (!IsUsingStub(NFC)) {
nfc_manager_client_.reset(NfcManagerClient::Create()); nfc_manager_client_.reset(NfcManagerClient::Create());
nfc_adapter_client_.reset( nfc_adapter_client_.reset(
NfcAdapterClient::Create(nfc_manager_client_.get())); NfcAdapterClient::Create(nfc_manager_client_.get()));
...@@ -240,40 +238,46 @@ DBusClientBundle::DBusClientBundle() { ...@@ -240,40 +238,46 @@ DBusClientBundle::DBusClientBundle() {
nfc_record_client_.reset(new FakeNfcRecordClient); nfc_record_client_.reset(new FakeNfcRecordClient);
} }
if (!DBusThreadManager::IsUsingStub(PERMISSION_BROKER)) if (!IsUsingStub(PERMISSION_BROKER))
permission_broker_client_.reset(PermissionBrokerClient::Create()); permission_broker_client_.reset(PermissionBrokerClient::Create());
else else
permission_broker_client_.reset(new FakePermissionBrokerClient); permission_broker_client_.reset(new FakePermissionBrokerClient);
power_manager_client_.reset(PowerManagerClient::Create( power_manager_client_.reset(PowerManagerClient::Create(
DBusThreadManager::IsUsingStub(POWER_MANAGER) ? IsUsingStub(POWER_MANAGER) ? STUB_DBUS_CLIENT_IMPLEMENTATION
STUB_DBUS_CLIENT_IMPLEMENTATION : : REAL_DBUS_CLIENT_IMPLEMENTATION));
REAL_DBUS_CLIENT_IMPLEMENTATION));
session_manager_client_.reset(SessionManagerClient::Create( session_manager_client_.reset(SessionManagerClient::Create(
DBusThreadManager::IsUsingStub(SESSION_MANAGER) ? IsUsingStub(SESSION_MANAGER) ? STUB_DBUS_CLIENT_IMPLEMENTATION
STUB_DBUS_CLIENT_IMPLEMENTATION : : REAL_DBUS_CLIENT_IMPLEMENTATION));
REAL_DBUS_CLIENT_IMPLEMENTATION));
if (!DBusThreadManager::IsUsingStub(SMS)) if (!IsUsingStub(SMS))
sms_client_.reset(SMSClient::Create()); sms_client_.reset(SMSClient::Create());
else else
sms_client_.reset(new FakeSMSClient); sms_client_.reset(new FakeSMSClient);
if (!DBusThreadManager::IsUsingStub(SYSTEM_CLOCK)) if (!IsUsingStub(SYSTEM_CLOCK))
system_clock_client_.reset(SystemClockClient::Create()); system_clock_client_.reset(SystemClockClient::Create());
else else
system_clock_client_.reset(new FakeSystemClockClient); system_clock_client_.reset(new FakeSystemClockClient);
update_engine_client_.reset(UpdateEngineClient::Create( update_engine_client_.reset(UpdateEngineClient::Create(
DBusThreadManager::IsUsingStub(UPDATE_ENGINE) ? IsUsingStub(UPDATE_ENGINE) ? STUB_DBUS_CLIENT_IMPLEMENTATION
STUB_DBUS_CLIENT_IMPLEMENTATION : : REAL_DBUS_CLIENT_IMPLEMENTATION));
REAL_DBUS_CLIENT_IMPLEMENTATION));
} }
DBusClientBundle::~DBusClientBundle() { DBusClientBundle::~DBusClientBundle() {
} }
bool DBusClientBundle::IsUsingStub(DBusClientType client) {
return !(unstub_client_mask_ & client);
}
bool DBusClientBundle::IsUsingAnyRealClient() {
// 'Using any real client' is equivalent to 'Unstubbed any client'.
return unstub_client_mask_ != 0;
}
void DBusClientBundle::SetupDefaultEnvironment() { void DBusClientBundle::SetupDefaultEnvironment() {
ShillManagerClient::TestInterface* manager = ShillManagerClient::TestInterface* manager =
shill_manager_client_->GetTestInterface(); shill_manager_client_->GetTestInterface();
...@@ -291,7 +295,7 @@ DBusClientBundle::DBusClientTypeMask DBusClientBundle::ParseUnstubList( ...@@ -291,7 +295,7 @@ DBusClientBundle::DBusClientTypeMask DBusClientBundle::ParseUnstubList(
unstub_components.begin(); unstub_components.begin();
iter != unstub_components.end(); ++iter) { iter != unstub_components.end(); ++iter) {
DBusClientBundle::DBusClientType client = GetDBusClientType(*iter); DBusClientBundle::DBusClientType client = GetDBusClientType(*iter);
if (client != DBusClientBundle::NO_CLIENTS) { if (client != NO_CLIENT) {
LOG(WARNING) << "Unstubbing dbus client for " << *iter; LOG(WARNING) << "Unstubbing dbus client for " << *iter;
unstub_mask |= client; unstub_mask |= client;
} else { } else {
......
...@@ -51,12 +51,12 @@ class UpdateEngineClient; ...@@ -51,12 +51,12 @@ class UpdateEngineClient;
// system bus. See also the comment in the destructor of DBusThreadManagerImpl. // system bus. See also the comment in the destructor of DBusThreadManagerImpl.
class CHROMEOS_EXPORT DBusClientBundle { class CHROMEOS_EXPORT DBusClientBundle {
public: public:
typedef unsigned int DBusClientTypeMask; typedef int DBusClientTypeMask;
// TODO(zelidrag): We might want to collapse few more of these subsystems if // TODO(zelidrag): We might want to collapse few more of these subsystems if
// their dbus interfaced correspond to the same daemon. // their dbus interfaced correspond to the same daemon.
enum DBusClientType { enum DBusClientType {
NO_CLIENTS = 0, NO_CLIENT = 0,
BLUETOOTH = 1 << 0, BLUETOOTH = 1 << 0,
CRAS = 1 << 1, CRAS = 1 << 1,
CROS_DISKS = 1 << 2, CROS_DISKS = 1 << 2,
...@@ -76,12 +76,17 @@ class CHROMEOS_EXPORT DBusClientBundle { ...@@ -76,12 +76,17 @@ class CHROMEOS_EXPORT DBusClientBundle {
SMS = 1 << 16, SMS = 1 << 16,
SYSTEM_CLOCK = 1 << 17, SYSTEM_CLOCK = 1 << 17,
UPDATE_ENGINE = 1 << 18, UPDATE_ENGINE = 1 << 18,
ALL_CLIENTS = ~static_cast<DBusClientTypeMask>(0),
}; };
DBusClientBundle(); DBusClientBundle(DBusClientTypeMask unstub_client_mask);
~DBusClientBundle(); ~DBusClientBundle();
// Returns true if |client| is stubbed.
bool IsUsingStub(DBusClientType client);
// Returns true if any real DBusClient is used.
bool IsUsingAnyRealClient();
// Initialize proper runtime environment for its dbus clients. // Initialize proper runtime environment for its dbus clients.
void SetupDefaultEnvironment(); void SetupDefaultEnvironment();
...@@ -232,6 +237,10 @@ class CHROMEOS_EXPORT DBusClientBundle { ...@@ -232,6 +237,10 @@ class CHROMEOS_EXPORT DBusClientBundle {
private: private:
friend class DBusThreadManagerSetter; friend class DBusThreadManagerSetter;
// Bitmask that defines which dbus clients are not stubbed out. Bitmap flags
// are defined within DBusClientType enum.
DBusClientTypeMask unstub_client_mask_;
scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_; scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_;
scoped_ptr<BluetoothAgentManagerClient> bluetooth_agent_manager_client_; scoped_ptr<BluetoothAgentManagerClient> bluetooth_agent_manager_client_;
scoped_ptr<BluetoothDeviceClient> bluetooth_device_client_; scoped_ptr<BluetoothDeviceClient> bluetooth_device_client_;
...@@ -258,7 +267,7 @@ class CHROMEOS_EXPORT DBusClientBundle { ...@@ -258,7 +267,7 @@ class CHROMEOS_EXPORT DBusClientBundle {
scoped_ptr<IntrospectableClient> introspectable_client_; scoped_ptr<IntrospectableClient> introspectable_client_;
scoped_ptr<ModemMessagingClient> modem_messaging_client_; scoped_ptr<ModemMessagingClient> modem_messaging_client_;
// The declaration order for NFC client objects is important. See // The declaration order for NFC client objects is important. See
// DBusThreadManager::CreateDefaultClients for the dependencies. // DBusThreadManager::InitializeClients for the dependencies.
scoped_ptr<NfcManagerClient> nfc_manager_client_; scoped_ptr<NfcManagerClient> nfc_manager_client_;
scoped_ptr<NfcAdapterClient> nfc_adapter_client_; scoped_ptr<NfcAdapterClient> nfc_adapter_client_;
scoped_ptr<NfcDeviceClient> nfc_device_client_; scoped_ptr<NfcDeviceClient> nfc_device_client_;
......
...@@ -11,8 +11,7 @@ ...@@ -11,8 +11,7 @@
namespace chromeos { namespace chromeos {
TEST(DBusClientBundleTest, UnstubFlagParser) { TEST(DBusClientBundleTest, UnstubFlagParser) {
EXPECT_EQ(DBusClientBundle::NO_CLIENTS, EXPECT_EQ(0, DBusClientBundle::ParseUnstubList("foo"));
DBusClientBundle::ParseUnstubList("foo"));
EXPECT_EQ(DBusClientBundle::BLUETOOTH, EXPECT_EQ(DBusClientBundle::BLUETOOTH,
DBusClientBundle::ParseUnstubList("BLUETOOTH")); DBusClientBundle::ParseUnstubList("BLUETOOTH"));
......
...@@ -54,12 +54,12 @@ namespace chromeos { ...@@ -54,12 +54,12 @@ namespace chromeos {
static DBusThreadManager* g_dbus_thread_manager = NULL; static DBusThreadManager* g_dbus_thread_manager = NULL;
static bool g_using_dbus_thread_manager_for_testing = false; static bool g_using_dbus_thread_manager_for_testing = false;
DBusClientBundle::DBusClientTypeMask DBusThreadManager::DBusThreadManager(scoped_ptr<DBusClientBundle> client_bundle)
DBusThreadManager::unstub_client_mask_ = DBusClientBundle::NO_CLIENTS; : client_bundle_(client_bundle.Pass()) {
DBusThreadManager::DBusThreadManager() {
dbus::statistics::Initialize(); dbus::statistics::Initialize();
if (!DBusThreadManager::IsUsingStub(DBusClientBundle::ALL_CLIENTS)) {
if (client_bundle_->IsUsingAnyRealClient()) {
// At least one real DBusClient is used.
// Create the D-Bus thread. // Create the D-Bus thread.
base::Thread::Options thread_options; base::Thread::Options thread_options;
thread_options.message_loop_type = base::MessageLoop::TYPE_IO; thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
...@@ -74,7 +74,9 @@ DBusThreadManager::DBusThreadManager() { ...@@ -74,7 +74,9 @@ DBusThreadManager::DBusThreadManager() {
system_bus_ = new dbus::Bus(system_bus_options); system_bus_ = new dbus::Bus(system_bus_options);
} }
CreateDefaultClients(); // TODO(crbug.com/345586): Move PowerPolicyController out of
// DBusThreadManager.
power_policy_controller_.reset(new PowerPolicyController);
} }
DBusThreadManager::~DBusThreadManager() { DBusThreadManager::~DBusThreadManager() {
...@@ -95,7 +97,7 @@ DBusThreadManager::~DBusThreadManager() { ...@@ -95,7 +97,7 @@ DBusThreadManager::~DBusThreadManager() {
dbus::statistics::Shutdown(); dbus::statistics::Shutdown();
if (g_dbus_thread_manager == NULL) if (!g_dbus_thread_manager)
return; // Called form Shutdown() or local test instance. return; // Called form Shutdown() or local test instance.
// There should never be both a global instance and a local instance. // There should never be both a global instance and a local instance.
...@@ -268,13 +270,6 @@ PowerPolicyController* DBusThreadManager::GetPowerPolicyController() { ...@@ -268,13 +270,6 @@ PowerPolicyController* DBusThreadManager::GetPowerPolicyController() {
return power_policy_controller_.get(); return power_policy_controller_.get();
} }
void DBusThreadManager::CreateDefaultClients() {
client_bundle_.reset(new DBusClientBundle());
// TODO(crbug.com/345586): Move PowerPolicyController out of
// DBusThreadManager.
power_policy_controller_.reset(new PowerPolicyController);
}
void DBusThreadManager::InitializeClients() { void DBusThreadManager::InitializeClients() {
GetBluetoothAdapterClient()->Init(GetSystemBus()); GetBluetoothAdapterClient()->Init(GetSystemBus());
GetBluetoothAgentManagerClient()->Init(GetSystemBus()); GetBluetoothAgentManagerClient()->Init(GetSystemBus());
...@@ -329,9 +324,8 @@ void DBusThreadManager::InitializeClients() { ...@@ -329,9 +324,8 @@ void DBusThreadManager::InitializeClients() {
client_bundle_->SetupDefaultEnvironment(); client_bundle_->SetupDefaultEnvironment();
} }
// static
bool DBusThreadManager::IsUsingStub(DBusClientBundle::DBusClientType client) { bool DBusThreadManager::IsUsingStub(DBusClientBundle::DBusClientType client) {
return !(unstub_client_mask_ & client); return client_bundle_->IsUsingStub(client);
} }
// static // static
...@@ -341,7 +335,7 @@ void DBusThreadManager::Initialize() { ...@@ -341,7 +335,7 @@ void DBusThreadManager::Initialize() {
if (g_using_dbus_thread_manager_for_testing) if (g_using_dbus_thread_manager_for_testing)
return; return;
CHECK(g_dbus_thread_manager == NULL); CHECK(!g_dbus_thread_manager);
bool use_dbus_stub = !base::SysInfo::IsRunningOnChromeOS() || bool use_dbus_stub = !base::SysInfo::IsRunningOnChromeOS() ||
CommandLine::ForCurrentProcess()->HasSwitch( CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kDbusStub); chromeos::switches::kDbusStub);
...@@ -355,7 +349,7 @@ void DBusThreadManager::Initialize() { ...@@ -355,7 +349,7 @@ void DBusThreadManager::Initialize() {
} else if (use_dbus_stub) { } else if (use_dbus_stub) {
InitializeWithStubs(); InitializeWithStubs();
} else { } else {
InitializeRegular(); InitializeWithRealClients();
} }
} }
...@@ -370,39 +364,37 @@ scoped_ptr<DBusThreadManagerSetter> DBusThreadManager::GetSetterForTesting() { ...@@ -370,39 +364,37 @@ scoped_ptr<DBusThreadManagerSetter> DBusThreadManager::GetSetterForTesting() {
} }
// static // static
void DBusThreadManager::CreateGlobalInstance() { void DBusThreadManager::CreateGlobalInstance(
DBusClientBundle::DBusClientTypeMask unstub_client_mask) {
CHECK(!g_dbus_thread_manager); CHECK(!g_dbus_thread_manager);
g_dbus_thread_manager = new DBusThreadManager(); g_dbus_thread_manager = new DBusThreadManager(
make_scoped_ptr(new DBusClientBundle(unstub_client_mask)));
g_dbus_thread_manager->InitializeClients(); g_dbus_thread_manager->InitializeClients();
} }
// static // static
void DBusThreadManager::InitializeRegular() { void DBusThreadManager::InitializeWithRealClients() {
unstub_client_mask_ = DBusClientBundle::ALL_CLIENTS; CreateGlobalInstance(~static_cast<DBusClientBundle::DBusClientTypeMask>(0));
CreateGlobalInstance();
VLOG(1) << "DBusThreadManager initialized for Chrome OS"; VLOG(1) << "DBusThreadManager initialized for Chrome OS";
} }
// static // static
void DBusThreadManager::InitializeWithStubs() { void DBusThreadManager::InitializeWithStubs() {
unstub_client_mask_ = DBusClientBundle::NO_CLIENTS; CreateGlobalInstance(0 /* unstub_client_mask */);
CreateGlobalInstance();
VLOG(1) << "DBusThreadManager created for testing"; VLOG(1) << "DBusThreadManager created for testing";
} }
// static // static
void DBusThreadManager::InitializeWithPartialStub( void DBusThreadManager::InitializeWithPartialStub(
const std::string& unstub_clients) { const std::string& unstub_clients) {
unstub_client_mask_ = DBusClientBundle::ParseUnstubList(unstub_clients); DBusClientBundle::DBusClientTypeMask unstub_client_mask =
DBusClientBundle::ParseUnstubList(unstub_clients);
// We should have something parsed correctly here. // We should have something parsed correctly here.
if (unstub_client_mask_ == 0) { LOG_IF(FATAL, unstub_client_mask == 0)
LOG(FATAL) << "Switch values for --" << "Switch values for --" << chromeos::switches::kDbusUnstubClients
<< chromeos::switches::kDbusUnstubClients << " cannot be parsed: " << unstub_clients;
<< " cannot be parsed: "
<< unstub_clients;
}
VLOG(1) << "DBusThreadManager initialized for mixed runtime environment"; VLOG(1) << "DBusThreadManager initialized for mixed runtime environment";
CreateGlobalInstance(); CreateGlobalInstance(unstub_client_mask);
} }
// static // static
......
...@@ -86,6 +86,8 @@ class CHROMEOS_EXPORT DBusThreadManager { ...@@ -86,6 +86,8 @@ class CHROMEOS_EXPORT DBusThreadManager {
// Sets the global instance. Must be called before any calls to Get(). // Sets the global instance. Must be called before any calls to Get().
// We explicitly initialize and shut down the global object, rather than // We explicitly initialize and shut down the global object, rather than
// making it a Singleton, to ensure clean startup and shutdown. // making it a Singleton, to ensure clean startup and shutdown.
// This will initialize real or stub DBusClients depending on command-line
// arguments and whether this process runs in a ChromeOS environment.
static void Initialize(); static void Initialize();
// Returns a DBusThreadManagerSetter instance that allows tests to // Returns a DBusThreadManagerSetter instance that allows tests to
...@@ -104,7 +106,7 @@ class CHROMEOS_EXPORT DBusThreadManager { ...@@ -104,7 +106,7 @@ class CHROMEOS_EXPORT DBusThreadManager {
static DBusThreadManager* Get(); static DBusThreadManager* Get();
// Returns true if |client| is stubbed. // Returns true if |client| is stubbed.
static bool IsUsingStub(DBusClientBundle::DBusClientType client); bool IsUsingStub(DBusClientBundle::DBusClientType client);
// Returns various D-Bus bus instances, owned by DBusThreadManager. // Returns various D-Bus bus instances, owned by DBusThreadManager.
dbus::Bus* GetSystemBus(); dbus::Bus* GetSystemBus();
...@@ -151,36 +153,34 @@ class CHROMEOS_EXPORT DBusThreadManager { ...@@ -151,36 +153,34 @@ class CHROMEOS_EXPORT DBusThreadManager {
private: private:
friend class DBusThreadManagerSetter; friend class DBusThreadManagerSetter;
DBusThreadManager(); // Creates a new DBusThreadManager using the DBusClients set in
// |client_bundle|.
explicit DBusThreadManager(scoped_ptr<DBusClientBundle> client_bundle);
~DBusThreadManager(); ~DBusThreadManager();
// Creates a global instance of DBusThreadManager. Can not be called more // Creates a global instance of DBusThreadManager with the real
// than once. // implementations for all clients that are listed in |unstub_client_mask| and
static void CreateGlobalInstance(); // stub implementations for all clients that are not included. Cannot be
// called more than once.
static void CreateGlobalInstance(
DBusClientBundle::DBusClientTypeMask unstub_client_mask);
// Initialize global thread manager instance. // Initialize global thread manager instance with all real dbus client
static void InitializeRegular(); // implementations.
static void InitializeWithRealClients();
// Initialize global thread manager instance with stubbed-out dbus clients // Initialize global thread manager instance with stubbed-out dbus clients
// implementation. // implementation.
static void InitializeWithStubs(); static void InitializeWithStubs();
// Initialize with stub implementations for only certain clients that are // Initialize with stub implementations for only certain clients that are
// not included in comma-separated |unstub_clients| list. // not included in the comma-separated |unstub_clients| list.
static void InitializeWithPartialStub(const std::string& unstub_clients); static void InitializeWithPartialStub(const std::string& unstub_clients);
// Constructs all clients and stores them in the respective *_client_ member // Initializes all currently stored DBusClients with the system bus and
// variable. // performs additional setup.
void CreateDefaultClients();
// Constructs all clients and stores them in the respective *_client_ member
// variable.
void InitializeClients(); void InitializeClients();
// Bitmask that defines which dbus clients are not stubbed out. Bitmap flags
// are defined within DBusClientBundle::DBusClientType enum.
static DBusClientBundle::DBusClientTypeMask unstub_client_mask_;
scoped_ptr<base::Thread> dbus_thread_; scoped_ptr<base::Thread> dbus_thread_;
scoped_refptr<dbus::Bus> system_bus_; scoped_refptr<dbus::Bus> system_bus_;
scoped_ptr<DBusClientBundle> client_bundle_; scoped_ptr<DBusClientBundle> client_bundle_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment