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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "examples/hello_mojo/hello_mojo.mojom.h" | 9 #include "examples/hello_mojo/hello_mojo.mojom.h" |
10 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
11 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
13 #include "mojo/public/cpp/application/application_runner.h" | 13 #include "mojo/public/cpp/application/application_runner.h" |
14 #include "mojo/public/cpp/application/interface_factory.h" | |
15 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
16 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
17 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
18 | 17 |
19 using examples::HelloMojo; | 18 using examples::HelloMojo; |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 class HelloMojoImpl : public HelloMojo { | 22 class HelloMojoImpl : public HelloMojo { |
24 public: | 23 public: |
25 explicit HelloMojoImpl(mojo::InterfaceRequest<HelloMojo> hello_mojo_request) | 24 explicit HelloMojoImpl(mojo::InterfaceRequest<HelloMojo> hello_mojo_request) |
26 : strong_binding_(this, std::move(hello_mojo_request)) {} | 25 : strong_binding_(this, std::move(hello_mojo_request)) {} |
27 ~HelloMojoImpl() override {} | 26 ~HelloMojoImpl() override {} |
28 | 27 |
29 // |examples::HelloMojo| implementation: | 28 // |examples::HelloMojo| implementation: |
30 void Say(const mojo::String& request, | 29 void Say(const mojo::String& request, |
31 const mojo::Callback<void(mojo::String)>& callback) override { | 30 const mojo::Callback<void(mojo::String)>& callback) override { |
32 callback.Run((request.get() == "hello") ? "mojo" : "WAT"); | 31 callback.Run((request.get() == "hello") ? "mojo" : "WAT"); |
33 } | 32 } |
34 | 33 |
35 private: | 34 private: |
36 mojo::StrongBinding<HelloMojo> strong_binding_; | 35 mojo::StrongBinding<HelloMojo> strong_binding_; |
37 | 36 |
38 MOJO_DISALLOW_COPY_AND_ASSIGN(HelloMojoImpl); | 37 MOJO_DISALLOW_COPY_AND_ASSIGN(HelloMojoImpl); |
39 }; | 38 }; |
40 | 39 |
41 class HelloMojoServerApp : public mojo::ApplicationDelegate, | 40 class HelloMojoServerApp : public mojo::ApplicationDelegate { |
42 public mojo::InterfaceFactory<HelloMojo> { | |
43 public: | 41 public: |
44 HelloMojoServerApp() {} | 42 HelloMojoServerApp() {} |
45 ~HelloMojoServerApp() override {} | 43 ~HelloMojoServerApp() override {} |
46 | 44 |
47 // |mojo::ApplicationDelegate| implementation: | 45 // |mojo::ApplicationDelegate| implementation: |
48 bool ConfigureIncomingConnection( | 46 bool ConfigureIncomingConnection( |
49 mojo::ApplicationConnection* application_connection) override { | 47 mojo::ApplicationConnection* application_connection) override { |
50 application_connection->AddService<HelloMojo>(this); | 48 application_connection->GetServiceProviderImpl().AddService<HelloMojo>( |
| 49 [](const mojo::ConnectionContext& connection_context, |
| 50 mojo::InterfaceRequest<HelloMojo> hello_mojo_request) { |
| 51 new HelloMojoImpl(std::move(hello_mojo_request)); // Owns itself. |
| 52 }); |
51 return true; | 53 return true; |
52 } | 54 } |
53 | 55 |
54 // |mojo::InterfaceFactory<HelloMojo>| implementation: | |
55 void Create(const mojo::ConnectionContext& connection_context, | |
56 mojo::InterfaceRequest<HelloMojo> hello_mojo_request) override { | |
57 new HelloMojoImpl(std::move(hello_mojo_request)); // Owns itself. | |
58 } | |
59 | |
60 private: | 56 private: |
61 MOJO_DISALLOW_COPY_AND_ASSIGN(HelloMojoServerApp); | 57 MOJO_DISALLOW_COPY_AND_ASSIGN(HelloMojoServerApp); |
62 }; | 58 }; |
63 | 59 |
64 } // namespace | 60 } // namespace |
65 | 61 |
66 MojoResult MojoMain(MojoHandle application_request) { | 62 MojoResult MojoMain(MojoHandle application_request) { |
67 return mojo::ApplicationRunner(std::unique_ptr<mojo::ApplicationDelegate>( | 63 return mojo::ApplicationRunner(std::unique_ptr<mojo::ApplicationDelegate>( |
68 new HelloMojoServerApp())) | 64 new HelloMojoServerApp())) |
69 .Run(application_request); | 65 .Run(application_request); |
70 } | 66 } |
OLD | NEW |