Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: examples/echo_terminal/main.cc

Issue 999193002: Add a simple prototype "terminal" example. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: review comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/echo_terminal/README.md ('k') | examples/echo_terminal/terminal_client.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "examples/echo_terminal/terminal_client.mojom.h"
10 #include "mojo/application/application_runner_chromium.h"
11 #include "mojo/common/weak_binding_set.h"
12 #include "mojo/public/c/system/main.h"
13 #include "mojo/public/cpp/application/application_connection.h"
14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/interface_factory.h"
16 #include "services/files/file.mojom.h"
17 #include "services/files/types.mojom.h"
18
19 const uint32_t kMaxBytesToRead = 1000;
20
21 // Owns itself.
22 class TerminalEchoer : mojo::ErrorHandler {
23 public:
24 explicit TerminalEchoer(mojo::files::FilePtr terminal)
25 : terminal_(terminal.Pass()), last_bytes_read_size_(0) {}
26
27 void StartReading() {
28 // TODO(vtl): Are |offset| and |whence| correct?
29 terminal_->Read(
30 kMaxBytesToRead, 0, mojo::files::WHENCE_FROM_CURRENT,
31 base::Bind(&TerminalEchoer::OnRead, base::Unretained(this)));
32 }
33
34 private:
35 ~TerminalEchoer() override {}
36
37 // |mojo::ErrorHandler| implementation:
38 void OnConnectionError() override { delete this; }
39
40 // |Read()| callback:
41 void OnRead(mojo::files::Error error, mojo::Array<uint8_t> bytes_read) {
42 if (error) {
43 LOG(ERROR) << "Error: Read(): " << error;
44 delete this;
45 return;
46 }
47
48 if (!bytes_read) {
49 LOG(ERROR) << "Error: no bytes read (null)";
50 delete this;
51 return;
52 }
53
54 if (bytes_read.size() == 0 || bytes_read.size() > kMaxBytesToRead) {
55 LOG(ERROR) << "Error: invalid amount of bytes read: " << bytes_read.size()
56 << " bytes";
57 delete this;
58 return;
59 }
60
61 // Save this, so that we can check in |OnWrite()|.
62 last_bytes_read_size_ = bytes_read.size();
63
64 // TODO(vtl): Are |offset| and |whence| correct?
65 terminal_->Write(
66 bytes_read.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT,
67 base::Bind(&TerminalEchoer::OnWrite, base::Unretained(this)));
68 }
69
70 // |Write()| callback:
71 void OnWrite(mojo::files::Error error, uint32_t num_bytes_written) {
72 if (error) {
73 LOG(ERROR) << "Error: Write(): " << error;
74 delete this;
75 return;
76 }
77
78 if (num_bytes_written != last_bytes_read_size_) {
79 LOG(ERROR) << "Error: failed to write all bytes (last read: "
80 << last_bytes_read_size_
81 << " bytes; wrote: " << num_bytes_written << " bytes)";
82 delete this;
83 return;
84 }
85
86 StartReading();
87 }
88
89 mojo::files::FilePtr terminal_;
90 size_t last_bytes_read_size_;
91
92 DISALLOW_COPY_AND_ASSIGN(TerminalEchoer);
93 };
94
95 class EchoTerminalApp
96 : public mojo::ApplicationDelegate,
97 public mojo::InterfaceFactory<mojo::terminal::TerminalClient>,
98 public mojo::terminal::TerminalClient {
99 public:
100 EchoTerminalApp() {}
101 ~EchoTerminalApp() override {}
102
103 private:
104 // |ApplicationDelegate| override:
105 bool ConfigureIncomingConnection(
106 mojo::ApplicationConnection* connection) override {
107 connection->AddService<mojo::terminal::TerminalClient>(this);
108 return true;
109 }
110
111 // |InterfaceFactory<mojo::terminal::TerminalClient>| implementation:
112 void Create(
113 mojo::ApplicationConnection* connection,
114 mojo::InterfaceRequest<mojo::terminal::TerminalClient> request) override {
115 terminal_clients_.AddBinding(this, request.Pass());
116 }
117
118 // |mojo::terminal::TerminalClient| implementation:
119 void ConnectToTerminal(mojo::files::FilePtr terminal) override {
120 DCHECK(terminal);
121 // The |TerminalEchoer| will own itself.
122 (new TerminalEchoer(terminal.Pass()))->StartReading();
123 }
124
125 mojo::WeakBindingSet<mojo::terminal::TerminalClient> terminal_clients_;
126
127 DISALLOW_COPY_AND_ASSIGN(EchoTerminalApp);
128 };
129
130 MojoResult MojoMain(MojoHandle shell_handle) {
131 mojo::ApplicationRunnerChromium runner(new EchoTerminalApp());
132 return runner.Run(shell_handle);
133 }
OLDNEW
« no previous file with comments | « examples/echo_terminal/README.md ('k') | examples/echo_terminal/terminal_client.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698