| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/dbus_thread_manager.h" | 5 #include "chromeos/dbus/dbus_thread_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "chromeos/dbus/shill_stub_helper.h" | 45 #include "chromeos/dbus/shill_stub_helper.h" |
| 46 #include "chromeos/dbus/sms_client.h" | 46 #include "chromeos/dbus/sms_client.h" |
| 47 #include "chromeos/dbus/system_clock_client.h" | 47 #include "chromeos/dbus/system_clock_client.h" |
| 48 #include "chromeos/dbus/update_engine_client.h" | 48 #include "chromeos/dbus/update_engine_client.h" |
| 49 #include "dbus/bus.h" | 49 #include "dbus/bus.h" |
| 50 #include "dbus/dbus_statistics.h" | 50 #include "dbus/dbus_statistics.h" |
| 51 | 51 |
| 52 namespace chromeos { | 52 namespace chromeos { |
| 53 | 53 |
| 54 static DBusThreadManager* g_dbus_thread_manager = NULL; | 54 static DBusThreadManager* g_dbus_thread_manager = NULL; |
| 55 static bool g_dbus_thread_manager_set_for_testing = false; | 55 static DBusThreadManager* g_dbus_thread_manager_for_testing = NULL; |
| 56 | 56 |
| 57 // The DBusThreadManager implementation used in production. | 57 // The DBusThreadManager implementation used in production. |
| 58 class DBusThreadManagerImpl : public DBusThreadManager { | 58 class DBusThreadManagerImpl : public DBusThreadManager { |
| 59 public: | 59 public: |
| 60 explicit DBusThreadManagerImpl() { | 60 explicit DBusThreadManagerImpl() { |
| 61 // Create the D-Bus thread. | 61 // Create the D-Bus thread. |
| 62 base::Thread::Options thread_options; | 62 base::Thread::Options thread_options; |
| 63 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 63 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 64 dbus_thread_.reset(new base::Thread("D-Bus thread")); | 64 dbus_thread_.reset(new base::Thread("D-Bus thread")); |
| 65 dbus_thread_->StartWithOptions(thread_options); | 65 dbus_thread_->StartWithOptions(thread_options); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 scoped_ptr<PowerManagerClient> power_manager_client_; | 323 scoped_ptr<PowerManagerClient> power_manager_client_; |
| 324 scoped_ptr<SessionManagerClient> session_manager_client_; | 324 scoped_ptr<SessionManagerClient> session_manager_client_; |
| 325 scoped_ptr<SMSClient> sms_client_; | 325 scoped_ptr<SMSClient> sms_client_; |
| 326 scoped_ptr<UpdateEngineClient> update_engine_client_; | 326 scoped_ptr<UpdateEngineClient> update_engine_client_; |
| 327 | 327 |
| 328 scoped_ptr<PowerPolicyController> power_policy_controller_; | 328 scoped_ptr<PowerPolicyController> power_policy_controller_; |
| 329 }; | 329 }; |
| 330 | 330 |
| 331 // static | 331 // static |
| 332 void DBusThreadManager::Initialize() { | 332 void DBusThreadManager::Initialize() { |
| 333 // Ignore Initialize() if we set a test DBusThreadManager. | |
| 334 if (g_dbus_thread_manager_set_for_testing) | |
| 335 return; | |
| 336 // If we initialize DBusThreadManager twice we may also be shutting it down | 333 // If we initialize DBusThreadManager twice we may also be shutting it down |
| 337 // early; do not allow that. | 334 // early; do not allow that. |
| 338 CHECK(g_dbus_thread_manager == NULL); | 335 CHECK(g_dbus_thread_manager == NULL); |
| 339 | 336 |
| 337 if (g_dbus_thread_manager_for_testing) { |
| 338 g_dbus_thread_manager = g_dbus_thread_manager_for_testing; |
| 339 InitializeClients(); |
| 340 VLOG(1) << "DBusThreadManager initialized with test implementation"; |
| 341 return; |
| 342 } |
| 340 // Determine whether we use stub or real client implementations. | 343 // Determine whether we use stub or real client implementations. |
| 341 if (base::SysInfo::IsRunningOnChromeOS()) { | 344 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 342 g_dbus_thread_manager = new DBusThreadManagerImpl; | 345 g_dbus_thread_manager = new DBusThreadManagerImpl; |
| 343 InitializeClients(); | 346 InitializeClients(); |
| 344 VLOG(1) << "DBusThreadManager initialized for ChromeOS"; | 347 VLOG(1) << "DBusThreadManager initialized for ChromeOS"; |
| 345 } else { | 348 } else { |
| 346 InitializeWithStub(); | 349 InitializeWithStub(); |
| 347 return; | |
| 348 } | 350 } |
| 349 } | 351 } |
| 350 | 352 |
| 351 // static | 353 // static |
| 354 void DBusThreadManager::SetInstanceForTesting( |
| 355 DBusThreadManager* dbus_thread_manager) { |
| 356 CHECK(!g_dbus_thread_manager); |
| 357 CHECK(!g_dbus_thread_manager_for_testing); |
| 358 g_dbus_thread_manager_for_testing = dbus_thread_manager; |
| 359 } |
| 360 |
| 361 // static |
| 352 void DBusThreadManager::InitializeForTesting( | 362 void DBusThreadManager::InitializeForTesting( |
| 353 DBusThreadManager* dbus_thread_manager) { | 363 DBusThreadManager* dbus_thread_manager) { |
| 354 // If we initialize DBusThreadManager twice we may also be shutting it down | 364 SetInstanceForTesting(dbus_thread_manager); |
| 355 // early; do not allow that. | 365 Initialize(); |
| 356 CHECK(g_dbus_thread_manager == NULL); | |
| 357 CHECK(dbus_thread_manager); | |
| 358 g_dbus_thread_manager = dbus_thread_manager; | |
| 359 g_dbus_thread_manager_set_for_testing = true; | |
| 360 InitializeClients(); | |
| 361 VLOG(1) << "DBusThreadManager initialized with test implementation"; | |
| 362 } | 366 } |
| 363 | 367 |
| 364 // static | 368 // static |
| 365 void DBusThreadManager::InitializeWithStub() { | 369 void DBusThreadManager::InitializeWithStub() { |
| 366 // If we initialize DBusThreadManager twice we may also be shutting it down | 370 // If we initialize DBusThreadManager twice we may also be shutting it down |
| 367 // early; do not allow that. | 371 // early; do not allow that. |
| 368 CHECK(g_dbus_thread_manager == NULL); | 372 CHECK(g_dbus_thread_manager == NULL); |
| 369 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; | 373 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; |
| 370 fake_dbus_thread_manager->SetFakeClients(); | 374 fake_dbus_thread_manager->SetFakeClients(); |
| 371 g_dbus_thread_manager = fake_dbus_thread_manager; | 375 g_dbus_thread_manager = fake_dbus_thread_manager; |
| 372 InitializeClients(); | 376 InitializeClients(); |
| 373 shill_stub_helper::SetupDefaultEnvironment(); | 377 shill_stub_helper::SetupDefaultEnvironment(); |
| 374 VLOG(1) << "DBusThreadManager initialized with stub implementation"; | 378 VLOG(1) << "DBusThreadManager initialized with stub implementation"; |
| 375 } | 379 } |
| 376 | 380 |
| 377 // static | 381 // static |
| 378 bool DBusThreadManager::IsInitialized() { | 382 bool DBusThreadManager::IsInitialized() { |
| 379 return g_dbus_thread_manager != NULL; | 383 return g_dbus_thread_manager != NULL; |
| 380 } | 384 } |
| 381 | 385 |
| 382 // static | 386 // static |
| 383 void DBusThreadManager::Shutdown() { | 387 void DBusThreadManager::Shutdown() { |
| 384 // If we called InitializeForTesting, this may get called more than once. | 388 // If we called InitializeForTesting, this may get called more than once. |
| 385 // Ensure that we only shutdown DBusThreadManager once. | 389 // Ensure that we only shutdown DBusThreadManager once. |
| 386 CHECK(g_dbus_thread_manager || g_dbus_thread_manager_set_for_testing); | 390 CHECK(g_dbus_thread_manager || g_dbus_thread_manager_for_testing); |
| 387 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; | 391 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; |
| 388 g_dbus_thread_manager = NULL; | 392 g_dbus_thread_manager = NULL; |
| 393 g_dbus_thread_manager_for_testing = NULL; |
| 389 delete dbus_thread_manager; | 394 delete dbus_thread_manager; |
| 390 VLOG(1) << "DBusThreadManager Shutdown completed"; | 395 VLOG(1) << "DBusThreadManager Shutdown completed"; |
| 391 } | 396 } |
| 392 | 397 |
| 393 DBusThreadManager::DBusThreadManager() { | 398 DBusThreadManager::DBusThreadManager() { |
| 394 dbus::statistics::Initialize(); | 399 dbus::statistics::Initialize(); |
| 395 } | 400 } |
| 396 | 401 |
| 397 DBusThreadManager::~DBusThreadManager() { | 402 DBusThreadManager::~DBusThreadManager() { |
| 398 dbus::statistics::Shutdown(); | 403 dbus::statistics::Shutdown(); |
| 399 if (g_dbus_thread_manager == NULL) | 404 if (g_dbus_thread_manager == NULL) |
| 400 return; // Called form Shutdown() or local test instance. | 405 return; // Called form Shutdown() or local test instance. |
| 401 // There should never be both a global instance and a local instance. | 406 // There should never be both a global instance and a local instance. |
| 402 CHECK(this == g_dbus_thread_manager); | 407 CHECK(this == g_dbus_thread_manager); |
| 403 if (g_dbus_thread_manager_set_for_testing) { | 408 if (g_dbus_thread_manager_for_testing) { |
| 404 g_dbus_thread_manager = NULL; | 409 g_dbus_thread_manager = NULL; |
| 405 g_dbus_thread_manager_set_for_testing = false; | 410 g_dbus_thread_manager_for_testing = NULL; |
| 406 VLOG(1) << "DBusThreadManager destroyed"; | 411 VLOG(1) << "DBusThreadManager destroyed"; |
| 407 } else { | 412 } else { |
| 408 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; | 413 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; |
| 409 } | 414 } |
| 410 } | 415 } |
| 411 | 416 |
| 412 // static | 417 // static |
| 413 DBusThreadManager* DBusThreadManager::Get() { | 418 DBusThreadManager* DBusThreadManager::Get() { |
| 414 CHECK(g_dbus_thread_manager) | 419 CHECK(g_dbus_thread_manager) |
| 415 << "DBusThreadManager::Get() called before Initialize()"; | 420 << "DBusThreadManager::Get() called before Initialize()"; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 g_dbus_thread_manager->GetSystemBus()->GetManagedObjects(); | 466 g_dbus_thread_manager->GetSystemBus()->GetManagedObjects(); |
| 462 } | 467 } |
| 463 | 468 |
| 464 // static | 469 // static |
| 465 void DBusThreadManager::InitClient(DBusClient* client) { | 470 void DBusThreadManager::InitClient(DBusClient* client) { |
| 466 if (client) | 471 if (client) |
| 467 client->Init(g_dbus_thread_manager->GetSystemBus()); | 472 client->Init(g_dbus_thread_manager->GetSystemBus()); |
| 468 } | 473 } |
| 469 | 474 |
| 470 } // namespace chromeos | 475 } // namespace chromeos |
| OLD | NEW |