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
3 // found in the LICENSE file.
5 #include "mojo/shell/shell_impl.h"
8 #include "base/stl_util.h"
9 #include "mojo/application/public/interfaces/content_handler.mojom.h"
10 #include "mojo/common/common_type_converters.h"
11 #include "mojo/common/url_type_converters.h"
12 #include "mojo/shell/application_manager.h"
17 ShellImpl::QueuedClientRequest::QueuedClientRequest() {
20 ShellImpl::QueuedClientRequest::~QueuedClientRequest() {
23 ShellImpl::ShellImpl(ApplicationPtr application
,
24 ApplicationManager
* manager
,
25 const Identity
& identity
,
26 const base::Closure
& on_application_end
)
29 on_application_end_(on_application_end
),
30 application_(application
.Pass()),
32 queue_requests_(false) {
33 binding_
.set_error_handler(this);
36 ShellImpl::~ShellImpl() {
37 STLDeleteElements(&queued_client_requests_
);
40 void ShellImpl::InitializeApplication() {
42 binding_
.Bind(GetProxy(&shell
));
43 application_
->Initialize(shell
.Pass(), identity_
.url
.spec());
46 void ShellImpl::ConnectToClient(const GURL
& requested_url
,
47 const GURL
& requestor_url
,
48 InterfaceRequest
<ServiceProvider
> services
,
49 ServiceProviderPtr exposed_services
) {
50 if (queue_requests_
) {
51 QueuedClientRequest
* queued_request
= new QueuedClientRequest
;
52 queued_request
->requested_url
= requested_url
;
53 queued_request
->requestor_url
= requestor_url
;
54 queued_request
->services
= services
.Pass();
55 queued_request
->exposed_services
= exposed_services
.Pass();
56 queued_client_requests_
.push_back(queued_request
);
60 application_
->AcceptConnection(requestor_url
.spec(), services
.Pass(),
61 exposed_services
.Pass(), requested_url
.spec());
64 // Shell implementation:
65 void ShellImpl::ConnectToApplication(mojo::URLRequestPtr app_request
,
66 InterfaceRequest
<ServiceProvider
> services
,
67 ServiceProviderPtr exposed_services
) {
68 GURL
app_gurl(app_request
->url
.To
<std::string
>());
69 if (!app_gurl
.is_valid()) {
70 LOG(ERROR
) << "Error: invalid URL: " << app_request
;
73 manager_
->ConnectToApplication(app_request
.Pass(), identity_
.url
,
74 services
.Pass(), exposed_services
.Pass(),
78 void ShellImpl::QuitApplication() {
79 queue_requests_
= true;
80 application_
->OnQuitRequested(base::Bind(&ShellImpl::OnQuitRequestedResult
,
81 base::Unretained(this)));
84 void ShellImpl::OnConnectionError() {
85 std::vector
<QueuedClientRequest
*> queued_client_requests
;
86 queued_client_requests_
.swap(queued_client_requests
);
87 auto manager
= manager_
;
88 manager_
->OnShellImplError(this);
91 // If any queued requests came to shell during time it was shutting down,
93 for (auto request
: queued_client_requests
) {
94 mojo::URLRequestPtr
url(mojo::URLRequest::New());
95 url
->url
= mojo::String::From(request
->requested_url
.spec());
96 manager
->ConnectToApplication(url
.Pass(), request
->requestor_url
,
97 request
->services
.Pass(),
98 request
->exposed_services
.Pass(),
101 STLDeleteElements(&queued_client_requests
);
104 void ShellImpl::OnQuitRequestedResult(bool can_quit
) {
108 queue_requests_
= false;
109 for (auto request
: queued_client_requests_
) {
110 application_
->AcceptConnection(request
->requestor_url
.spec(),
111 request
->services
.Pass(),
112 request
->exposed_services
.Pass(),
113 request
->requested_url
.spec());
115 STLDeleteElements(&queued_client_requests_
);