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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/dbus/cros_dbus_service.h"
8 #include "base/chromeos/chromeos_version.h"
9 #include "base/stl_util.h"
10 #include "base/threading/platform_thread.h"
11 #include "chrome/browser/chromeos/dbus/proxy_resolution_service_provider.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "dbus/exported_object.h"
15 #include "dbus/object_path.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
22 CrosDBusService
* g_cros_dbus_service
= NULL
;
26 // The CrosDBusService implementation used in production, and unit tests.
27 class CrosDBusServiceImpl
: public CrosDBusService
{
29 explicit CrosDBusServiceImpl(dbus::Bus
* bus
)
30 : service_started_(false),
31 origin_thread_id_(base::PlatformThread::CurrentId()),
35 virtual ~CrosDBusServiceImpl() {
36 STLDeleteElements(&service_providers_
);
39 // Starts the D-Bus service.
41 // Make sure we're running on the origin thread (i.e. the UI thread in
43 DCHECK(OnOriginThread());
45 // Return if the service has been already started.
49 bus_
->RequestOwnership(kLibCrosServiceName
,
50 base::Bind(&CrosDBusServiceImpl::OnOwnership
,
51 base::Unretained(this)));
53 exported_object_
= bus_
->GetExportedObject(
54 dbus::ObjectPath(kLibCrosServicePath
));
56 for (size_t i
= 0; i
< service_providers_
.size(); ++i
)
57 service_providers_
[i
]->Start(exported_object_
);
59 service_started_
= true;
61 VLOG(1) << "CrosDBusServiceImpl started.";
64 // Registers a service provider. This must be done before Start().
65 // |provider| will be owned by CrosDBusService.
66 void RegisterServiceProvider(ServiceProviderInterface
* provider
) {
67 service_providers_
.push_back(provider
);
71 // Returns true if the current thread is on the origin thread.
72 bool OnOriginThread() {
73 return base::PlatformThread::CurrentId() == origin_thread_id_
;
76 // Called when an ownership request is completed.
77 void OnOwnership(const std::string
& service_name
,
79 LOG_IF(ERROR
, !success
) << "Failed to own: " << service_name
;
82 bool service_started_
;
83 base::PlatformThreadId origin_thread_id_
;
85 scoped_refptr
<dbus::ExportedObject
> exported_object_
;
87 // Service providers that form CrosDBusService.
88 std::vector
<ServiceProviderInterface
*> service_providers_
;
91 // The stub CrosDBusService implementation used on Linux desktop,
92 // which does nothing as of now.
93 class CrosDBusServiceStubImpl
: public CrosDBusService
{
95 CrosDBusServiceStubImpl() {
98 virtual ~CrosDBusServiceStubImpl() {
103 void CrosDBusService::Initialize() {
104 if (g_cros_dbus_service
) {
105 LOG(WARNING
) << "CrosDBusService was already initialized";
108 dbus::Bus
* bus
= DBusThreadManager::Get()->GetSystemBus();
109 if (base::chromeos::IsRunningOnChromeOS() && bus
) {
110 CrosDBusServiceImpl
* service
= new CrosDBusServiceImpl(bus
);
111 service
->RegisterServiceProvider(ProxyResolutionServiceProvider::Create());
112 g_cros_dbus_service
= service
;
115 g_cros_dbus_service
= new CrosDBusServiceStubImpl
;
117 VLOG(1) << "CrosDBusService initialized";
121 void CrosDBusService::InitializeForTesting(
123 ServiceProviderInterface
* proxy_resolution_service
) {
124 if (g_cros_dbus_service
) {
125 LOG(WARNING
) << "CrosDBusService was already initialized";
128 CrosDBusServiceImpl
* service
= new CrosDBusServiceImpl(bus
);
129 service
->RegisterServiceProvider(proxy_resolution_service
);
131 g_cros_dbus_service
= service
;
132 VLOG(1) << "CrosDBusService initialized";
136 void CrosDBusService::Shutdown() {
137 delete g_cros_dbus_service
;
138 g_cros_dbus_service
= NULL
;
139 VLOG(1) << "CrosDBusService Shutdown completed";
142 CrosDBusService::~CrosDBusService() {
145 CrosDBusService::ServiceProviderInterface::~ServiceProviderInterface() {
148 } // namespace chromeos