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

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

Issue 1228743002: Mandoline: Move ContentHandlerConnection into separate file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix build Created 5 years, 5 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 | « mojo/shell/application_manager.h ('k') | mojo/shell/application_manager_unittest.cc » ('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 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/shell/application_manager.h" 5 #include "mojo/shell/application_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
14 #include "mojo/application/public/interfaces/content_handler.mojom.h" 14 #include "mojo/application/public/interfaces/content_handler.mojom.h"
15 #include "mojo/public/cpp/bindings/binding.h" 15 #include "mojo/public/cpp/bindings/binding.h"
16 #include "mojo/public/cpp/bindings/error_handler.h" 16 #include "mojo/public/cpp/bindings/error_handler.h"
17 #include "mojo/shell/content_handler_connection.h"
17 #include "mojo/shell/fetcher.h" 18 #include "mojo/shell/fetcher.h"
18 #include "mojo/shell/local_fetcher.h" 19 #include "mojo/shell/local_fetcher.h"
19 #include "mojo/shell/network_fetcher.h" 20 #include "mojo/shell/network_fetcher.h"
20 #include "mojo/shell/query_util.h" 21 #include "mojo/shell/query_util.h"
21 #include "mojo/shell/shell_impl.h" 22 #include "mojo/shell/shell_impl.h"
22 #include "mojo/shell/switches.h" 23 #include "mojo/shell/switches.h"
23 #include "mojo/shell/update_fetcher.h" 24 #include "mojo/shell/update_fetcher.h"
24 25
25 namespace mojo { 26 namespace mojo {
26 namespace shell { 27 namespace shell {
27 28
28 namespace { 29 namespace {
29 30
30 // Used by TestAPI. 31 // Used by TestAPI.
31 bool has_created_instance = false; 32 bool has_created_instance = false;
32 33
33 } // namespace 34 } // namespace
34 35
35 class ApplicationManager::ContentHandlerConnection : public ErrorHandler {
36 public:
37 ContentHandlerConnection(ApplicationManager* manager,
38 const GURL& content_handler_url,
39 const GURL& requestor_url,
40 const std::string& qualifier)
41 : manager_(manager),
42 content_handler_url_(content_handler_url),
43 content_handler_qualifier_(qualifier) {
44 ServiceProviderPtr services;
45 mojo::URLRequestPtr request(mojo::URLRequest::New());
46 request->url = mojo::String::From(content_handler_url.spec());
47 manager->ConnectToApplicationInternal(
48 request.Pass(), qualifier, requestor_url, GetProxy(&services),
49 nullptr, base::Closure());
50 MessagePipe pipe;
51 content_handler_.Bind(
52 InterfacePtrInfo<ContentHandler>(pipe.handle0.Pass(), 0u));
53 services->ConnectToService(ContentHandler::Name_, pipe.handle1.Pass());
54 content_handler_.set_error_handler(this);
55 }
56
57 ContentHandler* content_handler() { return content_handler_.get(); }
58
59 GURL content_handler_url() { return content_handler_url_; }
60 std::string content_handler_qualifier() { return content_handler_qualifier_; }
61
62 private:
63 // ErrorHandler implementation:
64 void OnConnectionError() override { manager_->OnContentHandlerError(this); }
65
66 ApplicationManager* manager_;
67 GURL content_handler_url_;
68 std::string content_handler_qualifier_;
69 ContentHandlerPtr content_handler_;
70
71 DISALLOW_COPY_AND_ASSIGN(ContentHandlerConnection);
72 };
73
74 // static 36 // static
75 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) 37 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager)
76 : manager_(manager) { 38 : manager_(manager) {
77 } 39 }
78 40
79 ApplicationManager::TestAPI::~TestAPI() { 41 ApplicationManager::TestAPI::~TestAPI() {
80 } 42 }
81 43
82 bool ApplicationManager::TestAPI::HasCreatedInstance() { 44 bool ApplicationManager::TestAPI::HasCreatedInstance() {
83 return has_created_instance; 45 return has_created_instance;
84 } 46 }
85 47
86 bool ApplicationManager::TestAPI::HasFactoryForURL(const GURL& url) const { 48 bool ApplicationManager::TestAPI::HasFactoryForURL(const GURL& url) const {
87 return manager_->identity_to_shell_impl_.find(Identity(url)) != 49 return manager_->identity_to_shell_impl_.find(Identity(url)) !=
88 manager_->identity_to_shell_impl_.end(); 50 manager_->identity_to_shell_impl_.end();
89 } 51 }
90 52
91 ApplicationManager::ApplicationManager(Delegate* delegate) 53 ApplicationManager::ApplicationManager(Delegate* delegate)
92 : delegate_(delegate), weak_ptr_factory_(this) { 54 : delegate_(delegate), weak_ptr_factory_(this) {
93 } 55 }
94 56
95 ApplicationManager::~ApplicationManager() { 57 ApplicationManager::~ApplicationManager() {
96 STLDeleteValues(&url_to_content_handler_); 58 URLToContentHandlerMap url_to_content_handler(url_to_content_handler_);
59 for (auto& pair : url_to_content_handler)
60 pair.second->CloseConnection();
97 TerminateShellConnections(); 61 TerminateShellConnections();
98 STLDeleteValues(&url_to_loader_); 62 STLDeleteValues(&url_to_loader_);
99 STLDeleteValues(&scheme_to_loader_); 63 STLDeleteValues(&scheme_to_loader_);
100 } 64 }
101 65
102 void ApplicationManager::TerminateShellConnections() { 66 void ApplicationManager::TerminateShellConnections() {
103 STLDeleteValues(&identity_to_shell_impl_); 67 STLDeleteValues(&identity_to_shell_impl_);
104 } 68 }
105 69
106 void ApplicationManager::ConnectToApplication( 70 void ApplicationManager::ConnectToApplication(
107 mojo::URLRequestPtr requested_url, 71 mojo::URLRequestPtr requested_url,
108 const GURL& requestor_url,
109 InterfaceRequest<ServiceProvider> services,
110 ServiceProviderPtr exposed_services,
111 const base::Closure& on_application_end) {
112 ConnectToApplicationInternal(
113 requested_url.Pass(), std::string(), requestor_url, services.Pass(),
114 exposed_services.Pass(), on_application_end);
115 }
116
117 void ApplicationManager::ConnectToApplicationInternal(
118 mojo::URLRequestPtr requested_url,
119 const std::string& qualifier, 72 const std::string& qualifier,
120 const GURL& requestor_url, 73 const GURL& requestor_url,
121 InterfaceRequest<ServiceProvider> services, 74 InterfaceRequest<ServiceProvider> services,
122 ServiceProviderPtr exposed_services, 75 ServiceProviderPtr exposed_services,
123 const base::Closure& on_application_end) { 76 const base::Closure& on_application_end) {
124 GURL requested_gurl(requested_url->url.To<std::string>()); 77 GURL requested_gurl(requested_url->url.To<std::string>());
125 TRACE_EVENT_INSTANT1( 78 TRACE_EVENT_INSTANT1(
126 "mojo_shell", "ApplicationManager::ConnectToApplication", 79 "mojo_shell", "ApplicationManager::ConnectToApplication",
127 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_gurl.spec()); 80 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_gurl.spec());
128 DCHECK(requested_gurl.is_valid()); 81 DCHECK(requested_gurl.is_valid());
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 GURL redirect_url = fetcher->GetRedirectURL(); 259 GURL redirect_url = fetcher->GetRedirectURL();
307 if (!redirect_url.is_empty()) { 260 if (!redirect_url.is_empty()) {
308 // And around we go again... Whee! 261 // And around we go again... Whee!
309 // TODO(sky): this loses |requested_url|. 262 // TODO(sky): this loses |requested_url|.
310 mojo::URLRequestPtr request(mojo::URLRequest::New()); 263 mojo::URLRequestPtr request(mojo::URLRequest::New());
311 request->url = mojo::String::From(redirect_url.spec()); 264 request->url = mojo::String::From(redirect_url.spec());
312 HttpHeaderPtr header = HttpHeader::New(); 265 HttpHeaderPtr header = HttpHeader::New();
313 header->name = "Referer"; 266 header->name = "Referer";
314 header->value = fetcher->GetRedirectReferer().spec(); 267 header->value = fetcher->GetRedirectReferer().spec();
315 request->headers.push_back(header.Pass()); 268 request->headers.push_back(header.Pass());
316 ConnectToApplicationInternal(request.Pass(), qualifier, requestor_url, 269 ConnectToApplication(request.Pass(), qualifier, requestor_url,
317 services.Pass(), exposed_services.Pass(), 270 services.Pass(), exposed_services.Pass(),
318 on_application_end); 271 on_application_end);
319 return; 272 return;
320 } 273 }
321 274
322 // We already checked if the application was running before we fetched it, but 275 // We already checked if the application was running before we fetched it, but
323 // it might have started while the fetch was outstanding. We don't want to 276 // it might have started while the fetch was outstanding. We don't want to
324 // have two copies of the app running, so check again. 277 // have two copies of the app running, so check again.
325 // 278 //
326 // Also, it's possible the original URL was redirected to an app that is 279 // Also, it's possible the original URL was redirected to an app that is
327 // already running. 280 // already running.
328 if (ConnectToRunningApplication(requested_url, qualifier, requestor_url, 281 if (ConnectToRunningApplication(requested_url, qualifier, requestor_url,
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 base::Closure on_application_end = shell_impl->on_application_end(); 469 base::Closure on_application_end = shell_impl->on_application_end();
517 // Remove the shell. 470 // Remove the shell.
518 auto it = identity_to_shell_impl_.find(identity); 471 auto it = identity_to_shell_impl_.find(identity);
519 DCHECK(it != identity_to_shell_impl_.end()); 472 DCHECK(it != identity_to_shell_impl_.end());
520 delete it->second; 473 delete it->second;
521 identity_to_shell_impl_.erase(it); 474 identity_to_shell_impl_.erase(it);
522 if (!on_application_end.is_null()) 475 if (!on_application_end.is_null())
523 on_application_end.Run(); 476 on_application_end.Run();
524 } 477 }
525 478
526 void ApplicationManager::OnContentHandlerError( 479 void ApplicationManager::OnContentHandlerConnectionClosed(
527 ContentHandlerConnection* content_handler) { 480 ContentHandlerConnection* content_handler) {
528 // Remove the mapping to the content handler. 481 // Remove the mapping to the content handler.
529 auto it = url_to_content_handler_.find( 482 auto it = url_to_content_handler_.find(
530 std::make_pair(content_handler->content_handler_url(), 483 std::make_pair(content_handler->content_handler_url(),
531 content_handler->content_handler_qualifier())); 484 content_handler->content_handler_qualifier()));
532 DCHECK(it != url_to_content_handler_.end()); 485 DCHECK(it != url_to_content_handler_.end());
533 delete it->second;
534 url_to_content_handler_.erase(it); 486 url_to_content_handler_.erase(it);
535 } 487 }
536 488
537 ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( 489 ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName(
538 const GURL& application_url, 490 const GURL& application_url,
539 const std::string& interface_name) { 491 const std::string& interface_name) {
540 ServiceProviderPtr services; 492 ServiceProviderPtr services;
541 mojo::URLRequestPtr request(mojo::URLRequest::New()); 493 mojo::URLRequestPtr request(mojo::URLRequest::New());
542 request->url = mojo::String::From(application_url.spec()); 494 request->url = mojo::String::From(application_url.spec());
543 ConnectToApplication(request.Pass(), GURL(), GetProxy(&services), nullptr, 495 ConnectToApplication(request.Pass(), std::string(), GURL(),
544 base::Closure()); 496 GetProxy(&services), nullptr, base::Closure());
545 MessagePipe pipe; 497 MessagePipe pipe;
546 services->ConnectToService(interface_name, pipe.handle1.Pass()); 498 services->ConnectToService(interface_name, pipe.handle1.Pass());
547 return pipe.handle0.Pass(); 499 return pipe.handle0.Pass();
548 } 500 }
549 501
550 void ApplicationManager::CleanupRunner(NativeRunner* runner) { 502 void ApplicationManager::CleanupRunner(NativeRunner* runner) {
551 native_runners_.erase( 503 native_runners_.erase(
552 std::find(native_runners_.begin(), native_runners_.end(), runner)); 504 std::find(native_runners_.begin(), native_runners_.end(), runner));
553 } 505 }
554 506
555 } // namespace shell 507 } // namespace shell
556 } // namespace mojo 508 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/application_manager.h ('k') | mojo/shell/application_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698