| 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 // This is a terminal client (i.e., a "raw" |mojo.terminal.Terminal| -- e.g., | 5 // This is a terminal client (i.e., a "raw" |mojo.terminal.Terminal| -- e.g., |
| 6 // moterm -- can be asked to talk to this) that prompts the user for a native | 6 // moterm -- can be asked to talk to this) that prompts the user for a native |
| 7 // (Linux) binary to run and then does so (via mojo:native_support). | 7 // (Linux) binary to run and then does so (via mojo:native_support). |
| 8 // | 8 // |
| 9 // E.g., first run mojo:moterm_example_app (embedded by a window manager). Then, | 9 // E.g., first run mojo:moterm_example_app (embedded by a window manager). Then, |
| 10 // at the prompt, enter "mojo:native_run_app". At the next prompt, enter "bash" | 10 // at the prompt, enter "mojo:native_run_app". At the next prompt, enter "bash" |
| 11 // (or "echo hello mojo"). | 11 // (or "echo hello mojo"). |
| 12 // | 12 // |
| 13 // TODO(vtl): Maybe it should optionally be able to extract the binary path (and | 13 // TODO(vtl): Maybe it should optionally be able to extract the binary path (and |
| 14 // args) from the connection URL? | 14 // args) from the connection URL? |
| 15 | 15 |
| 16 #include <string.h> | 16 #include <string.h> |
| 17 | 17 |
| 18 #include <string> | 18 #include <string> |
| 19 #include <utility> |
| 19 #include <vector> | 20 #include <vector> |
| 20 | 21 |
| 21 #include "base/logging.h" | 22 #include "base/logging.h" |
| 22 #include "base/macros.h" | 23 #include "base/macros.h" |
| 23 #include "base/strings/string_split.h" | 24 #include "base/strings/string_split.h" |
| 24 #include "mojo/application/application_runner_chromium.h" | 25 #include "mojo/application/application_runner_chromium.h" |
| 25 #include "mojo/public/c/system/main.h" | 26 #include "mojo/public/c/system/main.h" |
| 26 #include "mojo/public/cpp/application/application_connection.h" | 27 #include "mojo/public/cpp/application/application_connection.h" |
| 27 #include "mojo/public/cpp/application/application_delegate.h" | 28 #include "mojo/public/cpp/application/application_delegate.h" |
| 28 #include "mojo/public/cpp/application/application_impl.h" | 29 #include "mojo/public/cpp/application/application_impl.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 using mojo::terminal::TerminalClient; | 40 using mojo::terminal::TerminalClient; |
| 40 | 41 |
| 41 mojo::Array<uint8_t> ToByteArray(const std::string& s) { | 42 mojo::Array<uint8_t> ToByteArray(const std::string& s) { |
| 42 auto rv = mojo::Array<uint8_t>::New(s.size()); | 43 auto rv = mojo::Array<uint8_t>::New(s.size()); |
| 43 memcpy(rv.data(), s.data(), s.size()); | 44 memcpy(rv.data(), s.data(), s.size()); |
| 44 return rv; | 45 return rv; |
| 45 } | 46 } |
| 46 | 47 |
| 47 class TerminalConnection { | 48 class TerminalConnection { |
| 48 public: | 49 public: |
| 49 explicit TerminalConnection(mojo::files::FilePtr terminal, | 50 explicit TerminalConnection(mojo::InterfaceHandle<mojo::files::File> terminal, |
| 50 native_support::Process* native_support_process) | 51 native_support::Process* native_support_process) |
| 51 : terminal_(terminal.Pass()), | 52 : terminal_(mojo::files::FilePtr::Create(std::move(terminal))), |
| 52 native_support_process_(native_support_process) { | 53 native_support_process_(native_support_process) { |
| 53 terminal_.set_connection_error_handler([this]() { delete this; }); | 54 terminal_.set_connection_error_handler([this]() { delete this; }); |
| 54 Start(); | 55 Start(); |
| 55 } | 56 } |
| 56 ~TerminalConnection() {} | 57 ~TerminalConnection() {} |
| 57 | 58 |
| 58 private: | 59 private: |
| 59 void Write(const char* s, mojo::files::File::WriteCallback callback) { | 60 void Write(const char* s, mojo::files::File::WriteCallback callback) { |
| 60 size_t length = strlen(s); | 61 size_t length = strlen(s); |
| 61 auto a = mojo::Array<uint8_t>::New(length); | 62 auto a = mojo::Array<uint8_t>::New(length); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Now, we can spawn. | 152 // Now, we can spawn. |
| 152 mojo::Array<mojo::Array<uint8_t>> argv; | 153 mojo::Array<mojo::Array<uint8_t>> argv; |
| 153 for (const auto& arg : command_line_) | 154 for (const auto& arg : command_line_) |
| 154 argv.push_back(ToByteArray(arg)); | 155 argv.push_back(ToByteArray(arg)); |
| 155 | 156 |
| 156 // TODO(vtl): If the |InterfacePtr| underlying |native_support_process_| | 157 // TODO(vtl): If the |InterfacePtr| underlying |native_support_process_| |
| 157 // encounters an error, then we're sort of dead in the water. | 158 // encounters an error, then we're sort of dead in the water. |
| 158 native_support_process_->SpawnWithTerminal( | 159 native_support_process_->SpawnWithTerminal( |
| 159 ToByteArray(command_line_[0]), argv.Pass(), nullptr, terminal_.Pass(), | 160 ToByteArray(command_line_[0]), argv.Pass(), nullptr, |
| 160 GetProxy(&process_controller_), [this](mojo::files::Error error) { | 161 terminal_.PassInterfaceHandle(), GetProxy(&process_controller_), |
| 162 [this](mojo::files::Error error) { |
| 161 this->DidSpawnWithTerminal(error); | 163 this->DidSpawnWithTerminal(error); |
| 162 }); | 164 }); |
| 163 process_controller_.set_connection_error_handler([this]() { delete this; }); | 165 process_controller_.set_connection_error_handler([this]() { delete this; }); |
| 164 } | 166 } |
| 165 void DidSpawnWithTerminal(mojo::files::Error error) { | 167 void DidSpawnWithTerminal(mojo::files::Error error) { |
| 166 if (error != mojo::files::Error::OK) { | 168 if (error != mojo::files::Error::OK) { |
| 167 LOG(ERROR) << "SpawnWithTerminal() error: " << error; | 169 LOG(ERROR) << "SpawnWithTerminal() error: " << error; |
| 168 delete this; | 170 delete this; |
| 169 return; | 171 return; |
| 170 } | 172 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 194 | 196 |
| 195 class TerminalClientImpl : public TerminalClient { | 197 class TerminalClientImpl : public TerminalClient { |
| 196 public: | 198 public: |
| 197 TerminalClientImpl(mojo::InterfaceRequest<TerminalClient> request, | 199 TerminalClientImpl(mojo::InterfaceRequest<TerminalClient> request, |
| 198 native_support::Process* native_support_process) | 200 native_support::Process* native_support_process) |
| 199 : binding_(this, request.Pass()), | 201 : binding_(this, request.Pass()), |
| 200 native_support_process_(native_support_process) {} | 202 native_support_process_(native_support_process) {} |
| 201 ~TerminalClientImpl() override {} | 203 ~TerminalClientImpl() override {} |
| 202 | 204 |
| 203 // |TerminalClient| implementation: | 205 // |TerminalClient| implementation: |
| 204 void ConnectToTerminal(mojo::files::FilePtr terminal) override { | 206 void ConnectToTerminal( |
| 207 mojo::InterfaceHandle<mojo::files::File> terminal) override { |
| 205 if (terminal) { | 208 if (terminal) { |
| 206 // Owns itself. | 209 // Owns itself. |
| 207 new TerminalConnection(terminal.Pass(), native_support_process_); | 210 new TerminalConnection(std::move(terminal), native_support_process_); |
| 208 } else { | 211 } else { |
| 209 LOG(ERROR) << "No terminal"; | 212 LOG(ERROR) << "No terminal"; |
| 210 } | 213 } |
| 211 } | 214 } |
| 212 | 215 |
| 213 private: | 216 private: |
| 214 mojo::StrongBinding<TerminalClient> binding_; | 217 mojo::StrongBinding<TerminalClient> binding_; |
| 215 native_support::Process* native_support_process_; | 218 native_support::Process* native_support_process_; |
| 216 | 219 |
| 217 DISALLOW_COPY_AND_ASSIGN(TerminalClientImpl); | 220 DISALLOW_COPY_AND_ASSIGN(TerminalClientImpl); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 247 mojo::ApplicationImpl* application_impl_; | 250 mojo::ApplicationImpl* application_impl_; |
| 248 native_support::ProcessPtr native_support_process_; | 251 native_support::ProcessPtr native_support_process_; |
| 249 | 252 |
| 250 DISALLOW_COPY_AND_ASSIGN(NativeRunApp); | 253 DISALLOW_COPY_AND_ASSIGN(NativeRunApp); |
| 251 }; | 254 }; |
| 252 | 255 |
| 253 MojoResult MojoMain(MojoHandle application_request) { | 256 MojoResult MojoMain(MojoHandle application_request) { |
| 254 mojo::ApplicationRunnerChromium runner(new NativeRunApp()); | 257 mojo::ApplicationRunnerChromium runner(new NativeRunApp()); |
| 255 return runner.Run(application_request); | 258 return runner.Run(application_request); |
| 256 } | 259 } |
| OLD | NEW |