Chromium Code Reviews| 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" |
| 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" |
|
yzshen1
2015/09/23 16:53:46
This include is probably no longer needed.
| |
| 15 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
| 16 #include "mojo/shell/application_instance.h" | 16 #include "mojo/shell/application_instance.h" |
| 17 #include "mojo/shell/content_handler_connection.h" | |
| 18 #include "mojo/shell/fetcher.h" | 17 #include "mojo/shell/fetcher.h" |
| 19 #include "mojo/shell/package_manager.h" | 18 #include "mojo/shell/package_manager.h" |
| 20 #include "mojo/shell/query_util.h" | 19 #include "mojo/shell/query_util.h" |
| 21 #include "mojo/shell/switches.h" | 20 #include "mojo/shell/switches.h" |
| 22 | 21 |
| 23 namespace mojo { | 22 namespace mojo { |
| 24 namespace shell { | 23 namespace shell { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 46 | 45 |
| 47 bool ApplicationManager::TestAPI::HasRunningInstanceForURL( | 46 bool ApplicationManager::TestAPI::HasRunningInstanceForURL( |
| 48 const GURL& url) const { | 47 const GURL& url) const { |
| 49 return manager_->identity_to_instance_.find(Identity(url)) != | 48 return manager_->identity_to_instance_.find(Identity(url)) != |
| 50 manager_->identity_to_instance_.end(); | 49 manager_->identity_to_instance_.end(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 ApplicationManager::ApplicationManager( | 52 ApplicationManager::ApplicationManager( |
| 54 scoped_ptr<PackageManager> package_manager) | 53 scoped_ptr<PackageManager> package_manager) |
| 55 : package_manager_(package_manager.Pass()), | 54 : package_manager_(package_manager.Pass()), |
| 56 content_handler_id_counter_(0u), | 55 task_runner_(nullptr), |
| 56 weak_ptr_factory_(this) { | |
| 57 package_manager_->SetApplicationManager(this); | |
| 58 } | |
| 59 | |
| 60 ApplicationManager::ApplicationManager( | |
| 61 scoped_ptr<PackageManager> package_manager, | |
| 62 scoped_ptr<NativeRunnerFactory> native_runner_factory, | |
| 63 base::TaskRunner* task_runner) | |
| 64 : package_manager_(package_manager.Pass()), | |
| 65 task_runner_(task_runner), | |
| 66 native_runner_factory_(native_runner_factory.Pass()), | |
| 57 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this) { |
| 58 package_manager_->SetApplicationManager(this); | 68 package_manager_->SetApplicationManager(this); |
| 59 } | 69 } |
| 60 | 70 |
| 61 ApplicationManager::~ApplicationManager() { | 71 ApplicationManager::~ApplicationManager() { |
| 62 IdentityToContentHandlerMap identity_to_content_handler( | |
| 63 identity_to_content_handler_); | |
| 64 for (auto& pair : identity_to_content_handler) | |
| 65 pair.second->CloseConnection(); | |
| 66 TerminateShellConnections(); | 72 TerminateShellConnections(); |
| 67 STLDeleteValues(&url_to_loader_); | 73 STLDeleteValues(&url_to_loader_); |
| 68 } | 74 } |
| 69 | 75 |
| 70 void ApplicationManager::TerminateShellConnections() { | 76 void ApplicationManager::TerminateShellConnections() { |
| 71 STLDeleteValues(&identity_to_instance_); | 77 STLDeleteValues(&identity_to_instance_); |
| 72 } | 78 } |
| 73 | 79 |
| 74 void ApplicationManager::ConnectToApplication( | 80 void ApplicationManager::ConnectToApplication( |
| 75 scoped_ptr<ConnectToApplicationParams> params) { | 81 scoped_ptr<ConnectToApplicationParams> params) { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 return; | 168 return; |
| 163 | 169 |
| 164 Identity source = params->source(); | 170 Identity source = params->source(); |
| 165 Identity target = params->target(); | 171 Identity target = params->target(); |
| 166 Shell::ConnectToApplicationCallback connect_callback = | 172 Shell::ConnectToApplicationCallback connect_callback = |
| 167 params->connect_callback(); | 173 params->connect_callback(); |
| 168 params->set_connect_callback(EmptyConnectCallback()); | 174 params->set_connect_callback(EmptyConnectCallback()); |
| 169 ApplicationInstance* app = nullptr; | 175 ApplicationInstance* app = nullptr; |
| 170 InterfaceRequest<Application> request(CreateInstance(params.Pass(), &app)); | 176 InterfaceRequest<Application> request(CreateInstance(params.Pass(), &app)); |
| 171 | 177 |
| 178 uint32_t content_handler_id = package_manager_->HandleWithContentHandler( | |
| 179 fetcher.get(), source, target.url(), target.filter(), &request); | |
| 180 if (content_handler_id != Shell::kInvalidContentHandlerID) { | |
| 181 app->set_requesting_content_handler_id(content_handler_id); | |
| 182 connect_callback.Run(content_handler_id); | |
| 183 return; | |
| 184 } | |
| 172 | 185 |
| 173 GURL content_handler_url; | 186 // TODO(erg): Have a better way of switching the sandbox on. For now, switch |
| 174 URLResponsePtr new_response; | 187 // it on hard coded when we're using some of the sandboxable core services. |
| 175 std::string qualifier; | 188 bool start_sandboxed = false; |
| 176 if (package_manager_->HandleWithContentHandler(fetcher.get(), | 189 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 177 target.url(), | 190 switches::kMojoNoSandbox)) { |
| 178 blocking_pool_, | 191 start_sandboxed = (target.url() == GURL("mojo://core_services/") && |
| 179 &new_response, | 192 target.qualifier() == "Core") || |
| 180 &content_handler_url, | 193 target.url() == GURL("mojo://html_viewer/"); |
| 181 &qualifier)) { | 194 } |
| 182 Identity content_handler(content_handler_url, qualifier, target.filter()); | |
| 183 LoadWithContentHandler(source, content_handler, connect_callback, app, | |
| 184 request.Pass(), new_response.Pass()); | |
| 185 } else { | |
| 186 // TODO(erg): Have a better way of switching the sandbox on. For now, switch | |
| 187 // it on hard coded when we're using some of the sandboxable core services. | |
| 188 bool start_sandboxed = false; | |
| 189 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 190 switches::kMojoNoSandbox)) { | |
| 191 start_sandboxed = (target.url() == GURL("mojo://core_services/") && | |
| 192 target.qualifier() == "Core") || | |
| 193 target.url() == GURL("mojo://html_viewer/"); | |
| 194 } | |
| 195 | 195 |
| 196 connect_callback.Run(Shell::kInvalidContentHandlerID); | 196 connect_callback.Run(Shell::kInvalidContentHandlerID); |
| 197 | 197 |
| 198 fetcher->AsPath(blocking_pool_, | 198 fetcher->AsPath(task_runner_, |
| 199 base::Bind(&ApplicationManager::RunNativeApplication, | 199 base::Bind(&ApplicationManager::RunNativeApplication, |
| 200 weak_ptr_factory_.GetWeakPtr(), | 200 weak_ptr_factory_.GetWeakPtr(), |
| 201 base::Passed(request.Pass()), start_sandboxed, | 201 base::Passed(request.Pass()), start_sandboxed, |
| 202 base::Passed(fetcher.Pass()))); | 202 base::Passed(fetcher.Pass()))); |
| 203 } | |
| 204 } | 203 } |
| 205 | 204 |
| 206 void ApplicationManager::RunNativeApplication( | 205 void ApplicationManager::RunNativeApplication( |
| 207 InterfaceRequest<Application> application_request, | 206 InterfaceRequest<Application> application_request, |
| 208 bool start_sandboxed, | 207 bool start_sandboxed, |
| 209 scoped_ptr<Fetcher> fetcher, | 208 scoped_ptr<Fetcher> fetcher, |
| 210 const base::FilePath& path, | 209 const base::FilePath& path, |
| 211 bool path_exists) { | 210 bool path_exists) { |
| 212 // We only passed fetcher to keep it alive. Done with it now. | 211 // We only passed fetcher to keep it alive. Done with it now. |
| 213 fetcher.reset(); | 212 fetcher.reset(); |
| 214 | 213 |
| 215 DCHECK(application_request.is_pending()); | 214 DCHECK(application_request.is_pending()); |
| 216 | 215 |
| 217 if (!path_exists) { | 216 if (!path_exists) { |
| 218 LOG(ERROR) << "Library not started because library path '" << path.value() | 217 LOG(ERROR) << "Library not started because library path '" << path.value() |
| 219 << "' does not exist."; | 218 << "' does not exist."; |
| 220 return; | 219 return; |
| 221 } | 220 } |
| 222 | 221 |
| 223 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", | 222 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", |
| 224 path.AsUTF8Unsafe()); | 223 path.AsUTF8Unsafe()); |
| 225 NativeRunner* runner = native_runner_factory_->Create().release(); | 224 NativeRunner* runner = native_runner_factory_->Create().release(); |
| 226 native_runners_.push_back(runner); | 225 native_runners_.push_back(runner); |
| 227 runner->Start(path, start_sandboxed, application_request.Pass(), | 226 runner->Start(path, start_sandboxed, application_request.Pass(), |
| 228 base::Bind(&ApplicationManager::CleanupRunner, | 227 base::Bind(&ApplicationManager::CleanupRunner, |
| 229 weak_ptr_factory_.GetWeakPtr(), runner)); | 228 weak_ptr_factory_.GetWeakPtr(), runner)); |
| 230 } | 229 } |
| 231 | 230 |
| 232 void ApplicationManager::LoadWithContentHandler( | |
| 233 const Identity& source, | |
| 234 const Identity& content_handler, | |
| 235 const Shell::ConnectToApplicationCallback& connect_callback, | |
| 236 ApplicationInstance* app, | |
| 237 InterfaceRequest<Application> application_request, | |
| 238 URLResponsePtr url_response) { | |
| 239 ContentHandlerConnection* connection = nullptr; | |
| 240 // TODO(beng): Figure out the extent to which capability filter should be | |
| 241 // factored into handler identity. | |
| 242 IdentityToContentHandlerMap::iterator iter = | |
| 243 identity_to_content_handler_.find(content_handler); | |
| 244 if (iter != identity_to_content_handler_.end()) { | |
| 245 connection = iter->second; | |
| 246 } else { | |
| 247 connection = new ContentHandlerConnection( | |
| 248 this, source, content_handler, ++content_handler_id_counter_); | |
| 249 identity_to_content_handler_[content_handler] = connection; | |
| 250 } | |
| 251 | |
| 252 app->set_requesting_content_handler_id(connection->id()); | |
| 253 connection->content_handler()->StartApplication(application_request.Pass(), | |
| 254 url_response.Pass()); | |
| 255 connect_callback.Run(connection->id()); | |
| 256 } | |
| 257 | |
| 258 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, | 231 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, |
| 259 const GURL& url) { | 232 const GURL& url) { |
| 260 URLToLoaderMap::iterator it = url_to_loader_.find(url); | 233 URLToLoaderMap::iterator it = url_to_loader_.find(url); |
| 261 if (it != url_to_loader_.end()) | 234 if (it != url_to_loader_.end()) |
| 262 delete it->second; | 235 delete it->second; |
| 263 url_to_loader_[url] = loader.release(); | 236 url_to_loader_[url] = loader.release(); |
| 264 } | 237 } |
| 265 | 238 |
| 266 ApplicationLoader* ApplicationManager::GetLoaderForURL(const GURL& url) { | 239 ApplicationLoader* ApplicationManager::GetLoaderForURL(const GURL& url) { |
| 267 auto url_it = url_to_loader_.find(GetBaseURLAndQuery(url, nullptr)); | 240 auto url_it = url_to_loader_.find(GetBaseURLAndQuery(url, nullptr)); |
| 268 if (url_it != url_to_loader_.end()) | 241 if (url_it != url_to_loader_.end()) |
| 269 return url_it->second; | 242 return url_it->second; |
| 270 return default_loader_.get(); | 243 return default_loader_.get(); |
| 271 } | 244 } |
| 272 | 245 |
| 273 void ApplicationManager::OnApplicationInstanceError( | 246 void ApplicationManager::OnApplicationInstanceError( |
| 274 ApplicationInstance* instance) { | 247 ApplicationInstance* instance) { |
| 275 // Called from ~ApplicationInstance, so we do not need to call Destroy here. | 248 // Called from ~ApplicationInstance, so we do not need to call Destroy here. |
| 276 const Identity identity = instance->identity(); | 249 const Identity identity = instance->identity(); |
| 277 base::Closure on_application_end = instance->on_application_end(); | 250 base::Closure on_application_end = instance->on_application_end(); |
| 278 // Remove the shell. | 251 // Remove the shell. |
| 279 auto it = identity_to_instance_.find(identity); | 252 auto it = identity_to_instance_.find(identity); |
| 280 DCHECK(it != identity_to_instance_.end()); | 253 DCHECK(it != identity_to_instance_.end()); |
| 281 delete it->second; | 254 delete it->second; |
| 282 identity_to_instance_.erase(it); | 255 identity_to_instance_.erase(it); |
| 283 if (!on_application_end.is_null()) | 256 if (!on_application_end.is_null()) |
| 284 on_application_end.Run(); | 257 on_application_end.Run(); |
| 285 } | 258 } |
| 286 | 259 |
| 287 void ApplicationManager::OnContentHandlerConnectionClosed( | |
| 288 ContentHandlerConnection* content_handler) { | |
| 289 // Remove the mapping to the content handler. | |
| 290 auto it = identity_to_content_handler_.find(content_handler->identity()); | |
| 291 DCHECK(it != identity_to_content_handler_.end()); | |
| 292 identity_to_content_handler_.erase(it); | |
| 293 } | |
| 294 | |
| 295 void ApplicationManager::CleanupRunner(NativeRunner* runner) { | 260 void ApplicationManager::CleanupRunner(NativeRunner* runner) { |
| 296 native_runners_.erase( | 261 native_runners_.erase( |
| 297 std::find(native_runners_.begin(), native_runners_.end(), runner)); | 262 std::find(native_runners_.begin(), native_runners_.end(), runner)); |
| 298 } | 263 } |
| 299 | 264 |
| 300 Shell::ConnectToApplicationCallback EmptyConnectCallback() { | 265 Shell::ConnectToApplicationCallback EmptyConnectCallback() { |
| 301 return base::Bind(&OnEmptyOnConnectCallback); | 266 return base::Bind(&OnEmptyOnConnectCallback); |
| 302 } | 267 } |
| 303 | 268 |
| 304 } // namespace shell | 269 } // namespace shell |
| 305 } // namespace mojo | 270 } // namespace mojo |
| OLD | NEW |