Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / dbus / cros_dbus_service.cc
blobb9a4d83f20af883135e749f7610cc9ccb6046383
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"
7 #include "base/bind.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"
13 #include "dbus/bus.h"
14 #include "dbus/exported_object.h"
15 #include "dbus/object_path.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
18 namespace chromeos {
20 namespace {
22 CrosDBusService* g_cros_dbus_service = NULL;
24 } // namespace
26 // The CrosDBusService implementation used in production, and unit tests.
27 class CrosDBusServiceImpl : public CrosDBusService {
28 public:
29 explicit CrosDBusServiceImpl(dbus::Bus* bus)
30 : service_started_(false),
31 origin_thread_id_(base::PlatformThread::CurrentId()),
32 bus_(bus) {
35 virtual ~CrosDBusServiceImpl() {
36 STLDeleteElements(&service_providers_);
39 // Starts the D-Bus service.
40 void Start() {
41 // Make sure we're running on the origin thread (i.e. the UI thread in
42 // production).
43 DCHECK(OnOriginThread());
45 // Return if the service has been already started.
46 if (service_started_)
47 return;
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);
70 private:
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,
78 bool success) {
79 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
82 bool service_started_;
83 base::PlatformThreadId origin_thread_id_;
84 dbus::Bus* bus_;
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 {
94 public:
95 CrosDBusServiceStubImpl() {
98 virtual ~CrosDBusServiceStubImpl() {
102 // static
103 void CrosDBusService::Initialize() {
104 if (g_cros_dbus_service) {
105 LOG(WARNING) << "CrosDBusService was already initialized";
106 return;
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;
113 service->Start();
114 } else {
115 g_cros_dbus_service = new CrosDBusServiceStubImpl;
117 VLOG(1) << "CrosDBusService initialized";
120 // static
121 void CrosDBusService::InitializeForTesting(
122 dbus::Bus* bus,
123 ServiceProviderInterface* proxy_resolution_service) {
124 if (g_cros_dbus_service) {
125 LOG(WARNING) << "CrosDBusService was already initialized";
126 return;
128 CrosDBusServiceImpl* service = new CrosDBusServiceImpl(bus);
129 service->RegisterServiceProvider(proxy_resolution_service);
130 service->Start();
131 g_cros_dbus_service = service;
132 VLOG(1) << "CrosDBusService initialized";
135 // static
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