summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/dbus_thread_manager.cc
blob: 471f69d155b600acbfb3f99427f9e34587a39fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) 2012 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 "chromeos/dbus/dbus_thread_manager.h"

#include <map>

#include "base/chromeos/chromeos_version.h"
#include "base/command_line.h"
#include "base/threading/thread.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/bluetooth_adapter_client.h"
#include "chromeos/dbus/bluetooth_device_client.h"
#include "chromeos/dbus/bluetooth_input_client.h"
#include "chromeos/dbus/bluetooth_manager_client.h"
#include "chromeos/dbus/bluetooth_node_client.h"
#include "chromeos/dbus/bluetooth_out_of_band_client.h"
#include "chromeos/dbus/cashew_client.h"
#include "chromeos/dbus/cros_disks_client.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_client_implementation_type.h"
#include "chromeos/dbus/debug_daemon_client.h"
#include "chromeos/dbus/flimflam_device_client.h"
#include "chromeos/dbus/flimflam_ipconfig_client.h"
#include "chromeos/dbus/flimflam_manager_client.h"
#include "chromeos/dbus/flimflam_network_client.h"
#include "chromeos/dbus/flimflam_profile_client.h"
#include "chromeos/dbus/flimflam_service_client.h"
#include "chromeos/dbus/gsm_sms_client.h"
#include "chromeos/dbus/ibus/ibus_client.h"
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
#include "chromeos/dbus/ibus/ibus_engine_service.h"
#include "chromeos/dbus/ibus/ibus_input_context_client.h"
#include "chromeos/dbus/image_burner_client.h"
#include "chromeos/dbus/introspectable_client.h"
#include "chromeos/dbus/media_transfer_protocol_daemon_client.h"
#include "chromeos/dbus/modem_messaging_client.h"
#include "chromeos/dbus/permission_broker_client.h"
#include "chromeos/dbus/power_manager_client.h"
#include "chromeos/dbus/session_manager_client.h"
#include "chromeos/dbus/sms_client.h"
#include "chromeos/dbus/speech_synthesizer_client.h"
#include "chromeos/dbus/update_engine_client.h"
#include "dbus/bus.h"

namespace chromeos {

static DBusThreadManager* g_dbus_thread_manager = NULL;

// The DBusThreadManager implementation used in production.
class DBusThreadManagerImpl : public DBusThreadManager {
 public:
  explicit DBusThreadManagerImpl(DBusClientImplementationType client_type) {
    // If --dbus-stub was requested, pass STUB to specific components;
    // Many components like login are not useful with a stub implementation.
    DBusClientImplementationType client_type_maybe_stub = client_type;
    if (CommandLine::ForCurrentProcess()->HasSwitch(
            chromeos::switches::kDbusStub))
      client_type_maybe_stub = STUB_DBUS_CLIENT_IMPLEMENTATION;

    // Create the D-Bus thread.
    base::Thread::Options thread_options;
    thread_options.message_loop_type = MessageLoop::TYPE_IO;
    dbus_thread_.reset(new base::Thread("D-Bus thread"));
    dbus_thread_->StartWithOptions(thread_options);

    // Create the connection to the system bus.
    dbus::Bus::Options system_bus_options;
    system_bus_options.bus_type = dbus::Bus::SYSTEM;
    system_bus_options.connection_type = dbus::Bus::PRIVATE;
    system_bus_options.dbus_thread_message_loop_proxy =
        dbus_thread_->message_loop_proxy();
    system_bus_ = new dbus::Bus(system_bus_options);

    // Create the bluetooth clients.
    bluetooth_manager_client_.reset(BluetoothManagerClient::Create(
        client_type, system_bus_.get()));
    bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create(
        client_type, system_bus_.get(), bluetooth_manager_client_.get()));
    bluetooth_device_client_.reset(BluetoothDeviceClient::Create(
        client_type, system_bus_.get(), bluetooth_adapter_client_.get()));
    bluetooth_input_client_.reset(BluetoothInputClient::Create(
        client_type, system_bus_.get(), bluetooth_adapter_client_.get()));
    bluetooth_node_client_.reset(BluetoothNodeClient::Create(
        client_type, system_bus_.get(), bluetooth_device_client_.get()));
    bluetooth_out_of_band_client_.reset(BluetoothOutOfBandClient::Create(
        client_type, system_bus_.get()));
    // Create the Cashew client.
    cashew_client_.reset(CashewClient::Create(client_type, system_bus_.get()));
    // Create the cros-disks client.
    cros_disks_client_.reset(
        CrosDisksClient::Create(client_type, system_bus_.get()));
    // Create the Cryptohome client.
    cryptohome_client_.reset(
        CryptohomeClient::Create(client_type, system_bus_.get()));
    // Create the debugdaemon client.
    debugdaemon_client_.reset(
        DebugDaemonClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam Device client.
    flimflam_device_client_.reset(
        FlimflamDeviceClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam IPConfig client.
    flimflam_ipconfig_client_.reset(
        FlimflamIPConfigClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam Manager client.
    flimflam_manager_client_.reset(
        FlimflamManagerClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam Network client.
    flimflam_network_client_.reset(
        FlimflamNetworkClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam Profile client.
    flimflam_profile_client_.reset(
        FlimflamProfileClient::Create(client_type, system_bus_.get()));
    // Create the Flimflam Service client.
    flimflam_service_client_.reset(
        FlimflamServiceClient::Create(client_type, system_bus_.get()));
    // Create the Gsm SMS client.
    gsm_sms_client_.reset(
        GsmSMSClient::Create(client_type, system_bus_.get()));
    // Create the image burner client.
    image_burner_client_.reset(ImageBurnerClient::Create(client_type,
                                                         system_bus_.get()));
    // Create the introspectable object client.
    introspectable_client_.reset(
        IntrospectableClient::Create(client_type, system_bus_.get()));
    // Create the media transfer protocol daemon client.
    media_transfer_protocol_daemon_client_.reset(
        MediaTransferProtocolDaemonClient::Create(client_type,
                                                  system_bus_.get()));
    // Create the ModemMessaging client.
    modem_messaging_client_.reset(
        ModemMessagingClient::Create(client_type, system_bus_.get()));
    // Create the permission broker client.
    permission_broker_client_.reset(
        PermissionBrokerClient::Create(client_type, system_bus_.get()));
    // Create the power manager client.
    power_manager_client_.reset(
        PowerManagerClient::Create(client_type_maybe_stub, system_bus_.get()));
    // Create the session manager client.
    session_manager_client_.reset(
        SessionManagerClient::Create(client_type, system_bus_.get()));
    // Create the SMS client.
    sms_client_.reset(
        SMSClient::Create(client_type, system_bus_.get()));
    // Create the speech synthesizer client.
    speech_synthesizer_client_.reset(
        SpeechSynthesizerClient::Create(client_type, system_bus_.get()));
    // Create the update engine client.
    update_engine_client_.reset(
        UpdateEngineClient::Create(client_type, system_bus_.get()));
  }

  virtual ~DBusThreadManagerImpl() {
    // Shut down the bus. During the browser shutdown, it's ok to shut down
    // the bus synchronously.
    system_bus_->ShutdownOnDBusThreadAndBlock();
    if (ibus_bus_.get())
      ibus_bus_->ShutdownOnDBusThreadAndBlock();

    // Release IBusEngineService instances.
    for (std::map<dbus::ObjectPath, IBusEngineService*>::iterator it
            = ibus_engine_services_.begin();
         it != ibus_engine_services_.end(); it++) {
      delete it->second;
    }

    // Stop the D-Bus thread.
    dbus_thread_->Stop();
  }

  // DBusThreadManager override.
  virtual void InitIBusBus(const std::string &ibus_address) OVERRIDE {
    DCHECK(!ibus_bus_);
    dbus::Bus::Options ibus_bus_options;
    ibus_bus_options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
    ibus_bus_options.address = ibus_address;
    ibus_bus_options.connection_type = dbus::Bus::PRIVATE;
    ibus_bus_options.dbus_thread_message_loop_proxy =
        dbus_thread_->message_loop_proxy();
    ibus_bus_ = new dbus::Bus(ibus_bus_options);
    ibus_address_ = ibus_address;
    VLOG(1) << "Connected to ibus-daemon: " << ibus_address;

    DBusClientImplementationType client_type =
        base::chromeos::IsRunningOnChromeOS() ? REAL_DBUS_CLIENT_IMPLEMENTATION
                                              : STUB_DBUS_CLIENT_IMPLEMENTATION;

    // Create the ibus client.
    ibus_client_.reset(
        IBusClient::Create(client_type, ibus_bus_.get()));

    // Create the ibus input context client.
    ibus_input_context_client_.reset(
        IBusInputContextClient::Create(client_type));

    // Create the ibus engine factory service.
    ibus_engine_factory_service_.reset(
        IBusEngineFactoryService::Create(ibus_bus_.get(), client_type));

    ibus_engine_services_.clear();
  }

  // DBusThreadManager override.
  virtual dbus::Bus* GetSystemBus() OVERRIDE {
    return system_bus_.get();
  }

  // DBusThreadManager override.
  virtual dbus::Bus* GetIBusBus() OVERRIDE {
    return ibus_bus_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothAdapterClient* GetBluetoothAdapterClient() OVERRIDE {
    return bluetooth_adapter_client_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothDeviceClient* GetBluetoothDeviceClient() OVERRIDE {
    return bluetooth_device_client_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothInputClient* GetBluetoothInputClient() OVERRIDE {
    return bluetooth_input_client_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothManagerClient* GetBluetoothManagerClient() OVERRIDE {
    return bluetooth_manager_client_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothNodeClient* GetBluetoothNodeClient() OVERRIDE {
    return bluetooth_node_client_.get();
  }

  // DBusThreadManager override.
  virtual BluetoothOutOfBandClient* GetBluetoothOutOfBandClient() OVERRIDE {
    return bluetooth_out_of_band_client_.get();
  }

  // DBusThreadManager override.
  virtual CashewClient* GetCashewClient() OVERRIDE {
    return cashew_client_.get();
  }

  // DBusThreadManager override.
  virtual CrosDisksClient* GetCrosDisksClient() OVERRIDE {
    return cros_disks_client_.get();
  }

  // DBusThreadManager override.
  virtual CryptohomeClient* GetCryptohomeClient() OVERRIDE {
    return cryptohome_client_.get();
  }

  // DBusThreadManager override.
  virtual DebugDaemonClient* GetDebugDaemonClient() OVERRIDE {
    return debugdaemon_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamDeviceClient* GetFlimflamDeviceClient() OVERRIDE {
    return flimflam_device_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamIPConfigClient* GetFlimflamIPConfigClient() OVERRIDE {
    return flimflam_ipconfig_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamManagerClient* GetFlimflamManagerClient() OVERRIDE {
    return flimflam_manager_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamNetworkClient* GetFlimflamNetworkClient() OVERRIDE {
    return flimflam_network_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamProfileClient* GetFlimflamProfileClient() OVERRIDE {
    return flimflam_profile_client_.get();
  }

  // DBusThreadManager override.
  virtual FlimflamServiceClient* GetFlimflamServiceClient() OVERRIDE {
    return flimflam_service_client_.get();
  }

  // DBusThreadManager override.
  virtual GsmSMSClient* GetGsmSMSClient() OVERRIDE {
    return gsm_sms_client_.get();
  }

  // DBusThreadManager override.
  virtual ImageBurnerClient* GetImageBurnerClient() OVERRIDE {
    return image_burner_client_.get();
  }

  // DBusThreadManager override.
  virtual IntrospectableClient* GetIntrospectableClient() OVERRIDE {
    return introspectable_client_.get();
  }

  // DBusThreadManager override.
  virtual MediaTransferProtocolDaemonClient*
  GetMediaTransferProtocolDaemonClient() OVERRIDE {
    return media_transfer_protocol_daemon_client_.get();
  }

  // DBusThreadManager override.
  virtual ModemMessagingClient* GetModemMessagingClient() OVERRIDE {
    return modem_messaging_client_.get();
  }

  // DBusThreadManager override.
  virtual PermissionBrokerClient* GetPermissionBrokerClient() OVERRIDE {
    return permission_broker_client_.get();
  }

  // DBusThreadManager override.
  virtual PowerManagerClient* GetPowerManagerClient() OVERRIDE {
    return power_manager_client_.get();
  }

  // DBusThreadManager override.
  virtual SessionManagerClient* GetSessionManagerClient() OVERRIDE {
    return session_manager_client_.get();
  }

  // DBusThreadManager override.
  virtual SMSClient* GetSMSClient() OVERRIDE {
    return sms_client_.get();
  }

  // DBusThreadManager override.
  virtual SpeechSynthesizerClient* GetSpeechSynthesizerClient() OVERRIDE {
    return speech_synthesizer_client_.get();
  }

  // DBusThreadManager override.
  virtual UpdateEngineClient* GetUpdateEngineClient() OVERRIDE {
    return update_engine_client_.get();
  }

  // DBusThreadManager override.
  virtual IBusClient* GetIBusClient() OVERRIDE {
    return ibus_client_.get();
  }

  // DBusThreadManager override.
  virtual IBusInputContextClient* GetIBusInputContextClient() OVERRIDE {
    return ibus_input_context_client_.get();
  }

  // DBusThreadManager override.
  virtual IBusEngineFactoryService* GetIBusEngineFactoryService() OVERRIDE {
    return ibus_engine_factory_service_.get();
  }

  // DBusThreadManager override.
  virtual IBusEngineService* GetIBusEngineService(
      const dbus::ObjectPath& object_path) OVERRIDE {
    const DBusClientImplementationType client_type =
        base::chromeos::IsRunningOnChromeOS() ? REAL_DBUS_CLIENT_IMPLEMENTATION
                                              : STUB_DBUS_CLIENT_IMPLEMENTATION;

    if (ibus_engine_services_.find(object_path)
            == ibus_engine_services_.end()) {
      ibus_engine_services_[object_path] =
          IBusEngineService::Create(client_type, ibus_bus_.get(), object_path);
    }
    return ibus_engine_services_[object_path];
  }

  // DBusThreadManager override.
  virtual void RemoveIBusEngineService(
      const dbus::ObjectPath& object_path) OVERRIDE {
    if (ibus_engine_services_.find(object_path) !=
        ibus_engine_services_.end()) {
      LOG(WARNING) << "Object path not found: " << object_path.value();
      return;
    }
    delete ibus_engine_services_[object_path];
    ibus_engine_services_.erase(object_path);
  }

  scoped_ptr<base::Thread> dbus_thread_;
  scoped_refptr<dbus::Bus> system_bus_;
  scoped_refptr<dbus::Bus> ibus_bus_;
  scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_;
  scoped_ptr<BluetoothDeviceClient> bluetooth_device_client_;
  scoped_ptr<BluetoothInputClient> bluetooth_input_client_;
  scoped_ptr<BluetoothManagerClient> bluetooth_manager_client_;
  scoped_ptr<BluetoothNodeClient> bluetooth_node_client_;
  scoped_ptr<BluetoothOutOfBandClient> bluetooth_out_of_band_client_;
  scoped_ptr<CashewClient> cashew_client_;
  scoped_ptr<CrosDisksClient> cros_disks_client_;
  scoped_ptr<CryptohomeClient> cryptohome_client_;
  scoped_ptr<DebugDaemonClient> debugdaemon_client_;
  scoped_ptr<FlimflamDeviceClient> flimflam_device_client_;
  scoped_ptr<FlimflamIPConfigClient> flimflam_ipconfig_client_;
  scoped_ptr<FlimflamManagerClient> flimflam_manager_client_;
  scoped_ptr<FlimflamNetworkClient> flimflam_network_client_;
  scoped_ptr<FlimflamProfileClient> flimflam_profile_client_;
  scoped_ptr<FlimflamServiceClient> flimflam_service_client_;
  scoped_ptr<GsmSMSClient> gsm_sms_client_;
  scoped_ptr<ImageBurnerClient> image_burner_client_;
  scoped_ptr<IntrospectableClient> introspectable_client_;
  scoped_ptr<MediaTransferProtocolDaemonClient>
      media_transfer_protocol_daemon_client_;
  scoped_ptr<ModemMessagingClient> modem_messaging_client_;
  scoped_ptr<PermissionBrokerClient> permission_broker_client_;
  scoped_ptr<PowerManagerClient> power_manager_client_;
  scoped_ptr<SessionManagerClient> session_manager_client_;
  scoped_ptr<SMSClient> sms_client_;
  scoped_ptr<SpeechSynthesizerClient> speech_synthesizer_client_;
  scoped_ptr<UpdateEngineClient> update_engine_client_;
  scoped_ptr<IBusClient> ibus_client_;
  scoped_ptr<IBusInputContextClient> ibus_input_context_client_;
  scoped_ptr<IBusEngineFactoryService> ibus_engine_factory_service_;
  std::map<dbus::ObjectPath, IBusEngineService*> ibus_engine_services_;

  std::string ibus_address_;
};

// static
void DBusThreadManager::Initialize() {
  if (g_dbus_thread_manager) {
    LOG(WARNING) << "DBusThreadManager was already initialized";
    return;
  }
  // Determine whether we use stub or real client implementations.
  if (base::chromeos::IsRunningOnChromeOS()) {
    g_dbus_thread_manager =
        new DBusThreadManagerImpl(REAL_DBUS_CLIENT_IMPLEMENTATION);
    VLOG(1) << "DBusThreadManager initialized for ChromeOS";
  } else {
    g_dbus_thread_manager =
        new DBusThreadManagerImpl(STUB_DBUS_CLIENT_IMPLEMENTATION);
    VLOG(1) << "DBusThreadManager initialized with Stub";
  }
}

// static
void DBusThreadManager::InitializeForTesting(
    DBusThreadManager* dbus_thread_manager) {
  if (g_dbus_thread_manager) {
    LOG(WARNING) << "DBusThreadManager was already initialized";
    return;
  }
  CHECK(dbus_thread_manager);
  g_dbus_thread_manager = dbus_thread_manager;
  VLOG(1) << "DBusThreadManager initialized with test implementation";
}

// static
void DBusThreadManager::InitializeWithStub() {
  g_dbus_thread_manager =
        new DBusThreadManagerImpl(STUB_DBUS_CLIENT_IMPLEMENTATION);
    VLOG(1) << "DBusThreadManager initialized with stub implementation";
}

// static
void DBusThreadManager::Shutdown() {
  if (!g_dbus_thread_manager) {
    // TODO(satorux): Make it a DCHECK() once it's ready.
    LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager";
    return;
  }
  delete g_dbus_thread_manager;
  g_dbus_thread_manager = NULL;
  VLOG(1) << "DBusThreadManager Shutdown completed";
}

DBusThreadManager::DBusThreadManager() {
}

DBusThreadManager::~DBusThreadManager() {
}

// static
DBusThreadManager* DBusThreadManager::Get() {
  CHECK(g_dbus_thread_manager)
      << "DBusThreadManager::Get() called before Initialize()";
  return g_dbus_thread_manager;
}

}  // namespace chromeos