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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/public/cpp/bindings/binding_set.h" | 9 #include "mojo/public/cpp/bindings/binding_set.h" |
10 #include "services/service_manager/public/c/main.h" | 10 #include "services/service_manager/public/c/main.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 public InterfaceFactory<test::mojom::ConnectTestService>, | 26 public InterfaceFactory<test::mojom::ConnectTestService>, |
27 public InterfaceFactory<test::mojom::ClassInterface>, | 27 public InterfaceFactory<test::mojom::ClassInterface>, |
28 public test::mojom::ConnectTestService, | 28 public test::mojom::ConnectTestService, |
29 public test::mojom::ClassInterface { | 29 public test::mojom::ClassInterface { |
30 public: | 30 public: |
31 ConnectTestClassApp() {} | 31 ConnectTestClassApp() {} |
32 ~ConnectTestClassApp() override {} | 32 ~ConnectTestClassApp() override {} |
33 | 33 |
34 private: | 34 private: |
35 // service_manager::Service: | 35 // service_manager::Service: |
| 36 void OnStart(ServiceContext* context) override { |
| 37 context_ = context; |
| 38 } |
| 39 |
36 bool OnConnect(const ServiceInfo& remote_info, | 40 bool OnConnect(const ServiceInfo& remote_info, |
37 InterfaceRegistry* registry) override { | 41 InterfaceRegistry* registry) override { |
38 registry->AddInterface<test::mojom::ConnectTestService>(this); | 42 registry->AddInterface<test::mojom::ConnectTestService>(this); |
39 registry->AddInterface<test::mojom::ClassInterface>(this); | 43 registry->AddInterface<test::mojom::ClassInterface>(this); |
40 inbound_connections_.insert(registry); | 44 inbound_connections_.insert(registry); |
41 registry->AddConnectionLostClosure( | 45 registry->AddConnectionLostClosure( |
42 base::Bind(&ConnectTestClassApp::OnConnectionError, | 46 base::Bind(&ConnectTestClassApp::OnConnectionError, |
43 base::Unretained(this), registry)); | 47 base::Unretained(this), registry)); |
44 return true; | 48 return true; |
45 } | 49 } |
46 | 50 |
47 // InterfaceFactory<test::mojom::ConnectTestService>: | 51 // InterfaceFactory<test::mojom::ConnectTestService>: |
48 void Create(const Identity& remote_identity, | 52 void Create(const Identity& remote_identity, |
49 test::mojom::ConnectTestServiceRequest request) override { | 53 test::mojom::ConnectTestServiceRequest request) override { |
50 bindings_.AddBinding(this, std::move(request)); | 54 bindings_.AddBinding(this, std::move(request)); |
51 } | 55 } |
52 | 56 |
53 // InterfaceFactory<test::mojom::ClassInterface>: | 57 // InterfaceFactory<test::mojom::ClassInterface>: |
54 void Create(const Identity& remote_identity, | 58 void Create(const Identity& remote_identity, |
55 test::mojom::ClassInterfaceRequest request) override { | 59 test::mojom::ClassInterfaceRequest request) override { |
56 class_interface_bindings_.AddBinding(this, std::move(request)); | 60 class_interface_bindings_.AddBinding(this, std::move(request)); |
57 } | 61 } |
58 | 62 |
59 // test::mojom::ConnectTestService: | 63 // test::mojom::ConnectTestService: |
60 void GetTitle(const GetTitleCallback& callback) override { | 64 void GetTitle(const GetTitleCallback& callback) override { |
61 callback.Run("CLASS APP"); | 65 callback.Run("CLASS APP"); |
62 } | 66 } |
63 void GetInstance(const GetInstanceCallback& callback) override { | 67 void GetInstance(const GetInstanceCallback& callback) override { |
64 callback.Run(context()->identity().instance()); | 68 callback.Run(context_->identity().instance()); |
65 } | 69 } |
66 | 70 |
67 // test::mojom::ClassInterface: | 71 // test::mojom::ClassInterface: |
68 void Ping(const PingCallback& callback) override { | 72 void Ping(const PingCallback& callback) override { |
69 callback.Run("PONG"); | 73 callback.Run("PONG"); |
70 } | 74 } |
71 | 75 |
72 void OnConnectionError(InterfaceRegistry* registry) { | 76 void OnConnectionError(InterfaceRegistry* registry) { |
73 auto it = inbound_connections_.find(registry); | 77 auto it = inbound_connections_.find(registry); |
74 DCHECK(it != inbound_connections_.end()); | 78 DCHECK(it != inbound_connections_.end()); |
75 inbound_connections_.erase(it); | 79 inbound_connections_.erase(it); |
76 if (inbound_connections_.empty()) | 80 if (inbound_connections_.empty()) |
77 context()->QuitNow(); | 81 context_->QuitNow(); |
78 } | 82 } |
79 | 83 |
| 84 ServiceContext* context_ = nullptr; |
80 std::set<InterfaceRegistry*> inbound_connections_; | 85 std::set<InterfaceRegistry*> inbound_connections_; |
81 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 86 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |
82 mojo::BindingSet<test::mojom::ClassInterface> class_interface_bindings_; | 87 mojo::BindingSet<test::mojom::ClassInterface> class_interface_bindings_; |
83 | 88 |
84 DISALLOW_COPY_AND_ASSIGN(ConnectTestClassApp); | 89 DISALLOW_COPY_AND_ASSIGN(ConnectTestClassApp); |
85 }; | 90 }; |
86 | 91 |
87 } // namespace service_manager | 92 } // namespace service_manager |
88 | 93 |
89 MojoResult ServiceMain(MojoHandle service_request_handle) { | 94 MojoResult ServiceMain(MojoHandle service_request_handle) { |
90 service_manager::ServiceRunner runner( | 95 service_manager::ServiceRunner runner( |
91 new service_manager::ConnectTestClassApp); | 96 new service_manager::ConnectTestClassApp); |
92 return runner.Run(service_request_handle); | 97 return runner.Run(service_request_handle); |
93 } | 98 } |
OLD | NEW |