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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "chromeos/chromeos_switches.h" | 10 #include "chromeos/chromeos_switches.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_using_dbus_thread_manager_for_testing = false; | 55 static bool g_using_dbus_thread_manager_for_testing = false; |
56 | 56 |
57 DBusClientBundle::DBusClientTypeMask | 57 DBusThreadManager::DBusThreadManager(scoped_ptr<DBusClientBundle> client_bundle) |
58 DBusThreadManager::unstub_client_mask_ = DBusClientBundle::NO_CLIENTS; | 58 : client_bundle_(client_bundle.Pass()) { |
| 59 dbus::statistics::Initialize(); |
59 | 60 |
60 DBusThreadManager::DBusThreadManager() { | 61 if (client_bundle_->IsUsingAnyRegularClient()) { |
61 dbus::statistics::Initialize(); | 62 // At least one real DBusClient is used. |
62 if (!DBusThreadManager::IsUsingStub(DBusClientBundle::ALL_CLIENTS)) { | |
63 // Create the D-Bus thread. | 63 // Create the D-Bus thread. |
64 base::Thread::Options thread_options; | 64 base::Thread::Options thread_options; |
65 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 65 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
66 dbus_thread_.reset(new base::Thread("D-Bus thread")); | 66 dbus_thread_.reset(new base::Thread("D-Bus thread")); |
67 dbus_thread_->StartWithOptions(thread_options); | 67 dbus_thread_->StartWithOptions(thread_options); |
68 | 68 |
69 // Create the connection to the system bus. | 69 // Create the connection to the system bus. |
70 dbus::Bus::Options system_bus_options; | 70 dbus::Bus::Options system_bus_options; |
71 system_bus_options.bus_type = dbus::Bus::SYSTEM; | 71 system_bus_options.bus_type = dbus::Bus::SYSTEM; |
72 system_bus_options.connection_type = dbus::Bus::PRIVATE; | 72 system_bus_options.connection_type = dbus::Bus::PRIVATE; |
73 system_bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 73 system_bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
74 system_bus_ = new dbus::Bus(system_bus_options); | 74 system_bus_ = new dbus::Bus(system_bus_options); |
75 } | 75 } |
76 | 76 |
77 CreateDefaultClients(); | 77 // TODO(crbug.com/345586): Move PowerPolicyController out of |
| 78 // DBusThreadManager. |
| 79 power_policy_controller_.reset(new PowerPolicyController); |
78 } | 80 } |
79 | 81 |
80 DBusThreadManager::~DBusThreadManager() { | 82 DBusThreadManager::~DBusThreadManager() { |
81 // PowerPolicyController's destructor depends on PowerManagerClient. | 83 // PowerPolicyController's destructor depends on PowerManagerClient. |
82 power_policy_controller_.reset(); | 84 power_policy_controller_.reset(); |
83 | 85 |
84 // Delete all D-Bus clients before shutting down the system bus. | 86 // Delete all D-Bus clients before shutting down the system bus. |
85 client_bundle_.reset(); | 87 client_bundle_.reset(); |
86 | 88 |
87 // Shut down the bus. During the browser shutdown, it's ok to shut down | 89 // Shut down the bus. During the browser shutdown, it's ok to shut down |
88 // the bus synchronously. | 90 // the bus synchronously. |
89 if (system_bus_) | 91 if (system_bus_) |
90 system_bus_->ShutdownOnDBusThreadAndBlock(); | 92 system_bus_->ShutdownOnDBusThreadAndBlock(); |
91 | 93 |
92 // Stop the D-Bus thread. | 94 // Stop the D-Bus thread. |
93 if (dbus_thread_) | 95 if (dbus_thread_) |
94 dbus_thread_->Stop(); | 96 dbus_thread_->Stop(); |
95 | 97 |
96 dbus::statistics::Shutdown(); | 98 dbus::statistics::Shutdown(); |
97 | 99 |
98 if (g_dbus_thread_manager == NULL) | 100 if (!g_dbus_thread_manager) |
99 return; // Called form Shutdown() or local test instance. | 101 return; // Called form Shutdown() or local test instance. |
100 | 102 |
101 // There should never be both a global instance and a local instance. | 103 // There should never be both a global instance and a local instance. |
102 CHECK(this == g_dbus_thread_manager); | 104 CHECK(this == g_dbus_thread_manager); |
103 if (g_using_dbus_thread_manager_for_testing) { | 105 if (g_using_dbus_thread_manager_for_testing) { |
104 g_dbus_thread_manager = NULL; | 106 g_dbus_thread_manager = NULL; |
105 g_using_dbus_thread_manager_for_testing = false; | 107 g_using_dbus_thread_manager_for_testing = false; |
106 VLOG(1) << "DBusThreadManager destroyed"; | 108 VLOG(1) << "DBusThreadManager destroyed"; |
107 } else { | 109 } else { |
108 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; | 110 LOG(FATAL) << "~DBusThreadManager() called outside of Shutdown()"; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 263 } |
262 | 264 |
263 UpdateEngineClient* DBusThreadManager::GetUpdateEngineClient() { | 265 UpdateEngineClient* DBusThreadManager::GetUpdateEngineClient() { |
264 return client_bundle_->update_engine_client(); | 266 return client_bundle_->update_engine_client(); |
265 } | 267 } |
266 | 268 |
267 PowerPolicyController* DBusThreadManager::GetPowerPolicyController() { | 269 PowerPolicyController* DBusThreadManager::GetPowerPolicyController() { |
268 return power_policy_controller_.get(); | 270 return power_policy_controller_.get(); |
269 } | 271 } |
270 | 272 |
271 void DBusThreadManager::CreateDefaultClients() { | |
272 client_bundle_.reset(new DBusClientBundle()); | |
273 // TODO(crbug.com/345586): Move PowerPolicyController out of | |
274 // DBusThreadManager. | |
275 power_policy_controller_.reset(new PowerPolicyController); | |
276 } | |
277 | |
278 void DBusThreadManager::InitializeClients() { | 273 void DBusThreadManager::InitializeClients() { |
279 GetBluetoothAdapterClient()->Init(GetSystemBus()); | 274 GetBluetoothAdapterClient()->Init(GetSystemBus()); |
280 GetBluetoothAgentManagerClient()->Init(GetSystemBus()); | 275 GetBluetoothAgentManagerClient()->Init(GetSystemBus()); |
281 GetBluetoothDeviceClient()->Init(GetSystemBus()); | 276 GetBluetoothDeviceClient()->Init(GetSystemBus()); |
282 GetBluetoothGattCharacteristicClient()->Init(GetSystemBus()); | 277 GetBluetoothGattCharacteristicClient()->Init(GetSystemBus()); |
283 GetBluetoothGattDescriptorClient()->Init(GetSystemBus()); | 278 GetBluetoothGattDescriptorClient()->Init(GetSystemBus()); |
284 GetBluetoothGattManagerClient()->Init(GetSystemBus()); | 279 GetBluetoothGattManagerClient()->Init(GetSystemBus()); |
285 GetBluetoothGattServiceClient()->Init(GetSystemBus()); | 280 GetBluetoothGattServiceClient()->Init(GetSystemBus()); |
286 GetBluetoothInputClient()->Init(GetSystemBus()); | 281 GetBluetoothInputClient()->Init(GetSystemBus()); |
287 GetBluetoothProfileManagerClient()->Init(GetSystemBus()); | 282 GetBluetoothProfileManagerClient()->Init(GetSystemBus()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 GetPowerPolicyController()->Init(this); | 317 GetPowerPolicyController()->Init(this); |
323 | 318 |
324 // This must be called after the list of clients so they've each had a | 319 // This must be called after the list of clients so they've each had a |
325 // chance to register with their object g_dbus_thread_managers. | 320 // chance to register with their object g_dbus_thread_managers. |
326 if (GetSystemBus()) | 321 if (GetSystemBus()) |
327 GetSystemBus()->GetManagedObjects(); | 322 GetSystemBus()->GetManagedObjects(); |
328 | 323 |
329 client_bundle_->SetupDefaultEnvironment(); | 324 client_bundle_->SetupDefaultEnvironment(); |
330 } | 325 } |
331 | 326 |
332 // static | |
333 bool DBusThreadManager::IsUsingStub(DBusClientBundle::DBusClientType client) { | 327 bool DBusThreadManager::IsUsingStub(DBusClientBundle::DBusClientType client) { |
334 return !(unstub_client_mask_ & client); | 328 return client_bundle_->IsUsingStub(client); |
335 } | 329 } |
336 | 330 |
337 // static | 331 // static |
338 void DBusThreadManager::Initialize() { | 332 void DBusThreadManager::Initialize() { |
339 // 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 |
340 // early; do not allow that. | 334 // early; do not allow that. |
341 if (g_using_dbus_thread_manager_for_testing) | 335 if (g_using_dbus_thread_manager_for_testing) |
342 return; | 336 return; |
343 | 337 |
344 CHECK(g_dbus_thread_manager == NULL); | 338 CHECK(!g_dbus_thread_manager); |
345 bool use_dbus_stub = !base::SysInfo::IsRunningOnChromeOS() || | 339 bool use_dbus_stub = !base::SysInfo::IsRunningOnChromeOS() || |
346 CommandLine::ForCurrentProcess()->HasSwitch( | 340 CommandLine::ForCurrentProcess()->HasSwitch( |
347 chromeos::switches::kDbusStub); | 341 chromeos::switches::kDbusStub); |
348 bool force_unstub_clients = CommandLine::ForCurrentProcess()->HasSwitch( | 342 bool force_unstub_clients = CommandLine::ForCurrentProcess()->HasSwitch( |
349 chromeos::switches::kDbusUnstubClients); | 343 chromeos::switches::kDbusUnstubClients); |
350 // Determine whether we use stub or real client implementations. | 344 // Determine whether we use stub or real client implementations. |
351 if (force_unstub_clients) { | 345 if (force_unstub_clients) { |
352 InitializeWithPartialStub( | 346 InitializeWithPartialStub( |
353 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 347 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
354 chromeos::switches::kDbusUnstubClients)); | 348 chromeos::switches::kDbusUnstubClients)); |
355 } else if (use_dbus_stub) { | 349 } else if (use_dbus_stub) { |
356 InitializeWithStubs(); | 350 InitializeWithStubs(); |
357 } else { | 351 } else { |
358 InitializeRegular(); | 352 InitializeRegular(); |
359 } | 353 } |
360 } | 354 } |
361 | 355 |
362 // static | 356 // static |
363 scoped_ptr<DBusThreadManagerSetter> DBusThreadManager::GetSetterForTesting() { | 357 scoped_ptr<DBusThreadManagerSetter> DBusThreadManager::GetSetterForTesting() { |
364 if (!g_using_dbus_thread_manager_for_testing) { | 358 if (!g_using_dbus_thread_manager_for_testing) { |
365 g_using_dbus_thread_manager_for_testing = true; | 359 g_using_dbus_thread_manager_for_testing = true; |
366 InitializeWithStubs(); | 360 InitializeWithStubs(); |
367 } | 361 } |
368 | 362 |
369 return make_scoped_ptr(new DBusThreadManagerSetter()); | 363 return make_scoped_ptr(new DBusThreadManagerSetter()); |
370 } | 364 } |
371 | 365 |
372 // static | 366 // static |
373 void DBusThreadManager::CreateGlobalInstance() { | 367 void DBusThreadManager::CreateGlobalInstance( |
| 368 DBusClientBundle::DBusClientTypeMask unstub_client_mask) { |
374 CHECK(!g_dbus_thread_manager); | 369 CHECK(!g_dbus_thread_manager); |
375 g_dbus_thread_manager = new DBusThreadManager(); | 370 g_dbus_thread_manager = new DBusThreadManager( |
| 371 make_scoped_ptr(new DBusClientBundle(unstub_client_mask))); |
376 g_dbus_thread_manager->InitializeClients(); | 372 g_dbus_thread_manager->InitializeClients(); |
377 } | 373 } |
378 | 374 |
379 // static | 375 // static |
380 void DBusThreadManager::InitializeRegular() { | 376 void DBusThreadManager::InitializeRegular() { |
381 unstub_client_mask_ = DBusClientBundle::ALL_CLIENTS; | 377 CreateGlobalInstance(DBusClientBundle::ALL_CLIENTS); |
382 CreateGlobalInstance(); | |
383 VLOG(1) << "DBusThreadManager initialized for Chrome OS"; | 378 VLOG(1) << "DBusThreadManager initialized for Chrome OS"; |
384 } | 379 } |
385 | 380 |
386 // static | 381 // static |
387 void DBusThreadManager::InitializeWithStubs() { | 382 void DBusThreadManager::InitializeWithStubs() { |
388 unstub_client_mask_ = DBusClientBundle::NO_CLIENTS; | 383 CreateGlobalInstance(DBusClientBundle::NO_CLIENTS); |
389 CreateGlobalInstance(); | |
390 VLOG(1) << "DBusThreadManager created for testing"; | 384 VLOG(1) << "DBusThreadManager created for testing"; |
391 } | 385 } |
392 | 386 |
393 // static | 387 // static |
394 void DBusThreadManager::InitializeWithPartialStub( | 388 void DBusThreadManager::InitializeWithPartialStub( |
395 const std::string& unstub_clients) { | 389 const std::string& unstub_clients) { |
396 unstub_client_mask_ = DBusClientBundle::ParseUnstubList(unstub_clients); | 390 DBusClientBundle::DBusClientTypeMask unstub_client_mask = |
| 391 DBusClientBundle::ParseUnstubList(unstub_clients); |
397 // We should have something parsed correctly here. | 392 // We should have something parsed correctly here. |
398 if (unstub_client_mask_ == 0) { | 393 LOG_IF(FATAL, unstub_client_mask == 0) |
399 LOG(FATAL) << "Switch values for --" | 394 << "Switch values for --" << chromeos::switches::kDbusUnstubClients |
400 << chromeos::switches::kDbusUnstubClients | 395 << " cannot be parsed: " << unstub_clients; |
401 << " cannot be parsed: " | |
402 << unstub_clients; | |
403 } | |
404 VLOG(1) << "DBusThreadManager initialized for mixed runtime environment"; | 396 VLOG(1) << "DBusThreadManager initialized for mixed runtime environment"; |
405 CreateGlobalInstance(); | 397 CreateGlobalInstance(unstub_client_mask); |
406 } | 398 } |
407 | 399 |
408 // static | 400 // static |
409 bool DBusThreadManager::IsInitialized() { | 401 bool DBusThreadManager::IsInitialized() { |
410 return g_dbus_thread_manager != NULL; | 402 return g_dbus_thread_manager != NULL; |
411 } | 403 } |
412 | 404 |
413 // static | 405 // static |
414 void DBusThreadManager::Shutdown() { | 406 void DBusThreadManager::Shutdown() { |
415 // Ensure that we only shutdown DBusThreadManager once. | 407 // Ensure that we only shutdown DBusThreadManager once. |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 client.Pass(); | 623 client.Pass(); |
632 } | 624 } |
633 | 625 |
634 void DBusThreadManagerSetter::SetUpdateEngineClient( | 626 void DBusThreadManagerSetter::SetUpdateEngineClient( |
635 scoped_ptr<UpdateEngineClient> client) { | 627 scoped_ptr<UpdateEngineClient> client) { |
636 DBusThreadManager::Get()->client_bundle_->update_engine_client_ = | 628 DBusThreadManager::Get()->client_bundle_->update_engine_client_ = |
637 client.Pass(); | 629 client.Pass(); |
638 } | 630 } |
639 | 631 |
640 } // namespace chromeos | 632 } // namespace chromeos |
OLD | NEW |