OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <iostream> | 5 #include <iostream> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "mojo/application/application_runner_chromium.h" | 9 #include "mojo/application/application_runner_chromium.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/interface_factory.h" | 13 #include "mojo/public/cpp/application/interface_factory.h" |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
15 #include "mojo/services/console/public/interfaces/console.mojom.h" | 15 #include "mojo/services/console/public/interfaces/console.mojom.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 | 18 |
19 class ConsoleImpl : public Console { | 19 class ConsoleImpl : public InterfaceImpl<Console> { |
20 public: | 20 public: |
21 ConsoleImpl(const std::string& app_name, InterfaceRequest<Console> request) | 21 explicit ConsoleImpl(const std::string& app_name) : app_name_(app_name) {} |
22 : app_name_(app_name), binding_(this, request.Pass()) {} | |
23 ~ConsoleImpl() override {} | 22 ~ConsoleImpl() override {} |
24 | 23 |
25 void ReadLine(const Callback<void(bool, String)>& callback) override { | 24 void ReadLine(const Callback<void(bool, String)>& callback) override { |
26 std::cout << "[" << app_name_ << "]> "; | 25 std::cout << "[" << app_name_ << "]> "; |
27 std::cout.flush(); | 26 std::cout.flush(); |
28 | 27 |
29 std::string line; | 28 std::string line; |
30 std::getline(std::cin, line); | 29 std::getline(std::cin, line); |
31 | 30 |
32 // This will send an empty string on eof. | 31 // This will send an empty string on eof. |
33 String return_value = String::From(line); | 32 String return_value = String::From(line); |
34 bool is_good = std::cin.good(); | 33 bool is_good = std::cin.good(); |
35 if (!is_good) | 34 if (!is_good) |
36 return_value = String::From(""); | 35 return_value = String::From(""); |
37 | 36 |
38 callback.Run(is_good, return_value); | 37 callback.Run(is_good, return_value); |
39 } | 38 } |
40 | 39 |
41 void PrintLines(Array<mojo::String> data, | 40 void PrintLines(Array<mojo::String> data, |
42 const Callback<void(bool)>& callback) override { | 41 const Callback<void(bool)>& callback) override { |
43 for (size_t i = 0; i < data.size(); ++i) | 42 for (size_t i = 0; i < data.size(); ++i) |
44 std::cout << "[" << app_name_ << "] " << data[i].get() << std::endl; | 43 std::cout << "[" << app_name_ << "] " << data[i].get() << std::endl; |
45 | 44 |
46 callback.Run(true); | 45 callback.Run(true); |
47 } | 46 } |
48 | 47 |
49 private: | 48 private: |
50 const std::string app_name_; | 49 const std::string app_name_; |
51 StrongBinding<Console> binding_; | |
52 | 50 |
53 DISALLOW_COPY_AND_ASSIGN(ConsoleImpl); | 51 DISALLOW_COPY_AND_ASSIGN(ConsoleImpl); |
54 }; | 52 }; |
55 | 53 |
56 class ConsoleDelegate : public ApplicationDelegate, | 54 class ConsoleDelegate : public ApplicationDelegate, |
57 public InterfaceFactory<Console> { | 55 public InterfaceFactory<Console> { |
58 public: | 56 public: |
59 ConsoleDelegate() {} | 57 ConsoleDelegate() {} |
60 ~ConsoleDelegate() override {} | 58 ~ConsoleDelegate() override {} |
61 | 59 |
62 // ApplicationDelegate implementation. | 60 // ApplicationDelegate implementation. |
63 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 61 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
64 connection->AddService(this); | 62 connection->AddService(this); |
65 return true; | 63 return true; |
66 } | 64 } |
67 | 65 |
68 // InterfaceFactory<Console> implementation. | 66 // InterfaceFactory<Console> implementation. |
69 void Create(ApplicationConnection* connection, | 67 void Create(ApplicationConnection* connection, |
70 InterfaceRequest<Console> request) override { | 68 InterfaceRequest<Console> request) override { |
71 new ConsoleImpl(connection->GetRemoteApplicationURL(), request.Pass()); | 69 BindToRequest(new ConsoleImpl(connection->GetRemoteApplicationURL()), |
| 70 &request); |
72 } | 71 } |
73 | 72 |
74 private: | 73 private: |
75 DISALLOW_COPY_AND_ASSIGN(ConsoleDelegate); | 74 DISALLOW_COPY_AND_ASSIGN(ConsoleDelegate); |
76 }; | 75 }; |
77 | 76 |
78 } // namespace mojo | 77 } // namespace mojo |
79 | 78 |
80 MojoResult MojoMain(MojoHandle shell_handle) { | 79 MojoResult MojoMain(MojoHandle shell_handle) { |
81 mojo::ApplicationRunnerChromium runner(new mojo::ConsoleDelegate); | 80 mojo::ApplicationRunnerChromium runner(new mojo::ConsoleDelegate); |
82 return runner.Run(shell_handle); | 81 return runner.Run(shell_handle); |
83 } | 82 } |
OLD | NEW |