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 "services/data_decoder/data_decoder_service.h" | 5 #include "services/data_decoder/data_decoder_service.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 #include "services/data_decoder/image_decoder_impl.h" | 12 #include "services/data_decoder/image_decoder_impl.h" |
13 #include "services/data_decoder/public/interfaces/image_decoder.mojom.h" | 13 #include "services/data_decoder/public/interfaces/image_decoder.mojom.h" |
14 #include "services/service_manager/public/cpp/interface_registry.h" | |
15 #include "services/service_manager/public/cpp/service_context.h" | 14 #include "services/service_manager/public/cpp/service_context.h" |
16 | 15 |
17 namespace data_decoder { | 16 namespace data_decoder { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 void OnConnectionLost(std::unique_ptr<service_manager::ServiceContextRef> ref) { | |
22 // No-op. This merely takes ownership of |ref| so it can be destroyed when | |
23 // this function is invoked. | |
24 } | |
25 | |
26 void OnImageDecoderRequest( | 20 void OnImageDecoderRequest( |
27 service_manager::ServiceContextRefFactory* ref_factory, | 21 service_manager::ServiceContextRefFactory* ref_factory, |
28 mojom::ImageDecoderRequest request) { | 22 mojom::ImageDecoderRequest request) { |
29 mojo::MakeStrongBinding( | 23 mojo::MakeStrongBinding( |
30 base::MakeUnique<ImageDecoderImpl>(ref_factory->CreateRef()), | 24 base::MakeUnique<ImageDecoderImpl>(ref_factory->CreateRef()), |
31 std::move(request)); | 25 std::move(request)); |
32 } | 26 } |
33 | 27 |
34 } // namespace | 28 } // namespace |
35 | 29 |
36 DataDecoderService::DataDecoderService() : weak_factory_(this) {} | 30 DataDecoderService::DataDecoderService() : weak_factory_(this) {} |
37 | 31 |
38 DataDecoderService::~DataDecoderService() = default; | 32 DataDecoderService::~DataDecoderService() = default; |
39 | 33 |
40 // static | 34 // static |
41 std::unique_ptr<service_manager::Service> DataDecoderService::Create() { | 35 std::unique_ptr<service_manager::Service> DataDecoderService::Create() { |
42 return base::MakeUnique<DataDecoderService>(); | 36 return base::MakeUnique<DataDecoderService>(); |
43 } | 37 } |
44 | 38 |
45 void DataDecoderService::OnStart() { | 39 void DataDecoderService::OnStart() { |
46 ref_factory_.reset(new service_manager::ServiceContextRefFactory(base::Bind( | 40 ref_factory_.reset(new service_manager::ServiceContextRefFactory(base::Bind( |
47 &DataDecoderService::MaybeRequestQuitDelayed, base::Unretained(this)))); | 41 &DataDecoderService::MaybeRequestQuitDelayed, base::Unretained(this)))); |
| 42 registry_.AddInterface( |
| 43 base::Bind(&OnImageDecoderRequest, ref_factory_.get())); |
48 } | 44 } |
49 | 45 |
50 bool DataDecoderService::OnConnect( | 46 void DataDecoderService::OnBindInterface( |
51 const service_manager::ServiceInfo& remote_info, | 47 const service_manager::ServiceInfo& source_info, |
52 service_manager::InterfaceRegistry* registry) { | 48 const std::string& interface_name, |
53 // Add a reference to the service and tie it to the lifetime of the | 49 mojo::ScopedMessagePipeHandle interface_pipe) { |
54 // InterfaceRegistry's connection. | 50 registry_.BindInterface(source_info.identity, interface_name, |
55 std::unique_ptr<service_manager::ServiceContextRef> connection_ref = | 51 std::move(interface_pipe)); |
56 ref_factory_->CreateRef(); | |
57 registry->AddConnectionLostClosure( | |
58 base::Bind(&OnConnectionLost, base::Passed(&connection_ref))); | |
59 registry->AddInterface( | |
60 base::Bind(&OnImageDecoderRequest, ref_factory_.get())); | |
61 return true; | |
62 } | 52 } |
63 | 53 |
64 void DataDecoderService::MaybeRequestQuitDelayed() { | 54 void DataDecoderService::MaybeRequestQuitDelayed() { |
65 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 55 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
66 FROM_HERE, | 56 FROM_HERE, |
67 base::Bind(&DataDecoderService::MaybeRequestQuit, | 57 base::Bind(&DataDecoderService::MaybeRequestQuit, |
68 weak_factory_.GetWeakPtr()), | 58 weak_factory_.GetWeakPtr()), |
69 base::TimeDelta::FromSeconds(5)); | 59 base::TimeDelta::FromSeconds(5)); |
70 } | 60 } |
71 | 61 |
72 void DataDecoderService::MaybeRequestQuit() { | 62 void DataDecoderService::MaybeRequestQuit() { |
73 DCHECK(ref_factory_); | 63 DCHECK(ref_factory_); |
74 if (ref_factory_->HasNoRefs()) | 64 if (ref_factory_->HasNoRefs()) |
75 context()->RequestQuit(); | 65 context()->RequestQuit(); |
76 } | 66 } |
77 | 67 |
78 } // namespace data_decoder | 68 } // namespace data_decoder |
OLD | NEW |