| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #include "chromeos/dbus/update_engine_client.h" | 38 #include "chromeos/dbus/update_engine_client.h" |
| 39 #include "dbus/bus.h" | 39 #include "dbus/bus.h" |
| 40 #include "dbus/dbus_statistics.h" | 40 #include "dbus/dbus_statistics.h" |
| 41 | 41 |
| 42 namespace chromeos { | 42 namespace chromeos { |
| 43 | 43 |
| 44 static DBusThreadManager* g_dbus_thread_manager = NULL; | 44 static DBusThreadManager* g_dbus_thread_manager = NULL; |
| 45 static bool g_using_dbus_thread_manager_for_testing = false; | 45 static bool g_using_dbus_thread_manager_for_testing = false; |
| 46 | 46 |
| 47 DBusThreadManager::DBusThreadManager(ProcessMask process_mask, | 47 DBusThreadManager::DBusThreadManager(ProcessMask process_mask, |
| 48 DBusClientTypeMask real_client_mask) | 48 bool use_real_clients) |
| 49 : clients_common_(new DBusClientsCommon(real_client_mask)) { | 49 : use_real_clients_(use_real_clients), |
| 50 clients_common_(new DBusClientsCommon(use_real_clients)) { |
| 50 if (process_mask & PROCESS_BROWSER) | 51 if (process_mask & PROCESS_BROWSER) |
| 51 clients_browser_.reset(new DBusClientsBrowser(real_client_mask)); | 52 clients_browser_.reset(new DBusClientsBrowser(use_real_clients)); |
| 52 // NOTE: When there are clients only used by ash, create them here. | 53 // NOTE: When there are clients only used by ash, create them here. |
| 53 | 54 |
| 54 dbus::statistics::Initialize(); | 55 dbus::statistics::Initialize(); |
| 55 | 56 |
| 56 if (real_client_mask != 0) { | 57 if (use_real_clients) { |
| 57 // At least one real DBusClient is used. | |
| 58 // Create the D-Bus thread. | 58 // Create the D-Bus thread. |
| 59 base::Thread::Options thread_options; | 59 base::Thread::Options thread_options; |
| 60 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 60 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 61 dbus_thread_.reset(new base::Thread("D-Bus thread")); | 61 dbus_thread_.reset(new base::Thread("D-Bus thread")); |
| 62 dbus_thread_->StartWithOptions(thread_options); | 62 dbus_thread_->StartWithOptions(thread_options); |
| 63 | 63 |
| 64 // Create the connection to the system bus. | 64 // Create the connection to the system bus. |
| 65 dbus::Bus::Options system_bus_options; | 65 dbus::Bus::Options system_bus_options; |
| 66 system_bus_options.bus_type = dbus::Bus::SYSTEM; | 66 system_bus_options.bus_type = dbus::Bus::SYSTEM; |
| 67 system_bus_options.connection_type = dbus::Bus::PRIVATE; | 67 system_bus_options.connection_type = dbus::Bus::PRIVATE; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 193 |
| 194 SystemClockClient* DBusThreadManager::GetSystemClockClient() { | 194 SystemClockClient* DBusThreadManager::GetSystemClockClient() { |
| 195 return clients_common_->system_clock_client_.get(); | 195 return clients_common_->system_clock_client_.get(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 UpdateEngineClient* DBusThreadManager::GetUpdateEngineClient() { | 198 UpdateEngineClient* DBusThreadManager::GetUpdateEngineClient() { |
| 199 return clients_common_->update_engine_client_.get(); | 199 return clients_common_->update_engine_client_.get(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void DBusThreadManager::InitializeClients() { | 202 void DBusThreadManager::InitializeClients() { |
| 203 // Some clients call DBusThreadManager::Get() during initialization. |
| 204 DCHECK(g_dbus_thread_manager); |
| 205 |
| 203 clients_common_->Initialize(GetSystemBus()); | 206 clients_common_->Initialize(GetSystemBus()); |
| 204 if (clients_browser_) | 207 if (clients_browser_) |
| 205 clients_browser_->Initialize(GetSystemBus()); | 208 clients_browser_->Initialize(GetSystemBus()); |
| 209 |
| 210 if (use_real_clients_) |
| 211 VLOG(1) << "DBusThreadManager initialized for Chrome OS"; |
| 212 else |
| 213 VLOG(1) << "DBusThreadManager created for testing"; |
| 206 } | 214 } |
| 207 | 215 |
| 208 bool DBusThreadManager::IsUsingFake(DBusClientType client) { | 216 bool DBusThreadManager::IsUsingFakes() { |
| 209 return !clients_common_->IsUsingReal(client); | 217 return !use_real_clients_; |
| 210 } | 218 } |
| 211 | 219 |
| 212 // static | 220 // static |
| 213 void DBusThreadManager::Initialize(ProcessMask process_mask) { | 221 void DBusThreadManager::Initialize(ProcessMask process_mask) { |
| 214 // If we initialize DBusThreadManager twice we may also be shutting it down | 222 // If we initialize DBusThreadManager twice we may also be shutting it down |
| 215 // early; do not allow that. | 223 // early; do not allow that. |
| 216 if (g_using_dbus_thread_manager_for_testing) | 224 if (g_using_dbus_thread_manager_for_testing) |
| 217 return; | 225 return; |
| 218 | 226 |
| 219 CHECK(!g_dbus_thread_manager); | 227 CHECK(!g_dbus_thread_manager); |
| 220 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 228 bool use_real_clients = base::SysInfo::IsRunningOnChromeOS() && |
| 221 bool use_fakes = !base::SysInfo::IsRunningOnChromeOS() || | 229 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 222 command_line->HasSwitch(chromeos::switches::kDbusStub); | 230 chromeos::switches::kDbusStub); |
| 223 // TODO(jamescook): Delete this after M56 branches. | 231 g_dbus_thread_manager = new DBusThreadManager(process_mask, use_real_clients); |
| 224 if (command_line->HasSwitch(chromeos::switches::kDbusUnstubClients)) | 232 g_dbus_thread_manager->InitializeClients(); |
| 225 LOG(FATAL) << "Use --dbus-real-clients instead of --dbus-unstub-clients."; | |
| 226 bool force_real_clients = | |
| 227 command_line->HasSwitch(chromeos::switches::kDbusRealClients); | |
| 228 // Determine whether we use fake or real client implementations. | |
| 229 if (force_real_clients) { | |
| 230 InitializeWithPartialFakes(process_mask, | |
| 231 command_line->GetSwitchValueASCII( | |
| 232 chromeos::switches::kDbusRealClients)); | |
| 233 } else if (use_fakes) { | |
| 234 InitializeWithFakeClients(process_mask); | |
| 235 } else { | |
| 236 InitializeWithRealClients(process_mask); | |
| 237 } | |
| 238 } | 233 } |
| 239 | 234 |
| 240 // static | 235 // static |
| 241 void DBusThreadManager::Initialize() { | 236 void DBusThreadManager::Initialize() { |
| 242 Initialize(PROCESS_ALL); | 237 Initialize(PROCESS_ALL); |
| 243 } | 238 } |
| 244 | 239 |
| 245 // static | 240 // static |
| 246 std::unique_ptr<DBusThreadManagerSetter> | 241 std::unique_ptr<DBusThreadManagerSetter> |
| 247 DBusThreadManager::GetSetterForTesting() { | 242 DBusThreadManager::GetSetterForTesting() { |
| 248 if (!g_using_dbus_thread_manager_for_testing) { | 243 if (!g_using_dbus_thread_manager_for_testing) { |
| 249 g_using_dbus_thread_manager_for_testing = true; | 244 g_using_dbus_thread_manager_for_testing = true; |
| 250 // TODO(jamescook): Don't initialize clients as a side-effect of using a | 245 // TODO(jamescook): Don't initialize clients as a side-effect of using a |
| 251 // test API. For now, assume the caller wants all clients. | 246 // test API. For now, assume the caller wants all clients. |
| 252 InitializeWithFakeClients(PROCESS_ALL); | 247 g_dbus_thread_manager = |
| 248 new DBusThreadManager(PROCESS_ALL, false /* use_real_clients */); |
| 249 g_dbus_thread_manager->InitializeClients(); |
| 253 } | 250 } |
| 254 | 251 |
| 255 return base::WrapUnique(new DBusThreadManagerSetter()); | 252 return base::WrapUnique(new DBusThreadManagerSetter()); |
| 256 } | 253 } |
| 257 | 254 |
| 258 // static | 255 // static |
| 259 void DBusThreadManager::CreateGlobalInstance( | |
| 260 ProcessMask process_mask, | |
| 261 DBusClientTypeMask real_client_mask) { | |
| 262 CHECK(!g_dbus_thread_manager); | |
| 263 g_dbus_thread_manager = new DBusThreadManager(process_mask, real_client_mask); | |
| 264 g_dbus_thread_manager->InitializeClients(); | |
| 265 } | |
| 266 | |
| 267 // static | |
| 268 void DBusThreadManager::InitializeWithRealClients(ProcessMask process_mask) { | |
| 269 CreateGlobalInstance(process_mask, | |
| 270 static_cast<DBusClientTypeMask>(DBusClientType::ALL)); | |
| 271 VLOG(1) << "DBusThreadManager initialized for Chrome OS"; | |
| 272 } | |
| 273 | |
| 274 // static | |
| 275 void DBusThreadManager::InitializeWithFakeClients(ProcessMask process_mask) { | |
| 276 CreateGlobalInstance(process_mask, | |
| 277 static_cast<DBusClientTypeMask>(DBusClientType::NONE)); | |
| 278 VLOG(1) << "DBusThreadManager created for testing"; | |
| 279 } | |
| 280 | |
| 281 // static | |
| 282 void DBusThreadManager::InitializeWithPartialFakes( | |
| 283 ProcessMask process_mask, | |
| 284 const std::string& force_real_clients) { | |
| 285 DBusClientTypeMask real_client_mask = | |
| 286 ParseDBusRealClientsList(force_real_clients); | |
| 287 // We should have something parsed correctly here. | |
| 288 LOG_IF(FATAL, real_client_mask == 0) | |
| 289 << "Switch values for --" << chromeos::switches::kDbusRealClients | |
| 290 << " cannot be parsed: " << real_client_mask; | |
| 291 VLOG(1) << "DBusThreadManager initialized for mixed runtime environment"; | |
| 292 CreateGlobalInstance(process_mask, real_client_mask); | |
| 293 } | |
| 294 | |
| 295 // static | |
| 296 bool DBusThreadManager::IsInitialized() { | 256 bool DBusThreadManager::IsInitialized() { |
| 297 return g_dbus_thread_manager != NULL; | 257 return g_dbus_thread_manager != NULL; |
| 298 } | 258 } |
| 299 | 259 |
| 300 // static | 260 // static |
| 301 void DBusThreadManager::Shutdown() { | 261 void DBusThreadManager::Shutdown() { |
| 302 // Ensure that we only shutdown DBusThreadManager once. | 262 // Ensure that we only shutdown DBusThreadManager once. |
| 303 CHECK(g_dbus_thread_manager); | 263 CHECK(g_dbus_thread_manager); |
| 304 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; | 264 DBusThreadManager* dbus_thread_manager = g_dbus_thread_manager; |
| 305 g_dbus_thread_manager = NULL; | 265 g_dbus_thread_manager = NULL; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 std::move(client); | 364 std::move(client); |
| 405 } | 365 } |
| 406 | 366 |
| 407 void DBusThreadManagerSetter::SetUpdateEngineClient( | 367 void DBusThreadManagerSetter::SetUpdateEngineClient( |
| 408 std::unique_ptr<UpdateEngineClient> client) { | 368 std::unique_ptr<UpdateEngineClient> client) { |
| 409 DBusThreadManager::Get()->clients_common_->update_engine_client_ = | 369 DBusThreadManager::Get()->clients_common_->update_engine_client_ = |
| 410 std::move(client); | 370 std::move(client); |
| 411 } | 371 } |
| 412 | 372 |
| 413 } // namespace chromeos | 373 } // namespace chromeos |
| OLD | NEW |