blob: dbc354b00d69c15df108dd67c098e19dd0720672 [file] [log] [blame]
Hans Muller3f2b6462014-11-21 09:54:34 -08001// 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 Luu1209a822016-05-27 11:06:08 -07007#include "mojo/public/cpp/application/application_impl_base.h"
8#include "mojo/public/cpp/application/run_application.h"
Viet-Trung Luub237bca2016-05-13 16:35:11 -07009#include "mojo/public/cpp/application/service_provider_impl.h"
Hans Muller3f2b6462014-11-21 09:54:34 -080010#include "mojo/public/cpp/bindings/binding.h"
James Robinson20c55d82015-02-12 14:33:22 -080011#include "mojo/public/cpp/bindings/strong_binding.h"
Hans Muller3f2b6462014-11-21 09:54:34 -080012
13namespace mojo {
14namespace examples {
15
James Robinson20c55d82015-02-12 14:33:22 -080016class IndirectIntegerServiceImpl : public IndirectIntegerService,
17 public IntegerService {
Hans Muller3f2b6462014-11-21 09:54:34 -080018 public:
James Robinson20c55d82015-02-12 14:33:22 -080019 IndirectIntegerServiceImpl(InterfaceRequest<IndirectIntegerService> request)
20 : binding_(this, request.Pass()) {}
Hans Muller3f2b6462014-11-21 09:54:34 -080021
22 ~IndirectIntegerServiceImpl() override {
23 for (auto itr = bindings_.begin(); itr < bindings_.end(); itr++)
24 delete *itr;
25 }
26
27 // IndirectIntegerService
28
Vardhan Mudunuruc3575c42016-02-11 15:08:21 -080029 void Set(InterfaceHandle<IntegerService> service) override {
30 integer_service_ = IntegerServicePtr::Create(std::move(service));
Hans Muller3f2b6462014-11-21 09:54:34 -080031 }
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
44private:
45 IntegerServicePtr integer_service_;
46 std::vector<Binding<IntegerService>*> bindings_;
James Robinson20c55d82015-02-12 14:33:22 -080047 StrongBinding<IndirectIntegerService> binding_;
Hans Muller3f2b6462014-11-21 09:54:34 -080048};
49
Viet-Trung Luu1209a822016-05-27 11:06:08 -070050class IndirectIntegerServiceApp : public ApplicationImplBase {
Hans Muller3f2b6462014-11-21 09:54:34 -080051 public:
Viet-Trung Luu1209a822016-05-27 11:06:08 -070052 bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override {
Viet-Trung Luu22e78b32016-05-13 15:27:15 -070053 service_provider_impl->AddService<IndirectIntegerService>(
Viet-Trung Luu66402b52016-05-13 11:19:54 -070054 [](const ConnectionContext& connection_context,
55 InterfaceRequest<IndirectIntegerService> request) {
56 new IndirectIntegerServiceImpl(request.Pass());
57 });
Hans Muller3f2b6462014-11-21 09:54:34 -080058 return true;
59 }
Hans Muller3f2b6462014-11-21 09:54:34 -080060};
61
62} // namespace examples
63} // namespace mojo
64
Mitch Rudominer963aa772015-04-03 08:22:46 -070065MojoResult MojoMain(MojoHandle application_request) {
Viet-Trung Luu1209a822016-05-27 11:06:08 -070066 mojo::examples::IndirectIntegerServiceApp indirect_integer_service_app;
Viet-Trung Luu911b5de2016-05-31 12:19:19 -070067 return mojo::RunApplication(application_request,
68 &indirect_integer_service_app);
Hans Muller3f2b6462014-11-21 09:54:34 -080069}