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

Side by Side Diff: mojo/application_manager/application_manager.cc

Issue 718473003: Add mojo::Binding<Interface> for more flexible pipe<->impl binding (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Rename Binding::router() -> Binding::internal_router() Created 6 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "mojo/application_manager/application_manager.h" 5 #include "mojo/application_manager/application_manager.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "mojo/application_manager/application_loader.h" 14 #include "mojo/application_manager/application_loader.h"
15 #include "mojo/common/common_type_converters.h" 15 #include "mojo/common/common_type_converters.h"
16 #include "mojo/public/cpp/application/connect.h" 16 #include "mojo/public/cpp/application/connect.h"
17 #include "mojo/public/cpp/bindings/binding.h"
17 #include "mojo/public/cpp/bindings/error_handler.h" 18 #include "mojo/public/cpp/bindings/error_handler.h"
18 #include "mojo/public/interfaces/application/application.mojom.h" 19 #include "mojo/public/interfaces/application/application.mojom.h"
19 #include "mojo/public/interfaces/application/shell.mojom.h" 20 #include "mojo/public/interfaces/application/shell.mojom.h"
20 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h" 21 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h"
21 22
22 namespace mojo { 23 namespace mojo {
23 24
24 namespace { 25 namespace {
25 // Used by TestAPI. 26 // Used by TestAPI.
26 bool has_created_instance = false; 27 bool has_created_instance = false;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 } 92 }
92 } 93 }
93 94
94 base::WeakPtr<ApplicationManager> manager_; 95 base::WeakPtr<ApplicationManager> manager_;
95 GURL requested_url_; 96 GURL requested_url_;
96 GURL resolved_url_; 97 GURL resolved_url_;
97 GURL requestor_url_; 98 GURL requestor_url_;
98 ServiceProviderPtr service_provider_; 99 ServiceProviderPtr service_provider_;
99 }; 100 };
100 101
101 class ApplicationManager::ShellImpl : public InterfaceImpl<Shell> { 102 class ApplicationManager::ShellImpl : public Shell, public ErrorHandler {
102 public: 103 public:
103 ShellImpl(ApplicationManager* manager, 104 ShellImpl(ScopedMessagePipeHandle handle,
105 ApplicationManager* manager,
104 const GURL& requested_url, 106 const GURL& requested_url,
105 const GURL& url) 107 const GURL& url)
106 : manager_(manager), requested_url_(requested_url), url_(url) {} 108 : ShellImpl(manager, requested_url, url) {
109 binding_.Bind(handle.Pass());
110 }
111
112 ShellImpl(ShellPtr* ptr,
113 ApplicationManager* manager,
114 const GURL& requested_url,
115 const GURL& url)
116 : ShellImpl(manager, requested_url, url) {
117 binding_.Bind(ptr);
118 }
107 119
108 ~ShellImpl() override {} 120 ~ShellImpl() override {}
109 121
110 void ConnectToClient(const GURL& requestor_url, 122 void ConnectToClient(const GURL& requestor_url,
111 ServiceProviderPtr service_provider) { 123 ServiceProviderPtr service_provider) {
112 client()->AcceptConnection(String::From(requestor_url), 124 client()->AcceptConnection(String::From(requestor_url),
113 service_provider.Pass()); 125 service_provider.Pass());
114 } 126 }
115 127
116 // ServiceProvider implementation: 128 Application* client() { return binding_.client(); }
129 const GURL& url() const { return url_; }
130 const GURL& requested_url() const { return requested_url_; }
131
132 private:
133 ShellImpl(ApplicationManager* manager,
134 const GURL& requested_url,
135 const GURL& url)
136 : manager_(manager),
137 requested_url_(requested_url),
138 url_(url),
139 binding_(this) {
140 binding_.set_error_handler(this);
141 }
142
143 // Shell implementation:
117 void ConnectToApplication( 144 void ConnectToApplication(
118 const String& app_url, 145 const String& app_url,
119 InterfaceRequest<ServiceProvider> in_service_provider) override { 146 InterfaceRequest<ServiceProvider> in_service_provider) override {
120 ServiceProviderPtr out_service_provider; 147 ServiceProviderPtr out_service_provider;
121 out_service_provider.Bind(in_service_provider.PassMessagePipe()); 148 out_service_provider.Bind(in_service_provider.PassMessagePipe());
122 manager_->ConnectToApplication( 149 manager_->ConnectToApplication(
123 app_url.To<GURL>(), url_, out_service_provider.Pass()); 150 app_url.To<GURL>(), url_, out_service_provider.Pass());
124 } 151 }
125 152
126 const GURL& url() const { return url_; } 153 // ErrorHandler implementation:
127 const GURL& requested_url() const { return requested_url_; }
128
129 private:
130 void OnConnectionError() override { manager_->OnShellImplError(this); } 154 void OnConnectionError() override { manager_->OnShellImplError(this); }
131 155
132 ApplicationManager* const manager_; 156 ApplicationManager* const manager_;
133 const GURL requested_url_; 157 const GURL requested_url_;
134 const GURL url_; 158 const GURL url_;
159 Binding<Shell> binding_;
135 160
136 DISALLOW_COPY_AND_ASSIGN(ShellImpl); 161 DISALLOW_COPY_AND_ASSIGN(ShellImpl);
137 }; 162 };
138 163
139 class ApplicationManager::ContentHandlerConnection : public ErrorHandler { 164 class ApplicationManager::ContentHandlerConnection : public ErrorHandler {
140 public: 165 public:
141 ContentHandlerConnection(ApplicationManager* manager, 166 ContentHandlerConnection(ApplicationManager* manager,
142 const GURL& content_handler_url) 167 const GURL& content_handler_url)
143 : manager_(manager), content_handler_url_(content_handler_url) { 168 : manager_(manager), content_handler_url_(content_handler_url) {
144 ServiceProviderPtr service_provider; 169 ServiceProviderPtr service_provider;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 requestor_url, 279 requestor_url,
255 interceptor_->OnConnectToClient(url, service_provider.Pass())); 280 interceptor_->OnConnectToClient(url, service_provider.Pass()));
256 } else { 281 } else {
257 shell_impl->ConnectToClient(requestor_url, service_provider.Pass()); 282 shell_impl->ConnectToClient(requestor_url, service_provider.Pass());
258 } 283 }
259 } 284 }
260 285
261 void ApplicationManager::RegisterExternalApplication( 286 void ApplicationManager::RegisterExternalApplication(
262 const GURL& url, 287 const GURL& url,
263 ScopedMessagePipeHandle shell_handle) { 288 ScopedMessagePipeHandle shell_handle) {
264 url_to_shell_impl_[url] = 289 url_to_shell_impl_[url] = new ShellImpl(shell_handle.Pass(), this, url, url);
265 WeakBindToPipe(new ShellImpl(this, url, url), shell_handle.Pass());
266 } 290 }
267 291
268 void ApplicationManager::RegisterLoadedApplication( 292 void ApplicationManager::RegisterLoadedApplication(
269 const GURL& requested_url, 293 const GURL& requested_url,
270 const GURL& resolved_url, 294 const GURL& resolved_url,
271 const GURL& requestor_url, 295 const GURL& requestor_url,
272 ServiceProviderPtr service_provider, 296 ServiceProviderPtr service_provider,
273 ScopedMessagePipeHandle* shell_handle) { 297 ScopedMessagePipeHandle* shell_handle) {
274 ShellImpl* shell_impl = NULL; 298 ShellImpl* shell_impl = NULL;
275 URLToShellImplMap::iterator iter = url_to_shell_impl_.find(resolved_url); 299 URLToShellImplMap::iterator iter = url_to_shell_impl_.find(resolved_url);
276 if (iter != url_to_shell_impl_.end()) { 300 if (iter != url_to_shell_impl_.end()) {
277 // This can happen because services are loaded asynchronously. So if we get 301 // This can happen because services are loaded asynchronously. So if we get
278 // two requests for the same service close to each other, we might get here 302 // two requests for the same service close to each other, we might get here
279 // and find that we already have it. 303 // and find that we already have it.
280 shell_impl = iter->second; 304 shell_impl = iter->second;
281 } else { 305 } else {
282 MessagePipe pipe; 306 MessagePipe pipe;
283 shell_impl = WeakBindToPipe( 307 shell_impl =
284 new ShellImpl(this, requested_url, resolved_url), pipe.handle1.Pass()); 308 new ShellImpl(pipe.handle1.Pass(), this, requested_url, resolved_url);
285 url_to_shell_impl_[resolved_url] = shell_impl; 309 url_to_shell_impl_[resolved_url] = shell_impl;
286 *shell_handle = pipe.handle0.Pass(); 310 *shell_handle = pipe.handle0.Pass();
287 shell_impl->client()->Initialize(GetArgsForURL(requested_url)); 311 shell_impl->client()->Initialize(GetArgsForURL(requested_url));
288 } 312 }
289 313
290 ConnectToClient(shell_impl, resolved_url, requestor_url, 314 ConnectToClient(shell_impl, resolved_url, requestor_url,
291 service_provider.Pass()); 315 service_provider.Pass());
292 } 316 }
293 317
294 void ApplicationManager::LoadWithContentHandler( 318 void ApplicationManager::LoadWithContentHandler(
295 const GURL& requested_url, 319 const GURL& requested_url,
296 const GURL& resolved_url, 320 const GURL& resolved_url,
297 const GURL& requestor_url, 321 const GURL& requestor_url,
298 const GURL& content_handler_url, 322 const GURL& content_handler_url,
299 URLResponsePtr url_response, 323 URLResponsePtr url_response,
300 ServiceProviderPtr service_provider) { 324 ServiceProviderPtr service_provider) {
301 ContentHandlerConnection* connection = NULL; 325 ContentHandlerConnection* connection = NULL;
302 URLToContentHandlerMap::iterator iter = 326 URLToContentHandlerMap::iterator iter =
303 url_to_content_handler_.find(content_handler_url); 327 url_to_content_handler_.find(content_handler_url);
304 if (iter != url_to_content_handler_.end()) { 328 if (iter != url_to_content_handler_.end()) {
305 connection = iter->second; 329 connection = iter->second;
306 } else { 330 } else {
307 connection = new ContentHandlerConnection(this, content_handler_url); 331 connection = new ContentHandlerConnection(this, content_handler_url);
308 url_to_content_handler_[content_handler_url] = connection; 332 url_to_content_handler_[content_handler_url] = connection;
309 } 333 }
310 334
311 ShellPtr shell_proxy; 335 ShellPtr shell_proxy;
312 ShellImpl* shell_impl = WeakBindToProxy( 336 ShellImpl* shell_impl =
313 new ShellImpl(this, requested_url, resolved_url), &shell_proxy); 337 new ShellImpl(&shell_proxy, this, requested_url, resolved_url);
314 content_shell_impls_.insert(shell_impl); 338 content_shell_impls_.insert(shell_impl);
315 shell_impl->client()->Initialize(GetArgsForURL(requested_url)); 339 shell_impl->client()->Initialize(GetArgsForURL(requested_url));
316 340
317 connection->content_handler()->StartApplication(shell_proxy.Pass(), 341 connection->content_handler()->StartApplication(shell_proxy.Pass(),
318 url_response.Pass()); 342 url_response.Pass());
319 ConnectToClient( 343 ConnectToClient(
320 shell_impl, resolved_url, requestor_url, service_provider.Pass()); 344 shell_impl, resolved_url, requestor_url, service_provider.Pass());
321 } 345 }
322 346
323 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, 347 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 return pipe.handle0.Pass(); 428 return pipe.handle0.Pass();
405 } 429 }
406 430
407 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) { 431 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) {
408 URLToArgsMap::const_iterator args_it = url_to_args_.find(url); 432 URLToArgsMap::const_iterator args_it = url_to_args_.find(url);
409 if (args_it != url_to_args_.end()) 433 if (args_it != url_to_args_.end())
410 return Array<String>::From(args_it->second); 434 return Array<String>::From(args_it->second);
411 return Array<String>(); 435 return Array<String>();
412 } 436 }
413 } // namespace mojo 437 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/application_manager/application_manager_unittest.cc » ('j') | mojo/public/cpp/bindings/interface_impl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698