| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/common/service_manager/service_manager_connection_impl.h" | 5 #include "content/common/service_manager/service_manager_connection_impl.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "content/common/service_manager/embedded_service_runner.h" | 18 #include "content/common/service_manager/embedded_service_runner.h" |
| 19 #include "content/public/common/connection_filter.h" | 19 #include "content/public/common/connection_filter.h" |
| 20 #include "mojo/public/cpp/bindings/binding_set.h" | 20 #include "mojo/public/cpp/bindings/binding_set.h" |
| 21 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
| 22 #include "services/service_manager/public/cpp/interface_registry.h" |
| 22 #include "services/service_manager/public/cpp/service.h" | 23 #include "services/service_manager/public/cpp/service.h" |
| 23 #include "services/service_manager/public/cpp/service_context.h" | 24 #include "services/service_manager/public/cpp/service_context.h" |
| 24 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 25 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 25 #include "services/service_manager/runner/common/client_util.h" | 26 #include "services/service_manager/runner/common/client_util.h" |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky | 31 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky |
| 31 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; | 32 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 const service_manager::InterfaceRegistry::Binder& binder) { | 120 const service_manager::InterfaceRegistry::Binder& binder) { |
| 120 DCHECK(!started_); | 121 DCHECK(!started_); |
| 121 default_browser_binder_ = base::Bind( | 122 default_browser_binder_ = base::Bind( |
| 122 &IOThreadContext::CallBinderOnTaskRunner, | 123 &IOThreadContext::CallBinderOnTaskRunner, |
| 123 base::ThreadTaskRunnerHandle::Get(), binder); | 124 base::ThreadTaskRunnerHandle::Get(), binder); |
| 124 } | 125 } |
| 125 | 126 |
| 126 private: | 127 private: |
| 127 friend class base::RefCountedThreadSafe<IOThreadContext>; | 128 friend class base::RefCountedThreadSafe<IOThreadContext>; |
| 128 | 129 |
| 130 // A forwarding service_manager::Service implementation to account for the |
| 131 // fact that IOThreadContext is a Service which owns its ServiceContext, but |
| 132 // ServiceContext should own its Service. |
| 133 // |
| 134 // TODO(rockot): Clean this up. |
| 135 class ForwardingServiceImpl : public service_manager::Service { |
| 136 public: |
| 137 explicit ForwardingServiceImpl(service_manager::Service* service) |
| 138 : service_(service) {} |
| 139 ~ForwardingServiceImpl() override {} |
| 140 |
| 141 // service_manager::Service: |
| 142 void OnStart(service_manager::ServiceContext* context) override { |
| 143 service_->OnStart(context); |
| 144 } |
| 145 |
| 146 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
| 147 service_manager::InterfaceRegistry* registry) override { |
| 148 return service_->OnConnect(remote_info, registry); |
| 149 } |
| 150 |
| 151 bool OnStop() override { return service_->OnStop(); } |
| 152 |
| 153 private: |
| 154 service_manager::Service* const service_; |
| 155 DISALLOW_COPY_AND_ASSIGN(ForwardingServiceImpl); |
| 156 }; |
| 157 |
| 129 class MessageLoopObserver : public base::MessageLoop::DestructionObserver { | 158 class MessageLoopObserver : public base::MessageLoop::DestructionObserver { |
| 130 public: | 159 public: |
| 131 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context) | 160 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context) |
| 132 : context_(context) { | 161 : context_(context) { |
| 133 base::MessageLoop::current()->AddDestructionObserver(this); | 162 base::MessageLoop::current()->AddDestructionObserver(this); |
| 134 } | 163 } |
| 135 | 164 |
| 136 ~MessageLoopObserver() override { | 165 ~MessageLoopObserver() override { |
| 137 base::MessageLoop::current()->RemoveDestructionObserver(this); | 166 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 138 } | 167 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 161 | 190 |
| 162 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); | 191 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); |
| 163 }; | 192 }; |
| 164 | 193 |
| 165 ~IOThreadContext() override {} | 194 ~IOThreadContext() override {} |
| 166 | 195 |
| 167 void StartOnIOThread() { | 196 void StartOnIOThread() { |
| 168 // Should bind |io_thread_checker_| to the context's thread. | 197 // Should bind |io_thread_checker_| to the context's thread. |
| 169 DCHECK(io_thread_checker_.CalledOnValidThread()); | 198 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 170 service_context_.reset(new service_manager::ServiceContext( | 199 service_context_.reset(new service_manager::ServiceContext( |
| 171 this, std::move(pending_service_request_), | 200 base::MakeUnique<ForwardingServiceImpl>(this), |
| 201 std::move(pending_service_request_), |
| 172 std::move(io_thread_connector_), | 202 std::move(io_thread_connector_), |
| 173 std::move(pending_connector_request_))); | 203 std::move(pending_connector_request_))); |
| 174 | 204 |
| 175 // MessageLoopObserver owns itself. | 205 // MessageLoopObserver owns itself. |
| 176 message_loop_observer_ = | 206 message_loop_observer_ = |
| 177 new MessageLoopObserver(weak_factory_.GetWeakPtr()); | 207 new MessageLoopObserver(weak_factory_.GetWeakPtr()); |
| 178 } | 208 } |
| 179 | 209 |
| 180 void ShutDownOnIOThread() { | 210 void ShutDownOnIOThread() { |
| 181 DCHECK(io_thread_checker_.CalledOnValidThread()); | 211 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 } | 244 } |
| 215 | 245 |
| 216 void OnBrowserConnectionLost() { | 246 void OnBrowserConnectionLost() { |
| 217 DCHECK(io_thread_checker_.CalledOnValidThread()); | 247 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 218 has_browser_connection_ = false; | 248 has_browser_connection_ = false; |
| 219 } | 249 } |
| 220 | 250 |
| 221 ///////////////////////////////////////////////////////////////////////////// | 251 ///////////////////////////////////////////////////////////////////////////// |
| 222 // service_manager::Service implementation | 252 // service_manager::Service implementation |
| 223 | 253 |
| 224 void OnStart(const service_manager::ServiceInfo& info) override { | 254 void OnStart(service_manager::ServiceContext* context) override { |
| 225 DCHECK(io_thread_checker_.CalledOnValidThread()); | 255 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 226 DCHECK(!initialize_handler_.is_null()); | 256 DCHECK(!initialize_handler_.is_null()); |
| 227 local_info_ = info; | 257 local_info_ = context->local_info(); |
| 228 | 258 |
| 229 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); | 259 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); |
| 230 callback_task_runner_->PostTask(FROM_HERE, | 260 callback_task_runner_->PostTask(FROM_HERE, |
| 231 base::Bind(handler, local_info_.identity)); | 261 base::Bind(handler, local_info_.identity)); |
| 232 } | 262 } |
| 233 | 263 |
| 234 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 264 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
| 235 service_manager::InterfaceRegistry* registry) override { | 265 service_manager::InterfaceRegistry* registry) override { |
| 236 DCHECK(io_thread_checker_.CalledOnValidThread()); | 266 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 237 | 267 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 | 566 |
| 537 void ServiceManagerConnectionImpl::GetInterface( | 567 void ServiceManagerConnectionImpl::GetInterface( |
| 538 service_manager::mojom::InterfaceProvider* provider, | 568 service_manager::mojom::InterfaceProvider* provider, |
| 539 const std::string& interface_name, | 569 const std::string& interface_name, |
| 540 mojo::ScopedMessagePipeHandle request_handle) { | 570 mojo::ScopedMessagePipeHandle request_handle) { |
| 541 provider->GetInterface(interface_name, std::move(request_handle)); | 571 provider->GetInterface(interface_name, std::move(request_handle)); |
| 542 } | 572 } |
| 543 | 573 |
| 544 } // namespace content | 574 } // namespace content |
| 545 | 575 |
| OLD | NEW |