Vardhan Mudunuru | 119fc1f | 2016-03-15 16:20:39 -0700 | [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 | // This example demonstrates the simple use of SynchronousInterfacePtr<>, which |
| 6 | // is the blocking, synchronous version of mojom interface calls (typically used |
| 7 | // via InterfacePtr<>). |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "examples/echo/echo.mojom-sync.h" |
Vardhan Mudunuru | 119fc1f | 2016-03-15 16:20:39 -0700 | [diff] [blame] | 13 | #include "mojo/public/c/system/main.h" |
| 14 | #include "mojo/public/cpp/application/application_delegate.h" |
| 15 | #include "mojo/public/cpp/application/application_impl.h" |
| 16 | #include "mojo/public/cpp/application/application_runner.h" |
Viet-Trung Luu | 8972b62 | 2016-04-25 18:06:17 -0700 | [diff] [blame] | 17 | #include "mojo/public/cpp/application/connect.h" |
Vardhan Mudunuru | 119fc1f | 2016-03-15 16:20:39 -0700 | [diff] [blame] | 18 | #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" |
| 19 | #include "mojo/public/cpp/environment/logging.h" |
| 20 | #include "mojo/public/cpp/utility/run_loop.h" |
| 21 | |
| 22 | namespace mojo { |
| 23 | namespace examples { |
| 24 | |
| 25 | class EchoClientDelegate : public ApplicationDelegate { |
| 26 | public: |
| 27 | void Initialize(ApplicationImpl* app) override { |
Viet-Trung Luu | 8972b62 | 2016-04-25 18:06:17 -0700 | [diff] [blame] | 28 | SynchronousInterfacePtr<Echo> echo; |
| 29 | ConnectToService(app->shell(), "mojo:echo_server", |
| 30 | GetSynchronousProxy(&echo)); |
Vardhan Mudunuru | 119fc1f | 2016-03-15 16:20:39 -0700 | [diff] [blame] | 31 | |
| 32 | mojo::String out = "yo!"; |
Viet-Trung Luu | 8972b62 | 2016-04-25 18:06:17 -0700 | [diff] [blame] | 33 | MOJO_CHECK(echo->EchoString("hello", &out)); |
Vardhan Mudunuru | 119fc1f | 2016-03-15 16:20:39 -0700 | [diff] [blame] | 34 | MOJO_LOG(INFO) << "Got response: " << out; |
| 35 | RunLoop::current()->Quit(); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | } // namespace examples |
| 40 | } // namespace mojo |
| 41 | |
| 42 | MojoResult MojoMain(MojoHandle application_request) { |
| 43 | mojo::ApplicationRunner runner( |
| 44 | std::unique_ptr<mojo::examples::EchoClientDelegate>( |
| 45 | new mojo::examples::EchoClientDelegate())); |
| 46 | return runner.Run(application_request); |
| 47 | } |