Chromium Code Reviews| Index: services/video_capture/service_manager_service_impl.cc |
| diff --git a/services/video_capture/service_manager_service_impl.cc b/services/video_capture/service_manager_service_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be8c2e414e341da774b312e43a2a185c2c8e4af6 |
| --- /dev/null |
| +++ b/services/video_capture/service_manager_service_impl.cc |
| @@ -0,0 +1,83 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
mcasas
2017/04/26 22:12:31
s/2016/2017/
chfremer
2017/04/26 23:33:36
No longer needed in PatchSet 2, because file name
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "services/video_capture/service_manager_service_impl.h" |
| + |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "services/service_manager/public/cpp/interface_registry.h" |
| +#include "services/service_manager/public/cpp/service_context.h" |
| +#include "services/video_capture/public/interfaces/constants.mojom.h" |
| +#include "services/video_capture/video_capture_service_impl.h" |
| + |
| +namespace video_capture { |
| + |
| +ServiceManagerServiceImpl::ServiceManagerServiceImpl() |
| + : shutdown_delay_in_seconds_(mojom::kDefaultShutdownDelayInSeconds), |
| + weak_factory_(this) {} |
| + |
| +ServiceManagerServiceImpl::~ServiceManagerServiceImpl() = default; |
| + |
| +void ServiceManagerServiceImpl::OnStart() { |
| + DVLOG(3) << "Service Starting"; |
| + ref_factory_ = base::MakeUnique<service_manager::ServiceContextRefFactory>( |
| + base::Bind(&ServiceManagerServiceImpl::MaybeRequestQuitDelayed, |
| + base::Unretained(this))); |
| + registry_.AddInterface<mojom::Service>( |
| + // Unretained |this| is safe because |registry_| is owned by |this|. |
| + base::Bind(&ServiceManagerServiceImpl::OnVideoCaptureServiceRequest, |
| + base::Unretained(this))); |
| +} |
| + |
| +void ServiceManagerServiceImpl::OnBindInterface( |
| + const service_manager::ServiceInfo& source_info, |
| + const std::string& interface_name, |
| + mojo::ScopedMessagePipeHandle interface_pipe) { |
| + DVLOG(3) << "Service Connecting"; |
| + registry_.BindInterface(source_info.identity, interface_name, |
| + std::move(interface_pipe)); |
| +} |
| + |
| +bool ServiceManagerServiceImpl::OnServiceManagerConnectionLost() { |
| + DVLOG(3) << "Service Stopping"; |
| + return true; |
| +} |
| + |
| +void ServiceManagerServiceImpl::OnVideoCaptureServiceRequest( |
| + mojom::ServiceRequest request) { |
| + mojo::MakeStrongBinding( |
| + base::MakeUnique<VideoCaptureServiceImpl>( |
| + ref_factory_->CreateRef(), |
| + // Use of unretained |this| is safe, because the |
| + // VideoCaptureServiceImpl has shared ownership of |this| via the |
| + // reference created by ref_factory->CreateRef(). |
| + base::Bind(&ServiceManagerServiceImpl::SetShutdownDelayInSeconds, |
| + base::Unretained(this))), |
| + std::move(request)); |
| +} |
| + |
| +void ServiceManagerServiceImpl::SetShutdownDelayInSeconds(float seconds) { |
| + DVLOG(3) << "Shutdown delay set to " << seconds << " seconds."; |
| + shutdown_delay_in_seconds_ = seconds; |
| +} |
| + |
| +void ServiceManagerServiceImpl::MaybeRequestQuitDelayed() { |
| + DVLOG(3) << "Scheduling service to quit"; |
| + base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&ServiceManagerServiceImpl::MaybeRequestQuit, |
| + weak_factory_.GetWeakPtr()), |
| + base::TimeDelta::FromSeconds(shutdown_delay_in_seconds_)); |
| +} |
| + |
| +void ServiceManagerServiceImpl::MaybeRequestQuit() { |
| + DCHECK(ref_factory_); |
| + if (ref_factory_->HasNoRefs()) { |
| + DVLOG(3) << "Service gonna quit now"; |
| + context()->RequestQuit(); |
| + } else { |
| + DVLOG(3) << "Service no longer quitting"; |
| + } |
| +} |
| + |
| +} // namespace video_capture |