Chromium Code Reviews

Side by Side Diff: examples/moterm_example_app/moterm_example_app.cc

Issue 1679023006: Reify view ownership as a message pipe. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « examples/moterm_example_app/BUILD.gn ('k') | examples/ui/noodles/noodles_app.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 14 matching lines...)
25 #include "mojo/public/cpp/bindings/strong_binding.h" 25 #include "mojo/public/cpp/bindings/strong_binding.h"
26 #include "mojo/public/interfaces/application/service_provider.mojom.h" 26 #include "mojo/public/interfaces/application/service_provider.mojom.h"
27 #include "mojo/public/interfaces/application/shell.mojom.h" 27 #include "mojo/public/interfaces/application/shell.mojom.h"
28 #include "mojo/services/files/interfaces/file.mojom.h" 28 #include "mojo/services/files/interfaces/file.mojom.h"
29 #include "mojo/services/files/interfaces/types.mojom.h" 29 #include "mojo/services/files/interfaces/types.mojom.h"
30 #include "mojo/services/terminal/interfaces/terminal.mojom.h" 30 #include "mojo/services/terminal/interfaces/terminal.mojom.h"
31 #include "mojo/services/terminal/interfaces/terminal_client.mojom.h" 31 #include "mojo/services/terminal/interfaces/terminal_client.mojom.h"
32 #include "mojo/services/ui/views/interfaces/view_manager.mojom.h" 32 #include "mojo/services/ui/views/interfaces/view_manager.mojom.h"
33 #include "mojo/services/ui/views/interfaces/view_provider.mojom.h" 33 #include "mojo/services/ui/views/interfaces/view_provider.mojom.h"
34 #include "mojo/services/ui/views/interfaces/views.mojom.h" 34 #include "mojo/services/ui/views/interfaces/views.mojom.h"
35 #include "mojo/ui/view_provider_app.h"
35 36
36 // Kind of like |fputs()| (doesn't wait for result). 37 // Kind of like |fputs()| (doesn't wait for result).
37 void Fputs(mojo::files::File* file, const char* s) { 38 void Fputs(mojo::files::File* file, const char* s) {
38 size_t length = strlen(s); 39 size_t length = strlen(s);
39 auto a = mojo::Array<uint8_t>::New(length); 40 auto a = mojo::Array<uint8_t>::New(length);
40 memcpy(&a[0], s, length); 41 memcpy(&a[0], s, length);
41 42
42 file->Write(a.Pass(), 0, mojo::files::Whence::FROM_CURRENT, 43 file->Write(a.Pass(), 0, mojo::files::Whence::FROM_CURRENT,
43 mojo::files::File::WriteCallback()); 44 mojo::files::File::WriteCallback());
44 } 45 }
45 46
46 class MotermExampleAppView { 47 class MotermExampleAppView {
47 public: 48 public:
48 MotermExampleAppView( 49 MotermExampleAppView(
49 mojo::Shell* shell, 50 mojo::Shell* shell,
50 const mojo::ui::ViewProvider::CreateViewCallback& callback) 51 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request)
51 : shell_(shell), weak_factory_(this) { 52 : shell_(shell), weak_factory_(this) {
52 // Connect to the moterm app. 53 // Connect to the moterm app.
53 LOG(INFO) << "Connecting to moterm"; 54 LOG(INFO) << "Connecting to moterm";
54 mojo::ServiceProviderPtr moterm_app; 55 mojo::ServiceProviderPtr moterm_app;
55 shell->ConnectToApplication("mojo:moterm", GetProxy(&moterm_app), nullptr); 56 shell->ConnectToApplication("mojo:moterm", GetProxy(&moterm_app), nullptr);
56 57
57 // Create the moterm view and pass it back to the client directly. 58 // Create the moterm view and pass it back to the client directly.
58 mojo::ConnectToService(moterm_app.get(), &moterm_view_provider_); 59 mojo::ConnectToService(moterm_app.get(), &moterm_view_provider_);
59 mojo::ServiceProviderPtr moterm_service_provider; 60 mojo::ServiceProviderPtr moterm_service_provider;
60 moterm_view_provider_->CreateView(GetProxy(&moterm_service_provider), 61 moterm_view_provider_->CreateView(
61 nullptr, callback); 62 view_owner_request.Pass(), GetProxy(&moterm_service_provider), nullptr);
62 63
63 // Connect to the moterm terminal service associated with the view 64 // Connect to the moterm terminal service associated with the view
64 // we just created. 65 // we just created.
65 mojo::ConnectToService(moterm_service_provider.get(), &moterm_terminal_); 66 mojo::ConnectToService(moterm_service_provider.get(), &moterm_terminal_);
66 67
67 // Start running. 68 // Start running.
68 StartPrompt(true); 69 StartPrompt(true);
69 } 70 }
70 71
71 ~MotermExampleAppView() {} 72 ~MotermExampleAppView() {}
(...skipping 75 matching lines...)
147 148
148 mojo::terminal::TerminalPtr moterm_terminal_; 149 mojo::terminal::TerminalPtr moterm_terminal_;
149 // Valid while prompting. 150 // Valid while prompting.
150 mojo::files::FilePtr moterm_file_; 151 mojo::files::FilePtr moterm_file_;
151 152
152 base::WeakPtrFactory<MotermExampleAppView> weak_factory_; 153 base::WeakPtrFactory<MotermExampleAppView> weak_factory_;
153 154
154 DISALLOW_COPY_AND_ASSIGN(MotermExampleAppView); 155 DISALLOW_COPY_AND_ASSIGN(MotermExampleAppView);
155 }; 156 };
156 157
157 class MotermExampleApp : public mojo::ApplicationDelegate, 158 class MotermExampleApp : public mojo::ui::ViewProviderApp {
158 public mojo::InterfaceFactory<mojo::ui::ViewProvider>,
159 public mojo::ui::ViewProvider {
160 public: 159 public:
161 MotermExampleApp() : application_impl_() {} 160 MotermExampleApp() {}
162 ~MotermExampleApp() override {} 161 ~MotermExampleApp() override {}
163 162
164 private: 163 // |ViewProviderApp|:
165 // |mojo::ApplicationDelegate|: 164 void CreateView(
166 void Initialize(mojo::ApplicationImpl* application_impl) override { 165 const std::string& connection_url,
167 DCHECK(!application_impl_); 166 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request,
168 application_impl_ = application_impl; 167 mojo::InterfaceRequest<mojo::ServiceProvider> services,
168 mojo::ServiceProviderPtr exposed_services) override {
169 new MotermExampleAppView(app_impl()->shell(), view_owner_request.Pass());
169 } 170 }
170 171
171 bool ConfigureIncomingConnection( 172 private:
172 mojo::ApplicationConnection* connection) override {
173 connection->AddService<mojo::ui::ViewProvider>(this);
174 return true;
175 }
176
177 // |InterfaceFactory<mojo::ui::ViewProvider>|:
178 void Create(mojo::ApplicationConnection* connection,
179 mojo::InterfaceRequest<mojo::ui::ViewProvider> request) override {
180 bindings_.AddBinding(this, request.Pass());
181 }
182
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 }
189
190 mojo::ApplicationImpl* application_impl_;
191 mojo::BindingSet<mojo::ui::ViewProvider> bindings_;
192
193 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp); 173 DISALLOW_COPY_AND_ASSIGN(MotermExampleApp);
194 }; 174 };
195 175
196 MojoResult MojoMain(MojoHandle application_request) { 176 MojoResult MojoMain(MojoHandle application_request) {
197 mojo::ApplicationRunnerChromium runner(new MotermExampleApp()); 177 mojo::ApplicationRunnerChromium runner(new MotermExampleApp());
198 return runner.Run(application_request); 178 return runner.Run(application_request);
199 } 179 }
OLDNEW
« no previous file with comments | « examples/moterm_example_app/BUILD.gn ('k') | examples/ui/noodles/noodles_app.h » ('j') | no next file with comments »

Powered by Google App Engine