Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 1 | // Copyright 2015 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 "mojo/public/c/system/main.h" |
| 6 | #include "mojo/public/cpp/application/application_delegate.h" |
| 7 | #include "mojo/public/cpp/application/application_impl.h" |
| 8 | #include "mojo/public/cpp/application/application_runner.h" |
| 9 | #include "mojo/public/cpp/application/interface_factory.h" |
| 10 | #include "mojo/public/cpp/bindings/callback.h" |
| 11 | #include "mojo/public/cpp/bindings/interface_request.h" |
| 12 | #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 | #include "shell/test/pingable.mojom.h" |
| 14 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 15 | using mojo::String; |
| 16 | |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 17 | class PingableImpl : public Pingable { |
| 18 | public: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 19 | PingableImpl(mojo::InterfaceRequest<Pingable> request, |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 20 | const std::string& app_url, |
| 21 | const std::string& connection_url) |
| 22 | : binding_(this, request.Pass()), |
| 23 | app_url_(app_url), |
| 24 | connection_url_(connection_url) {} |
| 25 | |
| 26 | ~PingableImpl() override {} |
| 27 | |
| 28 | private: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 29 | void Ping( |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 30 | const String& message, |
| 31 | const mojo::Callback<void(String, String, String)>& callback) override { |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 32 | callback.Run(app_url_, connection_url_, message); |
| 33 | } |
| 34 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 35 | mojo::StrongBinding<Pingable> binding_; |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 36 | std::string app_url_; |
| 37 | std::string connection_url_; |
| 38 | }; |
| 39 | |
| 40 | class PingableApp : public mojo::ApplicationDelegate, |
| 41 | public mojo::InterfaceFactory<Pingable> { |
| 42 | public: |
| 43 | PingableApp() {} |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 44 | ~PingableApp() override {} |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | // ApplicationDelegate: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 48 | void Initialize(mojo::ApplicationImpl* impl) override { |
| 49 | app_url_ = impl->url(); |
| 50 | } |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 51 | |
| 52 | bool ConfigureIncomingConnection( |
| 53 | mojo::ApplicationConnection* connection) override { |
| 54 | connection->AddService(this); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // InterfaceFactory<Pingable>: |
| 59 | void Create(mojo::ApplicationConnection* connection, |
| 60 | mojo::InterfaceRequest<Pingable> request) override { |
| 61 | new PingableImpl(request.Pass(), app_url_, connection->GetConnectionURL()); |
| 62 | } |
| 63 | |
| 64 | std::string app_url_; |
| 65 | }; |
| 66 | |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 67 | MojoResult MojoMain(MojoHandle application_request) { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 68 | mojo::ApplicationRunner runner(new PingableApp); |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 69 | return runner.Run(application_request); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 70 | } |