Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: chrome/browser/chromeos/dbus/dbus_thread_manager.cc

Issue 8343069: chromeos: Add MockDBusThreadManager and mock clients. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
oshima 2011/10/28 23:35:20 Not requirement, but I prefer to put class in .cc
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 }
oshima 2011/10/28 23:35:20 nuke {}
satorux1 2011/10/28 23:43:29 Done.
50
51 // Create bluetooth clients if bluetooth is enabled.
52 if (command_line.HasSwitch(switches::kEnableBluetooth)) {
53 bluetooth_manager_client_.reset(BluetoothManagerClient::Create(
54 system_bus_.get()));
55 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create(
56 system_bus_.get()));
57 }
58
59 // Create the power manager client.
60 power_manager_client_.reset(PowerManagerClient::Create(system_bus_.get()));
61 // Create the session manager client.
62 session_manager_client_.reset(
63 SessionManagerClient::Create(system_bus_.get()));
64 // Create the speech synthesizer client.
65 speech_synthesizer_client_.reset(
66 SpeechSynthesizerClient::Create(system_bus_.get()));
46 } 67 }
47 68
48 // Create bluetooth clients if bluetooth is enabled. 69 virtual ~DBusThreadManagerImpl() {
49 if (command_line.HasSwitch(switches::kEnableBluetooth)) { 70 // Shut down the bus. During the browser shutdown, it's ok to shut down
50 bluetooth_manager_client_.reset(BluetoothManagerClient::Create( 71 // the bus synchronously.
51 system_bus_.get())); 72 system_bus_->ShutdownOnDBusThreadAndBlock();
52 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create( 73
53 system_bus_.get())); 74 // Stop the D-Bus thread.
75 dbus_thread_->Stop();
54 } 76 }
55 77
56 // Create the power manager client. 78 // DBusThreadManager override.
57 power_manager_client_.reset(PowerManagerClient::Create(system_bus_.get())); 79 virtual BluetoothAdapterClient* bluetooth_adapter_client() OVERRIDE {
58 // Create the session manager client. 80 return bluetooth_adapter_client_.get();
59 session_manager_client_.reset( 81 }
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 82
66 DBusThreadManager::~DBusThreadManager() { 83 // DBusThreadManager override.
67 // Shut down the bus. During the browser shutdown, it's ok to shut down 84 virtual BluetoothManagerClient* bluetooth_manager_client() OVERRIDE {
68 // the bus synchronously. 85 return bluetooth_manager_client_.get();
69 system_bus_->ShutdownOnDBusThreadAndBlock(); 86 }
70 87
71 // Stop the D-Bus thread. 88 // DBusThreadManager override.
72 dbus_thread_->Stop(); 89 virtual PowerManagerClient* power_manager_client() OVERRIDE {
73 } 90 return power_manager_client_.get();
91 }
74 92
93 // DBusThreadManager override.
94 virtual SensorsClient* sensors_client() OVERRIDE {
95 return sensors_client_.get();
96 }
97
98 // DBusThreadManager override.
99 virtual SessionManagerClient* session_manager_client() OVERRIDE {
100 return session_manager_client_.get();
101 }
102
103 // DBusThreadManager override.
104 virtual SpeechSynthesizerClient* speech_synthesizer_client() OVERRIDE {
105 return speech_synthesizer_client_.get();
106 }
107
108 // DBusThreadManager override.
109 virtual void set_session_manager_client_for_testing(
110 SessionManagerClient* session_manager_client) OVERRIDE {
111 session_manager_client_.reset(session_manager_client);
112 }
113
114 scoped_ptr<base::Thread> dbus_thread_;
115 scoped_refptr<dbus::Bus> system_bus_;
116 scoped_ptr<CrosDBusService> cros_dbus_service_;
117 scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_;
118 scoped_ptr<BluetoothManagerClient> bluetooth_manager_client_;
119 scoped_ptr<PowerManagerClient> power_manager_client_;
120 scoped_ptr<SensorsClient> sensors_client_;
121 scoped_ptr<SessionManagerClient> session_manager_client_;
122 scoped_ptr<SpeechSynthesizerClient> speech_synthesizer_client_;
123 };
124
125 // static
75 void DBusThreadManager::Initialize() { 126 void DBusThreadManager::Initialize() {
76 if (g_dbus_thread_manager) { 127 if (g_dbus_thread_manager) {
77 // This can happen in tests. 128 // This can happen in tests.
oshima 2011/10/28 23:35:20 Given that we now have initialize for test, should
satorux1 2011/10/28 23:43:29 Tests are not yet converted to use InitializeForTe
78 LOG(WARNING) << "DBusThreadManager::Initialize() was already called"; 129 LOG(WARNING) << "DBusThreadManager was already initialized";
79 return; 130 return;
80 } 131 }
81 g_dbus_thread_manager = new DBusThreadManager; 132 g_dbus_thread_manager = new DBusThreadManagerImpl;
82 VLOG(1) << "DBusThreadManager initialized"; 133 VLOG(1) << "DBusThreadManager initialized";
83 } 134 }
84 135
136 // static
137 void DBusThreadManager::InitializeForTesting(
138 DBusThreadManager* dbus_thread_manager) {
139 if (g_dbus_thread_manager) {
140 // This can happen in tests.
141 LOG(WARNING) << "DBusThreadManager was already initialized";
oshima 2011/10/28 23:35:20 If this is expected, please change this to INFO or
satorux1 2011/10/28 23:43:29 This is not expected. I removed the irrelevant com
142 return;
143 }
144 g_dbus_thread_manager = dbus_thread_manager;
145 VLOG(1) << "DBusThreadManager initialized";
146 }
147
148 // static
85 void DBusThreadManager::Shutdown() { 149 void DBusThreadManager::Shutdown() {
86 if (!g_dbus_thread_manager) { 150 if (!g_dbus_thread_manager) {
87 // This can happen in tests. 151 // This can happen in tests.
88 LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager"; 152 LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager";
89 return; 153 return;
90 } 154 }
91 delete g_dbus_thread_manager; 155 delete g_dbus_thread_manager;
92 g_dbus_thread_manager = NULL; 156 g_dbus_thread_manager = NULL;
93 VLOG(1) << "DBusThreadManager Shutdown completed"; 157 VLOG(1) << "DBusThreadManager Shutdown completed";
94 } 158 }
95 159
160 DBusThreadManager::DBusThreadManager() {
161 }
162
163 DBusThreadManager::~DBusThreadManager() {
164 }
165
166 // static
96 DBusThreadManager* DBusThreadManager::Get() { 167 DBusThreadManager* DBusThreadManager::Get() {
97 CHECK(g_dbus_thread_manager) 168 CHECK(g_dbus_thread_manager)
98 << "DBusThreadManager::Get() called before Initialize()"; 169 << "DBusThreadManager::Get() called before Initialize()";
99 return g_dbus_thread_manager; 170 return g_dbus_thread_manager;
100 } 171 }
101 172
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 173 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/dbus/dbus_thread_manager.h ('k') | chrome/browser/chromeos/dbus/mock_bluetooth_adapter_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698