OLD | NEW |
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" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 TerminateShellConnections(); | 66 TerminateShellConnections(); |
67 STLDeleteValues(&url_to_loader_); | 67 STLDeleteValues(&url_to_loader_); |
68 } | 68 } |
69 | 69 |
70 void ApplicationManager::TerminateShellConnections() { | 70 void ApplicationManager::TerminateShellConnections() { |
71 STLDeleteValues(&identity_to_instance_); | 71 STLDeleteValues(&identity_to_instance_); |
72 } | 72 } |
73 | 73 |
74 void ApplicationManager::ConnectToApplication( | 74 void ApplicationManager::ConnectToApplication( |
75 scoped_ptr<ConnectToApplicationParams> params) { | 75 scoped_ptr<ConnectToApplicationParams> params) { |
| 76 GURL original_url = params->app_url(); |
| 77 URLRequestPtr original_url_request = params->TakeAppURLRequest(); |
| 78 |
76 TRACE_EVENT_INSTANT1("mojo_shell", "ApplicationManager::ConnectToApplication", | 79 TRACE_EVENT_INSTANT1("mojo_shell", "ApplicationManager::ConnectToApplication", |
77 TRACE_EVENT_SCOPE_THREAD, "original_url", | 80 TRACE_EVENT_SCOPE_THREAD, "original_url", |
78 params->app_url().spec()); | 81 original_url.spec()); |
79 DCHECK(params->app_url().is_valid()); | 82 DCHECK(original_url.is_valid()); |
| 83 DCHECK(original_url_request); |
80 | 84 |
81 // Connect to an existing matching instance, if possible. | 85 // We need to look for running instances based on both the unresolved and |
| 86 // resolved urls. |
82 if (ConnectToRunningApplication(¶ms)) | 87 if (ConnectToRunningApplication(¶ms)) |
83 return; | 88 return; |
84 | 89 |
85 ApplicationLoader* loader = GetLoaderForURL(params->app_url()); | 90 GURL resolved_url = package_manager_->ResolveURL(original_url); |
| 91 params->SetURLInfo(resolved_url); |
| 92 if (ConnectToRunningApplication(¶ms)) |
| 93 return; |
| 94 |
| 95 // The application is not running, let's compute the parameters. |
| 96 // NOTE: Set URL info using |original_url_request| instead of |original_url| |
| 97 // because it may contain more information (e.g., it is a POST request). |
| 98 params->SetURLInfo(original_url_request.Pass()); |
| 99 ApplicationLoader* loader = GetLoaderForURL(resolved_url); |
86 if (loader) { | 100 if (loader) { |
87 GURL url = params->app_url(); | 101 ConnectToApplicationWithLoader(¶ms, resolved_url, loader); |
88 loader->Load(url, CreateInstance(params.Pass(), nullptr)); | |
89 return; | 102 return; |
90 } | 103 } |
91 | 104 |
92 URLRequestPtr original_url_request = params->TakeAppURLRequest(); | 105 original_url_request = params->TakeAppURLRequest(); |
93 auto callback = | 106 auto callback = |
94 base::Bind(&ApplicationManager::HandleFetchCallback, | 107 base::Bind(&ApplicationManager::HandleFetchCallback, |
95 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms)); | 108 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms)); |
96 package_manager_->FetchRequest(original_url_request.Pass(), callback); | 109 package_manager_->FetchRequest(original_url_request.Pass(), callback); |
97 } | 110 } |
98 | 111 |
99 bool ApplicationManager::ConnectToRunningApplication( | 112 bool ApplicationManager::ConnectToRunningApplication( |
100 scoped_ptr<ConnectToApplicationParams>* params) { | 113 scoped_ptr<ConnectToApplicationParams>* params) { |
101 ApplicationInstance* instance = GetApplicationInstance( | 114 ApplicationInstance* instance = GetApplicationInstance( |
102 Identity((*params)->app_url(), (*params)->qualifier())); | 115 Identity((*params)->app_url(), (*params)->qualifier())); |
103 if (!instance) | 116 if (!instance) |
104 return false; | 117 return false; |
105 | 118 |
106 instance->ConnectToClient(params->Pass()); | 119 instance->ConnectToClient(params->Pass()); |
107 return true; | 120 return true; |
108 } | 121 } |
109 | 122 |
| 123 void ApplicationManager::ConnectToApplicationWithLoader( |
| 124 scoped_ptr<ConnectToApplicationParams>* params, |
| 125 const GURL& resolved_url, |
| 126 ApplicationLoader* loader) { |
| 127 if (!(*params)->app_url().SchemeIs("mojo")) |
| 128 (*params)->SetURLInfo(resolved_url); |
| 129 |
| 130 loader->Load(resolved_url, CreateInstance(params->Pass(), nullptr)); |
| 131 } |
| 132 |
110 InterfaceRequest<Application> ApplicationManager::CreateInstance( | 133 InterfaceRequest<Application> ApplicationManager::CreateInstance( |
111 scoped_ptr<ConnectToApplicationParams> params, | 134 scoped_ptr<ConnectToApplicationParams> params, |
112 ApplicationInstance** resulting_instance) { | 135 ApplicationInstance** resulting_instance) { |
113 Identity app_identity(params->app_url(), params->qualifier()); | 136 Identity app_identity(params->app_url(), params->qualifier()); |
114 | 137 |
115 ApplicationPtr application; | 138 ApplicationPtr application; |
116 InterfaceRequest<Application> application_request = GetProxy(&application); | 139 InterfaceRequest<Application> application_request = GetProxy(&application); |
117 ApplicationInstance* instance = new ApplicationInstance( | 140 ApplicationInstance* instance = new ApplicationInstance( |
118 application.Pass(), this, params->originator_identity(), app_identity, | 141 application.Pass(), this, params->originator_identity(), app_identity, |
119 params->filter(), Shell::kInvalidContentHandlerID, | 142 params->filter(), Shell::kInvalidContentHandlerID, |
120 params->on_application_end()); | 143 params->on_application_end()); |
121 DCHECK(identity_to_instance_.find(app_identity) == | 144 DCHECK(identity_to_instance_.find(app_identity) == |
122 identity_to_instance_.end()); | 145 identity_to_instance_.end()); |
123 identity_to_instance_[app_identity] = instance; | 146 identity_to_instance_[app_identity] = instance; |
124 instance->InitializeApplication(); | 147 instance->InitializeApplication(); |
125 instance->ConnectToClient(params.Pass()); | 148 instance->ConnectToClient(params.Pass()); |
126 if (resulting_instance) | 149 if (resulting_instance) |
127 *resulting_instance = instance; | 150 *resulting_instance = instance; |
128 return application_request.Pass(); | 151 return application_request.Pass(); |
129 } | 152 } |
130 | 153 |
131 ApplicationInstance* ApplicationManager::GetApplicationInstance( | 154 ApplicationInstance* ApplicationManager::GetApplicationInstance( |
132 const Identity& identity) const { | 155 const Identity& identity) const { |
133 const auto& it = identity_to_instance_.find(identity); | 156 const auto& instance_it = identity_to_instance_.find(identity); |
134 return it != identity_to_instance_.end() ? it->second : nullptr; | 157 if (instance_it != identity_to_instance_.end()) |
| 158 return instance_it->second; |
| 159 return nullptr; |
135 } | 160 } |
136 | 161 |
137 void ApplicationManager::HandleFetchCallback( | 162 void ApplicationManager::HandleFetchCallback( |
138 scoped_ptr<ConnectToApplicationParams> params, | 163 scoped_ptr<ConnectToApplicationParams> params, |
139 scoped_ptr<Fetcher> fetcher) { | 164 scoped_ptr<Fetcher> fetcher) { |
140 if (!fetcher) { | 165 if (!fetcher) { |
141 // Network error. Drop |params| to tell the requestor. | 166 // Network error. Drop |params| to tell the requestor. |
142 params->connect_callback().Run(Shell::kInvalidContentHandlerID); | 167 params->connect_callback().Run(Shell::kInvalidContentHandlerID); |
143 return; | 168 return; |
144 } | 169 } |
145 | 170 |
146 GURL redirect_url = fetcher->GetRedirectURL(); | 171 GURL redirect_url = fetcher->GetRedirectURL(); |
147 if (!redirect_url.is_empty()) { | 172 if (!redirect_url.is_empty()) { |
148 // And around we go again... Whee! | 173 // And around we go again... Whee! |
149 // TODO(sky): this loses the original URL info. | 174 // TODO(sky): this loses the original URL info. |
150 URLRequestPtr new_request = URLRequest::New(); | 175 URLRequestPtr new_request = URLRequest::New(); |
151 new_request->url = redirect_url.spec(); | 176 new_request->url = redirect_url.spec(); |
152 HttpHeaderPtr header = HttpHeader::New(); | 177 HttpHeaderPtr header = HttpHeader::New(); |
153 header->name = "Referer"; | 178 header->name = "Referer"; |
154 header->value = fetcher->GetRedirectReferer().spec(); | 179 header->value = fetcher->GetRedirectReferer().spec(); |
155 new_request->headers.push_back(header.Pass()); | 180 new_request->headers.push_back(header.Pass()); |
156 params->SetURLInfo(new_request.Pass()); | 181 params->SetURLInfo(new_request.Pass()); |
157 ConnectToApplication(params.Pass()); | 182 ConnectToApplication(params.Pass()); |
158 return; | 183 return; |
159 } | 184 } |
160 | 185 |
161 // We already checked if the application was running before we fetched it, but | 186 // We already checked if the application was running before we fetched it, but |
162 // it might have started while the fetch was outstanding. We don't want to | 187 // it might have started while the fetch was outstanding. We don't want to |
163 // have two copies of the app running, so check again. | 188 // have two copies of the app running, so check again. |
| 189 // |
| 190 // Also, it's possible the original URL was redirected to an app that is |
| 191 // already running. |
164 if (ConnectToRunningApplication(¶ms)) | 192 if (ConnectToRunningApplication(¶ms)) |
165 return; | 193 return; |
166 | 194 |
| 195 if (params->app_url().scheme() != "mojo") |
| 196 params->SetURLInfo(fetcher->GetURL()); |
| 197 |
167 Identity originator_identity = params->originator_identity(); | 198 Identity originator_identity = params->originator_identity(); |
168 CapabilityFilter originator_filter = params->originator_filter(); | 199 CapabilityFilter originator_filter = params->originator_filter(); |
169 CapabilityFilter filter = params->filter(); | 200 CapabilityFilter filter = params->filter(); |
170 GURL app_url = params->app_url(); | 201 GURL app_url = params->app_url(); |
171 std::string qualifier = params->qualifier(); | 202 std::string qualifier = params->qualifier(); |
172 Shell::ConnectToApplicationCallback connect_callback = | 203 Shell::ConnectToApplicationCallback connect_callback = |
173 params->connect_callback(); | 204 params->connect_callback(); |
174 params->set_connect_callback(EmptyConnectCallback()); | 205 params->set_connect_callback(EmptyConnectCallback()); |
175 ApplicationInstance* app = nullptr; | 206 ApplicationInstance* app = nullptr; |
176 InterfaceRequest<Application> request(CreateInstance(params.Pass(), &app)); | 207 InterfaceRequest<Application> request(CreateInstance(params.Pass(), &app)); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 native_runners_.erase( | 341 native_runners_.erase( |
311 std::find(native_runners_.begin(), native_runners_.end(), runner)); | 342 std::find(native_runners_.begin(), native_runners_.end(), runner)); |
312 } | 343 } |
313 | 344 |
314 Shell::ConnectToApplicationCallback EmptyConnectCallback() { | 345 Shell::ConnectToApplicationCallback EmptyConnectCallback() { |
315 return base::Bind(&OnEmptyOnConnectCallback); | 346 return base::Bind(&OnEmptyOnConnectCallback); |
316 } | 347 } |
317 | 348 |
318 } // namespace shell | 349 } // namespace shell |
319 } // namespace mojo | 350 } // namespace mojo |
OLD | NEW |