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.
5 #include "chromeos/dbus/dbus_client_bundle.h"
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_media_client.h"
20 #include "chromeos/dbus/bluetooth_media_transport_client.h"
21 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
22 #include "chromeos/dbus/cras_audio_client.h"
23 #include "chromeos/dbus/cros_disks_client.h"
24 #include "chromeos/dbus/cryptohome_client.h"
25 #include "chromeos/dbus/debug_daemon_client.h"
26 #include "chromeos/dbus/easy_unlock_client.h"
27 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
28 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
29 #include "chromeos/dbus/fake_bluetooth_device_client.h"
30 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
31 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
32 #include "chromeos/dbus/fake_bluetooth_gatt_manager_client.h"
33 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
34 #include "chromeos/dbus/fake_bluetooth_input_client.h"
35 #include "chromeos/dbus/fake_bluetooth_media_client.h"
36 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h"
37 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
38 #include "chromeos/dbus/fake_cras_audio_client.h"
39 #include "chromeos/dbus/fake_cryptohome_client.h"
40 #include "chromeos/dbus/fake_debug_daemon_client.h"
41 #include "chromeos/dbus/fake_easy_unlock_client.h"
42 #include "chromeos/dbus/fake_gsm_sms_client.h"
43 #include "chromeos/dbus/fake_image_burner_client.h"
44 #include "chromeos/dbus/fake_introspectable_client.h"
45 #include "chromeos/dbus/fake_lorgnette_manager_client.h"
46 #include "chromeos/dbus/fake_modem_messaging_client.h"
47 #include "chromeos/dbus/fake_nfc_adapter_client.h"
48 #include "chromeos/dbus/fake_nfc_device_client.h"
49 #include "chromeos/dbus/fake_nfc_manager_client.h"
50 #include "chromeos/dbus/fake_nfc_record_client.h"
51 #include "chromeos/dbus/fake_nfc_tag_client.h"
52 #include "chromeos/dbus/fake_peer_daemon_manager_client.h"
53 #include "chromeos/dbus/fake_permission_broker_client.h"
54 #include "chromeos/dbus/fake_privet_daemon_client.h"
55 #include "chromeos/dbus/fake_shill_device_client.h"
56 #include "chromeos/dbus/fake_shill_ipconfig_client.h"
57 #include "chromeos/dbus/fake_shill_manager_client.h"
58 #include "chromeos/dbus/fake_shill_profile_client.h"
59 #include "chromeos/dbus/fake_shill_service_client.h"
60 #include "chromeos/dbus/fake_shill_third_party_vpn_driver_client.h"
61 #include "chromeos/dbus/fake_sms_client.h"
62 #include "chromeos/dbus/fake_system_clock_client.h"
63 #include "chromeos/dbus/gsm_sms_client.h"
64 #include "chromeos/dbus/image_burner_client.h"
65 #include "chromeos/dbus/introspectable_client.h"
66 #include "chromeos/dbus/lorgnette_manager_client.h"
67 #include "chromeos/dbus/modem_messaging_client.h"
68 #include "chromeos/dbus/nfc_adapter_client.h"
69 #include "chromeos/dbus/nfc_device_client.h"
70 #include "chromeos/dbus/nfc_manager_client.h"
71 #include "chromeos/dbus/nfc_record_client.h"
72 #include "chromeos/dbus/nfc_tag_client.h"
73 #include "chromeos/dbus/peer_daemon_manager_client.h"
74 #include "chromeos/dbus/permission_broker_client.h"
75 #include "chromeos/dbus/power_manager_client.h"
76 #include "chromeos/dbus/power_policy_controller.h"
77 #include "chromeos/dbus/privet_daemon_client.h"
78 #include "chromeos/dbus/session_manager_client.h"
79 #include "chromeos/dbus/shill_device_client.h"
80 #include "chromeos/dbus/shill_ipconfig_client.h"
81 #include "chromeos/dbus/shill_manager_client.h"
82 #include "chromeos/dbus/shill_profile_client.h"
83 #include "chromeos/dbus/shill_service_client.h"
84 #include "chromeos/dbus/shill_third_party_vpn_driver_client.h"
85 #include "chromeos/dbus/sms_client.h"
86 #include "chromeos/dbus/system_clock_client.h"
87 #include "chromeos/dbus/update_engine_client.h"
93 // Command line switch mapping for --dbus-unstub-clients.
95 const char* param_name
;
96 DBusClientBundle::DBusClientType client_type
;
97 } client_type_map
[] = {
98 { "bluetooth", DBusClientBundle::BLUETOOTH
},
99 { "cras", DBusClientBundle::CRAS
},
100 { "cros_disks", DBusClientBundle::CROS_DISKS
},
101 { "cryptohome", DBusClientBundle::CRYPTOHOME
},
102 { "debug_daemon", DBusClientBundle::DEBUG_DAEMON
},
103 { "easy_unlock", DBusClientBundle::EASY_UNLOCK
},
104 { "lorgnette_manager", DBusClientBundle::LORGNETTE_MANAGER
},
105 { "shill", DBusClientBundle::SHILL
},
106 { "gsm_sms", DBusClientBundle::GSM_SMS
},
107 { "image_burner", DBusClientBundle::IMAGE_BURNER
},
108 { "introspectable", DBusClientBundle::INTROSPECTABLE
},
109 { "modem_messaging", DBusClientBundle::MODEM_MESSAGING
},
110 { "nfc", DBusClientBundle::NFC
},
111 { "peer_daemon", DBusClientBundle::PEER_DAEMON
},
112 { "permission_broker", DBusClientBundle::PERMISSION_BROKER
},
113 { "power_manager", DBusClientBundle::POWER_MANAGER
},
114 { "privet_daemon", DBusClientBundle::PRIVET_DAEMON
},
115 { "session_manager", DBusClientBundle::SESSION_MANAGER
},
116 { "sms", DBusClientBundle::SMS
},
117 { "system_clock", DBusClientBundle::SYSTEM_CLOCK
},
118 { "update_engine", DBusClientBundle::UPDATE_ENGINE
},
121 // Parses single command line param value for dbus subsystem. If successful,
122 // returns its enum representation. Otherwise returns NO_CLIENT.
123 DBusClientBundle::DBusClientType
GetDBusClientType(
124 const std::string
& client_type_name
) {
125 for (size_t i
= 0; i
< arraysize(client_type_map
); i
++) {
126 if (LowerCaseEqualsASCII(client_type_name
, client_type_map
[i
].param_name
))
127 return client_type_map
[i
].client_type
;
129 return DBusClientBundle::NO_CLIENT
;
134 DBusClientBundle::DBusClientBundle(DBusClientTypeMask unstub_client_mask
)
135 : unstub_client_mask_(unstub_client_mask
) {
136 if (!IsUsingStub(BLUETOOTH
)) {
137 bluetooth_adapter_client_
.reset(BluetoothAdapterClient::Create());
138 bluetooth_agent_manager_client_
.reset(
139 BluetoothAgentManagerClient::Create());
140 bluetooth_device_client_
.reset(BluetoothDeviceClient::Create());
141 bluetooth_input_client_
.reset(BluetoothInputClient::Create());
142 bluetooth_media_client_
.reset(BluetoothMediaClient::Create());
143 bluetooth_media_transport_client_
.reset(
144 BluetoothMediaTransportClient::Create());
145 bluetooth_profile_manager_client_
.reset(
146 BluetoothProfileManagerClient::Create());
147 bluetooth_gatt_characteristic_client_
.reset(
148 BluetoothGattCharacteristicClient::Create());
149 bluetooth_gatt_descriptor_client_
.reset(
150 BluetoothGattDescriptorClient::Create());
151 bluetooth_gatt_manager_client_
.reset(
152 BluetoothGattManagerClient::Create());
153 bluetooth_gatt_service_client_
.reset(
154 BluetoothGattServiceClient::Create());
156 bluetooth_adapter_client_
.reset(new FakeBluetoothAdapterClient
);
157 bluetooth_agent_manager_client_
.reset(new FakeBluetoothAgentManagerClient
);
158 bluetooth_device_client_
.reset(new FakeBluetoothDeviceClient
);
159 bluetooth_input_client_
.reset(new FakeBluetoothInputClient
);
160 bluetooth_media_client_
.reset(new FakeBluetoothMediaClient
);
161 bluetooth_media_transport_client_
.reset(
162 new FakeBluetoothMediaTransportClient
);
163 bluetooth_profile_manager_client_
.reset(
164 new FakeBluetoothProfileManagerClient
);
165 bluetooth_gatt_characteristic_client_
.reset(
166 new FakeBluetoothGattCharacteristicClient
);
167 bluetooth_gatt_descriptor_client_
.reset(
168 new FakeBluetoothGattDescriptorClient
);
169 bluetooth_gatt_manager_client_
.reset(new FakeBluetoothGattManagerClient
);
170 bluetooth_gatt_service_client_
.reset(new FakeBluetoothGattServiceClient
);
173 if (!IsUsingStub(CRAS
))
174 cras_audio_client_
.reset(CrasAudioClient::Create());
176 cras_audio_client_
.reset(new FakeCrasAudioClient
);
178 cros_disks_client_
.reset(CrosDisksClient::Create(
179 IsUsingStub(CROS_DISKS
) ? STUB_DBUS_CLIENT_IMPLEMENTATION
180 : REAL_DBUS_CLIENT_IMPLEMENTATION
));
182 if (!IsUsingStub(CRYPTOHOME
))
183 cryptohome_client_
.reset(CryptohomeClient::Create());
185 cryptohome_client_
.reset(new FakeCryptohomeClient
);
187 if (!IsUsingStub(DEBUG_DAEMON
))
188 debug_daemon_client_
.reset(DebugDaemonClient::Create());
190 debug_daemon_client_
.reset(new FakeDebugDaemonClient
);
192 if (!IsUsingStub(EASY_UNLOCK
))
193 easy_unlock_client_
.reset(EasyUnlockClient::Create());
195 easy_unlock_client_
.reset(new FakeEasyUnlockClient
);
197 if (!IsUsingStub(LORGNETTE_MANAGER
))
198 lorgnette_manager_client_
.reset(LorgnetteManagerClient::Create());
200 lorgnette_manager_client_
.reset(new FakeLorgnetteManagerClient
);
202 if (!IsUsingStub(SHILL
)) {
203 shill_manager_client_
.reset(ShillManagerClient::Create());
204 shill_device_client_
.reset(ShillDeviceClient::Create());
205 shill_ipconfig_client_
.reset(ShillIPConfigClient::Create());
206 shill_service_client_
.reset(ShillServiceClient::Create());
207 shill_profile_client_
.reset(ShillProfileClient::Create());
208 shill_third_party_vpn_driver_client_
.reset(
209 ShillThirdPartyVpnDriverClient::Create());
211 shill_manager_client_
.reset(new FakeShillManagerClient
);
212 shill_device_client_
.reset(new FakeShillDeviceClient
);
213 shill_ipconfig_client_
.reset(new FakeShillIPConfigClient
);
214 shill_service_client_
.reset(new FakeShillServiceClient
);
215 shill_profile_client_
.reset(new FakeShillProfileClient
);
216 shill_third_party_vpn_driver_client_
.reset(
217 new FakeShillThirdPartyVpnDriverClient
);
220 if (!IsUsingStub(GSM_SMS
)) {
221 gsm_sms_client_
.reset(GsmSMSClient::Create());
223 FakeGsmSMSClient
* gsm_sms_client
= new FakeGsmSMSClient();
224 gsm_sms_client
->set_sms_test_message_switch_present(
225 base::CommandLine::ForCurrentProcess()->HasSwitch(
226 chromeos::switches::kSmsTestMessages
));
227 gsm_sms_client_
.reset(gsm_sms_client
);
230 if (!IsUsingStub(IMAGE_BURNER
))
231 image_burner_client_
.reset(ImageBurnerClient::Create());
233 image_burner_client_
.reset(new FakeImageBurnerClient
);
235 if (!IsUsingStub(INTROSPECTABLE
))
236 introspectable_client_
.reset(IntrospectableClient::Create());
238 introspectable_client_
.reset(new FakeIntrospectableClient
);
240 if (!IsUsingStub(MODEM_MESSAGING
))
241 modem_messaging_client_
.reset(ModemMessagingClient::Create());
243 modem_messaging_client_
.reset(new FakeModemMessagingClient
);
245 // Create the NFC clients in the correct order based on their dependencies.
246 if (!IsUsingStub(NFC
)) {
247 nfc_manager_client_
.reset(NfcManagerClient::Create());
248 nfc_adapter_client_
.reset(
249 NfcAdapterClient::Create(nfc_manager_client_
.get()));
250 nfc_device_client_
.reset(
251 NfcDeviceClient::Create(nfc_adapter_client_
.get()));
252 nfc_tag_client_
.reset(NfcTagClient::Create(nfc_adapter_client_
.get()));
253 nfc_record_client_
.reset(NfcRecordClient::Create(nfc_device_client_
.get(),
254 nfc_tag_client_
.get()));
256 nfc_manager_client_
.reset(new FakeNfcManagerClient
);
257 nfc_adapter_client_
.reset(new FakeNfcAdapterClient
);
258 nfc_device_client_
.reset(new FakeNfcDeviceClient
);
259 nfc_tag_client_
.reset(new FakeNfcTagClient
);
260 nfc_record_client_
.reset(new FakeNfcRecordClient
);
263 if (!IsUsingStub(PEER_DAEMON
))
264 peer_daemon_manager_client_
.reset(PeerDaemonManagerClient::Create());
266 peer_daemon_manager_client_
.reset(new FakePeerDaemonManagerClient
);
268 if (!IsUsingStub(PERMISSION_BROKER
))
269 permission_broker_client_
.reset(PermissionBrokerClient::Create());
271 permission_broker_client_
.reset(new FakePermissionBrokerClient
);
273 if (!IsUsingStub(PRIVET_DAEMON
))
274 privet_daemon_client_
.reset(PrivetDaemonClient::Create());
276 privet_daemon_client_
.reset(new FakePrivetDaemonClient
);
278 power_manager_client_
.reset(PowerManagerClient::Create(
279 IsUsingStub(POWER_MANAGER
) ? STUB_DBUS_CLIENT_IMPLEMENTATION
280 : REAL_DBUS_CLIENT_IMPLEMENTATION
));
282 session_manager_client_
.reset(SessionManagerClient::Create(
283 IsUsingStub(SESSION_MANAGER
) ? STUB_DBUS_CLIENT_IMPLEMENTATION
284 : REAL_DBUS_CLIENT_IMPLEMENTATION
));
286 if (!IsUsingStub(SMS
))
287 sms_client_
.reset(SMSClient::Create());
289 sms_client_
.reset(new FakeSMSClient
);
291 if (!IsUsingStub(SYSTEM_CLOCK
))
292 system_clock_client_
.reset(SystemClockClient::Create());
294 system_clock_client_
.reset(new FakeSystemClockClient
);
296 update_engine_client_
.reset(UpdateEngineClient::Create(
297 IsUsingStub(UPDATE_ENGINE
) ? STUB_DBUS_CLIENT_IMPLEMENTATION
298 : REAL_DBUS_CLIENT_IMPLEMENTATION
));
301 DBusClientBundle::~DBusClientBundle() {
304 bool DBusClientBundle::IsUsingStub(DBusClientType client
) {
305 return !(unstub_client_mask_
& client
);
308 bool DBusClientBundle::IsUsingAnyRealClient() {
309 // 'Using any real client' is equivalent to 'Unstubbed any client'.
310 return unstub_client_mask_
!= 0;
313 void DBusClientBundle::SetupDefaultEnvironment() {
314 ShillManagerClient::TestInterface
* manager
=
315 shill_manager_client_
->GetTestInterface();
317 manager
->SetupDefaultEnvironment();
321 DBusClientBundle::DBusClientTypeMask
DBusClientBundle::ParseUnstubList(
322 const std::string
& unstub_list
) {
323 DBusClientTypeMask unstub_mask
= 0;
324 std::vector
<std::string
> unstub_components
;
325 base::SplitString(unstub_list
, ',', &unstub_components
);
326 for (std::vector
<std::string
>::const_iterator iter
=
327 unstub_components
.begin();
328 iter
!= unstub_components
.end(); ++iter
) {
329 DBusClientBundle::DBusClientType client
= GetDBusClientType(*iter
);
330 if (client
!= NO_CLIENT
) {
331 LOG(WARNING
) << "Unstubbing dbus client for " << *iter
;
332 unstub_mask
|= client
;
334 LOG(ERROR
) << "Unknown dbus client: " << *iter
;
341 } // namespace chromeos