Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chromeos / dbus / dbus_client_bundle.cc
blob159b3f2b368058003705052bd6c4521bb0451825
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_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/dbus_thread_manager.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_profile_manager_client.h"
36 #include "chromeos/dbus/fake_cryptohome_client.h"
37 #include "chromeos/dbus/fake_debug_daemon_client.h"
38 #include "chromeos/dbus/fake_easy_unlock_client.h"
39 #include "chromeos/dbus/fake_gsm_sms_client.h"
40 #include "chromeos/dbus/fake_image_burner_client.h"
41 #include "chromeos/dbus/fake_introspectable_client.h"
42 #include "chromeos/dbus/fake_lorgnette_manager_client.h"
43 #include "chromeos/dbus/fake_modem_messaging_client.h"
44 #include "chromeos/dbus/fake_nfc_adapter_client.h"
45 #include "chromeos/dbus/fake_nfc_device_client.h"
46 #include "chromeos/dbus/fake_nfc_manager_client.h"
47 #include "chromeos/dbus/fake_nfc_record_client.h"
48 #include "chromeos/dbus/fake_nfc_tag_client.h"
49 #include "chromeos/dbus/fake_permission_broker_client.h"
50 #include "chromeos/dbus/fake_shill_device_client.h"
51 #include "chromeos/dbus/fake_shill_ipconfig_client.h"
52 #include "chromeos/dbus/fake_shill_manager_client.h"
53 #include "chromeos/dbus/fake_shill_profile_client.h"
54 #include "chromeos/dbus/fake_shill_service_client.h"
55 #include "chromeos/dbus/fake_sms_client.h"
56 #include "chromeos/dbus/fake_system_clock_client.h"
57 #include "chromeos/dbus/gsm_sms_client.h"
58 #include "chromeos/dbus/image_burner_client.h"
59 #include "chromeos/dbus/introspectable_client.h"
60 #include "chromeos/dbus/lorgnette_manager_client.h"
61 #include "chromeos/dbus/modem_messaging_client.h"
62 #include "chromeos/dbus/nfc_adapter_client.h"
63 #include "chromeos/dbus/nfc_device_client.h"
64 #include "chromeos/dbus/nfc_manager_client.h"
65 #include "chromeos/dbus/nfc_record_client.h"
66 #include "chromeos/dbus/nfc_tag_client.h"
67 #include "chromeos/dbus/permission_broker_client.h"
68 #include "chromeos/dbus/power_manager_client.h"
69 #include "chromeos/dbus/power_policy_controller.h"
70 #include "chromeos/dbus/session_manager_client.h"
71 #include "chromeos/dbus/shill_device_client.h"
72 #include "chromeos/dbus/shill_ipconfig_client.h"
73 #include "chromeos/dbus/shill_manager_client.h"
74 #include "chromeos/dbus/shill_profile_client.h"
75 #include "chromeos/dbus/shill_service_client.h"
76 #include "chromeos/dbus/sms_client.h"
77 #include "chromeos/dbus/system_clock_client.h"
78 #include "chromeos/dbus/update_engine_client.h"
80 namespace chromeos {
82 namespace {
84 // Command line switch mapping for --dbus-unstub-clients.
85 const struct {
86 const char* param_name;
87 DBusClientBundle::DBusClientType client_type;
88 } client_type_map[] = {
89 { "bluetooth", DBusClientBundle::BLUETOOTH },
90 { "cras", DBusClientBundle::CRAS },
91 { "cros_disks", DBusClientBundle::CROS_DISKS },
92 { "cryptohome", DBusClientBundle::CRYPTOHOME },
93 { "debug_daemon", DBusClientBundle::DEBUG_DAEMON },
94 { "easy_unlock", DBusClientBundle::EASY_UNLOCK },
95 { "lorgnette_manager", DBusClientBundle::LORGNETTE_MANAGER },
96 { "shill", DBusClientBundle::SHILL },
97 { "gsm_sms", DBusClientBundle::GSM_SMS },
98 { "image_burner", DBusClientBundle::IMAGE_BURNER },
99 { "introspectable", DBusClientBundle::INTROSPECTABLE },
100 { "modem_messaging", DBusClientBundle::MODEM_MESSAGING },
101 { "nfc", DBusClientBundle::NFC },
102 { "permission_broker", DBusClientBundle::PERMISSION_BROKER },
103 { "power_manager", DBusClientBundle::POWER_MANAGER },
104 { "session_manager", DBusClientBundle::SESSION_MANAGER },
105 { "sms", DBusClientBundle::SMS },
106 { "system_clock", DBusClientBundle::SYSTEM_CLOCK },
107 { "update_engine", DBusClientBundle::UPDATE_ENGINE },
110 // Parses single command line param value for dbus subsystem and returns its
111 // enum representation. DBusClientType::UNKWNOWN is returned if |client_type|
112 // does not match any known dbus client.
113 DBusClientBundle::DBusClientType GetDBusClientType(
114 const std::string& client_type) {
115 for (size_t i = 0; i < arraysize(client_type_map); i++) {
116 if (LowerCaseEqualsASCII(client_type, client_type_map[i].param_name))
117 return client_type_map[i].client_type;
119 return DBusClientBundle::NO_CLIENTS;
122 } // namespace
124 DBusClientBundle::DBusClientBundle() {
125 if (!DBusThreadManager::IsUsingStub(BLUETOOTH)) {
126 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create());
127 bluetooth_agent_manager_client_.reset(
128 BluetoothAgentManagerClient::Create());
129 bluetooth_device_client_.reset(BluetoothDeviceClient::Create());
130 bluetooth_input_client_.reset(BluetoothInputClient::Create());
131 bluetooth_profile_manager_client_.reset(
132 BluetoothProfileManagerClient::Create());
133 bluetooth_gatt_characteristic_client_.reset(
134 BluetoothGattCharacteristicClient::Create());
135 bluetooth_gatt_descriptor_client_.reset(
136 BluetoothGattDescriptorClient::Create());
137 bluetooth_gatt_manager_client_.reset(
138 BluetoothGattManagerClient::Create());
139 bluetooth_gatt_service_client_.reset(
140 BluetoothGattServiceClient::Create());
141 } else {
142 bluetooth_adapter_client_.reset(new FakeBluetoothAdapterClient);
143 bluetooth_agent_manager_client_.reset(new FakeBluetoothAgentManagerClient);
144 bluetooth_device_client_.reset(new FakeBluetoothDeviceClient);
145 bluetooth_input_client_.reset(new FakeBluetoothInputClient);
146 bluetooth_profile_manager_client_.reset(
147 new FakeBluetoothProfileManagerClient);
148 bluetooth_gatt_characteristic_client_.reset(
149 new FakeBluetoothGattCharacteristicClient);
150 bluetooth_gatt_descriptor_client_.reset(
151 new FakeBluetoothGattDescriptorClient);
152 bluetooth_gatt_manager_client_.reset(new FakeBluetoothGattManagerClient);
153 bluetooth_gatt_service_client_.reset(new FakeBluetoothGattServiceClient);
156 if (!DBusThreadManager::IsUsingStub(CRAS))
157 cras_audio_client_.reset(CrasAudioClient::Create());
158 else
159 cras_audio_client_.reset(new CrasAudioClientStubImpl);
161 cros_disks_client_.reset(CrosDisksClient::Create(
162 DBusThreadManager::IsUsingStub(CROS_DISKS) ?
163 STUB_DBUS_CLIENT_IMPLEMENTATION :
164 REAL_DBUS_CLIENT_IMPLEMENTATION));
166 if (!DBusThreadManager::IsUsingStub(CRYPTOHOME))
167 cryptohome_client_.reset(CryptohomeClient::Create());
168 else
169 cryptohome_client_.reset(new FakeCryptohomeClient);
171 if (!DBusThreadManager::IsUsingStub(DEBUG_DAEMON))
172 debug_daemon_client_.reset(DebugDaemonClient::Create());
173 else
174 debug_daemon_client_.reset(new FakeDebugDaemonClient);
176 if (!DBusThreadManager::IsUsingStub(EASY_UNLOCK))
177 easy_unlock_client_.reset(EasyUnlockClient::Create());
178 else
179 easy_unlock_client_.reset(new FakeEasyUnlockClient);
181 if (!DBusThreadManager::IsUsingStub(LORGNETTE_MANAGER))
182 lorgnette_manager_client_.reset(LorgnetteManagerClient::Create());
183 else
184 lorgnette_manager_client_.reset(new FakeLorgnetteManagerClient);
186 if (!DBusThreadManager::IsUsingStub(SHILL)) {
187 shill_manager_client_.reset(ShillManagerClient::Create());
188 shill_device_client_.reset(ShillDeviceClient::Create());
189 shill_ipconfig_client_.reset(ShillIPConfigClient::Create());
190 shill_service_client_.reset(ShillServiceClient::Create());
191 shill_profile_client_.reset(ShillProfileClient::Create());
192 } else {
193 shill_manager_client_.reset(new FakeShillManagerClient);
194 shill_device_client_.reset(new FakeShillDeviceClient);
195 shill_ipconfig_client_.reset(new FakeShillIPConfigClient);
196 shill_service_client_.reset(new FakeShillServiceClient);
197 shill_profile_client_.reset(new FakeShillProfileClient);
200 if (!DBusThreadManager::IsUsingStub(GSM_SMS)) {
201 gsm_sms_client_.reset(GsmSMSClient::Create());
202 } else {
203 FakeGsmSMSClient* gsm_sms_client = new FakeGsmSMSClient();
204 gsm_sms_client->set_sms_test_message_switch_present(
205 CommandLine::ForCurrentProcess()->HasSwitch(
206 chromeos::switches::kSmsTestMessages));
207 gsm_sms_client_.reset(gsm_sms_client);
210 if (!DBusThreadManager::IsUsingStub(IMAGE_BURNER))
211 image_burner_client_.reset(ImageBurnerClient::Create());
212 else
213 image_burner_client_.reset(new FakeImageBurnerClient);
215 if (!DBusThreadManager::IsUsingStub(INTROSPECTABLE))
216 introspectable_client_.reset(IntrospectableClient::Create());
217 else
218 introspectable_client_.reset(new FakeIntrospectableClient);
220 if (!DBusThreadManager::IsUsingStub(MODEM_MESSAGING))
221 modem_messaging_client_.reset(ModemMessagingClient::Create());
222 else
223 modem_messaging_client_.reset(new FakeModemMessagingClient);
225 // Create the NFC clients in the correct order based on their dependencies.
226 if (!DBusThreadManager::IsUsingStub(NFC)) {
227 nfc_manager_client_.reset(NfcManagerClient::Create());
228 nfc_adapter_client_.reset(
229 NfcAdapterClient::Create(nfc_manager_client_.get()));
230 nfc_device_client_.reset(
231 NfcDeviceClient::Create(nfc_adapter_client_.get()));
232 nfc_tag_client_.reset(NfcTagClient::Create(nfc_adapter_client_.get()));
233 nfc_record_client_.reset(NfcRecordClient::Create(nfc_device_client_.get(),
234 nfc_tag_client_.get()));
235 } else {
236 nfc_manager_client_.reset(new FakeNfcManagerClient);
237 nfc_adapter_client_.reset(new FakeNfcAdapterClient);
238 nfc_device_client_.reset(new FakeNfcDeviceClient);
239 nfc_tag_client_.reset(new FakeNfcTagClient);
240 nfc_record_client_.reset(new FakeNfcRecordClient);
243 if (!DBusThreadManager::IsUsingStub(PERMISSION_BROKER))
244 permission_broker_client_.reset(PermissionBrokerClient::Create());
245 else
246 permission_broker_client_.reset(new FakePermissionBrokerClient);
248 power_manager_client_.reset(PowerManagerClient::Create(
249 DBusThreadManager::IsUsingStub(POWER_MANAGER) ?
250 STUB_DBUS_CLIENT_IMPLEMENTATION :
251 REAL_DBUS_CLIENT_IMPLEMENTATION));
253 session_manager_client_.reset(SessionManagerClient::Create(
254 DBusThreadManager::IsUsingStub(SESSION_MANAGER) ?
255 STUB_DBUS_CLIENT_IMPLEMENTATION :
256 REAL_DBUS_CLIENT_IMPLEMENTATION));
258 if (!DBusThreadManager::IsUsingStub(SMS))
259 sms_client_.reset(SMSClient::Create());
260 else
261 sms_client_.reset(new FakeSMSClient);
263 if (!DBusThreadManager::IsUsingStub(SYSTEM_CLOCK))
264 system_clock_client_.reset(SystemClockClient::Create());
265 else
266 system_clock_client_.reset(new FakeSystemClockClient);
268 update_engine_client_.reset(UpdateEngineClient::Create(
269 DBusThreadManager::IsUsingStub(UPDATE_ENGINE) ?
270 STUB_DBUS_CLIENT_IMPLEMENTATION :
271 REAL_DBUS_CLIENT_IMPLEMENTATION));
274 DBusClientBundle::~DBusClientBundle() {
277 void DBusClientBundle::SetupDefaultEnvironment() {
278 ShillManagerClient::TestInterface* manager =
279 shill_manager_client_->GetTestInterface();
280 if (manager)
281 manager->SetupDefaultEnvironment();
284 // static
285 DBusClientBundle::DBusClientTypeMask DBusClientBundle::ParseUnstubList(
286 const std::string& unstub_list) {
287 DBusClientTypeMask unstub_mask = 0;
288 std::vector<std::string> unstub_components;
289 base::SplitString(unstub_list, ',', &unstub_components);
290 for (std::vector<std::string>::const_iterator iter =
291 unstub_components.begin();
292 iter != unstub_components.end(); ++iter) {
293 DBusClientBundle::DBusClientType client = GetDBusClientType(*iter);
294 if (client != DBusClientBundle::NO_CLIENTS) {
295 LOG(WARNING) << "Unstubbing dbus client for " << *iter;
296 unstub_mask |= client;
297 } else {
298 LOG(ERROR) << "Unknown dbus client: " << *iter;
302 return unstub_mask;
305 } // namespace chromeos