Home | History | Annotate | Download | only in dbus
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chromeos/dbus/dbus_client_bundle.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/strings/string_split.h"
      9 #include "base/strings/string_util.h"
     10 #include "chromeos/chromeos_switches.h"
     11 #include "chromeos/dbus/bluetooth_adapter_client.h"
     12 #include "chromeos/dbus/bluetooth_agent_manager_client.h"
     13 #include "chromeos/dbus/bluetooth_device_client.h"
     14 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
     15 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h"
     16 #include "chromeos/dbus/bluetooth_gatt_manager_client.h"
     17 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
     18 #include "chromeos/dbus/bluetooth_input_client.h"
     19 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
     20 #include "chromeos/dbus/cras_audio_client.h"
     21 #include "chromeos/dbus/cras_audio_client_stub_impl.h"
     22 #include "chromeos/dbus/cros_disks_client.h"
     23 #include "chromeos/dbus/cryptohome_client.h"
     24 #include "chromeos/dbus/debug_daemon_client.h"
     25 #include "chromeos/dbus/easy_unlock_client.h"
     26 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
     27 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
     28 #include "chromeos/dbus/fake_bluetooth_device_client.h"
     29 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
     30 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
     31 #include "chromeos/dbus/fake_bluetooth_gatt_manager_client.h"
     32 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
     33 #include "chromeos/dbus/fake_bluetooth_input_client.h"
     34 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
     35 #include "chromeos/dbus/fake_cryptohome_client.h"
     36 #include "chromeos/dbus/fake_debug_daemon_client.h"
     37 #include "chromeos/dbus/fake_easy_unlock_client.h"
     38 #include "chromeos/dbus/fake_gsm_sms_client.h"
     39 #include "chromeos/dbus/fake_image_burner_client.h"
     40 #include "chromeos/dbus/fake_introspectable_client.h"
     41 #include "chromeos/dbus/fake_lorgnette_manager_client.h"
     42 #include "chromeos/dbus/fake_modem_messaging_client.h"
     43 #include "chromeos/dbus/fake_nfc_adapter_client.h"
     44 #include "chromeos/dbus/fake_nfc_device_client.h"
     45 #include "chromeos/dbus/fake_nfc_manager_client.h"
     46 #include "chromeos/dbus/fake_nfc_record_client.h"
     47 #include "chromeos/dbus/fake_nfc_tag_client.h"
     48 #include "chromeos/dbus/fake_permission_broker_client.h"
     49 #include "chromeos/dbus/fake_shill_device_client.h"
     50 #include "chromeos/dbus/fake_shill_ipconfig_client.h"
     51 #include "chromeos/dbus/fake_shill_manager_client.h"
     52 #include "chromeos/dbus/fake_shill_profile_client.h"
     53 #include "chromeos/dbus/fake_shill_service_client.h"
     54 #include "chromeos/dbus/fake_sms_client.h"
     55 #include "chromeos/dbus/fake_system_clock_client.h"
     56 #include "chromeos/dbus/gsm_sms_client.h"
     57 #include "chromeos/dbus/image_burner_client.h"
     58 #include "chromeos/dbus/introspectable_client.h"
     59 #include "chromeos/dbus/lorgnette_manager_client.h"
     60 #include "chromeos/dbus/modem_messaging_client.h"
     61 #include "chromeos/dbus/nfc_adapter_client.h"
     62 #include "chromeos/dbus/nfc_device_client.h"
     63 #include "chromeos/dbus/nfc_manager_client.h"
     64 #include "chromeos/dbus/nfc_record_client.h"
     65 #include "chromeos/dbus/nfc_tag_client.h"
     66 #include "chromeos/dbus/permission_broker_client.h"
     67 #include "chromeos/dbus/power_manager_client.h"
     68 #include "chromeos/dbus/power_policy_controller.h"
     69 #include "chromeos/dbus/session_manager_client.h"
     70 #include "chromeos/dbus/shill_device_client.h"
     71 #include "chromeos/dbus/shill_ipconfig_client.h"
     72 #include "chromeos/dbus/shill_manager_client.h"
     73 #include "chromeos/dbus/shill_profile_client.h"
     74 #include "chromeos/dbus/shill_service_client.h"
     75 #include "chromeos/dbus/sms_client.h"
     76 #include "chromeos/dbus/system_clock_client.h"
     77 #include "chromeos/dbus/update_engine_client.h"
     78 
     79 namespace chromeos {
     80 
     81 namespace {
     82 
     83 // Command line switch mapping for --dbus-unstub-clients.
     84 const struct {
     85   const char* param_name;
     86   DBusClientBundle::DBusClientType client_type;
     87 } client_type_map[] = {
     88     { "bluetooth",  DBusClientBundle::BLUETOOTH },
     89     { "cras",  DBusClientBundle::CRAS },
     90     { "cros_disks",  DBusClientBundle::CROS_DISKS },
     91     { "cryptohome",  DBusClientBundle::CRYPTOHOME },
     92     { "debug_daemon",  DBusClientBundle::DEBUG_DAEMON },
     93     { "easy_unlock",  DBusClientBundle::EASY_UNLOCK },
     94     { "lorgnette_manager",  DBusClientBundle::LORGNETTE_MANAGER },
     95     { "shill",  DBusClientBundle::SHILL },
     96     { "gsm_sms",  DBusClientBundle::GSM_SMS },
     97     { "image_burner",  DBusClientBundle::IMAGE_BURNER },
     98     { "introspectable",  DBusClientBundle::INTROSPECTABLE },
     99     { "modem_messaging",  DBusClientBundle::MODEM_MESSAGING },
    100     { "nfc",  DBusClientBundle::NFC },
    101     { "permission_broker",  DBusClientBundle::PERMISSION_BROKER },
    102     { "power_manager",  DBusClientBundle::POWER_MANAGER },
    103     { "session_manager",  DBusClientBundle::SESSION_MANAGER },
    104     { "sms",  DBusClientBundle::SMS },
    105     { "system_clock",  DBusClientBundle::SYSTEM_CLOCK },
    106     { "update_engine",  DBusClientBundle::UPDATE_ENGINE },
    107 };
    108 
    109 // Parses single command line param value for dbus subsystem. If successful,
    110 // returns its enum representation. Otherwise returns NO_CLIENT.
    111 DBusClientBundle::DBusClientType GetDBusClientType(
    112     const std::string& client_type_name) {
    113   for (size_t i = 0; i < arraysize(client_type_map); i++) {
    114     if (LowerCaseEqualsASCII(client_type_name, client_type_map[i].param_name))
    115       return client_type_map[i].client_type;
    116   }
    117   return DBusClientBundle::NO_CLIENT;
    118 }
    119 
    120 }  // namespace
    121 
    122 DBusClientBundle::DBusClientBundle(DBusClientTypeMask unstub_client_mask)
    123     : unstub_client_mask_(unstub_client_mask) {
    124   if (!IsUsingStub(BLUETOOTH)) {
    125     bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create());
    126     bluetooth_agent_manager_client_.reset(
    127         BluetoothAgentManagerClient::Create());
    128     bluetooth_device_client_.reset(BluetoothDeviceClient::Create());
    129     bluetooth_input_client_.reset(BluetoothInputClient::Create());
    130     bluetooth_profile_manager_client_.reset(
    131         BluetoothProfileManagerClient::Create());
    132     bluetooth_gatt_characteristic_client_.reset(
    133         BluetoothGattCharacteristicClient::Create());
    134     bluetooth_gatt_descriptor_client_.reset(
    135         BluetoothGattDescriptorClient::Create());
    136     bluetooth_gatt_manager_client_.reset(
    137         BluetoothGattManagerClient::Create());
    138     bluetooth_gatt_service_client_.reset(
    139         BluetoothGattServiceClient::Create());
    140   } else {
    141     bluetooth_adapter_client_.reset(new FakeBluetoothAdapterClient);
    142     bluetooth_agent_manager_client_.reset(new FakeBluetoothAgentManagerClient);
    143     bluetooth_device_client_.reset(new FakeBluetoothDeviceClient);
    144     bluetooth_input_client_.reset(new FakeBluetoothInputClient);
    145     bluetooth_profile_manager_client_.reset(
    146         new FakeBluetoothProfileManagerClient);
    147     bluetooth_gatt_characteristic_client_.reset(
    148         new FakeBluetoothGattCharacteristicClient);
    149     bluetooth_gatt_descriptor_client_.reset(
    150         new FakeBluetoothGattDescriptorClient);
    151     bluetooth_gatt_manager_client_.reset(new FakeBluetoothGattManagerClient);
    152     bluetooth_gatt_service_client_.reset(new FakeBluetoothGattServiceClient);
    153   }
    154 
    155   if (!IsUsingStub(CRAS))
    156     cras_audio_client_.reset(CrasAudioClient::Create());
    157   else
    158     cras_audio_client_.reset(new CrasAudioClientStubImpl);
    159 
    160   cros_disks_client_.reset(CrosDisksClient::Create(
    161       IsUsingStub(CROS_DISKS) ? STUB_DBUS_CLIENT_IMPLEMENTATION
    162                               : REAL_DBUS_CLIENT_IMPLEMENTATION));
    163 
    164   if (!IsUsingStub(CRYPTOHOME))
    165     cryptohome_client_.reset(CryptohomeClient::Create());
    166   else
    167     cryptohome_client_.reset(new FakeCryptohomeClient);
    168 
    169   if (!IsUsingStub(DEBUG_DAEMON))
    170     debug_daemon_client_.reset(DebugDaemonClient::Create());
    171   else
    172     debug_daemon_client_.reset(new FakeDebugDaemonClient);
    173 
    174   if (!IsUsingStub(EASY_UNLOCK))
    175     easy_unlock_client_.reset(EasyUnlockClient::Create());
    176   else
    177     easy_unlock_client_.reset(new FakeEasyUnlockClient);
    178 
    179   if (!IsUsingStub(LORGNETTE_MANAGER))
    180     lorgnette_manager_client_.reset(LorgnetteManagerClient::Create());
    181   else
    182     lorgnette_manager_client_.reset(new FakeLorgnetteManagerClient);
    183 
    184   if (!IsUsingStub(SHILL)) {
    185     shill_manager_client_.reset(ShillManagerClient::Create());
    186     shill_device_client_.reset(ShillDeviceClient::Create());
    187     shill_ipconfig_client_.reset(ShillIPConfigClient::Create());
    188     shill_service_client_.reset(ShillServiceClient::Create());
    189     shill_profile_client_.reset(ShillProfileClient::Create());
    190   } else {
    191     shill_manager_client_.reset(new FakeShillManagerClient);
    192     shill_device_client_.reset(new FakeShillDeviceClient);
    193     shill_ipconfig_client_.reset(new FakeShillIPConfigClient);
    194     shill_service_client_.reset(new FakeShillServiceClient);
    195     shill_profile_client_.reset(new FakeShillProfileClient);
    196   }
    197 
    198   if (!IsUsingStub(GSM_SMS)) {
    199     gsm_sms_client_.reset(GsmSMSClient::Create());
    200   } else {
    201     FakeGsmSMSClient* gsm_sms_client = new FakeGsmSMSClient();
    202     gsm_sms_client->set_sms_test_message_switch_present(
    203         CommandLine::ForCurrentProcess()->HasSwitch(
    204             chromeos::switches::kSmsTestMessages));
    205     gsm_sms_client_.reset(gsm_sms_client);
    206   }
    207 
    208   if (!IsUsingStub(IMAGE_BURNER))
    209     image_burner_client_.reset(ImageBurnerClient::Create());
    210   else
    211     image_burner_client_.reset(new FakeImageBurnerClient);
    212 
    213   if (!IsUsingStub(INTROSPECTABLE))
    214     introspectable_client_.reset(IntrospectableClient::Create());
    215   else
    216     introspectable_client_.reset(new FakeIntrospectableClient);
    217 
    218   if (!IsUsingStub(MODEM_MESSAGING))
    219     modem_messaging_client_.reset(ModemMessagingClient::Create());
    220   else
    221     modem_messaging_client_.reset(new FakeModemMessagingClient);
    222 
    223   // Create the NFC clients in the correct order based on their dependencies.
    224   if (!IsUsingStub(NFC)) {
    225     nfc_manager_client_.reset(NfcManagerClient::Create());
    226     nfc_adapter_client_.reset(
    227         NfcAdapterClient::Create(nfc_manager_client_.get()));
    228     nfc_device_client_.reset(
    229         NfcDeviceClient::Create(nfc_adapter_client_.get()));
    230     nfc_tag_client_.reset(NfcTagClient::Create(nfc_adapter_client_.get()));
    231     nfc_record_client_.reset(NfcRecordClient::Create(nfc_device_client_.get(),
    232                                                      nfc_tag_client_.get()));
    233   } else {
    234     nfc_manager_client_.reset(new FakeNfcManagerClient);
    235     nfc_adapter_client_.reset(new FakeNfcAdapterClient);
    236     nfc_device_client_.reset(new FakeNfcDeviceClient);
    237     nfc_tag_client_.reset(new FakeNfcTagClient);
    238     nfc_record_client_.reset(new FakeNfcRecordClient);
    239   }
    240 
    241   if (!IsUsingStub(PERMISSION_BROKER))
    242     permission_broker_client_.reset(PermissionBrokerClient::Create());
    243   else
    244     permission_broker_client_.reset(new FakePermissionBrokerClient);
    245 
    246   power_manager_client_.reset(PowerManagerClient::Create(
    247       IsUsingStub(POWER_MANAGER) ? STUB_DBUS_CLIENT_IMPLEMENTATION
    248                                  : REAL_DBUS_CLIENT_IMPLEMENTATION));
    249 
    250   session_manager_client_.reset(SessionManagerClient::Create(
    251       IsUsingStub(SESSION_MANAGER) ? STUB_DBUS_CLIENT_IMPLEMENTATION
    252                                    : REAL_DBUS_CLIENT_IMPLEMENTATION));
    253 
    254   if (!IsUsingStub(SMS))
    255     sms_client_.reset(SMSClient::Create());
    256   else
    257     sms_client_.reset(new FakeSMSClient);
    258 
    259   if (!IsUsingStub(SYSTEM_CLOCK))
    260     system_clock_client_.reset(SystemClockClient::Create());
    261   else
    262     system_clock_client_.reset(new FakeSystemClockClient);
    263 
    264   update_engine_client_.reset(UpdateEngineClient::Create(
    265       IsUsingStub(UPDATE_ENGINE) ? STUB_DBUS_CLIENT_IMPLEMENTATION
    266                                  : REAL_DBUS_CLIENT_IMPLEMENTATION));
    267 }
    268 
    269 DBusClientBundle::~DBusClientBundle() {
    270 }
    271 
    272 bool DBusClientBundle::IsUsingStub(DBusClientType client) {
    273   return !(unstub_client_mask_ & client);
    274 }
    275 
    276 bool DBusClientBundle::IsUsingAnyRealClient() {
    277   // 'Using any real client' is equivalent to 'Unstubbed any client'.
    278   return unstub_client_mask_ != 0;
    279 }
    280 
    281 void DBusClientBundle::SetupDefaultEnvironment() {
    282   ShillManagerClient::TestInterface* manager =
    283       shill_manager_client_->GetTestInterface();
    284   if (manager)
    285     manager->SetupDefaultEnvironment();
    286 }
    287 
    288 // static
    289 DBusClientBundle::DBusClientTypeMask DBusClientBundle::ParseUnstubList(
    290     const std::string& unstub_list) {
    291   DBusClientTypeMask unstub_mask = 0;
    292   std::vector<std::string> unstub_components;
    293   base::SplitString(unstub_list, ',', &unstub_components);
    294   for (std::vector<std::string>::const_iterator iter =
    295           unstub_components.begin();
    296        iter != unstub_components.end(); ++iter) {
    297     DBusClientBundle::DBusClientType client = GetDBusClientType(*iter);
    298     if (client != NO_CLIENT) {
    299       LOG(WARNING) << "Unstubbing dbus client for " << *iter;
    300       unstub_mask |= client;
    301     } else {
    302       LOG(ERROR) << "Unknown dbus client: " << *iter;
    303     }
    304   }
    305 
    306   return unstub_mask;
    307 }
    308 
    309 }  // namespace chromeos
    310