Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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 "examples/indirect_service/indirect_service_demo.mojom.h" |
| 6 | #include "mojo/public/c/system/main.h" |
Viet-Trung Luu | 1209a82 | 2016-05-27 11:06:08 -0700 | [diff] [blame] | 7 | #include "mojo/public/cpp/application/application_impl_base.h" |
| 8 | #include "mojo/public/cpp/application/run_application.h" |
Viet-Trung Luu | b237bca | 2016-05-13 16:35:11 -0700 | [diff] [blame] | 9 | #include "mojo/public/cpp/application/service_provider_impl.h" |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 10 | #include "mojo/public/cpp/bindings/binding.h" |
James Robinson | 20c55d8 | 2015-02-12 14:33:22 -0800 | [diff] [blame] | 11 | #include "mojo/public/cpp/bindings/strong_binding.h" |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 12 | |
| 13 | namespace mojo { |
| 14 | namespace examples { |
| 15 | |
James Robinson | 20c55d8 | 2015-02-12 14:33:22 -0800 | [diff] [blame] | 16 | class IndirectIntegerServiceImpl : public IndirectIntegerService, |
| 17 | public IntegerService { |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 18 | public: |
James Robinson | 20c55d8 | 2015-02-12 14:33:22 -0800 | [diff] [blame] | 19 | IndirectIntegerServiceImpl(InterfaceRequest<IndirectIntegerService> request) |
| 20 | : binding_(this, request.Pass()) {} |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 21 | |
| 22 | ~IndirectIntegerServiceImpl() override { |
| 23 | for (auto itr = bindings_.begin(); itr < bindings_.end(); itr++) |
| 24 | delete *itr; |
| 25 | } |
| 26 | |
| 27 | // IndirectIntegerService |
| 28 | |
Vardhan Mudunuru | c3575c4 | 2016-02-11 15:08:21 -0800 | [diff] [blame] | 29 | void Set(InterfaceHandle<IntegerService> service) override { |
| 30 | integer_service_ = IntegerServicePtr::Create(std::move(service)); |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void Get(InterfaceRequest<IntegerService> service) override { |
| 34 | bindings_.push_back(new Binding<IntegerService>(this, service.Pass())); |
| 35 | } |
| 36 | |
| 37 | // IntegerService |
| 38 | |
| 39 | void Increment(const Callback<void(int32_t)>& callback) override { |
| 40 | if (integer_service_.get()) |
| 41 | integer_service_->Increment(callback); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | IntegerServicePtr integer_service_; |
| 46 | std::vector<Binding<IntegerService>*> bindings_; |
James Robinson | 20c55d8 | 2015-02-12 14:33:22 -0800 | [diff] [blame] | 47 | StrongBinding<IndirectIntegerService> binding_; |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
Viet-Trung Luu | 1209a82 | 2016-05-27 11:06:08 -0700 | [diff] [blame] | 50 | class IndirectIntegerServiceApp : public ApplicationImplBase { |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 51 | public: |
Viet-Trung Luu | 1209a82 | 2016-05-27 11:06:08 -0700 | [diff] [blame] | 52 | bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override { |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 53 | service_provider_impl->AddService<IndirectIntegerService>( |
Viet-Trung Luu | 66402b5 | 2016-05-13 11:19:54 -0700 | [diff] [blame] | 54 | [](const ConnectionContext& connection_context, |
| 55 | InterfaceRequest<IndirectIntegerService> request) { |
| 56 | new IndirectIntegerServiceImpl(request.Pass()); |
| 57 | }); |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 58 | return true; |
| 59 | } |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace examples |
| 63 | } // namespace mojo |
| 64 | |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 65 | MojoResult MojoMain(MojoHandle application_request) { |
Viet-Trung Luu | 1209a82 | 2016-05-27 11:06:08 -0700 | [diff] [blame] | 66 | mojo::examples::IndirectIntegerServiceApp indirect_integer_service_app; |
Viet-Trung Luu | 911b5de | 2016-05-31 12:19:19 -0700 | [diff] [blame] | 67 | return mojo::RunApplication(application_request, |
| 68 | &indirect_integer_service_app); |
Hans Muller | 3f2b646 | 2014-11-21 09:54:34 -0800 | [diff] [blame] | 69 | } |