| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 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/common/binding_set.h" | 10 #include "mojo/common/binding_set.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class TerminalEchoer { | 22 class TerminalEchoer { |
| 23 public: | 23 public: |
| 24 explicit TerminalEchoer(mojo::files::FilePtr terminal) | 24 explicit TerminalEchoer(mojo::files::FilePtr terminal) |
| 25 : terminal_(terminal.Pass()), last_bytes_read_size_(0) { | 25 : terminal_(terminal.Pass()), last_bytes_read_size_(0) { |
| 26 terminal_.set_connection_error_handler([this]() { OnConnectionError(); }); | 26 terminal_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void StartReading() { | 29 void StartReading() { |
| 30 // TODO(vtl): Are |offset| and |whence| correct? | 30 // TODO(vtl): Are |offset| and |whence| correct? |
| 31 terminal_->Read( | 31 terminal_->Read( |
| 32 kMaxBytesToRead, 0, mojo::files::WHENCE_FROM_CURRENT, | 32 kMaxBytesToRead, 0, mojo::files::Whence::FROM_CURRENT, |
| 33 base::Bind(&TerminalEchoer::OnRead, base::Unretained(this))); | 33 base::Bind(&TerminalEchoer::OnRead, base::Unretained(this))); |
| 34 } | 34 } |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 ~TerminalEchoer() {} | 37 ~TerminalEchoer() {} |
| 38 | 38 |
| 39 // Error callback: | 39 // Error callback: |
| 40 void OnConnectionError() { delete this; } | 40 void OnConnectionError() { delete this; } |
| 41 | 41 |
| 42 // |Read()| callback: | 42 // |Read()| callback: |
| 43 void OnRead(mojo::files::Error error, mojo::Array<uint8_t> bytes_read) { | 43 void OnRead(mojo::files::Error error, mojo::Array<uint8_t> bytes_read) { |
| 44 if (error != mojo::files::ERROR_OK) { | 44 if (error != mojo::files::Error::OK) { |
| 45 LOG(ERROR) << "Error: Read(): " << error; | 45 LOG(ERROR) << "Error: Read(): " << error; |
| 46 delete this; | 46 delete this; |
| 47 return; | 47 return; |
| 48 } | 48 } |
| 49 | 49 |
| 50 if (!bytes_read) { | 50 if (!bytes_read) { |
| 51 LOG(ERROR) << "Error: no bytes read (null)"; | 51 LOG(ERROR) << "Error: no bytes read (null)"; |
| 52 delete this; | 52 delete this; |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 | 55 |
| 56 if (bytes_read.size() == 0 || bytes_read.size() > kMaxBytesToRead) { | 56 if (bytes_read.size() == 0 || bytes_read.size() > kMaxBytesToRead) { |
| 57 LOG(ERROR) << "Error: invalid amount of bytes read: " << bytes_read.size() | 57 LOG(ERROR) << "Error: invalid amount of bytes read: " << bytes_read.size() |
| 58 << " bytes"; | 58 << " bytes"; |
| 59 delete this; | 59 delete this; |
| 60 return; | 60 return; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Save this, so that we can check in |OnWrite()|. | 63 // Save this, so that we can check in |OnWrite()|. |
| 64 last_bytes_read_size_ = bytes_read.size(); | 64 last_bytes_read_size_ = bytes_read.size(); |
| 65 | 65 |
| 66 // TODO(vtl): Are |offset| and |whence| correct? | 66 // TODO(vtl): Are |offset| and |whence| correct? |
| 67 terminal_->Write( | 67 terminal_->Write( |
| 68 bytes_read.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT, | 68 bytes_read.Pass(), 0, mojo::files::Whence::FROM_CURRENT, |
| 69 base::Bind(&TerminalEchoer::OnWrite, base::Unretained(this))); | 69 base::Bind(&TerminalEchoer::OnWrite, base::Unretained(this))); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // |Write()| callback: | 72 // |Write()| callback: |
| 73 void OnWrite(mojo::files::Error error, uint32_t num_bytes_written) { | 73 void OnWrite(mojo::files::Error error, uint32_t num_bytes_written) { |
| 74 if (error != mojo::files::ERROR_OK) { | 74 if (error != mojo::files::Error::OK) { |
| 75 LOG(ERROR) << "Error: Write(): " << error; | 75 LOG(ERROR) << "Error: Write(): " << error; |
| 76 delete this; | 76 delete this; |
| 77 return; | 77 return; |
| 78 } | 78 } |
| 79 | 79 |
| 80 if (num_bytes_written != last_bytes_read_size_) { | 80 if (num_bytes_written != last_bytes_read_size_) { |
| 81 LOG(ERROR) << "Error: failed to write all bytes (last read: " | 81 LOG(ERROR) << "Error: failed to write all bytes (last read: " |
| 82 << last_bytes_read_size_ | 82 << last_bytes_read_size_ |
| 83 << " bytes; wrote: " << num_bytes_written << " bytes)"; | 83 << " bytes; wrote: " << num_bytes_written << " bytes)"; |
| 84 delete this; | 84 delete this; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 126 |
| 127 mojo::BindingSet<mojo::terminal::TerminalClient> terminal_clients_; | 127 mojo::BindingSet<mojo::terminal::TerminalClient> terminal_clients_; |
| 128 | 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(EchoTerminalApp); | 129 DISALLOW_COPY_AND_ASSIGN(EchoTerminalApp); |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 MojoResult MojoMain(MojoHandle application_request) { | 132 MojoResult MojoMain(MojoHandle application_request) { |
| 133 mojo::ApplicationRunnerChromium runner(new EchoTerminalApp()); | 133 mojo::ApplicationRunnerChromium runner(new EchoTerminalApp()); |
| 134 return runner.Run(application_request); | 134 return runner.Run(application_request); |
| 135 } | 135 } |
| OLD | NEW |