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 #include "mojo/shell/application_instance.h" | 5 #include "mojo/shell/application_instance.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 9 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
10 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
11 #include "mojo/common/url_type_converters.h" | 11 #include "mojo/common/url_type_converters.h" |
12 #include "mojo/shell/application_manager.h" | 12 #include "mojo/shell/application_manager.h" |
13 #include "mojo/shell/content_handler_connection.h" | 13 #include "mojo/shell/content_handler_connection.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace shell { | 16 namespace shell { |
17 namespace { | |
18 | |
19 // It's valid to specify mojo: URLs in the filter either as mojo:foo or | |
20 // mojo://foo/ - but we store the filter in the latter form. | |
21 CapabilityFilter CanonicalizeFilter(const CapabilityFilter& filter) { | |
22 CapabilityFilter canonicalized; | |
23 for (CapabilityFilter::const_iterator it = filter.begin(); | |
24 it != filter.end(); | |
25 ++it) { | |
26 if (it->first == "*") | |
27 canonicalized[it->first] = it->second; | |
28 else | |
29 canonicalized[GURL(it->first).spec()] = it->second; | |
30 } | |
31 return canonicalized; | |
32 } | |
33 | |
34 } // namespace | |
35 | 17 |
36 ApplicationInstance::ApplicationInstance( | 18 ApplicationInstance::ApplicationInstance( |
37 ApplicationPtr application, | 19 ApplicationPtr application, |
38 ApplicationManager* manager, | 20 ApplicationManager* manager, |
39 const Identity& originator_identity, | |
40 const Identity& identity, | 21 const Identity& identity, |
41 const CapabilityFilter& filter, | |
42 uint32_t requesting_content_handler_id, | 22 uint32_t requesting_content_handler_id, |
43 const base::Closure& on_application_end) | 23 const base::Closure& on_application_end) |
44 : manager_(manager), | 24 : manager_(manager), |
45 originator_identity_(originator_identity), | |
46 identity_(identity), | 25 identity_(identity), |
47 filter_(CanonicalizeFilter(filter)), | 26 allow_any_application_(identity.filter().size() == 1 && |
48 allow_any_application_(filter.size() == 1 && filter.count("*") == 1), | 27 identity.filter().count("*") == 1), |
49 requesting_content_handler_id_(requesting_content_handler_id), | 28 requesting_content_handler_id_(requesting_content_handler_id), |
50 on_application_end_(on_application_end), | 29 on_application_end_(on_application_end), |
51 application_(application.Pass()), | 30 application_(application.Pass()), |
52 binding_(this), | 31 binding_(this), |
53 queue_requests_(false) { | 32 queue_requests_(false) { |
54 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 33 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
55 } | 34 } |
56 | 35 |
57 ApplicationInstance::~ApplicationInstance() { | 36 ApplicationInstance::~ApplicationInstance() { |
58 for (auto request : queued_client_requests_) | 37 for (auto request : queued_client_requests_) |
59 request->connect_callback().Run(kInvalidContentHandlerID); | 38 request->connect_callback().Run(kInvalidContentHandlerID); |
60 STLDeleteElements(&queued_client_requests_); | 39 STLDeleteElements(&queued_client_requests_); |
61 } | 40 } |
62 | 41 |
63 void ApplicationInstance::InitializeApplication() { | 42 void ApplicationInstance::InitializeApplication() { |
64 ShellPtr shell; | 43 ShellPtr shell; |
65 binding_.Bind(GetProxy(&shell)); | 44 binding_.Bind(GetProxy(&shell)); |
66 application_->Initialize(shell.Pass(), identity_.url.spec()); | 45 application_->Initialize(shell.Pass(), identity_.url().spec()); |
67 } | 46 } |
68 | 47 |
69 void ApplicationInstance::ConnectToClient( | 48 void ApplicationInstance::ConnectToClient( |
70 scoped_ptr<ConnectToApplicationParams> params) { | 49 scoped_ptr<ConnectToApplicationParams> params) { |
71 if (queue_requests_) { | 50 if (queue_requests_) { |
72 queued_client_requests_.push_back(params.release()); | 51 queued_client_requests_.push_back(params.release()); |
73 return; | 52 return; |
74 } | 53 } |
75 | 54 |
76 CallAcceptConnection(params.Pass()); | 55 CallAcceptConnection(params.Pass()); |
77 } | 56 } |
78 | 57 |
79 // Shell implementation: | 58 // Shell implementation: |
80 void ApplicationInstance::ConnectToApplication( | 59 void ApplicationInstance::ConnectToApplication( |
81 URLRequestPtr app_request, | 60 URLRequestPtr app_request, |
82 InterfaceRequest<ServiceProvider> services, | 61 InterfaceRequest<ServiceProvider> services, |
83 ServiceProviderPtr exposed_services, | 62 ServiceProviderPtr exposed_services, |
84 CapabilityFilterPtr filter, | 63 CapabilityFilterPtr filter, |
85 const ConnectToApplicationCallback& callback) { | 64 const ConnectToApplicationCallback& callback) { |
86 std::string url_string = app_request->url.To<std::string>(); | 65 std::string url_string = app_request->url.To<std::string>(); |
87 GURL url(url_string); | 66 GURL url(url_string); |
88 if (!url.is_valid()) { | 67 if (!url.is_valid()) { |
89 LOG(ERROR) << "Error: invalid URL: " << url_string; | 68 LOG(ERROR) << "Error: invalid URL: " << url_string; |
90 callback.Run(kInvalidContentHandlerID); | 69 callback.Run(kInvalidContentHandlerID); |
91 return; | 70 return; |
92 } | 71 } |
93 if (allow_any_application_ || filter_.find(url.spec()) != filter_.end()) { | 72 if (allow_any_application_ || |
| 73 identity_.filter().find(url.spec()) != identity_.filter().end()) { |
94 CapabilityFilter capability_filter = GetPermissiveCapabilityFilter(); | 74 CapabilityFilter capability_filter = GetPermissiveCapabilityFilter(); |
95 if (!filter.is_null()) | 75 if (!filter.is_null()) |
96 capability_filter = filter->filter.To<CapabilityFilter>(); | 76 capability_filter = filter->filter.To<CapabilityFilter>(); |
97 | 77 |
98 scoped_ptr<ConnectToApplicationParams> params( | 78 scoped_ptr<ConnectToApplicationParams> params( |
99 new ConnectToApplicationParams); | 79 new ConnectToApplicationParams); |
100 params->SetOriginatorInfo(this); | 80 params->SetSource(this); |
101 params->SetURLInfo(app_request.Pass()); | 81 GURL app_url(app_request->url); |
| 82 params->SetTargetURLRequest( |
| 83 app_request.Pass(), |
| 84 Identity(app_url, std::string(), capability_filter)); |
102 params->set_services(services.Pass()); | 85 params->set_services(services.Pass()); |
103 params->set_exposed_services(exposed_services.Pass()); | 86 params->set_exposed_services(exposed_services.Pass()); |
104 params->set_filter(capability_filter); | |
105 params->set_connect_callback(callback); | 87 params->set_connect_callback(callback); |
106 manager_->ConnectToApplication(params.Pass()); | 88 manager_->ConnectToApplication(params.Pass()); |
107 } else { | 89 } else { |
108 LOG(WARNING) << "CapabilityFilter prevented connection from: " << | 90 LOG(WARNING) << "CapabilityFilter prevented connection from: " << |
109 identity_.url << " to: " << url.spec(); | 91 identity_.url() << " to: " << url.spec(); |
110 callback.Run(kInvalidContentHandlerID); | 92 callback.Run(kInvalidContentHandlerID); |
111 } | 93 } |
112 } | 94 } |
113 | 95 |
114 void ApplicationInstance::QuitApplication() { | 96 void ApplicationInstance::QuitApplication() { |
115 queue_requests_ = true; | 97 queue_requests_ = true; |
116 application_->OnQuitRequested( | 98 application_->OnQuitRequested( |
117 base::Bind(&ApplicationInstance::OnQuitRequestedResult, | 99 base::Bind(&ApplicationInstance::OnQuitRequestedResult, |
118 base::Unretained(this))); | 100 base::Unretained(this))); |
119 } | 101 } |
120 | 102 |
121 void ApplicationInstance::CallAcceptConnection( | 103 void ApplicationInstance::CallAcceptConnection( |
122 scoped_ptr<ConnectToApplicationParams> params) { | 104 scoped_ptr<ConnectToApplicationParams> params) { |
123 params->connect_callback().Run(requesting_content_handler_id_); | 105 params->connect_callback().Run(requesting_content_handler_id_); |
124 AllowedInterfaces interfaces; | 106 AllowedInterfaces interfaces; |
125 interfaces.insert("*"); | 107 interfaces.insert("*"); |
126 if (!params->originator_identity().is_null()) | 108 if (!params->source().is_null()) |
127 interfaces = GetAllowedInterfaces(params->originator_filter(), identity_); | 109 interfaces = GetAllowedInterfaces(params->source().filter(), identity_); |
128 | 110 |
129 application_->AcceptConnection( | 111 application_->AcceptConnection( |
130 params->originator_identity().url.spec(), params->TakeServices(), | 112 params->source().url().spec(), params->TakeServices(), |
131 params->TakeExposedServices(), Array<String>::From(interfaces).Pass(), | 113 params->TakeExposedServices(), Array<String>::From(interfaces).Pass(), |
132 params->app_url().spec()); | 114 params->target().url().spec()); |
133 } | 115 } |
134 | 116 |
135 void ApplicationInstance::OnConnectionError() { | 117 void ApplicationInstance::OnConnectionError() { |
136 std::vector<ConnectToApplicationParams*> queued_client_requests; | 118 std::vector<ConnectToApplicationParams*> queued_client_requests; |
137 queued_client_requests_.swap(queued_client_requests); | 119 queued_client_requests_.swap(queued_client_requests); |
138 auto manager = manager_; | 120 auto manager = manager_; |
139 manager_->OnApplicationInstanceError(this); | 121 manager_->OnApplicationInstanceError(this); |
140 //|this| is deleted. | 122 //|this| is deleted. |
141 | 123 |
142 // If any queued requests came to shell during time it was shutting down, | 124 // If any queued requests came to shell during time it was shutting down, |
143 // start them now. | 125 // start them now. |
144 for (auto request : queued_client_requests) { | 126 for (auto request : queued_client_requests) { |
145 // Unfortunately, it is possible that |request->app_url_request()| is null | 127 // Unfortunately, it is possible that |request->target_url_request()| is |
146 // at this point. Consider the following sequence: | 128 // null at this point. Consider the following sequence: |
147 // 1) connect_request_1 arrives at the application manager; the manager | 129 // 1) connect_request_1 arrives at the application manager; the manager |
148 // decides to fetch the app. | 130 // decides to fetch the app. |
149 // 2) connect_request_2 arrives for the same app; because the app is not | 131 // 2) connect_request_2 arrives for the same app; because the app is not |
150 // running yet, the manager decides to fetch the app again. | 132 // running yet, the manager decides to fetch the app again. |
151 // 3) The fetch for step (1) completes and an application instance app_a is | 133 // 3) The fetch for step (1) completes and an application instance app_a is |
152 // registered. | 134 // registered. |
153 // 4) app_a goes into two-phase shutdown. | 135 // 4) app_a goes into two-phase shutdown. |
154 // 5) The fetch for step (2) completes; the manager finds that there is a | 136 // 5) The fetch for step (2) completes; the manager finds that there is a |
155 // running app already, so it connects to app_a. | 137 // running app already, so it connects to app_a. |
156 // 6) connect_request_2 is queued (and eventually gets here), but its | 138 // 6) connect_request_2 is queued (and eventually gets here), but its |
157 // original_request field was already lost to NetworkFetcher at step (2). | 139 // original_request field was already lost to NetworkFetcher at step (2). |
158 // | 140 // |
159 // TODO(yzshen): It seems we should register a pending application instance | 141 // TODO(yzshen): It seems we should register a pending application instance |
160 // before starting the fetch. So at step (2) the application manager knows | 142 // before starting the fetch. So at step (2) the application manager knows |
161 // that it can wait for the first fetch to complete instead of doing a | 143 // that it can wait for the first fetch to complete instead of doing a |
162 // second one directly. | 144 // second one directly. |
163 if (!request->app_url_request()) { | 145 if (!request->target_url_request()) { |
164 URLRequestPtr url_request = mojo::URLRequest::New(); | 146 URLRequestPtr url_request = mojo::URLRequest::New(); |
165 url_request->url = request->app_url().spec(); | 147 url_request->url = request->target().url().spec(); |
166 request->SetURLInfo(url_request.Pass()); | 148 request->SetTargetURLRequest(url_request.Pass(), request->target()); |
167 } | 149 } |
168 manager->ConnectToApplication(make_scoped_ptr(request)); | 150 manager->ConnectToApplication(make_scoped_ptr(request)); |
169 } | 151 } |
170 } | 152 } |
171 | 153 |
172 void ApplicationInstance::OnQuitRequestedResult(bool can_quit) { | 154 void ApplicationInstance::OnQuitRequestedResult(bool can_quit) { |
173 if (can_quit) | 155 if (can_quit) |
174 return; | 156 return; |
175 | 157 |
176 queue_requests_ = false; | 158 queue_requests_ = false; |
177 for (auto request : queued_client_requests_) | 159 for (auto request : queued_client_requests_) |
178 CallAcceptConnection(make_scoped_ptr(request)); | 160 CallAcceptConnection(make_scoped_ptr(request)); |
179 | 161 |
180 queued_client_requests_.clear(); | 162 queued_client_requests_.clear(); |
181 } | 163 } |
182 | 164 |
183 } // namespace shell | 165 } // namespace shell |
184 } // namespace mojo | 166 } // namespace mojo |
OLD | NEW |