Home | History | Annotate | Download | only in dbus
      1 // Copyright (c) 2012 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_thread_manager.h"
      6 
      7 #include <map>
      8 
      9 #include "base/command_line.h"
     10 #include "base/observer_list.h"
     11 #include "base/sys_info.h"
     12 #include "base/threading/thread.h"
     13 #include "chromeos/chromeos_switches.h"
     14 #include "chromeos/dbus/bluetooth_adapter_client.h"
     15 #include "chromeos/dbus/bluetooth_agent_manager_client.h"
     16 #include "chromeos/dbus/bluetooth_device_client.h"
     17 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
     18 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h"
     19 #include "chromeos/dbus/bluetooth_gatt_manager_client.h"
     20 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
     21 #include "chromeos/dbus/bluetooth_input_client.h"
     22 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
     23 #include "chromeos/dbus/cras_audio_client.h"
     24 #include "chromeos/dbus/cros_disks_client.h"
     25 #include "chromeos/dbus/cryptohome_client.h"
     26 #include "chromeos/dbus/dbus_client.h"
     27 #include "chromeos/dbus/dbus_thread_manager_observer.h"
     28 #include "chromeos/dbus/debug_daemon_client.h"
     29 #include "chromeos/dbus/fake_dbus_thread_manager.h"
     30 #include "chromeos/dbus/gsm_sms_client.h"
     31 #include "chromeos/dbus/image_burner_client.h"
     32 #include "chromeos/dbus/introspectable_client.h"
     33 #include "chromeos/dbus/lorgnette_manager_client.h"
     34 #include "chromeos/dbus/modem_messaging_client.h"
     35 #include "chromeos/dbus/nfc_adapter_client.h"
     36 #include "chromeos/dbus/nfc_device_client.h"
     37 #include "chromeos/dbus/nfc_manager_client.h"
     38 #include "chromeos/dbus/nfc_record_client.h"
     39 #include "chromeos/dbus/nfc_tag_client.h"
     40 #include "chromeos/dbus/permission_broker_client.h"
     41 #include "chromeos/dbus/power_manager_client.h"
     42 #include "chromeos/dbus/power_policy_controller.h"
     43 #include "chromeos/dbus/session_manager_client.h"
     44 #include "chromeos/dbus/shill_device_client.h"
     45 #include "chromeos/dbus/shill_ipconfig_client.h"
     46 #include "chromeos/dbus/shill_manager_client.h"
     47 #include "chromeos/dbus/shill_profile_client.h"
     48 #include "chromeos/dbus/shill_service_client.h"
     49 #include "chromeos/dbus/sms_client.h"
     50 #include "chromeos/dbus/system_clock_client.h"
     51 #include "chromeos/dbus/update_engine_client.h"
     52 #include "dbus/bus.h"
     53 #include "dbus/dbus_statistics.h"
     54 
     55 namespace chromeos {
     56 
     57 static DBusThreadManager* g_dbus_thread_manager = NULL;
     58 static DBusThreadManager* g_dbus_thread_manager_for_testing = NULL;
     59 
     60 // The bundle of all D-Bus clients used in DBusThreadManagerImpl. The bundle
     61 // is used to delete them at once in the right order before shutting down the
     62 // system bus. See also the comment in the destructor of DBusThreadManagerImpl.
     63 class DBusClientBundle {
     64  public:
     65   DBusClientBundle() {
     66     const DBusClientImplementationType type = REAL_DBUS_CLIENT_IMPLEMENTATION;
     67 
     68     bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create());
     69     bluetooth_agent_manager_client_.reset(
     70         BluetoothAgentManagerClient::Create());
     71     bluetooth_device_client_.reset(BluetoothDeviceClient::Create());
     72     bluetooth_gatt_characteristic_client_.reset(
     73         BluetoothGattCharacteristicClient::Create());
     74     bluetooth_gatt_descriptor_client_.reset(
     75         BluetoothGattDescriptorClient::Create());
     76     bluetooth_gatt_manager_client_.reset(BluetoothGattManagerClient::Create());
     77     bluetooth_gatt_service_client_.reset(BluetoothGattServiceClient::Create());
     78     bluetooth_input_client_.reset(BluetoothInputClient::Create());
     79     bluetooth_profile_manager_client_.reset(
     80         BluetoothProfileManagerClient::Create());
     81     cras_audio_client_.reset(CrasAudioClient::Create());
     82     cros_disks_client_.reset(CrosDisksClient::Create(type));
     83     cryptohome_client_.reset(CryptohomeClient::Create());
     84     debug_daemon_client_.reset(DebugDaemonClient::Create());
     85     lorgnette_manager_client_.reset(LorgnetteManagerClient::Create());
     86     shill_manager_client_.reset(ShillManagerClient::Create());
     87     shill_device_client_.reset(ShillDeviceClient::Create());
     88     shill_ipconfig_client_.reset(ShillIPConfigClient::Create());
     89     shill_service_client_.reset(ShillServiceClient::Create());
     90     shill_profile_client_.reset(ShillProfileClient::Create());
     91     gsm_sms_client_.reset(GsmSMSClient::Create());
     92     image_burner_client_.reset(ImageBurnerClient::Create());
     93     introspectable_client_.reset(IntrospectableClient::Create());
     94     modem_messaging_client_.reset(ModemMessagingClient::Create());
     95     // Create the NFC clients in the correct order based on their dependencies.
     96     nfc_manager_client_.reset(NfcManagerClient::Create());
     97     nfc_adapter_client_.reset(
     98         NfcAdapterClient::Create(nfc_manager_client_.get()));
     99     nfc_device_client_.reset(
    100         NfcDeviceClient::Create(nfc_adapter_client_.get()));
    101     nfc_tag_client_.reset(NfcTagClient::Create(nfc_adapter_client_.get()));
    102     nfc_record_client_.reset(NfcRecordClient::Create(nfc_device_client_.get(),
    103                                                     nfc_tag_client_.get()));
    104     permission_broker_client_.reset(PermissionBrokerClient::Create());
    105     power_manager_client_.reset(PowerManagerClient::Create(type));
    106     session_manager_client_.reset(SessionManagerClient::Create(type));
    107     sms_client_.reset(SMSClient::Create());
    108     system_clock_client_.reset(SystemClockClient::Create());
    109     update_engine_client_.reset(UpdateEngineClient::Create(type));
    110   }
    111 
    112   BluetoothAdapterClient* bluetooth_adapter_client() {
    113     return bluetooth_adapter_client_.get();
    114   }
    115   BluetoothAgentManagerClient* bluetooth_agent_manager_client() {
    116     return bluetooth_agent_manager_client_.get();
    117   }
    118   BluetoothDeviceClient* bluetooth_device_client() {
    119     return bluetooth_device_client_.get();
    120   }
    121   BluetoothGattCharacteristicClient* bluetooth_gatt_characteristic_client() {
    122     return bluetooth_gatt_characteristic_client_.get();
    123   }
    124   BluetoothGattDescriptorClient* bluetooth_gatt_descriptor_client() {
    125     return bluetooth_gatt_descriptor_client_.get();
    126   }
    127   BluetoothGattManagerClient* bluetooth_gatt_manager_client() {
    128     return bluetooth_gatt_manager_client_.get();
    129   }
    130   BluetoothGattServiceClient* bluetooth_gatt_service_client() {
    131     return bluetooth_gatt_service_client_.get();
    132   }
    133   BluetoothInputClient* bluetooth_input_client() {
    134     return bluetooth_input_client_.get();
    135   }
    136   BluetoothProfileManagerClient* bluetooth_profile_manager_client() {
    137     return bluetooth_profile_manager_client_.get();
    138   }
    139   CrasAudioClient* cras_audio_client() {
    140     return cras_audio_client_.get();
    141   }
    142   CrosDisksClient* cros_disks_client() {
    143     return cros_disks_client_.get();
    144   }
    145   CryptohomeClient* cryptohome_client() {
    146     return cryptohome_client_.get();
    147   }
    148   DebugDaemonClient* debug_daemon_client() {
    149     return debug_daemon_client_.get();
    150   }
    151   LorgnetteManagerClient* lorgnette_manager_client() {
    152     return lorgnette_manager_client_.get();
    153   }
    154   ShillDeviceClient* shill_device_client() {
    155     return shill_device_client_.get();
    156   }
    157   ShillIPConfigClient* shill_ipconfig_client() {
    158     return shill_ipconfig_client_.get();
    159   }
    160   ShillManagerClient* shill_manager_client() {
    161     return shill_manager_client_.get();
    162   }
    163   ShillServiceClient* shill_service_client() {
    164     return shill_service_client_.get();
    165   }
    166   ShillProfileClient* shill_profile_client() {
    167     return shill_profile_client_.get();
    168   }
    169   GsmSMSClient* gsm_sms_client() {
    170     return gsm_sms_client_.get();
    171   }
    172   ImageBurnerClient* image_burner_client() {
    173     return image_burner_client_.get();
    174   }
    175   IntrospectableClient* introspectable_client() {
    176     return introspectable_client_.get();
    177   }
    178   ModemMessagingClient* modem_messaging_client() {
    179     return modem_messaging_client_.get();
    180   }
    181   NfcManagerClient* nfc_manager_client() {
    182     return nfc_manager_client_.get();
    183   }
    184   NfcAdapterClient* nfc_adapter_client() {
    185     return nfc_adapter_client_.get();
    186   }
    187   NfcDeviceClient* nfc_device_client() {
    188     return nfc_device_client_.get();
    189   }
    190   NfcTagClient* nfc_tag_client() {
    191     return nfc_tag_client_.get();
    192   }
    193   NfcRecordClient* nfc_record_client() {
    194     return nfc_record_client_.get();
    195   }
    196   PermissionBrokerClient* permission_broker_client() {
    197     return permission_broker_client_.get();
    198   }
    199   SystemClockClient* system_clock_client() {
    200     return system_clock_client_.get();
    201   }
    202   PowerManagerClient* power_manager_client() {
    203     return power_manager_client_.get();
    204   }
    205   SessionManagerClient* session_manager_client() {
    206     return session_manager_client_.get();
    207   }
    208   SMSClient* sms_client() {
    209     return sms_client_.get();
    210   }
    211   UpdateEngineClient* update_engine_client() {
    212     return update_engine_client_.get();
    213   }
    214 
    215  private:
    216   scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_;
    217   scoped_ptr<BluetoothAgentManagerClient> bluetooth_agent_manager_client_;
    218   scoped_ptr<BluetoothDeviceClient> bluetooth_device_client_;
    219   scoped_ptr<BluetoothGattCharacteristicClient>
    220       bluetooth_gatt_characteristic_client_;
    221   scoped_ptr<BluetoothGattDescriptorClient> bluetooth_gatt_descriptor_client_;
    222   scoped_ptr<BluetoothGattManagerClient> bluetooth_gatt_manager_client_;
    223   scoped_ptr<BluetoothGattServiceClient> bluetooth_gatt_service_client_;
    224   scoped_ptr<BluetoothInputClient> bluetooth_input_client_;
    225   scoped_ptr<BluetoothProfileManagerClient> bluetooth_profile_manager_client_;
    226   scoped_ptr<CrasAudioClient> cras_audio_client_;
    227   scoped_ptr<CrosDisksClient> cros_disks_client_;
    228   scoped_ptr<CryptohomeClient> cryptohome_client_;
    229   scoped_ptr<DebugDaemonClient> debug_daemon_client_;
    230   scoped_ptr<LorgnetteManagerClient> lorgnette_manager_client_;
    231   scoped_ptr<ShillDeviceClient> shill_device_client_;
    232   scoped_ptr<ShillIPConfigClient> shill_ipconfig_client_;
    233   scoped_ptr<ShillManagerClient> shill_manager_client_;
    234   scoped_ptr<ShillServiceClient> shill_service_client_;
    235   scoped_ptr<ShillProfileClient> shill_profile_client_;
    236   scoped_ptr<GsmSMSClient> gsm_sms_client_;
    237   scoped_ptr<ImageBurnerClient> image_burner_client_;
    238   scoped_ptr<IntrospectableClient> introspectable_client_;
    239   scoped_ptr<ModemMessagingClient> modem_messaging_client_;
    240   // The declaration order for NFC client objects is important. See
    241   // DBusThreadManager::CreateDefaultClients for the dependencies.
    242   scoped_ptr<NfcManagerClient> nfc_manager_client_;
    243   scoped_ptr<NfcAdapterClient> nfc_adapter_client_;
    244   scoped_ptr<NfcDeviceClient> nfc_device_client_;
    245   scoped_ptr<NfcTagClient> nfc_tag_client_;
    246   scoped_ptr<NfcRecordClient> nfc_record_client_;
    247   scoped_ptr<PermissionBrokerClient> permission_broker_client_;
    248   scoped_ptr<SystemClockClient> system_clock_client_;
    249   scoped_ptr<PowerManagerClient> power_manager_client_;
    250   scoped_ptr<SessionManagerClient> session_manager_client_;
    251   scoped_ptr<SMSClient> sms_client_;
    252   scoped_ptr<UpdateEngineClient> update_engine_client_;
    253 
    254   DISALLOW_COPY_AND_ASSIGN(DBusClientBundle);
    255 };
    256 
    257 // The DBusThreadManager implementation used in production.
    258 class DBusThreadManagerImpl : public DBusThreadManager {
    259  public:
    260   explicit DBusThreadManagerImpl() {
    261     // Create the D-Bus thread.
    262     base::Thread::Options thread_options;
    263     thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
    264     dbus_thread_.reset(new base::Thread("D-Bus thread"));
    265     dbus_thread_->StartWithOptions(thread_options);
    266 
    267     // Create the connection to the system bus.
    268     dbus::Bus::Options system_bus_options;
    269     system_bus_options.bus_type = dbus::Bus::SYSTEM;
    270     system_bus_options.connection_type = dbus::Bus::PRIVATE;
    271     system_bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
    272     system_bus_ = new dbus::Bus(system_bus_options);
    273 
    274     CreateDefaultClients();
    275   }
    276 
    277   virtual ~DBusThreadManagerImpl() {
    278     FOR_EACH_OBSERVER(DBusThreadManagerObserver, observers_,
    279                       OnDBusThreadManagerDestroying(this));
    280 
    281     // PowerPolicyController's destructor depends on PowerManagerClient.
    282     power_policy_controller_.reset();
    283 
    284     // Delete all D-Bus clients before shutting down the system bus.
    285     client_bundle_.reset();
    286 
    287     // Shut down the bus. During the browser shutdown, it's ok to shut down
    288     // the bus synchronously.
    289     system_bus_->ShutdownOnDBusThreadAndBlock();
    290 
    291     // Stop the D-Bus thread.
    292     dbus_thread_->Stop();
    293   }
    294 
    295   // DBusThreadManager overrides:
    296   virtual void AddObserver(DBusThreadManagerObserver* observer) OVERRIDE {
    297     DCHECK(observer);
    298     observers_.AddObserver(observer);
    299   }
    300 
    301   virtual void RemoveObserver(DBusThreadManagerObserver* observer) OVERRIDE {
    302     DCHECK(observer);
    303     observers_.RemoveObserver(observer);
    304   }
    305 
    306   virtual dbus::Bus* GetSystemBus() OVERRIDE {
    307     return system_bus_.get();
    308   }
    309 
    310   virtual BluetoothAdapterClient* GetBluetoothAdapterClient() OVERRIDE {
    311     return client_bundle_->bluetooth_adapter_client();
    312   }
    313 
    314   virtual BluetoothAgentManagerClient* GetBluetoothAgentManagerClient()
    315       OVERRIDE {
    316     return client_bundle_->bluetooth_agent_manager_client();
    317   }
    318 
    319   virtual BluetoothDeviceClient* GetBluetoothDeviceClient() OVERRIDE {
    320     return client_bundle_->bluetooth_device_client();
    321   }
    322 
    323   virtual BluetoothGattCharacteristicClient*
    324       GetBluetoothGattCharacteristicClient() OVERRIDE {
    325     return client_bundle_->bluetooth_gatt_characteristic_client();
    326   }
    327 
    328   virtual BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient()
    329       OVERRIDE {
    330     return client_bundle_->bluetooth_gatt_descriptor_client();
    331   }
    332 
    333   virtual BluetoothGattManagerClient* GetBluetoothGattManagerClient() OVERRIDE {
    334     return client_bundle_->bluetooth_gatt_manager_client();
    335   }
    336 
    337   virtual BluetoothGattServiceClient* GetBluetoothGattServiceClient() OVERRIDE {
    338     return client_bundle_->bluetooth_gatt_service_client();
    339   }
    340 
    341   virtual BluetoothInputClient* GetBluetoothInputClient() OVERRIDE {
    342     return client_bundle_->bluetooth_input_client();
    343   }
    344 
    345   virtual BluetoothProfileManagerClient* GetBluetoothProfileManagerClient()
    346       OVERRIDE {
    347     return client_bundle_->bluetooth_profile_manager_client();
    348   }
    349 
    350   virtual CrasAudioClient* GetCrasAudioClient() OVERRIDE {
    351     return client_bundle_->cras_audio_client();
    352   }
    353 
    354   virtual CrosDisksClient* GetCrosDisksClient() OVERRIDE {
    355     return client_bundle_->cros_disks_client();
    356   }
    357 
    358   virtual CryptohomeClient* GetCryptohomeClient() OVERRIDE {
    359     return client_bundle_->cryptohome_client();
    360   }
    361 
    362   virtual DebugDaemonClient* GetDebugDaemonClient() OVERRIDE {
    363     return client_bundle_->debug_daemon_client();
    364   }
    365 
    366   virtual LorgnetteManagerClient* GetLorgnetteManagerClient() OVERRIDE {
    367     return client_bundle_->lorgnette_manager_client();
    368   }
    369 
    370   virtual ShillDeviceClient* GetShillDeviceClient() OVERRIDE {
    371     return client_bundle_->shill_device_client();
    372   }
    373 
    374   virtual ShillIPConfigClient* GetShillIPConfigClient() OVERRIDE {
    375     return client_bundle_->shill_ipconfig_client();
    376   }
    377 
    378   virtual ShillManagerClient* GetShillManagerClient() OVERRIDE {
    379     return client_bundle_->shill_manager_client();
    380   }
    381 
    382   virtual ShillServiceClient* GetShillServiceClient() OVERRIDE {
    383     return client_bundle_->shill_service_client();
    384   }
    385 
    386   virtual ShillProfileClient* GetShillProfileClient() OVERRIDE {
    387     return client_bundle_->shill_profile_client();
    388   }
    389 
    390   virtual GsmSMSClient* GetGsmSMSClient() OVERRIDE {
    391     return client_bundle_->gsm_sms_client();
    392   }
    393 
    394   virtual ImageBurnerClient* GetImageBurnerClient() OVERRIDE {
    395     return client_bundle_->image_burner_client();
    396   }
    397 
    398   virtual IntrospectableClient* GetIntrospectableClient() OVERRIDE {
    399     return client_bundle_->introspectable_client();
    400   }
    401 
    402   virtual ModemMessagingClient* GetModemMessagingClient() OVERRIDE {
    403     return client_bundle_->modem_messaging_client();
    404   }
    405 
    406   virtual NfcAdapterClient* GetNfcAdapterClient() OVERRIDE {
    407     return client_bundle_->nfc_adapter_client();
    408   }
    409 
    410   virtual NfcDeviceClient* GetNfcDeviceClient() OVERRIDE {
    411     return client_bundle_->nfc_device_client();
    412   }
    413 
    414   virtual NfcManagerClient* GetNfcManagerClient() OVERRIDE {
    415     return client_bundle_->nfc_manager_client();
    416   }
    417 
    418   virtual NfcRecordClient* GetNfcRecordClient() OVERRIDE {
    419     return client_bundle_->nfc_record_client();
    420   }
    421 
    422   virtual NfcTagClient* GetNfcTagClient() OVERRIDE {
    423     return client_bundle_->nfc_tag_client();
    424   }
    425 
    426   virtual PermissionBrokerClient* GetPermissionBrokerClient() OVERRIDE {
    427     return client_bundle_->permission_broker_client();
    428   }
    429 
    430   virtual PowerManagerClient* GetPowerManagerClient() OVERRIDE {
    431     return client_bundle_->power_manager_client();
    432   }
    433 
    434   virtual SessionManagerClient* GetSessionManagerClient() OVERRIDE {
    435     return client_bundle_->session_manager_client();
    436   }
    437 
    438   virtual SMSClient* GetSMSClient() OVERRIDE {
    439     return client_bundle_->sms_client();
    440   }
    441 
    442   virtual SystemClockClient* GetSystemClockClient() OVERRIDE {
    443     return client_bundle_->system_clock_client();
    444   }
    445 
    446   virtual UpdateEngineClient* GetUpdateEngineClient() OVERRIDE {
    447     return client_bundle_->update_engine_client();
    448   }
    449 
    450   virtual PowerPolicyController* GetPowerPolicyController() OVERRIDE {
    451     return power_policy_controller_.get();
    452   }
    453 
    454  private:
    455   // Constructs all clients and stores them in the respective *_client_ member
    456   // variable.
    457   void CreateDefaultClients() {
    458     client_bundle_.reset(new DBusClientBundle);
    459     power_policy_controller_.reset(new PowerPolicyController);
    460   }
    461 
    462   // Note: Keep this before other members so they can call AddObserver() in
    463   // their c'tors.
    464   ObserverList<DBusThreadManagerObserver> observers_;
    465 
    466   scoped_ptr<base::Thread> dbus_thread_;
    467   scoped_refptr<dbus::Bus> system_bus_;
    468   scoped_ptr<DBusClientBundle> client_bundle_;
    469   scoped_ptr<PowerPolicyController> power_policy_controller_;
    470 };
    471 
    472 // static
    473 void DBusThreadManager::Initialize() {
    474   // If we initialize DBusThreadManager twice we may also be shutting it down
    475   // early; do not allow that.
    476   CHECK(g_dbus_thread_manager == NULL);
    477 
    478   if (g_dbus_thread_manager_for_testing) {
    479     g_dbus_thread_manager = g_dbus_thread_manager_for_testing;
    480     InitializeClients();
    481     VLOG(1) << "DBusThreadManager initialized with test implementation";
    482     return;
    483   }
    484 
    485   // Determine whether we use stub or real client implementations.
    486   if (!base::SysInfo::IsRunningOnChromeOS() ||
    487       CommandLine::ForCurrentProcess()->HasSwitch(
    488           chromeos::switches::kDbusStub)) {
    489     InitializeWithStub();
    490   } else {
    491     g_dbus_thread_manager = new DBusThreadManagerImpl;
    492     InitializeClients();
    493     VLOG(1) << "DBusThreadManager initialized for Chrome OS";
    494   }
    495 }
    496 
    497 // static
    498 void DBusThreadManager::SetInstanceForTesting(
    499     DBusThreadManager* dbus_thread_manager) {
    500   CHECK(!g_dbus_thread_manager);
    501   CHECK(!g_dbus_thread_manager_for_testing);
    502   g_dbus_thread_manager_for_testing = dbus_thread_manager;
    503 }
    504 
    505 // static
    506 void DBusThreadManager::InitializeForTesting(
    507     DBusThreadManager* dbus_thread_manager) {
    508   SetInstanceForTesting(dbus_thread_manager);
    509   Initialize();
    510 }
    511 
    512 // static
    513 void DBusThreadManager::InitializeWithStub() {
    514   // If we initialize DBusThreadManager twice we may also be shutting it down
    515   // early; do not allow that.
    516   CHECK(g_dbus_thread_manager == NULL);
    517   FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
    518   fake_dbus_thread_manager->SetFakeClients();
    519   g_dbus_thread_manager = fake_dbus_thread_manager;
    520   InitializeClients();
    521   fake_dbus_thread_manager->SetupDefaultEnvironment();
    522   VLOG(1) << "DBusThreadManager initialized with stub implementation";
    523 }
    524 
    525 // static
    526 bool DBusThreadManager::IsInitialized() {
    527   return g_dbus_thread_manager != NULL;
    528 }
    529 
    530 // static
    531 void DBusThreadManager::Shutdown() {
    532   // If we called InitializeForTesting, this may get called more than once.
    533   // Ensure that we only shutdown DBusThreadManager once.
    534   CHECK(g_dbus_thread_manager || g_dbus_thread_manager_for_testing);
    535   DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager;
    536   g_dbus_thread_manager = NULL;
    537   g_dbus_thread_manager_for_testing = NULL;
    538   delete dbus_thread_manager;
    539   VLOG(1) << "DBusThreadManager Shutdown completed";
    540 }
    541 
    542 DBusThreadManager::DBusThreadManager() {
    543   dbus::statistics::Initialize();
    544 }
    545 
    546 DBusThreadManager::~DBusThreadManager() {
    547   dbus::statistics::Shutdown();
    548   if (g_dbus_thread_manager == NULL)
    549     return;  // Called form Shutdown() or local test instance.
    550   // There should never be both a global instance and a local instance.
    551   CHECK(this == g_dbus_thread_manager);
    552   if (g_dbus_thread_manager_for_testing) {
    553     g_dbus_thread_manager = NULL;
    554     g_dbus_thread_manager_for_testing = NULL;
    555     VLOG(1) << "DBusThreadManager destroyed";
    556   } else {
    557     LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()";
    558   }
    559 }
    560 
    561 // static
    562 DBusThreadManager* DBusThreadManager::Get() {
    563   CHECK(g_dbus_thread_manager)
    564       << "DBusThreadManager::Get() called before Initialize()";
    565   return g_dbus_thread_manager;
    566 }
    567 
    568 // static
    569 void DBusThreadManager::InitializeClients() {
    570   InitClient(g_dbus_thread_manager->GetBluetoothAdapterClient());
    571   InitClient(g_dbus_thread_manager->GetBluetoothAgentManagerClient());
    572   InitClient(g_dbus_thread_manager->GetBluetoothDeviceClient());
    573   InitClient(g_dbus_thread_manager->GetBluetoothGattCharacteristicClient());
    574   InitClient(g_dbus_thread_manager->GetBluetoothGattDescriptorClient());
    575   InitClient(g_dbus_thread_manager->GetBluetoothGattManagerClient());
    576   InitClient(g_dbus_thread_manager->GetBluetoothGattServiceClient());
    577   InitClient(g_dbus_thread_manager->GetBluetoothInputClient());
    578   InitClient(g_dbus_thread_manager->GetBluetoothProfileManagerClient());
    579   InitClient(g_dbus_thread_manager->GetCrasAudioClient());
    580   InitClient(g_dbus_thread_manager->GetCrosDisksClient());
    581   InitClient(g_dbus_thread_manager->GetCryptohomeClient());
    582   InitClient(g_dbus_thread_manager->GetDebugDaemonClient());
    583   InitClient(g_dbus_thread_manager->GetGsmSMSClient());
    584   InitClient(g_dbus_thread_manager->GetImageBurnerClient());
    585   InitClient(g_dbus_thread_manager->GetIntrospectableClient());
    586   InitClient(g_dbus_thread_manager->GetLorgnetteManagerClient());
    587   InitClient(g_dbus_thread_manager->GetModemMessagingClient());
    588   InitClient(g_dbus_thread_manager->GetPermissionBrokerClient());
    589   InitClient(g_dbus_thread_manager->GetPowerManagerClient());
    590   InitClient(g_dbus_thread_manager->GetSessionManagerClient());
    591   InitClient(g_dbus_thread_manager->GetShillDeviceClient());
    592   InitClient(g_dbus_thread_manager->GetShillIPConfigClient());
    593   InitClient(g_dbus_thread_manager->GetShillManagerClient());
    594   InitClient(g_dbus_thread_manager->GetShillServiceClient());
    595   InitClient(g_dbus_thread_manager->GetShillProfileClient());
    596   InitClient(g_dbus_thread_manager->GetSMSClient());
    597   InitClient(g_dbus_thread_manager->GetSystemClockClient());
    598   InitClient(g_dbus_thread_manager->GetUpdateEngineClient());
    599 
    600   // Initialize the NFC clients in the correct order. The order of
    601   // initialization matters due to dependencies that exist between the
    602   // client objects.
    603   InitClient(g_dbus_thread_manager->GetNfcManagerClient());
    604   InitClient(g_dbus_thread_manager->GetNfcAdapterClient());
    605   InitClient(g_dbus_thread_manager->GetNfcDeviceClient());
    606   InitClient(g_dbus_thread_manager->GetNfcTagClient());
    607   InitClient(g_dbus_thread_manager->GetNfcRecordClient());
    608 
    609   // PowerPolicyController is dependent on PowerManagerClient, so
    610   // initialize it after the main list of clients.
    611   if (g_dbus_thread_manager->GetPowerPolicyController()) {
    612     g_dbus_thread_manager->GetPowerPolicyController()->Init(
    613         g_dbus_thread_manager);
    614   }
    615 
    616   // This must be called after the list of clients so they've each had a
    617   // chance to register with their object g_dbus_thread_managers.
    618   if (g_dbus_thread_manager->GetSystemBus())
    619     g_dbus_thread_manager->GetSystemBus()->GetManagedObjects();
    620 }
    621 
    622 // static
    623 void DBusThreadManager::InitClient(DBusClient* client) {
    624   if (client)
    625     client->Init(g_dbus_thread_manager->GetSystemBus());
    626 }
    627 
    628 }  // namespace chromeos
    629