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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "chromeos/dbus/shill_stub_helper.h" | 44 #include "chromeos/dbus/shill_stub_helper.h" |
45 #include "chromeos/dbus/sms_client.h" | 45 #include "chromeos/dbus/sms_client.h" |
46 #include "chromeos/dbus/system_clock_client.h" | 46 #include "chromeos/dbus/system_clock_client.h" |
47 #include "chromeos/dbus/update_engine_client.h" | 47 #include "chromeos/dbus/update_engine_client.h" |
48 #include "dbus/bus.h" | 48 #include "dbus/bus.h" |
49 #include "dbus/dbus_statistics.h" | 49 #include "dbus/dbus_statistics.h" |
50 | 50 |
51 namespace chromeos { | 51 namespace chromeos { |
52 | 52 |
53 static DBusThreadManager* g_dbus_thread_manager = NULL; | 53 static DBusThreadManager* g_dbus_thread_manager = NULL; |
54 static bool g_dbus_thread_manager_set_for_testing = false; | 54 static DBusThreadManager* g_dbus_thread_manager_for_testing = NULL; |
55 | 55 |
56 // The DBusThreadManager implementation used in production. | 56 // The DBusThreadManager implementation used in production. |
57 class DBusThreadManagerImpl : public DBusThreadManager { | 57 class DBusThreadManagerImpl : public DBusThreadManager { |
58 public: | 58 public: |
59 explicit DBusThreadManagerImpl() { | 59 explicit DBusThreadManagerImpl() { |
60 // Create the D-Bus thread. | 60 // Create the D-Bus thread. |
61 base::Thread::Options thread_options; | 61 base::Thread::Options thread_options; |
62 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 62 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
63 dbus_thread_.reset(new base::Thread("D-Bus thread")); | 63 dbus_thread_.reset(new base::Thread("D-Bus thread")); |
64 dbus_thread_->StartWithOptions(thread_options); | 64 dbus_thread_->StartWithOptions(thread_options); |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 scoped_ptr<PowerManagerClient> power_manager_client_; | 312 scoped_ptr<PowerManagerClient> power_manager_client_; |
313 scoped_ptr<SessionManagerClient> session_manager_client_; | 313 scoped_ptr<SessionManagerClient> session_manager_client_; |
314 scoped_ptr<SMSClient> sms_client_; | 314 scoped_ptr<SMSClient> sms_client_; |
315 scoped_ptr<UpdateEngineClient> update_engine_client_; | 315 scoped_ptr<UpdateEngineClient> update_engine_client_; |
316 | 316 |
317 scoped_ptr<PowerPolicyController> power_policy_controller_; | 317 scoped_ptr<PowerPolicyController> power_policy_controller_; |
318 }; | 318 }; |
319 | 319 |
320 // static | 320 // static |
321 void DBusThreadManager::Initialize() { | 321 void DBusThreadManager::Initialize() { |
322 // Ignore Initialize() if we set a test DBusThreadManager. | |
323 if (g_dbus_thread_manager_set_for_testing) | |
324 return; | |
325 // If we initialize DBusThreadManager twice we may also be shutting it down | 322 // If we initialize DBusThreadManager twice we may also be shutting it down |
326 // early; do not allow that. | 323 // early; do not allow that. |
327 CHECK(g_dbus_thread_manager == NULL); | 324 CHECK(g_dbus_thread_manager == NULL); |
328 | 325 |
| 326 if (g_dbus_thread_manager_for_testing) { |
| 327 g_dbus_thread_manager = g_dbus_thread_manager_for_testing; |
| 328 InitializeClients(); |
| 329 VLOG(1) << "DBusThreadManager initialized with test implementation"; |
| 330 return; |
| 331 } |
329 // Determine whether we use stub or real client implementations. | 332 // Determine whether we use stub or real client implementations. |
330 if (base::SysInfo::IsRunningOnChromeOS()) { | 333 if (base::SysInfo::IsRunningOnChromeOS()) { |
331 g_dbus_thread_manager = new DBusThreadManagerImpl; | 334 g_dbus_thread_manager = new DBusThreadManagerImpl; |
332 InitializeClients(); | 335 InitializeClients(); |
333 VLOG(1) << "DBusThreadManager initialized for ChromeOS"; | 336 VLOG(1) << "DBusThreadManager initialized for ChromeOS"; |
334 } else { | 337 } else { |
335 InitializeWithStub(); | 338 InitializeWithStub(); |
336 return; | |
337 } | 339 } |
338 } | 340 } |
339 | 341 |
340 // static | 342 // static |
| 343 void DBusThreadManager::SetInstanceForTesting( |
| 344 DBusThreadManager* dbus_thread_manager) { |
| 345 CHECK(!g_dbus_thread_manager); |
| 346 CHECK(!g_dbus_thread_manager_for_testing); |
| 347 g_dbus_thread_manager_for_testing = dbus_thread_manager; |
| 348 } |
| 349 |
| 350 // static |
341 void DBusThreadManager::InitializeForTesting( | 351 void DBusThreadManager::InitializeForTesting( |
342 DBusThreadManager* dbus_thread_manager) { | 352 DBusThreadManager* dbus_thread_manager) { |
343 // If we initialize DBusThreadManager twice we may also be shutting it down | 353 SetInstanceForTesting(dbus_thread_manager); |
344 // early; do not allow that. | 354 Initialize(); |
345 CHECK(g_dbus_thread_manager == NULL); | |
346 CHECK(dbus_thread_manager); | |
347 g_dbus_thread_manager = dbus_thread_manager; | |
348 g_dbus_thread_manager_set_for_testing = true; | |
349 InitializeClients(); | |
350 VLOG(1) << "DBusThreadManager initialized with test implementation"; | |
351 } | 355 } |
352 | 356 |
353 // static | 357 // static |
354 void DBusThreadManager::InitializeWithStub() { | 358 void DBusThreadManager::InitializeWithStub() { |
355 // If we initialize DBusThreadManager twice we may also be shutting it down | 359 // If we initialize DBusThreadManager twice we may also be shutting it down |
356 // early; do not allow that. | 360 // early; do not allow that. |
357 CHECK(g_dbus_thread_manager == NULL); | 361 CHECK(g_dbus_thread_manager == NULL); |
358 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; | 362 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; |
359 fake_dbus_thread_manager->SetFakeClients(); | 363 fake_dbus_thread_manager->SetFakeClients(); |
360 g_dbus_thread_manager = fake_dbus_thread_manager; | 364 g_dbus_thread_manager = fake_dbus_thread_manager; |
361 InitializeClients(); | 365 InitializeClients(); |
362 shill_stub_helper::SetupDefaultEnvironment(); | 366 shill_stub_helper::SetupDefaultEnvironment(); |
363 VLOG(1) << "DBusThreadManager initialized with stub implementation"; | 367 VLOG(1) << "DBusThreadManager initialized with stub implementation"; |
364 } | 368 } |
365 | 369 |
366 // static | 370 // static |
367 bool DBusThreadManager::IsInitialized() { | 371 bool DBusThreadManager::IsInitialized() { |
368 return g_dbus_thread_manager != NULL; | 372 return g_dbus_thread_manager != NULL; |
369 } | 373 } |
370 | 374 |
371 // static | 375 // static |
372 void DBusThreadManager::Shutdown() { | 376 void DBusThreadManager::Shutdown() { |
373 // If we called InitializeForTesting, this may get called more than once. | 377 // If we called InitializeForTesting, this may get called more than once. |
374 // Ensure that we only shutdown DBusThreadManager once. | 378 // Ensure that we only shutdown DBusThreadManager once. |
375 CHECK(g_dbus_thread_manager || g_dbus_thread_manager_set_for_testing); | 379 CHECK(g_dbus_thread_manager || g_dbus_thread_manager_for_testing); |
376 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; | 380 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; |
377 g_dbus_thread_manager = NULL; | 381 g_dbus_thread_manager = NULL; |
| 382 g_dbus_thread_manager_for_testing = NULL; |
378 delete dbus_thread_manager; | 383 delete dbus_thread_manager; |
379 VLOG(1) << "DBusThreadManager Shutdown completed"; | 384 VLOG(1) << "DBusThreadManager Shutdown completed"; |
380 } | 385 } |
381 | 386 |
382 DBusThreadManager::DBusThreadManager() { | 387 DBusThreadManager::DBusThreadManager() { |
383 dbus::statistics::Initialize(); | 388 dbus::statistics::Initialize(); |
384 } | 389 } |
385 | 390 |
386 DBusThreadManager::~DBusThreadManager() { | 391 DBusThreadManager::~DBusThreadManager() { |
387 dbus::statistics::Shutdown(); | 392 dbus::statistics::Shutdown(); |
388 if (g_dbus_thread_manager == NULL) | 393 if (g_dbus_thread_manager == NULL) |
389 return; // Called form Shutdown() or local test instance. | 394 return; // Called form Shutdown() or local test instance. |
390 // There should never be both a global instance and a local instance. | 395 // There should never be both a global instance and a local instance. |
391 CHECK(this == g_dbus_thread_manager); | 396 CHECK(this == g_dbus_thread_manager); |
392 if (g_dbus_thread_manager_set_for_testing) { | 397 if (g_dbus_thread_manager_for_testing) { |
393 g_dbus_thread_manager = NULL; | 398 g_dbus_thread_manager = NULL; |
394 g_dbus_thread_manager_set_for_testing = false; | 399 g_dbus_thread_manager_for_testing = NULL; |
395 VLOG(1) << "DBusThreadManager destroyed"; | 400 VLOG(1) << "DBusThreadManager destroyed"; |
396 } else { | 401 } else { |
397 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; | 402 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; |
398 } | 403 } |
399 } | 404 } |
400 | 405 |
401 // static | 406 // static |
402 DBusThreadManager* DBusThreadManager::Get() { | 407 DBusThreadManager* DBusThreadManager::Get() { |
403 CHECK(g_dbus_thread_manager) | 408 CHECK(g_dbus_thread_manager) |
404 << "DBusThreadManager::Get() called before Initialize()"; | 409 << "DBusThreadManager::Get() called before Initialize()"; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 g_dbus_thread_manager->GetSystemBus()->GetManagedObjects(); | 455 g_dbus_thread_manager->GetSystemBus()->GetManagedObjects(); |
451 } | 456 } |
452 | 457 |
453 // static | 458 // static |
454 void DBusThreadManager::InitClient(DBusClient* client) { | 459 void DBusThreadManager::InitClient(DBusClient* client) { |
455 if (client) | 460 if (client) |
456 client->Init(g_dbus_thread_manager->GetSystemBus()); | 461 client->Init(g_dbus_thread_manager->GetSystemBus()); |
457 } | 462 } |
458 | 463 |
459 } // namespace chromeos | 464 } // namespace chromeos |
OLD | NEW |