blob: 6ab82628f66a035c14e9c6e8e399153b49ecdcb7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/dbus/cros_dbus_service.h"
#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
#include "base/stl_util.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/chromeos/dbus/display_power_service_provider.h"
#include "chrome/browser/chromeos/dbus/liveness_service_provider.h"
#include "chrome/browser/chromeos/dbus/printer_service_provider.h"
#include "chrome/browser/chromeos/dbus/proxy_resolution_service_provider.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace {
CrosDBusService* g_cros_dbus_service = NULL;
} // namespace
// The CrosDBusService implementation used in production, and unit tests.
class CrosDBusServiceImpl : public CrosDBusService {
public:
explicit CrosDBusServiceImpl(dbus::Bus* bus)
: service_started_(false),
origin_thread_id_(base::PlatformThread::CurrentId()),
bus_(bus) {
}
virtual ~CrosDBusServiceImpl() {
STLDeleteElements(&service_providers_);
}
// Starts the D-Bus service.
void Start() {
// Make sure we're running on the origin thread (i.e. the UI thread in
// production).
DCHECK(OnOriginThread());
// Return if the service has been already started.
if (service_started_)
return;
bus_->RequestOwnership(kLibCrosServiceName,
base::Bind(&CrosDBusServiceImpl::OnOwnership,
base::Unretained(this)));
exported_object_ = bus_->GetExportedObject(
dbus::ObjectPath(kLibCrosServicePath));
for (size_t i = 0; i < service_providers_.size(); ++i)
service_providers_[i]->Start(exported_object_);
service_started_ = true;
VLOG(1) << "CrosDBusServiceImpl started.";
}
// Registers a service provider. This must be done before Start().
// |provider| will be owned by CrosDBusService.
void RegisterServiceProvider(ServiceProviderInterface* provider) {
service_providers_.push_back(provider);
}
private:
// Returns true if the current thread is on the origin thread.
bool OnOriginThread() {
return base::PlatformThread::CurrentId() == origin_thread_id_;
}
// Called when an ownership request is completed.
void OnOwnership(const std::string& service_name,
bool success) {
LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
}
bool service_started_;
base::PlatformThreadId origin_thread_id_;
dbus::Bus* bus_;
scoped_refptr<dbus::ExportedObject> exported_object_;
// Service providers that form CrosDBusService.
std::vector<ServiceProviderInterface*> service_providers_;
};
// The stub CrosDBusService implementation used on Linux desktop,
// which does nothing as of now.
class CrosDBusServiceStubImpl : public CrosDBusService {
public:
CrosDBusServiceStubImpl() {
}
virtual ~CrosDBusServiceStubImpl() {
}
};
// static
void CrosDBusService::Initialize() {
if (g_cros_dbus_service) {
LOG(WARNING) << "CrosDBusService was already initialized";
return;
}
dbus::Bus* bus = DBusThreadManager::Get()->GetSystemBus();
if (base::chromeos::IsRunningOnChromeOS() && bus) {
CrosDBusServiceImpl* service = new CrosDBusServiceImpl(bus);
service->RegisterServiceProvider(ProxyResolutionServiceProvider::Create());
service->RegisterServiceProvider(new DisplayPowerServiceProvider);
service->RegisterServiceProvider(new LivenessServiceProvider);
service->RegisterServiceProvider(new PrinterServiceProvider);
g_cros_dbus_service = service;
service->Start();
} else {
g_cros_dbus_service = new CrosDBusServiceStubImpl;
}
VLOG(1) << "CrosDBusService initialized";
}
// static
void CrosDBusService::InitializeForTesting(
dbus::Bus* bus,
ServiceProviderInterface* proxy_resolution_service) {
if (g_cros_dbus_service) {
LOG(WARNING) << "CrosDBusService was already initialized";
return;
}
CrosDBusServiceImpl* service = new CrosDBusServiceImpl(bus);
service->RegisterServiceProvider(proxy_resolution_service);
service->Start();
g_cros_dbus_service = service;
VLOG(1) << "CrosDBusService initialized";
}
// static
void CrosDBusService::Shutdown() {
delete g_cros_dbus_service;
g_cros_dbus_service = NULL;
VLOG(1) << "CrosDBusService Shutdown completed";
}
CrosDBusService::~CrosDBusService() {
}
CrosDBusService::ServiceProviderInterface::~ServiceProviderInterface() {
}
} // namespace chromeos
|