| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 identity.filter().count("*") == 1), | 47 identity.filter().count("*") == 1), |
| 48 shell_client_(std::move(shell_client)), | 48 shell_client_(std::move(shell_client)), |
| 49 pid_receiver_binding_(this) { | 49 pid_receiver_binding_(this) { |
| 50 DCHECK_NE(kInvalidApplicationID, id_); | 50 DCHECK_NE(kInvalidApplicationID, id_); |
| 51 } | 51 } |
| 52 | 52 |
| 53 ~Instance() override {} | 53 ~Instance() override {} |
| 54 | 54 |
| 55 void Initialize() { | 55 void Initialize() { |
| 56 shell_client_->Initialize(connectors_.CreateInterfacePtrAndBind(this), | 56 shell_client_->Initialize(connectors_.CreateInterfacePtrAndBind(this), |
| 57 identity_.url().spec(), id_, identity_.user_id()); | 57 identity_.name(), id_, identity_.user_id()); |
| 58 connectors_.set_connection_error_handler( | 58 connectors_.set_connection_error_handler( |
| 59 base::Bind(&ApplicationManager::OnInstanceError, | 59 base::Bind(&ApplicationManager::OnInstanceError, |
| 60 base::Unretained(manager_), base::Unretained(this))); | 60 base::Unretained(manager_), base::Unretained(this))); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void ConnectToClient(scoped_ptr<ConnectParams> params) { | 63 void ConnectToClient(scoped_ptr<ConnectParams> params) { |
| 64 params->connect_callback().Run(id_); | 64 params->connect_callback().Run(id_); |
| 65 AllowedInterfaces interfaces; | 65 AllowedInterfaces interfaces; |
| 66 interfaces.insert("*"); | 66 interfaces.insert("*"); |
| 67 if (!params->source().is_null()) | 67 if (!params->source().is_null()) |
| 68 interfaces = GetAllowedInterfaces(params->source().filter(), identity_); | 68 interfaces = GetAllowedInterfaces(params->source().filter(), identity_); |
| 69 | 69 |
| 70 Instance* source = manager_->GetExistingInstance(params->source()); | 70 Instance* source = manager_->GetExistingInstance(params->source()); |
| 71 uint32_t source_id = source ? source->id() : kInvalidApplicationID; | 71 uint32_t source_id = source ? source->id() : kInvalidApplicationID; |
| 72 shell_client_->AcceptConnection( | 72 shell_client_->AcceptConnection( |
| 73 params->source().url().spec(), params->source().user_id(), source_id, | 73 params->source().name(), params->source().user_id(), source_id, |
| 74 params->TakeRemoteInterfaces(), params->TakeLocalInterfaces(), | 74 params->TakeRemoteInterfaces(), params->TakeLocalInterfaces(), |
| 75 Array<String>::From(interfaces), params->target().url().spec()); | 75 Array<String>::From(interfaces), params->target().name()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Required before GetProcessId can be called. | 78 // Required before GetProcessId can be called. |
| 79 void SetNativeRunner(NativeRunner* native_runner) { | 79 void SetNativeRunner(NativeRunner* native_runner) { |
| 80 native_runner_ = native_runner; | 80 native_runner_ = native_runner; |
| 81 } | 81 } |
| 82 | 82 |
| 83 void BindPIDReceiver(mojom::PIDReceiverRequest pid_receiver) { | 83 void BindPIDReceiver(mojom::PIDReceiverRequest pid_receiver) { |
| 84 pid_receiver_binding_.Bind(std::move(pid_receiver)); | 84 pid_receiver_binding_.Bind(std::move(pid_receiver)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 mojom::ShellClient* shell_client() { return shell_client_.get(); } | 87 mojom::ShellClient* shell_client() { return shell_client_.get(); } |
| 88 const Identity& identity() const { return identity_; } | 88 const Identity& identity() const { return identity_; } |
| 89 uint32_t id() const { return id_; } | 89 uint32_t id() const { return id_; } |
| 90 base::ProcessId pid() const { return pid_; } | 90 base::ProcessId pid() const { return pid_; } |
| 91 void set_pid(base::ProcessId pid) { pid_ = pid; } | 91 void set_pid(base::ProcessId pid) { pid_ = pid; } |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 // Connector implementation: | 94 // Connector implementation: |
| 95 void Connect(const String& app_url, | 95 void Connect(const String& app_name, |
| 96 uint32_t user_id, | 96 uint32_t user_id, |
| 97 shell::mojom::InterfaceProviderRequest remote_interfaces, | 97 shell::mojom::InterfaceProviderRequest remote_interfaces, |
| 98 shell::mojom::InterfaceProviderPtr local_interfaces, | 98 shell::mojom::InterfaceProviderPtr local_interfaces, |
| 99 const ConnectCallback& callback) override { | 99 const ConnectCallback& callback) override { |
| 100 GURL url = app_url.To<GURL>(); | 100 if (!IsValidName(app_name)) { |
| 101 if (!url.is_valid()) { | 101 LOG(ERROR) << "Error: invalid Name: " << app_name; |
| 102 LOG(ERROR) << "Error: invalid URL: " << app_url; | |
| 103 callback.Run(kInvalidApplicationID); | 102 callback.Run(kInvalidApplicationID); |
| 104 return; | 103 return; |
| 105 } | 104 } |
| 106 if (allow_any_application_ || | 105 if (allow_any_application_ || |
| 107 identity_.filter().find(url.spec()) != identity_.filter().end()) { | 106 identity_.filter().find(app_name) != identity_.filter().end()) { |
| 108 scoped_ptr<ConnectParams> params(new ConnectParams); | 107 scoped_ptr<ConnectParams> params(new ConnectParams); |
| 109 params->set_source(identity_); | 108 params->set_source(identity_); |
| 110 params->set_target(Identity(url, std::string(), user_id)); | 109 params->set_target(Identity(app_name, std::string(), user_id)); |
| 111 params->set_remote_interfaces(std::move(remote_interfaces)); | 110 params->set_remote_interfaces(std::move(remote_interfaces)); |
| 112 params->set_local_interfaces(std::move(local_interfaces)); | 111 params->set_local_interfaces(std::move(local_interfaces)); |
| 113 params->set_connect_callback(callback); | 112 params->set_connect_callback(callback); |
| 114 manager_->Connect(std::move(params)); | 113 manager_->Connect(std::move(params)); |
| 115 } | 114 } |
| 116 else { | 115 else { |
| 117 LOG(WARNING) << "CapabilityFilter prevented connection from: " << | 116 LOG(WARNING) << "CapabilityFilter prevented connection from: " << |
| 118 identity_.url() << " to: " << url.spec(); | 117 identity_.name() << " to: " << app_name; |
| 119 callback.Run(kInvalidApplicationID); | 118 callback.Run(kInvalidApplicationID); |
| 120 } | 119 } |
| 121 } | 120 } |
| 122 void Clone(mojom::ConnectorRequest request) override { | 121 void Clone(mojom::ConnectorRequest request) override { |
| 123 connectors_.AddBinding(this, std::move(request)); | 122 connectors_.AddBinding(this, std::move(request)); |
| 124 } | 123 } |
| 125 | 124 |
| 126 // PIDReceiver implementation: | 125 // PIDReceiver implementation: |
| 127 void SetPID(uint32_t pid) override { | 126 void SetPID(uint32_t pid) override { |
| 128 // This will call us back to update pid_. | 127 // This will call us back to update pid_. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 153 }; | 152 }; |
| 154 | 153 |
| 155 // static | 154 // static |
| 156 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) | 155 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) |
| 157 : manager_(manager) { | 156 : manager_(manager) { |
| 158 } | 157 } |
| 159 | 158 |
| 160 ApplicationManager::TestAPI::~TestAPI() { | 159 ApplicationManager::TestAPI::~TestAPI() { |
| 161 } | 160 } |
| 162 | 161 |
| 163 bool ApplicationManager::TestAPI::HasRunningInstanceForURL( | 162 bool ApplicationManager::TestAPI::HasRunningInstanceForName( |
| 164 const GURL& url) const { | 163 const std::string& name) const { |
| 165 return manager_->identity_to_instance_.find(Identity(url)) != | 164 return manager_->identity_to_instance_.find(Identity(name)) != |
| 166 manager_->identity_to_instance_.end(); | 165 manager_->identity_to_instance_.end(); |
| 167 } | 166 } |
| 168 | 167 |
| 169 //////////////////////////////////////////////////////////////////////////////// | 168 //////////////////////////////////////////////////////////////////////////////// |
| 170 // ApplicationManager, public: | 169 // ApplicationManager, public: |
| 171 | 170 |
| 172 ApplicationManager::ApplicationManager( | 171 ApplicationManager::ApplicationManager( |
| 173 scoped_ptr<NativeRunnerFactory> native_runner_factory, | 172 scoped_ptr<NativeRunnerFactory> native_runner_factory, |
| 174 base::TaskRunner* file_task_runner, | 173 base::TaskRunner* file_task_runner, |
| 175 bool register_mojo_url_schemes, | |
| 176 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog) | 174 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog) |
| 177 : file_task_runner_(file_task_runner), | 175 : file_task_runner_(file_task_runner), |
| 178 native_runner_factory_(std::move(native_runner_factory)), | 176 native_runner_factory_(std::move(native_runner_factory)), |
| 179 weak_ptr_factory_(this) { | 177 weak_ptr_factory_(this) { |
| 180 mojom::ShellClientRequest request; | 178 mojom::ShellClientRequest request; |
| 181 CreateInstance(CreateShellIdentity(), &request); | 179 CreateInstance(CreateShellIdentity(), &request); |
| 182 shell_connection_.reset(new ShellConnection(this, std::move(request))); | 180 shell_connection_.reset(new ShellConnection(this, std::move(request))); |
| 183 | 181 |
| 184 InitPackageManager(register_mojo_url_schemes, std::move(app_catalog)); | 182 InitPackageManager(std::move(app_catalog)); |
| 185 } | 183 } |
| 186 | 184 |
| 187 ApplicationManager::~ApplicationManager() { | 185 ApplicationManager::~ApplicationManager() { |
| 188 TerminateShellConnections(); | 186 TerminateShellConnections(); |
| 189 STLDeleteValues(&url_to_loader_); | 187 STLDeleteValues(&name_to_loader_); |
| 190 for (auto& runner : native_runners_) | 188 for (auto& runner : native_runners_) |
| 191 runner.reset(); | 189 runner.reset(); |
| 192 } | 190 } |
| 193 | 191 |
| 194 void ApplicationManager::SetInstanceQuitCallback( | 192 void ApplicationManager::SetInstanceQuitCallback( |
| 195 base::Callback<void(const Identity&)> callback) { | 193 base::Callback<void(const Identity&)> callback) { |
| 196 instance_quit_callback_ = callback; | 194 instance_quit_callback_ = callback; |
| 197 } | 195 } |
| 198 | 196 |
| 199 void ApplicationManager::Connect(scoped_ptr<ConnectParams> params) { | 197 void ApplicationManager::Connect(scoped_ptr<ConnectParams> params) { |
| 200 TRACE_EVENT_INSTANT1("mojo_shell", "ApplicationManager::Connect", | 198 TRACE_EVENT_INSTANT1("mojo_shell", "ApplicationManager::Connect", |
| 201 TRACE_EVENT_SCOPE_THREAD, "original_url", | 199 TRACE_EVENT_SCOPE_THREAD, "original_name", |
| 202 params->target().url().spec()); | 200 params->target().name()); |
| 203 DCHECK(params->target().url().is_valid()); | 201 DCHECK(IsValidName(params->target().name())); |
| 204 | 202 |
| 205 if (params->target().user_id() == mojom::Connector::kUserInherit) { | 203 if (params->target().user_id() == mojom::Connector::kUserInherit) { |
| 206 Instance* source = GetExistingInstance(params->source()); | 204 Instance* source = GetExistingInstance(params->source()); |
| 207 Identity target = params->target(); | 205 Identity target = params->target(); |
| 208 // TODO(beng): we should CHECK source. | 206 // TODO(beng): we should CHECK source. |
| 209 target.set_user_id(source ? source->identity().user_id() | 207 target.set_user_id(source ? source->identity().user_id() |
| 210 : mojom::Connector::kUserRoot); | 208 : mojom::Connector::kUserRoot); |
| 211 params->set_target(target); | 209 params->set_target(target); |
| 212 } | 210 } |
| 213 | 211 |
| 214 // Connect to an existing matching instance, if possible. | 212 // Connect to an existing matching instance, if possible. |
| 215 if (ConnectToExistingInstance(¶ms)) | 213 if (ConnectToExistingInstance(¶ms)) |
| 216 return; | 214 return; |
| 217 | 215 |
| 218 std::string url = params->target().url().spec(); | 216 std::string name = params->target().name(); |
| 219 shell_resolver_->ResolveMojoURL( | 217 shell_resolver_->ResolveMojoName( |
| 220 url, | 218 name, |
| 221 base::Bind(&ApplicationManager::OnGotResolvedURL, | 219 base::Bind(&ApplicationManager::OnGotResolvedName, |
| 222 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms))); | 220 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms))); |
| 223 } | 221 } |
| 224 | 222 |
| 225 mojom::ShellClientRequest ApplicationManager::InitInstanceForEmbedder( | 223 mojom::ShellClientRequest ApplicationManager::InitInstanceForEmbedder( |
| 226 const GURL& url) { | 224 const std::string& name) { |
| 227 DCHECK(!embedder_instance_); | 225 DCHECK(!embedder_instance_); |
| 228 | 226 |
| 229 mojo::shell::Identity target(url, std::string(), mojom::Connector::kUserRoot); | 227 mojo::shell::Identity target(name, std::string(), |
| 230 target.SetFilter(GetPermissiveCapabilityFilter()); | 228 mojom::Connector::kUserRoot); |
| 229 target.set_filter(GetPermissiveCapabilityFilter()); |
| 231 DCHECK(!GetExistingInstance(target)); | 230 DCHECK(!GetExistingInstance(target)); |
| 232 | 231 |
| 233 mojom::ShellClientRequest request; | 232 mojom::ShellClientRequest request; |
| 234 embedder_instance_ = CreateInstance(target, &request); | 233 embedder_instance_ = CreateInstance(target, &request); |
| 235 DCHECK(embedder_instance_); | 234 DCHECK(embedder_instance_); |
| 236 | 235 |
| 237 return request; | 236 return request; |
| 238 } | 237 } |
| 239 | 238 |
| 240 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, | 239 void ApplicationManager::SetLoaderForName(scoped_ptr<ApplicationLoader> loader, |
| 241 const GURL& url) { | 240 const std::string& name) { |
| 242 URLToLoaderMap::iterator it = url_to_loader_.find(url); | 241 NameToLoaderMap::iterator it = name_to_loader_.find(name); |
| 243 if (it != url_to_loader_.end()) | 242 if (it != name_to_loader_.end()) |
| 244 delete it->second; | 243 delete it->second; |
| 245 url_to_loader_[url] = loader.release(); | 244 name_to_loader_[name] = loader.release(); |
| 246 } | 245 } |
| 247 | 246 |
| 248 //////////////////////////////////////////////////////////////////////////////// | 247 //////////////////////////////////////////////////////////////////////////////// |
| 249 // ApplicationManager, ShellClient implementation: | 248 // ApplicationManager, ShellClient implementation: |
| 250 | 249 |
| 251 bool ApplicationManager::AcceptConnection(Connection* connection) { | 250 bool ApplicationManager::AcceptConnection(Connection* connection) { |
| 252 connection->AddInterface<mojom::ApplicationManager>(this); | 251 connection->AddInterface<mojom::ApplicationManager>(this); |
| 253 return true; | 252 return true; |
| 254 } | 253 } |
| 255 | 254 |
| 256 //////////////////////////////////////////////////////////////////////////////// | 255 //////////////////////////////////////////////////////////////////////////////// |
| 257 // ApplicationManager, InterfaceFactory<mojom::ApplicationManager> | 256 // ApplicationManager, InterfaceFactory<mojom::ApplicationManager> |
| 258 // implementation: | 257 // implementation: |
| 259 | 258 |
| 260 void ApplicationManager::Create(Connection* connection, | 259 void ApplicationManager::Create(Connection* connection, |
| 261 mojom::ApplicationManagerRequest request) { | 260 mojom::ApplicationManagerRequest request) { |
| 262 bindings_.AddBinding(this, std::move(request)); | 261 bindings_.AddBinding(this, std::move(request)); |
| 263 } | 262 } |
| 264 | 263 |
| 265 //////////////////////////////////////////////////////////////////////////////// | 264 //////////////////////////////////////////////////////////////////////////////// |
| 266 // ApplicationManager, mojom::ApplicationManager implemetation: | 265 // ApplicationManager, mojom::ApplicationManager implemetation: |
| 267 | 266 |
| 268 void ApplicationManager::CreateInstanceForHandle( | 267 void ApplicationManager::CreateInstanceForHandle( |
| 269 ScopedHandle channel, | 268 ScopedHandle channel, |
| 270 const String& url, | 269 const String& name, |
| 271 mojom::CapabilityFilterPtr filter, | 270 mojom::CapabilityFilterPtr filter, |
| 272 mojom::PIDReceiverRequest pid_receiver) { | 271 mojom::PIDReceiverRequest pid_receiver) { |
| 273 // We don't call ConnectToClient() here since the instance was created | 272 // We don't call ConnectToClient() here since the instance was created |
| 274 // manually by other code, not in response to a Connect() request. The newly | 273 // manually by other code, not in response to a Connect() request. The newly |
| 275 // created instance is identified by |url| and may be subsequently reached by | 274 // created instance is identified by |name| and may be subsequently reached by |
| 276 // client code using this identity. | 275 // client code using this identity. |
| 277 // TODO(beng): obtain userid from the inbound connection. | 276 // TODO(beng): obtain userid from the inbound connection. |
| 278 Identity target_id(url.To<GURL>(), std::string(), | 277 Identity target_id(name, std::string(), mojom::Connector::kUserInherit); |
| 279 mojom::Connector::kUserInherit); | 278 target_id.set_filter(filter->filter.To<CapabilityFilter>()); |
| 280 target_id.SetFilter(filter->filter.To<CapabilityFilter>()); | |
| 281 mojom::ShellClientRequest request; | 279 mojom::ShellClientRequest request; |
| 282 Instance* instance = CreateInstance(target_id, &request); | 280 Instance* instance = CreateInstance(target_id, &request); |
| 283 instance->BindPIDReceiver(std::move(pid_receiver)); | 281 instance->BindPIDReceiver(std::move(pid_receiver)); |
| 284 scoped_ptr<NativeRunner> runner = | 282 scoped_ptr<NativeRunner> runner = |
| 285 native_runner_factory_->Create(base::FilePath()); | 283 native_runner_factory_->Create(base::FilePath()); |
| 286 runner->InitHost(std::move(channel), std::move(request)); | 284 runner->InitHost(std::move(channel), std::move(request)); |
| 287 instance->SetNativeRunner(runner.get()); | 285 instance->SetNativeRunner(runner.get()); |
| 288 native_runners_.push_back(std::move(runner)); | 286 native_runners_.push_back(std::move(runner)); |
| 289 } | 287 } |
| 290 | 288 |
| 291 void ApplicationManager::AddListener( | 289 void ApplicationManager::AddListener( |
| 292 mojom::ApplicationManagerListenerPtr listener) { | 290 mojom::ApplicationManagerListenerPtr listener) { |
| 293 Array<mojom::ApplicationInfoPtr> applications; | 291 Array<mojom::ApplicationInfoPtr> applications; |
| 294 for (auto& entry : identity_to_instance_) | 292 for (auto& entry : identity_to_instance_) |
| 295 applications.push_back(CreateApplicationInfoForInstance(entry.second)); | 293 applications.push_back(CreateApplicationInfoForInstance(entry.second)); |
| 296 listener->SetRunningApplications(std::move(applications)); | 294 listener->SetRunningApplications(std::move(applications)); |
| 297 | 295 |
| 298 listeners_.AddInterfacePtr(std::move(listener)); | 296 listeners_.AddInterfacePtr(std::move(listener)); |
| 299 } | 297 } |
| 300 | 298 |
| 301 //////////////////////////////////////////////////////////////////////////////// | 299 //////////////////////////////////////////////////////////////////////////////// |
| 302 // ApplicationManager, private: | 300 // ApplicationManager, private: |
| 303 | 301 |
| 304 void ApplicationManager::InitPackageManager( | 302 void ApplicationManager::InitPackageManager( |
| 305 bool register_mojo_url_schemes, | |
| 306 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog) { | 303 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog) { |
| 307 scoped_ptr<ApplicationLoader> loader(new package_manager::Loader( | 304 scoped_ptr<ApplicationLoader> loader(new package_manager::Loader( |
| 308 file_task_runner_, register_mojo_url_schemes, std::move(app_catalog))); | 305 file_task_runner_, std::move(app_catalog))); |
| 309 | 306 |
| 310 mojom::ShellClientRequest request; | 307 mojom::ShellClientRequest request; |
| 311 GURL url("mojo://package_manager/"); | 308 std::string name = "mojo://package_manager/"; |
| 312 CreateInstance(Identity(url), &request); | 309 CreateInstance(Identity(name), &request); |
| 313 loader->Load(url, std::move(request)); | 310 loader->Load(name, std::move(request)); |
| 314 | 311 |
| 315 SetLoaderForURL(std::move(loader), url); | 312 SetLoaderForName(std::move(loader), name); |
| 316 | 313 |
| 317 ConnectToInterface(this, CreateShellIdentity(), url, &shell_resolver_); | 314 ConnectToInterface(this, CreateShellIdentity(), name, &shell_resolver_); |
| 318 } | 315 } |
| 319 | 316 |
| 320 void ApplicationManager::TerminateShellConnections() { | 317 void ApplicationManager::TerminateShellConnections() { |
| 321 STLDeleteValues(&identity_to_instance_); | 318 STLDeleteValues(&identity_to_instance_); |
| 322 } | 319 } |
| 323 | 320 |
| 324 void ApplicationManager::OnInstanceError(Instance* instance) { | 321 void ApplicationManager::OnInstanceError(Instance* instance) { |
| 325 const Identity identity = instance->identity(); | 322 const Identity identity = instance->identity(); |
| 326 // Remove the shell. | 323 // Remove the shell. |
| 327 auto it = identity_to_instance_.find(identity); | 324 auto it = identity_to_instance_.find(identity); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 [this, &application_info](mojom::ApplicationManagerListener* listener) { | 383 [this, &application_info](mojom::ApplicationManagerListener* listener) { |
| 387 listener->ApplicationInstanceCreated(application_info.Clone()); | 384 listener->ApplicationInstanceCreated(application_info.Clone()); |
| 388 }); | 385 }); |
| 389 instance->Initialize(); | 386 instance->Initialize(); |
| 390 return instance; | 387 return instance; |
| 391 } | 388 } |
| 392 | 389 |
| 393 void ApplicationManager::CreateShellClient( | 390 void ApplicationManager::CreateShellClient( |
| 394 const Identity& source, | 391 const Identity& source, |
| 395 const Identity& shell_client_factory, | 392 const Identity& shell_client_factory, |
| 396 const GURL& url, | 393 const std::string& name, |
| 397 mojom::ShellClientRequest request) { | 394 mojom::ShellClientRequest request) { |
| 398 mojom::ShellClientFactory* factory = | 395 mojom::ShellClientFactory* factory = |
| 399 GetShellClientFactory(shell_client_factory, source); | 396 GetShellClientFactory(shell_client_factory, source); |
| 400 factory->CreateShellClient(std::move(request), url.spec()); | 397 factory->CreateShellClient(std::move(request), name); |
| 401 } | 398 } |
| 402 | 399 |
| 403 mojom::ShellClientFactory* ApplicationManager::GetShellClientFactory( | 400 mojom::ShellClientFactory* ApplicationManager::GetShellClientFactory( |
| 404 const Identity& shell_client_factory_identity, | 401 const Identity& shell_client_factory_identity, |
| 405 const Identity& source_identity) { | 402 const Identity& source_identity) { |
| 406 auto it = shell_client_factories_.find(shell_client_factory_identity); | 403 auto it = shell_client_factories_.find(shell_client_factory_identity); |
| 407 if (it != shell_client_factories_.end()) | 404 if (it != shell_client_factories_.end()) |
| 408 return it->second.get(); | 405 return it->second.get(); |
| 409 | 406 |
| 410 mojom::ShellClientFactoryPtr factory; | 407 mojom::ShellClientFactoryPtr factory; |
| 411 // TODO(beng): we should forward the original source identity! | 408 // TODO(beng): we should forward the original source identity! |
| 412 ConnectToInterface(this, source_identity, shell_client_factory_identity, | 409 ConnectToInterface(this, source_identity, shell_client_factory_identity, |
| 413 &factory); | 410 &factory); |
| 414 mojom::ShellClientFactory* factory_interface = factory.get(); | 411 mojom::ShellClientFactory* factory_interface = factory.get(); |
| 415 factory.set_connection_error_handler( | 412 factory.set_connection_error_handler( |
| 416 base::Bind(&ApplicationManager::OnShellClientFactoryLost, | 413 base::Bind(&ApplicationManager::OnShellClientFactoryLost, |
| 417 weak_ptr_factory_.GetWeakPtr(), | 414 weak_ptr_factory_.GetWeakPtr(), |
| 418 shell_client_factory_identity)); | 415 shell_client_factory_identity)); |
| 419 shell_client_factories_[shell_client_factory_identity] = std::move(factory); | 416 shell_client_factories_[shell_client_factory_identity] = std::move(factory); |
| 420 return factory_interface; | 417 return factory_interface; |
| 421 } | 418 } |
| 422 | 419 |
| 423 void ApplicationManager::OnShellClientFactoryLost(const Identity& which) { | 420 void ApplicationManager::OnShellClientFactoryLost(const Identity& which) { |
| 424 // Remove the mapping. | 421 // Remove the mapping. |
| 425 auto it = shell_client_factories_.find(which); | 422 auto it = shell_client_factories_.find(which); |
| 426 DCHECK(it != shell_client_factories_.end()); | 423 DCHECK(it != shell_client_factories_.end()); |
| 427 shell_client_factories_.erase(it); | 424 shell_client_factories_.erase(it); |
| 428 } | 425 } |
| 429 | 426 |
| 430 void ApplicationManager::OnGotResolvedURL( | 427 void ApplicationManager::OnGotResolvedName( |
| 431 scoped_ptr<ConnectParams> params, | 428 scoped_ptr<ConnectParams> params, |
| 432 const String& resolved_url, | 429 const String& resolved_name, |
| 433 const String& resolved_qualifier, | 430 const String& resolved_qualifier, |
| 434 mojom::CapabilityFilterPtr base_filter, | 431 mojom::CapabilityFilterPtr base_filter, |
| 435 const String& file_url) { | 432 const String& file_url) { |
| 436 // It's possible that when this manifest request was issued, another one was | 433 // It's possible that when this manifest request was issued, another one was |
| 437 // already in-progress and completed by the time this one did, and so the | 434 // already in-progress and completed by the time this one did, and so the |
| 438 // requested application may already be running. | 435 // requested application may already be running. |
| 439 if (ConnectToExistingInstance(¶ms)) | 436 if (ConnectToExistingInstance(¶ms)) |
| 440 return; | 437 return; |
| 441 | 438 |
| 442 Identity source = params->source(); | 439 Identity source = params->source(); |
| 443 // |base_filter| can be null when there is no manifest, e.g. for URL types | 440 // |base_filter| can be null when there is no manifest, e.g. for URL types |
| 444 // not resolvable by the resolver. | 441 // not resolvable by the resolver. |
| 445 CapabilityFilter filter = GetPermissiveCapabilityFilter(); | 442 CapabilityFilter filter = GetPermissiveCapabilityFilter(); |
| 446 if (!base_filter.is_null()) | 443 if (!base_filter.is_null()) |
| 447 filter = base_filter->filter.To<CapabilityFilter>(); | 444 filter = base_filter->filter.To<CapabilityFilter>(); |
| 448 Identity target = params->target(); | 445 Identity target = params->target(); |
| 449 target.SetFilter(filter); | 446 target.set_filter(filter); |
| 450 | 447 |
| 451 mojom::ShellClientRequest request; | 448 mojom::ShellClientRequest request; |
| 452 Instance* instance = CreateInstance(target, &request); | 449 Instance* instance = CreateInstance(target, &request); |
| 453 instance->ConnectToClient(std::move(params)); | 450 instance->ConnectToClient(std::move(params)); |
| 454 | 451 |
| 455 if (LoadWithLoader(target, &request)) | 452 if (LoadWithLoader(target, &request)) |
| 456 return; | 453 return; |
| 457 | 454 |
| 458 CHECK(!file_url.is_null() && !base_filter.is_null()); | 455 CHECK(!file_url.is_null() && !base_filter.is_null()); |
| 459 | 456 |
| 460 GURL resolved_gurl = resolved_url.To<GURL>(); | 457 if (target.name() != resolved_name) { |
| 461 if (target.url().spec() != resolved_url) { | |
| 462 // In cases where a package alias is resolved, we have to use the qualifier | 458 // In cases where a package alias is resolved, we have to use the qualifier |
| 463 // from the original request rather than for the package itself, which will | 459 // from the original request rather than for the package itself, which will |
| 464 // always be the same. | 460 // always be the same. |
| 465 CreateShellClient( | 461 CreateShellClient( |
| 466 source, Identity(resolved_gurl, target.qualifier(), target.user_id()), | 462 source, Identity(resolved_name, target.qualifier(), target.user_id()), |
| 467 target.url(), std::move(request)); | 463 target.name(), std::move(request)); |
| 468 } else { | 464 } else { |
| 469 bool start_sandboxed = false; | 465 bool start_sandboxed = false; |
| 470 base::FilePath path = util::UrlToFilePath(file_url.To<GURL>()); | 466 base::FilePath path = util::UrlToFilePath(file_url.To<GURL>()); |
| 471 scoped_ptr<NativeRunner> runner = native_runner_factory_->Create(path); | 467 scoped_ptr<NativeRunner> runner = native_runner_factory_->Create(path); |
| 472 runner->Start(path, target, start_sandboxed, std::move(request), | 468 runner->Start(path, target, start_sandboxed, std::move(request), |
| 473 base::Bind(&ApplicationManager::ApplicationPIDAvailable, | 469 base::Bind(&ApplicationManager::ApplicationPIDAvailable, |
| 474 weak_ptr_factory_.GetWeakPtr(), instance->id()), | 470 weak_ptr_factory_.GetWeakPtr(), instance->id()), |
| 475 base::Bind(&ApplicationManager::CleanupRunner, | 471 base::Bind(&ApplicationManager::CleanupRunner, |
| 476 weak_ptr_factory_.GetWeakPtr(), runner.get())); | 472 weak_ptr_factory_.GetWeakPtr(), runner.get())); |
| 477 instance->SetNativeRunner(runner.get()); | 473 instance->SetNativeRunner(runner.get()); |
| 478 native_runners_.push_back(std::move(runner)); | 474 native_runners_.push_back(std::move(runner)); |
| 479 } | 475 } |
| 480 } | 476 } |
| 481 | 477 |
| 482 bool ApplicationManager::LoadWithLoader(const Identity& target, | 478 bool ApplicationManager::LoadWithLoader(const Identity& target, |
| 483 mojom::ShellClientRequest* request) { | 479 mojom::ShellClientRequest* request) { |
| 484 ApplicationLoader* loader = GetLoaderForURL(target.url()); | 480 ApplicationLoader* loader = GetLoaderForName(target.name()); |
| 485 if (!loader) | 481 if (!loader) |
| 486 return false; | 482 return false; |
| 487 loader->Load(target.url(), std::move(*request)); | 483 loader->Load(target.name(), std::move(*request)); |
| 488 return true; | 484 return true; |
| 489 } | 485 } |
| 490 | 486 |
| 491 ApplicationLoader* ApplicationManager::GetLoaderForURL(const GURL& url) { | 487 ApplicationLoader* ApplicationManager::GetLoaderForName( |
| 492 auto url_it = url_to_loader_.find(url); | 488 const std::string& name) { |
| 493 if (url_it != url_to_loader_.end()) | 489 auto name_it = name_to_loader_.find(name); |
| 494 return url_it->second; | 490 if (name_it != name_to_loader_.end()) |
| 491 return name_it->second; |
| 495 return default_loader_.get(); | 492 return default_loader_.get(); |
| 496 } | 493 } |
| 497 | 494 |
| 498 void ApplicationManager::CleanupRunner(NativeRunner* runner) { | 495 void ApplicationManager::CleanupRunner(NativeRunner* runner) { |
| 499 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { | 496 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { |
| 500 if (it->get() == runner) { | 497 if (it->get() == runner) { |
| 501 native_runners_.erase(it); | 498 native_runners_.erase(it); |
| 502 return; | 499 return; |
| 503 } | 500 } |
| 504 } | 501 } |
| 505 } | 502 } |
| 506 | 503 |
| 507 mojom::ApplicationInfoPtr ApplicationManager::CreateApplicationInfoForInstance( | 504 mojom::ApplicationInfoPtr ApplicationManager::CreateApplicationInfoForInstance( |
| 508 Instance* instance) const { | 505 Instance* instance) const { |
| 509 mojom::ApplicationInfoPtr info(mojom::ApplicationInfo::New()); | 506 mojom::ApplicationInfoPtr info(mojom::ApplicationInfo::New()); |
| 510 info->id = instance->id(); | 507 info->id = instance->id(); |
| 511 info->url = instance->identity().url().spec(); | 508 info->name = instance->identity().name(); |
| 512 info->qualifier = instance->identity().qualifier(); | 509 info->qualifier = instance->identity().qualifier(); |
| 513 if (instance->identity().url().spec() == "mojo://shell/") | 510 if (instance->identity().name() == "mojo:shell") |
| 514 info->pid = base::Process::Current().Pid(); | 511 info->pid = base::Process::Current().Pid(); |
| 515 else | 512 else |
| 516 info->pid = instance->pid(); | 513 info->pid = instance->pid(); |
| 517 return info; | 514 return info; |
| 518 } | 515 } |
| 519 | 516 |
| 520 } // namespace shell | 517 } // namespace shell |
| 521 } // namespace mojo | 518 } // namespace mojo |
| OLD | NEW |