OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | 5 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" | 9 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" |
10 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" | 10 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" |
11 #include "chrome/browser/chromeos/dbus/cros_dbus_service.h" | 11 #include "chrome/browser/chromeos/dbus/cros_dbus_service.h" |
12 #include "chrome/browser/chromeos/dbus/power_manager_client.h" | 12 #include "chrome/browser/chromeos/dbus/power_manager_client.h" |
13 #include "chrome/browser/chromeos/dbus/sensors_client.h" | 13 #include "chrome/browser/chromeos/dbus/sensors_client.h" |
14 #include "chrome/browser/chromeos/dbus/session_manager_client.h" | 14 #include "chrome/browser/chromeos/dbus/session_manager_client.h" |
15 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" | 15 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 #include "dbus/bus.h" | 17 #include "dbus/bus.h" |
18 | 18 |
19 namespace chromeos { | 19 namespace chromeos { |
20 | 20 |
21 static DBusThreadManager* g_dbus_thread_manager = NULL; | 21 static DBusThreadManager* g_dbus_thread_manager = NULL; |
22 | 22 |
23 DBusThreadManager::DBusThreadManager() { | 23 // The DBusThreadManager implementation used in production. |
24 // Create the D-Bus thread. | 24 class DBusThreadManagerImpl : public DBusThreadManager { |
25 base::Thread::Options thread_options; | 25 public: |
26 thread_options.message_loop_type = MessageLoop::TYPE_IO; | 26 DBusThreadManagerImpl() { |
27 dbus_thread_.reset(new base::Thread("D-Bus thread")); | 27 // Create the D-Bus thread. |
28 dbus_thread_->StartWithOptions(thread_options); | 28 base::Thread::Options thread_options; |
29 thread_options.message_loop_type = MessageLoop::TYPE_IO; | |
30 dbus_thread_.reset(new base::Thread("D-Bus thread")); | |
31 dbus_thread_->StartWithOptions(thread_options); | |
29 | 32 |
30 // Create the connection to the system bus. | 33 // Create the connection to the system bus. |
31 dbus::Bus::Options system_bus_options; | 34 dbus::Bus::Options system_bus_options; |
32 system_bus_options.bus_type = dbus::Bus::SYSTEM; | 35 system_bus_options.bus_type = dbus::Bus::SYSTEM; |
33 system_bus_options.connection_type = dbus::Bus::PRIVATE; | 36 system_bus_options.connection_type = dbus::Bus::PRIVATE; |
34 system_bus_options.dbus_thread_message_loop_proxy = | 37 system_bus_options.dbus_thread_message_loop_proxy = |
35 dbus_thread_->message_loop_proxy(); | 38 dbus_thread_->message_loop_proxy(); |
36 system_bus_ = new dbus::Bus(system_bus_options); | 39 system_bus_ = new dbus::Bus(system_bus_options); |
37 | 40 |
38 // Create and start the cros D-Bus service. | 41 // Create and start the cros D-Bus service. |
39 cros_dbus_service_.reset(CrosDBusService::Create(system_bus_.get())); | 42 cros_dbus_service_.reset(CrosDBusService::Create(system_bus_.get())); |
40 cros_dbus_service_->Start(); | 43 cros_dbus_service_->Start(); |
41 | 44 |
42 // Start monitoring sensors if needed. | 45 // Start monitoring sensors if needed. |
43 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 46 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
44 if (command_line.HasSwitch(switches::kEnableSensors)) { | 47 if (command_line.HasSwitch(switches::kEnableSensors)) |
45 sensors_client_.reset(SensorsClient::Create(system_bus_.get())); | 48 sensors_client_.reset(SensorsClient::Create(system_bus_.get())); |
49 | |
50 // Create bluetooth clients if bluetooth is enabled. | |
51 if (command_line.HasSwitch(switches::kEnableBluetooth)) { | |
52 bluetooth_manager_client_.reset(BluetoothManagerClient::Create( | |
53 system_bus_.get())); | |
54 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create( | |
55 system_bus_.get())); | |
56 } | |
57 | |
58 // Create the power manager client. | |
59 power_manager_client_.reset(PowerManagerClient::Create(system_bus_.get())); | |
60 // Create the session manager client. | |
61 session_manager_client_.reset( | |
62 SessionManagerClient::Create(system_bus_.get())); | |
63 // Create the speech synthesizer client. | |
64 speech_synthesizer_client_.reset( | |
65 SpeechSynthesizerClient::Create(system_bus_.get())); | |
46 } | 66 } |
47 | 67 |
48 // Create bluetooth clients if bluetooth is enabled. | 68 virtual ~DBusThreadManagerImpl() { |
49 if (command_line.HasSwitch(switches::kEnableBluetooth)) { | 69 // Shut down the bus. During the browser shutdown, it's ok to shut down |
50 bluetooth_manager_client_.reset(BluetoothManagerClient::Create( | 70 // the bus synchronously. |
51 system_bus_.get())); | 71 system_bus_->ShutdownOnDBusThreadAndBlock(); |
52 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create( | 72 |
53 system_bus_.get())); | 73 // Stop the D-Bus thread. |
74 dbus_thread_->Stop(); | |
54 } | 75 } |
55 | 76 |
56 // Create the power manager client. | 77 // DBusThreadManager override. |
57 power_manager_client_.reset(PowerManagerClient::Create(system_bus_.get())); | 78 virtual BluetoothAdapterClient* bluetooth_adapter_client() OVERRIDE { |
58 // Create the session manager client. | 79 return bluetooth_adapter_client_.get(); |
59 session_manager_client_.reset( | 80 } |
60 SessionManagerClient::Create(system_bus_.get())); | |
61 // Create the speech synthesizer client. | |
62 speech_synthesizer_client_.reset( | |
63 SpeechSynthesizerClient::Create(system_bus_.get())); | |
64 } | |
65 | 81 |
66 DBusThreadManager::~DBusThreadManager() { | 82 // DBusThreadManager override. |
67 // Shut down the bus. During the browser shutdown, it's ok to shut down | 83 virtual BluetoothManagerClient* bluetooth_manager_client() OVERRIDE { |
68 // the bus synchronously. | 84 return bluetooth_manager_client_.get(); |
69 system_bus_->ShutdownOnDBusThreadAndBlock(); | 85 } |
70 | 86 |
71 // Stop the D-Bus thread. | 87 // DBusThreadManager override. |
72 dbus_thread_->Stop(); | 88 virtual PowerManagerClient* power_manager_client() OVERRIDE { |
73 } | 89 return power_manager_client_.get(); |
90 } | |
74 | 91 |
92 // DBusThreadManager override. | |
93 virtual SensorsClient* sensors_client() OVERRIDE { | |
94 return sensors_client_.get(); | |
95 } | |
96 | |
97 // DBusThreadManager override. | |
98 virtual SessionManagerClient* session_manager_client() OVERRIDE { | |
99 return session_manager_client_.get(); | |
100 } | |
101 | |
102 // DBusThreadManager override. | |
103 virtual SpeechSynthesizerClient* speech_synthesizer_client() OVERRIDE { | |
104 return speech_synthesizer_client_.get(); | |
105 } | |
106 | |
107 // DBusThreadManager override. | |
108 virtual void set_session_manager_client_for_testing( | |
109 SessionManagerClient* session_manager_client) OVERRIDE { | |
110 session_manager_client_.reset(session_manager_client); | |
111 } | |
112 | |
113 scoped_ptr<base::Thread> dbus_thread_; | |
114 scoped_refptr<dbus::Bus> system_bus_; | |
115 scoped_ptr<CrosDBusService> cros_dbus_service_; | |
116 scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_; | |
117 scoped_ptr<BluetoothManagerClient> bluetooth_manager_client_; | |
118 scoped_ptr<PowerManagerClient> power_manager_client_; | |
119 scoped_ptr<SensorsClient> sensors_client_; | |
120 scoped_ptr<SessionManagerClient> session_manager_client_; | |
121 scoped_ptr<SpeechSynthesizerClient> speech_synthesizer_client_; | |
122 }; | |
123 | |
124 // static | |
75 void DBusThreadManager::Initialize() { | 125 void DBusThreadManager::Initialize() { |
76 if (g_dbus_thread_manager) { | 126 if (g_dbus_thread_manager) { |
77 // This can happen in tests. | 127 LOG(WARNING) << "DBusThreadManager was already initialized"; |
78 LOG(WARNING) << "DBusThreadManager::Initialize() was already called"; | |
79 return; | 128 return; |
80 } | 129 } |
81 g_dbus_thread_manager = new DBusThreadManager; | 130 g_dbus_thread_manager = new DBusThreadManagerImpl; |
82 VLOG(1) << "DBusThreadManager initialized"; | 131 VLOG(1) << "DBusThreadManager initialized"; |
83 } | 132 } |
84 | 133 |
134 // static | |
135 void DBusThreadManager::InitializeForTesting( | |
136 DBusThreadManager* dbus_thread_manager) { | |
137 if (g_dbus_thread_manager) { | |
138 LOG(WARNING) << "DBusThreadManager was already initialized"; | |
139 return; | |
140 } | |
141 g_dbus_thread_manager = dbus_thread_manager; | |
142 VLOG(1) << "DBusThreadManager initialized"; | |
143 } | |
144 | |
145 // static | |
85 void DBusThreadManager::Shutdown() { | 146 void DBusThreadManager::Shutdown() { |
86 if (!g_dbus_thread_manager) { | 147 if (!g_dbus_thread_manager) { |
87 // This can happen in tests. | 148 // This can happen in tests. |
oshima
2011/10/28 23:51:47
If this shouldn't happen but not ready to change t
satorux1
2011/10/28 23:54:35
Done.
| |
88 LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager"; | 149 LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager"; |
89 return; | 150 return; |
90 } | 151 } |
91 delete g_dbus_thread_manager; | 152 delete g_dbus_thread_manager; |
92 g_dbus_thread_manager = NULL; | 153 g_dbus_thread_manager = NULL; |
93 VLOG(1) << "DBusThreadManager Shutdown completed"; | 154 VLOG(1) << "DBusThreadManager Shutdown completed"; |
94 } | 155 } |
95 | 156 |
157 DBusThreadManager::DBusThreadManager() { | |
158 } | |
159 | |
160 DBusThreadManager::~DBusThreadManager() { | |
161 } | |
162 | |
163 // static | |
96 DBusThreadManager* DBusThreadManager::Get() { | 164 DBusThreadManager* DBusThreadManager::Get() { |
97 CHECK(g_dbus_thread_manager) | 165 CHECK(g_dbus_thread_manager) |
98 << "DBusThreadManager::Get() called before Initialize()"; | 166 << "DBusThreadManager::Get() called before Initialize()"; |
99 return g_dbus_thread_manager; | 167 return g_dbus_thread_manager; |
100 } | 168 } |
101 | 169 |
102 void DBusThreadManager::set_session_manager_client_for_testing( | |
103 SessionManagerClient* session_manager_client) { | |
104 session_manager_client_.reset(session_manager_client); | |
105 } | |
106 | |
107 } // namespace chromeos | 170 } // namespace chromeos |
OLD | NEW |