| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 ApplicationInstance* ApplicationManager::GetApplicationInstance( | 123 ApplicationInstance* ApplicationManager::GetApplicationInstance( |
| 124 const Identity& identity) const { | 124 const Identity& identity) const { |
| 125 const auto& it = identity_to_instance_.find(identity); | 125 const auto& it = identity_to_instance_.find(identity); |
| 126 return it != identity_to_instance_.end() ? it->second : nullptr; | 126 return it != identity_to_instance_.end() ? it->second : nullptr; |
| 127 } | 127 } |
| 128 | 128 |
| 129 void ApplicationManager::CreateInstanceForHandle( | 129 void ApplicationManager::CreateInstanceForHandle( |
| 130 ScopedHandle channel, | 130 ScopedHandle channel, |
| 131 const GURL& url, | 131 const GURL& url, |
| 132 CapabilityFilterPtr filter, | 132 mojom::CapabilityFilterPtr filter, |
| 133 InterfaceRequest<mojom::PIDReceiver> pid_receiver) { | 133 InterfaceRequest<mojom::PIDReceiver> pid_receiver) { |
| 134 // Instances created by others are considered unique, and thus have no | 134 // Instances created by others are considered unique, and thus have no |
| 135 // identity. As such they cannot be connected to by anyone else, and so we | 135 // identity. As such they cannot be connected to by anyone else, and so we |
| 136 // never call ConnectToClient(). | 136 // never call ConnectToClient(). |
| 137 // TODO(beng): GetPermissiveCapabilityFilter() here obviously cannot make it | 137 // TODO(beng): GetPermissiveCapabilityFilter() here obviously cannot make it |
| 138 // to production. See note in application_manager.mojom. | 138 // to production. See note in application_manager.mojom. |
| 139 // http://crbug.com/555392 | 139 // http://crbug.com/555392 |
| 140 CapabilityFilter local_filter = filter->filter.To<CapabilityFilter>(); | 140 CapabilityFilter local_filter = filter->filter.To<CapabilityFilter>(); |
| 141 Identity target_id(url, std::string(), local_filter); | 141 Identity target_id(url, std::string(), local_filter); |
| 142 ApplicationInstance* instance = nullptr; | 142 ApplicationInstance* instance = nullptr; |
| 143 InterfaceRequest<Application> application_request = | 143 InterfaceRequest<mojom::Application> application_request = |
| 144 CreateInstance(target_id, base::Closure(), &instance); | 144 CreateInstance(target_id, base::Closure(), &instance); |
| 145 instance->BindPIDReceiver(std::move(pid_receiver)); | 145 instance->BindPIDReceiver(std::move(pid_receiver)); |
| 146 scoped_ptr<NativeRunner> runner = | 146 scoped_ptr<NativeRunner> runner = |
| 147 native_runner_factory_->Create(base::FilePath()); | 147 native_runner_factory_->Create(base::FilePath()); |
| 148 runner->InitHost(std::move(channel), std::move(application_request)); | 148 runner->InitHost(std::move(channel), std::move(application_request)); |
| 149 instance->SetNativeRunner(runner.get()); | 149 instance->SetNativeRunner(runner.get()); |
| 150 native_runners_.push_back(std::move(runner)); | 150 native_runners_.push_back(std::move(runner)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void ApplicationManager::AddListener( | 153 void ApplicationManager::AddListener( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 168 instance.second->set_pid(pid); | 168 instance.second->set_pid(pid); |
| 169 break; | 169 break; |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 listeners_.ForAllPtrs( | 172 listeners_.ForAllPtrs( |
| 173 [this, id, pid](mojom::ApplicationManagerListener* listener) { | 173 [this, id, pid](mojom::ApplicationManagerListener* listener) { |
| 174 listener->ApplicationPIDAvailable(id, pid); | 174 listener->ApplicationPIDAvailable(id, pid); |
| 175 }); | 175 }); |
| 176 } | 176 } |
| 177 | 177 |
| 178 InterfaceRequest<Application> ApplicationManager::CreateAndConnectToInstance( | 178 InterfaceRequest<mojom::Application> |
| 179 scoped_ptr<ConnectToApplicationParams> params, | 179 ApplicationManager::CreateAndConnectToInstance( |
| 180 ApplicationInstance** resulting_instance) { | 180 scoped_ptr<ConnectToApplicationParams> params, |
| 181 ApplicationInstance** resulting_instance) { |
| 181 ApplicationInstance* instance = nullptr; | 182 ApplicationInstance* instance = nullptr; |
| 182 InterfaceRequest<Application> application_request = | 183 InterfaceRequest<mojom::Application> application_request = |
| 183 CreateInstance(params->target(), params->on_application_end(), &instance); | 184 CreateInstance(params->target(), params->on_application_end(), &instance); |
| 184 instance->ConnectToClient(std::move(params)); | 185 instance->ConnectToClient(std::move(params)); |
| 185 if (resulting_instance) | 186 if (resulting_instance) |
| 186 *resulting_instance = instance; | 187 *resulting_instance = instance; |
| 187 return application_request; | 188 return application_request; |
| 188 } | 189 } |
| 189 | 190 |
| 190 InterfaceRequest<Application> ApplicationManager::CreateInstance( | 191 InterfaceRequest<mojom::Application> ApplicationManager::CreateInstance( |
| 191 const Identity& target_id, | 192 const Identity& target_id, |
| 192 const base::Closure& on_application_end, | 193 const base::Closure& on_application_end, |
| 193 ApplicationInstance** resulting_instance) { | 194 ApplicationInstance** resulting_instance) { |
| 194 ApplicationPtr application; | 195 mojom::ApplicationPtr application; |
| 195 InterfaceRequest<Application> application_request = GetProxy(&application); | 196 InterfaceRequest<mojom::Application> application_request = |
| 197 GetProxy(&application); |
| 196 ApplicationInstance* instance = new ApplicationInstance( | 198 ApplicationInstance* instance = new ApplicationInstance( |
| 197 std::move(application), this, target_id, Shell::kInvalidApplicationID, | 199 std::move(application), this, target_id, |
| 198 on_application_end); | 200 mojom::Shell::kInvalidApplicationID, on_application_end); |
| 199 DCHECK(identity_to_instance_.find(target_id) == | 201 DCHECK(identity_to_instance_.find(target_id) == |
| 200 identity_to_instance_.end()); | 202 identity_to_instance_.end()); |
| 201 identity_to_instance_[target_id] = instance; | 203 identity_to_instance_[target_id] = instance; |
| 202 mojom::ApplicationInfoPtr application_info = | 204 mojom::ApplicationInfoPtr application_info = |
| 203 CreateApplicationInfoForInstance(instance); | 205 CreateApplicationInfoForInstance(instance); |
| 204 listeners_.ForAllPtrs( | 206 listeners_.ForAllPtrs( |
| 205 [this, &application_info](mojom::ApplicationManagerListener* listener) { | 207 [this, &application_info](mojom::ApplicationManagerListener* listener) { |
| 206 listener->ApplicationInstanceCreated(application_info.Clone()); | 208 listener->ApplicationInstanceCreated(application_info.Clone()); |
| 207 }); | 209 }); |
| 208 instance->InitializeApplication(); | 210 instance->InitializeApplication(); |
| 209 if (resulting_instance) | 211 if (resulting_instance) |
| 210 *resulting_instance = instance; | 212 *resulting_instance = instance; |
| 211 return application_request; | 213 return application_request; |
| 212 } | 214 } |
| 213 | 215 |
| 214 void ApplicationManager::HandleFetchCallback( | 216 void ApplicationManager::HandleFetchCallback( |
| 215 scoped_ptr<ConnectToApplicationParams> params, | 217 scoped_ptr<ConnectToApplicationParams> params, |
| 216 scoped_ptr<Fetcher> fetcher) { | 218 scoped_ptr<Fetcher> fetcher) { |
| 217 if (!fetcher) { | 219 if (!fetcher) { |
| 218 // Network error. Drop |params| to tell the requestor. | 220 // Network error. Drop |params| to tell the requestor. |
| 219 params->connect_callback().Run(Shell::kInvalidApplicationID, | 221 params->connect_callback().Run(mojom::Shell::kInvalidApplicationID, |
| 220 Shell::kInvalidApplicationID); | 222 mojom::Shell::kInvalidApplicationID); |
| 221 return; | 223 return; |
| 222 } | 224 } |
| 223 | 225 |
| 224 GURL redirect_url = fetcher->GetRedirectURL(); | 226 GURL redirect_url = fetcher->GetRedirectURL(); |
| 225 if (!redirect_url.is_empty()) { | 227 if (!redirect_url.is_empty()) { |
| 226 // And around we go again... Whee! | 228 // And around we go again... Whee! |
| 227 // TODO(sky): this loses the original URL info. | 229 // TODO(sky): this loses the original URL info. |
| 228 URLRequestPtr new_request = URLRequest::New(); | 230 URLRequestPtr new_request = URLRequest::New(); |
| 229 new_request->url = redirect_url.spec(); | 231 new_request->url = redirect_url.spec(); |
| 230 HttpHeaderPtr header = HttpHeader::New(); | 232 HttpHeaderPtr header = HttpHeader::New(); |
| 231 header->name = "Referer"; | 233 header->name = "Referer"; |
| 232 header->value = fetcher->GetRedirectReferer().spec(); | 234 header->value = fetcher->GetRedirectReferer().spec(); |
| 233 new_request->headers.push_back(std::move(header)); | 235 new_request->headers.push_back(std::move(header)); |
| 234 params->SetTargetURLRequest(std::move(new_request)); | 236 params->SetTargetURLRequest(std::move(new_request)); |
| 235 ConnectToApplication(std::move(params)); | 237 ConnectToApplication(std::move(params)); |
| 236 return; | 238 return; |
| 237 } | 239 } |
| 238 | 240 |
| 239 // We already checked if the application was running before we fetched it, but | 241 // We already checked if the application was running before we fetched it, but |
| 240 // it might have started while the fetch was outstanding. We don't want to | 242 // it might have started while the fetch was outstanding. We don't want to |
| 241 // have two copies of the app running, so check again. | 243 // have two copies of the app running, so check again. |
| 242 if (ConnectToRunningApplication(¶ms)) | 244 if (ConnectToRunningApplication(¶ms)) |
| 243 return; | 245 return; |
| 244 | 246 |
| 245 Identity source = params->source(); | 247 Identity source = params->source(); |
| 246 Identity target = params->target(); | 248 Identity target = params->target(); |
| 247 Shell::ConnectToApplicationCallback connect_callback = | 249 mojom::Shell::ConnectToApplicationCallback connect_callback = |
| 248 params->connect_callback(); | 250 params->connect_callback(); |
| 249 params->set_connect_callback(EmptyConnectCallback()); | 251 params->set_connect_callback(EmptyConnectCallback()); |
| 250 ApplicationInstance* app = nullptr; | 252 ApplicationInstance* app = nullptr; |
| 251 InterfaceRequest<Application> request( | 253 InterfaceRequest<mojom::Application> request( |
| 252 CreateAndConnectToInstance(std::move(params), &app)); | 254 CreateAndConnectToInstance(std::move(params), &app)); |
| 253 | 255 |
| 254 uint32_t content_handler_id = package_manager_->HandleWithContentHandler( | 256 uint32_t content_handler_id = package_manager_->HandleWithContentHandler( |
| 255 fetcher.get(), source, target.url(), target.filter(), &request); | 257 fetcher.get(), source, target.url(), target.filter(), &request); |
| 256 if (content_handler_id != Shell::kInvalidApplicationID) { | 258 if (content_handler_id != mojom::Shell::kInvalidApplicationID) { |
| 257 app->set_requesting_content_handler_id(content_handler_id); | 259 app->set_requesting_content_handler_id(content_handler_id); |
| 258 connect_callback.Run(app->id(), content_handler_id); | 260 connect_callback.Run(app->id(), content_handler_id); |
| 259 return; | 261 return; |
| 260 } | 262 } |
| 261 | 263 |
| 262 // TODO(erg): Have a better way of switching the sandbox on. For now, switch | 264 // TODO(erg): Have a better way of switching the sandbox on. For now, switch |
| 263 // it on hard coded when we're using some of the sandboxable core services. | 265 // it on hard coded when we're using some of the sandboxable core services. |
| 264 bool start_sandboxed = false; | 266 bool start_sandboxed = false; |
| 265 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 267 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 266 switches::kMojoNoSandbox)) { | 268 switches::kMojoNoSandbox)) { |
| 267 start_sandboxed = (target.url() == GURL("mojo://core_services/") && | 269 start_sandboxed = (target.url() == GURL("mojo://core_services/") && |
| 268 target.qualifier() == "Core") || | 270 target.qualifier() == "Core") || |
| 269 target.url() == GURL("mojo://html_viewer/"); | 271 target.url() == GURL("mojo://html_viewer/"); |
| 270 } | 272 } |
| 271 | 273 |
| 272 connect_callback.Run(app->id(), Shell::kInvalidApplicationID); | 274 connect_callback.Run(app->id(), mojom::Shell::kInvalidApplicationID); |
| 273 | 275 |
| 274 fetcher->AsPath( | 276 fetcher->AsPath( |
| 275 task_runner_, | 277 task_runner_, |
| 276 base::Bind(&ApplicationManager::RunNativeApplication, | 278 base::Bind(&ApplicationManager::RunNativeApplication, |
| 277 weak_ptr_factory_.GetWeakPtr(), | 279 weak_ptr_factory_.GetWeakPtr(), |
| 278 base::Passed(std::move(request)), start_sandboxed, | 280 base::Passed(std::move(request)), start_sandboxed, |
| 279 base::Passed(std::move(fetcher)), base::Unretained(app))); | 281 base::Passed(std::move(fetcher)), base::Unretained(app))); |
| 280 } | 282 } |
| 281 | 283 |
| 282 void ApplicationManager::RunNativeApplication( | 284 void ApplicationManager::RunNativeApplication( |
| 283 InterfaceRequest<Application> application_request, | 285 InterfaceRequest<mojom::Application> application_request, |
| 284 bool start_sandboxed, | 286 bool start_sandboxed, |
| 285 scoped_ptr<Fetcher> fetcher, | 287 scoped_ptr<Fetcher> fetcher, |
| 286 ApplicationInstance* instance, | 288 ApplicationInstance* instance, |
| 287 const base::FilePath& path, | 289 const base::FilePath& path, |
| 288 bool path_exists) { | 290 bool path_exists) { |
| 289 // We only passed fetcher to keep it alive. Done with it now. | 291 // We only passed fetcher to keep it alive. Done with it now. |
| 290 fetcher.reset(); | 292 fetcher.reset(); |
| 291 | 293 |
| 292 DCHECK(application_request.is_pending()); | 294 DCHECK(application_request.is_pending()); |
| 293 | 295 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 | 362 |
| 361 void ApplicationManager::CleanupRunner(NativeRunner* runner) { | 363 void ApplicationManager::CleanupRunner(NativeRunner* runner) { |
| 362 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { | 364 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { |
| 363 if (it->get() == runner) { | 365 if (it->get() == runner) { |
| 364 native_runners_.erase(it); | 366 native_runners_.erase(it); |
| 365 return; | 367 return; |
| 366 } | 368 } |
| 367 } | 369 } |
| 368 } | 370 } |
| 369 | 371 |
| 370 Shell::ConnectToApplicationCallback EmptyConnectCallback() { | 372 mojom::Shell::ConnectToApplicationCallback EmptyConnectCallback() { |
| 371 return base::Bind(&OnEmptyOnConnectCallback); | 373 return base::Bind(&OnEmptyOnConnectCallback); |
| 372 } | 374 } |
| 373 | 375 |
| 374 } // namespace shell | 376 } // namespace shell |
| 375 } // namespace mojo | 377 } // namespace mojo |
| OLD | NEW |