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" |
(...skipping 28 matching lines...) Expand all Loading... | |
39 using mojo::terminal::TerminalClient; | 39 using mojo::terminal::TerminalClient; |
40 | 40 |
41 mojo::Array<uint8_t> ToByteArray(const std::string& s) { | 41 mojo::Array<uint8_t> ToByteArray(const std::string& s) { |
42 auto rv = mojo::Array<uint8_t>::New(s.size()); | 42 auto rv = mojo::Array<uint8_t>::New(s.size()); |
43 memcpy(rv.data(), s.data(), s.size()); | 43 memcpy(rv.data(), s.data(), s.size()); |
44 return rv; | 44 return rv; |
45 } | 45 } |
46 | 46 |
47 class TerminalConnection { | 47 class TerminalConnection { |
48 public: | 48 public: |
49 explicit TerminalConnection(mojo::files::FilePtr terminal, | 49 explicit TerminalConnection(mojo::files::FilePtr terminal, |
viettrungluu
2016/02/10 01:04:08
You should probably make this take an InterfaceHan
vardhan
2016/02/11 22:47:53
Done.
| |
50 native_support::Process* native_support_process) | 50 native_support::Process* native_support_process) |
51 : terminal_(terminal.Pass()), | 51 : terminal_(terminal.Pass()), |
52 native_support_process_(native_support_process) { | 52 native_support_process_(native_support_process) { |
53 terminal_.set_connection_error_handler([this]() { delete this; }); | 53 terminal_.set_connection_error_handler([this]() { delete this; }); |
54 Start(); | 54 Start(); |
55 } | 55 } |
56 ~TerminalConnection() {} | 56 ~TerminalConnection() {} |
57 | 57 |
58 private: | 58 private: |
59 void Write(const char* s, mojo::files::File::WriteCallback callback) { | 59 void Write(const char* s, mojo::files::File::WriteCallback callback) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 } | 149 } |
150 | 150 |
151 // Now, we can spawn. | 151 // Now, we can spawn. |
152 mojo::Array<mojo::Array<uint8_t>> argv; | 152 mojo::Array<mojo::Array<uint8_t>> argv; |
153 for (const auto& arg : command_line_) | 153 for (const auto& arg : command_line_) |
154 argv.push_back(ToByteArray(arg)); | 154 argv.push_back(ToByteArray(arg)); |
155 | 155 |
156 // TODO(vtl): If the |InterfacePtr| underlying |native_support_process_| | 156 // TODO(vtl): If the |InterfacePtr| underlying |native_support_process_| |
157 // encounters an error, then we're sort of dead in the water. | 157 // encounters an error, then we're sort of dead in the water. |
158 native_support_process_->SpawnWithTerminal( | 158 native_support_process_->SpawnWithTerminal( |
159 ToByteArray(command_line_[0]), argv.Pass(), nullptr, terminal_.Pass(), | 159 ToByteArray(command_line_[0]), argv.Pass(), nullptr, |
160 GetProxy(&process_controller_), [this](mojo::files::Error error) { | 160 terminal_.PassInterfaceHandle(), GetProxy(&process_controller_), |
161 [this](mojo::files::Error error) { | |
161 this->DidSpawnWithTerminal(error); | 162 this->DidSpawnWithTerminal(error); |
162 }); | 163 }); |
163 process_controller_.set_connection_error_handler([this]() { delete this; }); | 164 process_controller_.set_connection_error_handler([this]() { delete this; }); |
164 } | 165 } |
165 void DidSpawnWithTerminal(mojo::files::Error error) { | 166 void DidSpawnWithTerminal(mojo::files::Error error) { |
166 if (error != mojo::files::Error::OK) { | 167 if (error != mojo::files::Error::OK) { |
167 LOG(ERROR) << "SpawnWithTerminal() error: " << error; | 168 LOG(ERROR) << "SpawnWithTerminal() error: " << error; |
168 delete this; | 169 delete this; |
169 return; | 170 return; |
170 } | 171 } |
(...skipping 23 matching lines...) Expand all Loading... | |
194 | 195 |
195 class TerminalClientImpl : public TerminalClient { | 196 class TerminalClientImpl : public TerminalClient { |
196 public: | 197 public: |
197 TerminalClientImpl(mojo::InterfaceRequest<TerminalClient> request, | 198 TerminalClientImpl(mojo::InterfaceRequest<TerminalClient> request, |
198 native_support::Process* native_support_process) | 199 native_support::Process* native_support_process) |
199 : binding_(this, request.Pass()), | 200 : binding_(this, request.Pass()), |
200 native_support_process_(native_support_process) {} | 201 native_support_process_(native_support_process) {} |
201 ~TerminalClientImpl() override {} | 202 ~TerminalClientImpl() override {} |
202 | 203 |
203 // |TerminalClient| implementation: | 204 // |TerminalClient| implementation: |
204 void ConnectToTerminal(mojo::files::FilePtr terminal) override { | 205 void ConnectToTerminal( |
206 mojo::InterfaceHandle<mojo::files::File> terminal) override { | |
205 if (terminal) { | 207 if (terminal) { |
206 // Owns itself. | 208 // Owns itself. |
207 new TerminalConnection(terminal.Pass(), native_support_process_); | 209 new TerminalConnection(mojo::files::FilePtr::Create(std::move(terminal)), |
210 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 |