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 simple example application (with an embeddable view), which embeds | 5 // This is a simple example application (with an embeddable view), which embeds |
6 // the Moterm view, uses it to prompt the user, etc. | 6 // the Moterm view, uses it to prompt the user, etc. |
7 | 7 |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "mojo/services/view_manager/public/cpp/view_manager_client_factory.h" | 32 #include "mojo/services/view_manager/public/cpp/view_manager_client_factory.h" |
33 #include "mojo/services/view_manager/public/cpp/view_manager_delegate.h" | 33 #include "mojo/services/view_manager/public/cpp/view_manager_delegate.h" |
34 #include "mojo/services/view_manager/public/cpp/view_observer.h" | 34 #include "mojo/services/view_manager/public/cpp/view_observer.h" |
35 | 35 |
36 // Kind of like |fputs()| (doesn't wait for result). | 36 // Kind of like |fputs()| (doesn't wait for result). |
37 void Fputs(mojo::files::File* file, const char* s) { | 37 void Fputs(mojo::files::File* file, const char* s) { |
38 size_t length = strlen(s); | 38 size_t length = strlen(s); |
39 mojo::Array<uint8_t> a(length); | 39 mojo::Array<uint8_t> a(length); |
40 memcpy(&a[0], s, length); | 40 memcpy(&a[0], s, length); |
41 | 41 |
42 file->Write(a.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT, | 42 file->Write(a.Pass(), 0, mojo::files::Whence::FROM_CURRENT, |
43 mojo::files::File::WriteCallback()); | 43 mojo::files::File::WriteCallback()); |
44 } | 44 } |
45 | 45 |
46 class MotermExampleAppView : public mojo::ViewObserver { | 46 class MotermExampleAppView : public mojo::ViewObserver { |
47 public: | 47 public: |
48 explicit MotermExampleAppView(mojo::Shell* shell, mojo::View* view) | 48 explicit MotermExampleAppView(mojo::Shell* shell, mojo::View* view) |
49 : shell_(shell), view_(view), moterm_view_(), weak_factory_(this) { | 49 : shell_(shell), view_(view), moterm_view_(), weak_factory_(this) { |
50 view_->AddObserver(this); | 50 view_->AddObserver(this); |
51 | 51 |
52 moterm_view_ = view_->view_manager()->CreateView(); | 52 moterm_view_ = view_->view_manager()->CreateView(); |
53 view_->AddChild(moterm_view_); | 53 view_->AddChild(moterm_view_); |
54 moterm_view_->SetBounds(view_->bounds()); | 54 moterm_view_->SetBounds(view_->bounds()); |
55 moterm_view_->SetVisible(true); | 55 moterm_view_->SetVisible(true); |
56 mojo::ServiceProviderPtr moterm_sp; | 56 mojo::ServiceProviderPtr moterm_sp; |
57 moterm_view_->Embed("mojo:moterm", GetProxy(&moterm_sp), nullptr); | 57 moterm_view_->Embed("mojo:moterm", GetProxy(&moterm_sp), nullptr); |
58 moterm_sp->ConnectToService(mojo::terminal::Terminal::Name_, | 58 moterm_sp->ConnectToService(mojo::terminal::Terminal::Name_, |
59 GetProxy(&moterm_terminal_).PassMessagePipe()); | 59 GetProxy(&moterm_terminal_).PassMessagePipe()); |
60 Resize(); | 60 Resize(); |
61 StartPrompt(true); | 61 StartPrompt(true); |
62 } | 62 } |
63 | 63 |
64 ~MotermExampleAppView() override {} | 64 ~MotermExampleAppView() override {} |
65 | 65 |
66 private: | 66 private: |
67 void Resize() { | 67 void Resize() { |
68 moterm_terminal_->SetSize(0, 0, false, [](mojo::files::Error error, | 68 moterm_terminal_->SetSize(0, 0, false, [](mojo::files::Error error, |
69 uint32_t rows, uint32_t columns) { | 69 uint32_t rows, uint32_t columns) { |
70 DCHECK_EQ(error, mojo::files::ERROR_OK); | 70 DCHECK_EQ(error, mojo::files::Error::OK); |
71 DVLOG(1) << "New size: " << rows << "x" << columns; | 71 DVLOG(1) << "New size: " << rows << "x" << columns; |
72 }); | 72 }); |
73 } | 73 } |
74 | 74 |
75 void StartPrompt(bool first_time) { | 75 void StartPrompt(bool first_time) { |
76 if (!moterm_file_) { | 76 if (!moterm_file_) { |
77 moterm_terminal_->Connect(GetProxy(&moterm_file_), false, | 77 moterm_terminal_->Connect(GetProxy(&moterm_file_), false, |
78 [](mojo::files::Error error) { | 78 [](mojo::files::Error error) { |
79 DCHECK_EQ(error, mojo::files::ERROR_OK); | 79 DCHECK_EQ(error, mojo::files::Error::OK); |
80 }); | 80 }); |
81 } | 81 } |
82 | 82 |
83 if (first_time) { | 83 if (first_time) { |
84 // Display some welcome text. | 84 // Display some welcome text. |
85 Fputs(moterm_file_.get(), | 85 Fputs(moterm_file_.get(), |
86 "Welcome to " | 86 "Welcome to " |
87 "\x1b[1m\x1b[34mM\x1b[31mo\x1b[33mt\x1b[34me\x1b[32mr\x1b[31mm\n" | 87 "\x1b[1m\x1b[34mM\x1b[31mo\x1b[33mt\x1b[34me\x1b[32mr\x1b[31mm\n" |
88 "\n"); | 88 "\n"); |
89 } | 89 } |
90 | 90 |
91 Fputs(moterm_file_.get(), "\x1b[0m\nWhere do you want to go today?\n:) "); | 91 Fputs(moterm_file_.get(), "\x1b[0m\nWhere do you want to go today?\n:) "); |
92 moterm_file_->Read(1000, 0, mojo::files::WHENCE_FROM_CURRENT, | 92 moterm_file_->Read(1000, 0, mojo::files::Whence::FROM_CURRENT, |
93 base::Bind(&MotermExampleAppView::OnInputFromPrompt, | 93 base::Bind(&MotermExampleAppView::OnInputFromPrompt, |
94 weak_factory_.GetWeakPtr())); | 94 weak_factory_.GetWeakPtr())); |
95 } | 95 } |
96 void OnInputFromPrompt(mojo::files::Error error, | 96 void OnInputFromPrompt(mojo::files::Error error, |
97 mojo::Array<uint8_t> bytes_read) { | 97 mojo::Array<uint8_t> bytes_read) { |
98 if (error != mojo::files::ERROR_OK || !bytes_read) { | 98 if (error != mojo::files::Error::OK || !bytes_read) { |
99 // TODO(vtl): Handle errors? | 99 // TODO(vtl): Handle errors? |
100 NOTIMPLEMENTED(); | 100 NOTIMPLEMENTED(); |
101 return; | 101 return; |
102 } | 102 } |
103 | 103 |
104 std::string dest_url; | 104 std::string dest_url; |
105 if (bytes_read.size() >= 1) { | 105 if (bytes_read.size() >= 1) { |
106 base::TrimWhitespaceASCII( | 106 base::TrimWhitespaceASCII( |
107 std::string(reinterpret_cast<const char*>(&bytes_read[0]), | 107 std::string(reinterpret_cast<const char*>(&bytes_read[0]), |
108 bytes_read.size()), | 108 bytes_read.size()), |
(...skipping 15 matching lines...) Expand all Loading... |
124 mojo::terminal::TerminalClientPtr dest_terminal_client; | 124 mojo::terminal::TerminalClientPtr dest_terminal_client; |
125 mojo::ConnectToService(dest_sp.get(), &dest_terminal_client); | 125 mojo::ConnectToService(dest_sp.get(), &dest_terminal_client); |
126 moterm_terminal_->ConnectToClient( | 126 moterm_terminal_->ConnectToClient( |
127 dest_terminal_client.Pass(), true, | 127 dest_terminal_client.Pass(), true, |
128 base::Bind(&MotermExampleAppView::OnDestinationDone, | 128 base::Bind(&MotermExampleAppView::OnDestinationDone, |
129 weak_factory_.GetWeakPtr())); | 129 weak_factory_.GetWeakPtr())); |
130 } | 130 } |
131 void OnDestinationDone(mojo::files::Error error) { | 131 void OnDestinationDone(mojo::files::Error error) { |
132 // We should always succeed. (It'll only fail on synchronous failures, which | 132 // We should always succeed. (It'll only fail on synchronous failures, which |
133 // only occur when it's busy.) | 133 // only occur when it's busy.) |
134 DCHECK_EQ(error, mojo::files::ERROR_OK); | 134 DCHECK_EQ(error, mojo::files::Error::OK); |
135 StartPrompt(false); | 135 StartPrompt(false); |
136 } | 136 } |
137 | 137 |
138 // |ViewObserver|: | 138 // |ViewObserver|: |
139 void OnViewDestroyed(mojo::View* view) override { | 139 void OnViewDestroyed(mojo::View* view) override { |
140 DCHECK(view == view_); | 140 DCHECK(view == view_); |
141 delete this; | 141 delete this; |
142 } | 142 } |
143 | 143 |
144 void OnViewBoundsChanged(mojo::View* view, | 144 void OnViewBoundsChanged(mojo::View* view, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 mojo::ApplicationImpl* application_impl_; | 195 mojo::ApplicationImpl* application_impl_; |
196 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | 196 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; |
197 | 197 |
198 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp); | 198 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp); |
199 }; | 199 }; |
200 | 200 |
201 MojoResult MojoMain(MojoHandle application_request) { | 201 MojoResult MojoMain(MojoHandle application_request) { |
202 mojo::ApplicationRunnerChromium runner(new MotermExampleApp()); | 202 mojo::ApplicationRunnerChromium runner(new MotermExampleApp()); |
203 return runner.Run(application_request); | 203 return runner.Run(application_request); |
204 } | 204 } |
OLD | NEW |