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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "mojo/application/application_runner_chromium.h" | 17 #include "mojo/application/application_runner_chromium.h" |
18 #include "mojo/common/binding_set.h" | |
18 #include "mojo/public/c/system/main.h" | 19 #include "mojo/public/c/system/main.h" |
19 #include "mojo/public/cpp/application/application_connection.h" | 20 #include "mojo/public/cpp/application/application_connection.h" |
20 #include "mojo/public/cpp/application/application_delegate.h" | 21 #include "mojo/public/cpp/application/application_delegate.h" |
21 #include "mojo/public/cpp/application/application_impl.h" | 22 #include "mojo/public/cpp/application/application_impl.h" |
22 #include "mojo/public/cpp/application/connect.h" | 23 #include "mojo/public/cpp/application/connect.h" |
23 #include "mojo/public/cpp/bindings/array.h" | 24 #include "mojo/public/cpp/bindings/array.h" |
25 #include "mojo/public/cpp/bindings/strong_binding.h" | |
24 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 26 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
25 #include "mojo/public/interfaces/application/shell.mojom.h" | 27 #include "mojo/public/interfaces/application/shell.mojom.h" |
26 #include "mojo/services/files/interfaces/file.mojom.h" | 28 #include "mojo/services/files/interfaces/file.mojom.h" |
27 #include "mojo/services/files/interfaces/types.mojom.h" | 29 #include "mojo/services/files/interfaces/types.mojom.h" |
28 #include "mojo/services/terminal/interfaces/terminal.mojom.h" | 30 #include "mojo/services/terminal/interfaces/terminal.mojom.h" |
29 #include "mojo/services/terminal/interfaces/terminal_client.mojom.h" | 31 #include "mojo/services/terminal/interfaces/terminal_client.mojom.h" |
30 #include "mojo/services/view_manager/cpp/view.h" | 32 #include "mojo/services/ui/views/interfaces/view_manager.mojom.h" |
31 #include "mojo/services/view_manager/cpp/view_manager.h" | 33 #include "mojo/services/ui/views/interfaces/view_provider.mojom.h" |
32 #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" | 34 #include "mojo/services/ui/views/interfaces/views.mojom.h" |
33 #include "mojo/services/view_manager/cpp/view_manager_delegate.h" | |
34 #include "mojo/services/view_manager/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 static void Fputs(mojo::files::File* file, const char* s) { |
viettrungluu
2016/01/26 18:33:35
If you're going to bother making this static, then
jeffbrown
2016/01/26 23:10:44
Meh, I'll just drop the static. :)
| |
38 size_t length = strlen(s); | 38 size_t length = strlen(s); |
39 auto a = mojo::Array<uint8_t>::New(length); | 39 auto a = mojo::Array<uint8_t>::New(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 { |
47 public: | 47 public: |
48 explicit MotermExampleAppView(mojo::Shell* shell, mojo::View* view) | 48 explicit MotermExampleAppView( |
viettrungluu
2016/01/26 18:33:35
nit (preexisting condition, admittedly): no explic
jeffbrown
2016/01/26 23:10:44
Done.
| |
49 : shell_(shell), view_(view), moterm_view_(), weak_factory_(this) { | 49 mojo::Shell* shell, |
50 view_->AddObserver(this); | 50 const mojo::ui::ViewProvider::CreateViewCallback& callback) |
51 : shell_(shell), weak_factory_(this) { | |
52 // Connect to the moterm app. | |
53 LOG(INFO) << "Connecting to moterm"; | |
54 mojo::ServiceProviderPtr moterm_app; | |
55 shell->ConnectToApplication("mojo:moterm", GetProxy(&moterm_app), nullptr); | |
51 | 56 |
52 moterm_view_ = view_->view_manager()->CreateView(); | 57 // Create the moterm view and pass it back to the client directly. |
53 view_->AddChild(moterm_view_); | 58 mojo::ConnectToService(moterm_app.get(), &moterm_view_provider_); |
54 moterm_view_->SetBounds(view_->bounds()); | 59 mojo::ServiceProviderPtr moterm_service_provider; |
55 moterm_view_->SetVisible(true); | 60 moterm_view_provider_->CreateView(GetProxy(&moterm_service_provider), |
56 mojo::ServiceProviderPtr moterm_sp; | 61 nullptr, callback); |
57 moterm_view_->Embed("mojo:moterm", GetProxy(&moterm_sp), nullptr); | 62 |
58 moterm_sp->ConnectToService(mojo::terminal::Terminal::Name_, | 63 // Connect to the moterm terminal service associated with the view |
59 GetProxy(&moterm_terminal_).PassMessagePipe()); | 64 // we just created. |
60 Resize(); | 65 mojo::ConnectToService(moterm_service_provider.get(), &moterm_terminal_); |
66 | |
67 // Start running. | |
61 StartPrompt(true); | 68 StartPrompt(true); |
62 } | 69 } |
63 | 70 |
64 ~MotermExampleAppView() override {} | 71 ~MotermExampleAppView() {} |
65 | 72 |
66 private: | 73 private: |
67 void Resize() { | 74 void Resize() { |
68 moterm_terminal_->SetSize(0, 0, false, [](mojo::files::Error error, | 75 moterm_terminal_->SetSize(0, 0, false, [](mojo::files::Error error, |
69 uint32_t rows, uint32_t columns) { | 76 uint32_t rows, uint32_t columns) { |
70 DCHECK_EQ(error, mojo::files::Error::OK); | 77 DCHECK_EQ(error, mojo::files::Error::OK); |
71 DVLOG(1) << "New size: " << rows << "x" << columns; | 78 DVLOG(1) << "New size: " << rows << "x" << columns; |
72 }); | 79 }); |
73 } | 80 } |
74 | 81 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 base::Bind(&MotermExampleAppView::OnDestinationDone, | 135 base::Bind(&MotermExampleAppView::OnDestinationDone, |
129 weak_factory_.GetWeakPtr())); | 136 weak_factory_.GetWeakPtr())); |
130 } | 137 } |
131 void OnDestinationDone(mojo::files::Error error) { | 138 void OnDestinationDone(mojo::files::Error error) { |
132 // We should always succeed. (It'll only fail on synchronous failures, which | 139 // We should always succeed. (It'll only fail on synchronous failures, which |
133 // only occur when it's busy.) | 140 // only occur when it's busy.) |
134 DCHECK_EQ(error, mojo::files::Error::OK); | 141 DCHECK_EQ(error, mojo::files::Error::OK); |
135 StartPrompt(false); | 142 StartPrompt(false); |
136 } | 143 } |
137 | 144 |
138 // |ViewObserver|: | 145 mojo::Shell* const shell_; |
139 void OnViewDestroyed(mojo::View* view) override { | 146 mojo::ui::ViewProviderPtr moterm_view_provider_; |
140 DCHECK(view == view_); | |
141 delete this; | |
142 } | |
143 | 147 |
144 void OnViewBoundsChanged(mojo::View* view, | |
145 const mojo::Rect& old_bounds, | |
146 const mojo::Rect& new_bounds) override { | |
147 DCHECK_EQ(view, view_); | |
148 moterm_view_->SetBounds(view_->bounds()); | |
149 Resize(); | |
150 } | |
151 | |
152 mojo::Shell* const shell_; | |
153 mojo::View* const view_; | |
154 | |
155 mojo::View* moterm_view_; | |
156 mojo::terminal::TerminalPtr moterm_terminal_; | 148 mojo::terminal::TerminalPtr moterm_terminal_; |
157 // Valid while prompting. | 149 // Valid while prompting. |
158 mojo::files::FilePtr moterm_file_; | 150 mojo::files::FilePtr moterm_file_; |
159 | 151 |
160 base::WeakPtrFactory<MotermExampleAppView> weak_factory_; | 152 base::WeakPtrFactory<MotermExampleAppView> weak_factory_; |
161 | 153 |
162 DISALLOW_COPY_AND_ASSIGN(MotermExampleAppView); | 154 DISALLOW_COPY_AND_ASSIGN(MotermExampleAppView); |
163 }; | 155 }; |
164 | 156 |
165 class MotermExampleApp : public mojo::ApplicationDelegate, | 157 class MotermExampleApp : public mojo::ApplicationDelegate, |
166 public mojo::ViewManagerDelegate { | 158 public mojo::InterfaceFactory<mojo::ui::ViewProvider>, |
159 public mojo::ui::ViewProvider { | |
167 public: | 160 public: |
168 MotermExampleApp() : application_impl_() {} | 161 MotermExampleApp() : application_impl_() {} |
169 ~MotermExampleApp() override {} | 162 ~MotermExampleApp() override {} |
170 | 163 |
171 private: | 164 private: |
172 // |mojo::ApplicationDelegate|: | 165 // |mojo::ApplicationDelegate|: |
173 void Initialize(mojo::ApplicationImpl* application_impl) override { | 166 void Initialize(mojo::ApplicationImpl* application_impl) override { |
174 DCHECK(!application_impl_); | 167 DCHECK(!application_impl_); |
175 application_impl_ = application_impl; | 168 application_impl_ = application_impl; |
176 view_manager_client_factory_.reset( | |
177 new mojo::ViewManagerClientFactory(application_impl_->shell(), this)); | |
178 } | 169 } |
179 | 170 |
180 bool ConfigureIncomingConnection( | 171 bool ConfigureIncomingConnection( |
181 mojo::ApplicationConnection* connection) override { | 172 mojo::ApplicationConnection* connection) override { |
182 connection->AddService(view_manager_client_factory_.get()); | 173 connection->AddService<mojo::ui::ViewProvider>(this); |
183 return true; | 174 return true; |
184 } | 175 } |
185 | 176 |
186 // |mojo::ViewManagerDelegate|: | 177 // |InterfaceFactory<mojo::ui::ViewProvider>|: |
187 void OnEmbed(mojo::View* root, | 178 void Create(mojo::ApplicationConnection* connection, |
188 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 179 mojo::InterfaceRequest<mojo::ui::ViewProvider> request) override { |
189 mojo::ServiceProviderPtr exposed_services) override { | 180 bindings_.AddBinding(this, request.Pass()); |
190 new MotermExampleAppView(application_impl_->shell(), root); | |
191 } | 181 } |
192 | 182 |
193 void OnViewManagerDisconnected(mojo::ViewManager* view_manager) override {} | 183 // |ViewProvider|: |
184 void CreateView(mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
185 mojo::ServiceProviderPtr exposed_services, | |
186 const CreateViewCallback& callback) override { | |
187 new MotermExampleAppView(application_impl_->shell(), callback); | |
188 } | |
194 | 189 |
195 mojo::ApplicationImpl* application_impl_; | 190 mojo::ApplicationImpl* application_impl_; |
196 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | 191 mojo::BindingSet<mojo::ui::ViewProvider> bindings_; |
197 | 192 |
198 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp); | 193 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp); |
199 }; | 194 }; |
200 | 195 |
201 MojoResult MojoMain(MojoHandle application_request) { | 196 MojoResult MojoMain(MojoHandle application_request) { |
202 mojo::ApplicationRunnerChromium runner(new MotermExampleApp()); | 197 mojo::ApplicationRunnerChromium runner(new MotermExampleApp()); |
203 return runner.Run(application_request); | 198 return runner.Run(application_request); |
204 } | 199 } |
OLD | NEW |