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

Side by Side Diff: chromeos/dbus/services/cros_dbus_service.cc

Issue 2693243002: chromeos: Make CrosDBusService not be a singleton. (Closed)
Patch Set: whoops, fix reversed logic in IsUsingFakes check Created 3 years, 10 months 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
OLDNEW
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/services/cros_dbus_service.h" 5 #include "chromeos/dbus/services/cros_dbus_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "base/sys_info.h" 13 #include "base/sys_info.h"
13 #include "chromeos/dbus/dbus_thread_manager.h" 14 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "dbus/bus.h" 15 #include "dbus/bus.h"
15 #include "dbus/exported_object.h" 16 #include "dbus/exported_object.h"
16 #include "dbus/object_path.h" 17 #include "dbus/object_path.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
18 18
19 namespace chromeos { 19 namespace chromeos {
20 20
21 namespace {
22
23 CrosDBusService* g_cros_dbus_service = NULL;
24
25 } // namespace
26
27 // The CrosDBusService implementation used in production, and unit tests. 21 // The CrosDBusService implementation used in production, and unit tests.
28 class CrosDBusServiceImpl : public CrosDBusService { 22 class CrosDBusServiceImpl : public CrosDBusService {
29 public: 23 public:
30 CrosDBusServiceImpl(dbus::Bus* bus, ServiceProviderList service_providers) 24 CrosDBusServiceImpl(dbus::Bus* bus,
25 const std::string& service_name,
26 const dbus::ObjectPath& object_path,
27 ServiceProviderList service_providers)
31 : service_started_(false), 28 : service_started_(false),
32 origin_thread_id_(base::PlatformThread::CurrentId()), 29 origin_thread_id_(base::PlatformThread::CurrentId()),
33 bus_(bus), 30 bus_(bus),
34 service_providers_(std::move(service_providers)) {} 31 service_name_(service_name),
32 object_path_(object_path),
33 service_providers_(std::move(service_providers)) {
34 DCHECK(bus);
35 DCHECK(!service_name_.empty());
36 DCHECK(object_path_.IsValid());
37 }
35 38
36 ~CrosDBusServiceImpl() override { 39 ~CrosDBusServiceImpl() override = default;
37 }
38 40
39 // Starts the D-Bus service. 41 // Starts the D-Bus service.
40 void Start() { 42 void Start() {
41 // Make sure we're running on the origin thread (i.e. the UI thread in 43 // Make sure we're running on the origin thread (i.e. the UI thread in
42 // production). 44 // production).
43 DCHECK(OnOriginThread()); 45 DCHECK(OnOriginThread());
44 46 DCHECK(!service_started_);
45 // Return if the service has been already started.
46 if (service_started_)
47 return;
48 47
49 // There are some situations, described in http://crbug.com/234382#c27, 48 // There are some situations, described in http://crbug.com/234382#c27,
50 // where processes on Linux can wind up stuck in an uninterruptible state 49 // where processes on Linux can wind up stuck in an uninterruptible state
51 // for tens of seconds. If this happens when Chrome is trying to exit, 50 // for tens of seconds. If this happens when Chrome is trying to exit, this
52 // this unkillable process can wind up clinging to ownership of 51 // unkillable process can wind up clinging to ownership of |service_name_|
53 // kLibCrosServiceName while the system is trying to restart the browser. 52 // while the system is trying to restart the browser. This leads to a fatal
54 // This leads to a fatal situation if we don't allow the new browser 53 // situation if we don't allow the new browser instance to replace the old
55 // instance to replace the old as the owner of kLibCrosServiceName as seen 54 // as the owner of |service_name_| as seen in http://crbug.com/234382.
56 // in http://crbug.com/234382. Hence, REQUIRE_PRIMARY_ALLOW_REPLACEMENT. 55 // Hence, REQUIRE_PRIMARY_ALLOW_REPLACEMENT.
57 bus_->RequestOwnership(kLibCrosServiceName, 56 bus_->RequestOwnership(
58 dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT, 57 service_name_, dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT,
59 base::Bind(&CrosDBusServiceImpl::OnOwnership, 58 base::Bind(&CrosDBusServiceImpl::OnOwnership, base::Unretained(this)));
60 base::Unretained(this)));
61 59
62 exported_object_ = bus_->GetExportedObject( 60 exported_object_ = bus_->GetExportedObject(object_path_);
63 dbus::ObjectPath(kLibCrosServicePath));
64
65 for (size_t i = 0; i < service_providers_.size(); ++i) 61 for (size_t i = 0; i < service_providers_.size(); ++i)
66 service_providers_[i]->Start(exported_object_); 62 service_providers_[i]->Start(exported_object_);
67 63
68 service_started_ = true; 64 service_started_ = true;
69
70 VLOG(1) << "CrosDBusServiceImpl started.";
71 } 65 }
72 66
73 private: 67 private:
74 // Returns true if the current thread is on the origin thread. 68 // Returns true if the current thread is on the origin thread.
75 bool OnOriginThread() { 69 bool OnOriginThread() {
76 return base::PlatformThread::CurrentId() == origin_thread_id_; 70 return base::PlatformThread::CurrentId() == origin_thread_id_;
77 } 71 }
78 72
79 // Called when an ownership request is completed. 73 // Called when an ownership request is completed.
80 void OnOwnership(const std::string& service_name, 74 void OnOwnership(const std::string& service_name,
81 bool success) { 75 bool success) {
82 LOG_IF(FATAL, !success) << "Failed to own: " << service_name; 76 LOG_IF(FATAL, !success) << "Failed to own: " << service_name;
83 } 77 }
84 78
85 bool service_started_; 79 bool service_started_;
86 base::PlatformThreadId origin_thread_id_; 80 base::PlatformThreadId origin_thread_id_;
87 dbus::Bus* bus_; 81 dbus::Bus* bus_;
82 std::string service_name_;
83 dbus::ObjectPath object_path_;
88 scoped_refptr<dbus::ExportedObject> exported_object_; 84 scoped_refptr<dbus::ExportedObject> exported_object_;
89 85
90 // Service providers that form CrosDBusService. 86 // Service providers that form CrosDBusService.
91 ServiceProviderList service_providers_; 87 ServiceProviderList service_providers_;
88
89 DISALLOW_COPY_AND_ASSIGN(CrosDBusServiceImpl);
92 }; 90 };
93 91
94 // The stub CrosDBusService implementation used on Linux desktop, 92 // The stub CrosDBusService implementation used on Linux desktop,
95 // which does nothing as of now. 93 // which does nothing as of now.
96 class CrosDBusServiceStubImpl : public CrosDBusService { 94 class CrosDBusServiceStubImpl : public CrosDBusService {
97 public: 95 public:
98 CrosDBusServiceStubImpl() { 96 CrosDBusServiceStubImpl() = default;
99 } 97 ~CrosDBusServiceStubImpl() override = default;
100 98
101 ~CrosDBusServiceStubImpl() override {} 99 private:
100 DISALLOW_COPY_AND_ASSIGN(CrosDBusServiceStubImpl);
102 }; 101 };
103 102
104 // static 103 // static
105 void CrosDBusService::Initialize(ServiceProviderList service_providers) { 104 std::unique_ptr<CrosDBusService> CrosDBusService::Create(
106 if (g_cros_dbus_service) { 105 const std::string& service_name,
107 LOG(WARNING) << "CrosDBusService was already initialized"; 106 const dbus::ObjectPath& object_path,
108 return; 107 ServiceProviderList service_providers) {
109 } 108 if (DBusThreadManager::Get()->IsUsingFakes())
110 dbus::Bus* bus = DBusThreadManager::Get()->GetSystemBus(); 109 return base::MakeUnique<CrosDBusServiceStubImpl>();
111 if (base::SysInfo::IsRunningOnChromeOS() && bus) { 110
112 auto* service = new CrosDBusServiceImpl(bus, std::move(service_providers)); 111 return CreateRealImpl(DBusThreadManager::Get()->GetSystemBus(), service_name,
113 g_cros_dbus_service = service; 112 object_path, std::move(service_providers));
114 service->Start();
115 } else {
116 g_cros_dbus_service = new CrosDBusServiceStubImpl;
117 }
118 VLOG(1) << "CrosDBusService initialized";
119 } 113 }
120 114
121 // static 115 // static
122 void CrosDBusService::InitializeForTesting( 116 std::unique_ptr<CrosDBusService> CrosDBusService::CreateRealImpl(
123 dbus::Bus* bus, 117 dbus::Bus* bus,
118 const std::string& service_name,
119 const dbus::ObjectPath& object_path,
124 ServiceProviderList service_providers) { 120 ServiceProviderList service_providers) {
125 if (g_cros_dbus_service) { 121 auto service = base::MakeUnique<CrosDBusServiceImpl>(
126 LOG(WARNING) << "CrosDBusService was already initialized"; 122 bus, service_name, object_path, std::move(service_providers));
127 return;
128 }
129 auto* service = new CrosDBusServiceImpl(bus, std::move(service_providers));
130 service->Start(); 123 service->Start();
131 g_cros_dbus_service = service; 124 return std::move(service);
132 VLOG(1) << "CrosDBusService initialized";
133 } 125 }
134 126
135 // static 127 CrosDBusService::~CrosDBusService() = default;
136 void CrosDBusService::Shutdown() {
137 delete g_cros_dbus_service;
138 g_cros_dbus_service = NULL;
139 VLOG(1) << "CrosDBusService Shutdown completed";
140 }
141 128
142 CrosDBusService::~CrosDBusService() { 129 CrosDBusService::CrosDBusService() = default;
143 }
144 130
145 CrosDBusService::ServiceProviderInterface::~ServiceProviderInterface() { 131 CrosDBusService::ServiceProviderInterface::~ServiceProviderInterface() =
146 } 132 default;
147 133
148 } // namespace chromeos 134 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/services/cros_dbus_service.h ('k') | chromeos/dbus/services/cros_dbus_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698