OLD | NEW |
---|---|
(Empty) | |
1 // 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
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/video_capture/service_manager_service_impl.h" | |
6 | |
7 #include "mojo/public/cpp/bindings/strong_binding.h" | |
8 #include "services/service_manager/public/cpp/interface_registry.h" | |
9 #include "services/service_manager/public/cpp/service_context.h" | |
10 #include "services/video_capture/public/interfaces/constants.mojom.h" | |
11 #include "services/video_capture/video_capture_service_impl.h" | |
12 | |
13 namespace video_capture { | |
14 | |
15 ServiceManagerServiceImpl::ServiceManagerServiceImpl() | |
16 : shutdown_delay_in_seconds_(mojom::kDefaultShutdownDelayInSeconds), | |
17 weak_factory_(this) {} | |
18 | |
19 ServiceManagerServiceImpl::~ServiceManagerServiceImpl() = default; | |
20 | |
21 void ServiceManagerServiceImpl::OnStart() { | |
22 DVLOG(3) << "Service Starting"; | |
23 ref_factory_ = base::MakeUnique<service_manager::ServiceContextRefFactory>( | |
24 base::Bind(&ServiceManagerServiceImpl::MaybeRequestQuitDelayed, | |
25 base::Unretained(this))); | |
26 registry_.AddInterface<mojom::Service>( | |
27 // Unretained |this| is safe because |registry_| is owned by |this|. | |
28 base::Bind(&ServiceManagerServiceImpl::OnVideoCaptureServiceRequest, | |
29 base::Unretained(this))); | |
30 } | |
31 | |
32 void ServiceManagerServiceImpl::OnBindInterface( | |
33 const service_manager::ServiceInfo& source_info, | |
34 const std::string& interface_name, | |
35 mojo::ScopedMessagePipeHandle interface_pipe) { | |
36 DVLOG(3) << "Service Connecting"; | |
37 registry_.BindInterface(source_info.identity, interface_name, | |
38 std::move(interface_pipe)); | |
39 } | |
40 | |
41 bool ServiceManagerServiceImpl::OnServiceManagerConnectionLost() { | |
42 DVLOG(3) << "Service Stopping"; | |
43 return true; | |
44 } | |
45 | |
46 void ServiceManagerServiceImpl::OnVideoCaptureServiceRequest( | |
47 mojom::ServiceRequest request) { | |
48 mojo::MakeStrongBinding( | |
49 base::MakeUnique<VideoCaptureServiceImpl>( | |
50 ref_factory_->CreateRef(), | |
51 // Use of unretained |this| is safe, because the | |
52 // VideoCaptureServiceImpl has shared ownership of |this| via the | |
53 // reference created by ref_factory->CreateRef(). | |
54 base::Bind(&ServiceManagerServiceImpl::SetShutdownDelayInSeconds, | |
55 base::Unretained(this))), | |
56 std::move(request)); | |
57 } | |
58 | |
59 void ServiceManagerServiceImpl::SetShutdownDelayInSeconds(float seconds) { | |
60 DVLOG(3) << "Shutdown delay set to " << seconds << " seconds."; | |
61 shutdown_delay_in_seconds_ = seconds; | |
62 } | |
63 | |
64 void ServiceManagerServiceImpl::MaybeRequestQuitDelayed() { | |
65 DVLOG(3) << "Scheduling service to quit"; | |
66 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
67 FROM_HERE, | |
68 base::Bind(&ServiceManagerServiceImpl::MaybeRequestQuit, | |
69 weak_factory_.GetWeakPtr()), | |
70 base::TimeDelta::FromSeconds(shutdown_delay_in_seconds_)); | |
71 } | |
72 | |
73 void ServiceManagerServiceImpl::MaybeRequestQuit() { | |
74 DCHECK(ref_factory_); | |
75 if (ref_factory_->HasNoRefs()) { | |
76 DVLOG(3) << "Service gonna quit now"; | |
77 context()->RequestQuit(); | |
78 } else { | |
79 DVLOG(3) << "Service no longer quitting"; | |
80 } | |
81 } | |
82 | |
83 } // namespace video_capture | |
OLD | NEW |