| 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 "services/shell/shell.h" | 5 #include "services/shell/shell.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "mojo/util/filename_util.h" | 24 #include "mojo/util/filename_util.h" |
| 25 #include "services/shell/connect_util.h" | 25 #include "services/shell/connect_util.h" |
| 26 #include "services/shell/public/cpp/connector.h" | 26 #include "services/shell/public/cpp/connector.h" |
| 27 #include "services/shell/public/cpp/names.h" | 27 #include "services/shell/public/cpp/names.h" |
| 28 #include "services/shell/public/cpp/shell_connection.h" | 28 #include "services/shell/public/cpp/shell_connection.h" |
| 29 #include "services/shell/public/interfaces/connector.mojom.h" | 29 #include "services/shell/public/interfaces/connector.mojom.h" |
| 30 #include "services/shell/public/interfaces/shell.mojom.h" | 30 #include "services/shell/public/interfaces/shell.mojom.h" |
| 31 #include "services/shell/public/interfaces/shell_client.mojom.h" | 31 #include "services/shell/public/interfaces/shell_client.mojom.h" |
| 32 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 33 | 33 |
| 34 namespace mojo { | |
| 35 namespace shell { | 34 namespace shell { |
| 35 |
| 36 namespace { | 36 namespace { |
| 37 |
| 37 const char kCatalogName[] = "mojo:catalog"; | 38 const char kCatalogName[] = "mojo:catalog"; |
| 38 const char kShellName[] = "mojo:shell"; | 39 const char kShellName[] = "mojo:shell"; |
| 39 const char kCapabilityClass_UserID[] = "user_id"; | 40 const char kCapabilityClass_UserID[] = "user_id"; |
| 40 const char kCapabilityClass_ClientProcess[] = "client_process"; | 41 const char kCapabilityClass_ClientProcess[] = "client_process"; |
| 41 const char kCapabilityClass_InstanceName[] = "instance_name"; | 42 const char kCapabilityClass_InstanceName[] = "instance_name"; |
| 42 const char kCapabilityClass_AllUsers[] = "all_users"; | 43 const char kCapabilityClass_AllUsers[] = "all_users"; |
| 43 | 44 |
| 44 void EmptyResolverCallback(mojom::ResolveResultPtr result) {} | 45 void EmptyResolverCallback(mojom::ResolveResultPtr result) {} |
| 45 | 46 |
| 46 } | 47 } // namespace |
| 47 | 48 |
| 48 Identity CreateShellIdentity() { | 49 Identity CreateShellIdentity() { |
| 49 return Identity(kShellName, mojom::kRootUserID); | 50 return Identity(kShellName, mojom::kRootUserID); |
| 50 } | 51 } |
| 51 | 52 |
| 52 Identity CreateCatalogIdentity() { | 53 Identity CreateCatalogIdentity() { |
| 53 return Identity(kCatalogName, mojom::kRootUserID); | 54 return Identity(kCatalogName, mojom::kRootUserID); |
| 54 } | 55 } |
| 55 | 56 |
| 56 CapabilitySpec GetPermissiveCapabilities() { | 57 CapabilitySpec GetPermissiveCapabilities() { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ~Instance() override { | 130 ~Instance() override { |
| 130 if (parent_) | 131 if (parent_) |
| 131 parent_->RemoveChild(this); | 132 parent_->RemoveChild(this); |
| 132 // |children_| will be modified during destruction. | 133 // |children_| will be modified during destruction. |
| 133 std::set<Instance*> children = children_; | 134 std::set<Instance*> children = children_; |
| 134 for (auto child : children) | 135 for (auto child : children) |
| 135 shell_->OnInstanceError(child); | 136 shell_->OnInstanceError(child); |
| 136 } | 137 } |
| 137 | 138 |
| 138 Instance* parent() { return parent_; } | 139 Instance* parent() { return parent_; } |
| 140 |
| 139 void AddChild(Instance* child) { | 141 void AddChild(Instance* child) { |
| 140 children_.insert(child); | 142 children_.insert(child); |
| 141 child->parent_ = this; | 143 child->parent_ = this; |
| 142 } | 144 } |
| 145 |
| 143 void RemoveChild(Instance* child) { | 146 void RemoveChild(Instance* child) { |
| 144 auto it = children_.find(child); | 147 auto it = children_.find(child); |
| 145 DCHECK(it != children_.end()); | 148 DCHECK(it != children_.end()); |
| 146 children_.erase(it); | 149 children_.erase(it); |
| 147 child->parent_ = nullptr; | 150 child->parent_ = nullptr; |
| 148 } | 151 } |
| 149 | 152 |
| 150 void ConnectToClient(scoped_ptr<ConnectParams> params) { | 153 void ConnectToClient(std::unique_ptr<ConnectParams> params) { |
| 151 CHECK(shell_client_.is_bound()); | 154 CHECK(shell_client_.is_bound()); |
| 152 params->connect_callback().Run(mojom::ConnectResult::SUCCEEDED, | 155 params->connect_callback().Run(mojom::ConnectResult::SUCCEEDED, |
| 153 identity_.user_id(), id_); | 156 identity_.user_id(), id_); |
| 154 uint32_t source_id = mojom::kInvalidInstanceID; | 157 uint32_t source_id = mojom::kInvalidInstanceID; |
| 155 CapabilityRequest spec; | 158 CapabilityRequest spec; |
| 156 spec.interfaces.insert("*"); | 159 spec.interfaces.insert("*"); |
| 157 Instance* source = shell_->GetExistingInstance(params->source()); | 160 Instance* source = shell_->GetExistingInstance(params->source()); |
| 158 if (source) { | 161 if (source) { |
| 159 spec = GenerateCapabilityRequestForConnection( | 162 spec = GenerateCapabilityRequestForConnection( |
| 160 source->capability_spec_, identity_, capability_spec_); | 163 source->capability_spec_, identity_, capability_spec_); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 182 mojom::ShellClientPtr client; | 185 mojom::ShellClientPtr client; |
| 183 client.Bind(mojom::ShellClientPtrInfo( | 186 client.Bind(mojom::ShellClientPtrInfo( |
| 184 std::move(client_process_connection->shell_client), 0)); | 187 std::move(client_process_connection->shell_client), 0)); |
| 185 pid_receiver_binding_.Bind( | 188 pid_receiver_binding_.Bind( |
| 186 std::move(client_process_connection->pid_receiver_request)); | 189 std::move(client_process_connection->pid_receiver_request)); |
| 187 StartWithClient(std::move(client)); | 190 StartWithClient(std::move(client)); |
| 188 } | 191 } |
| 189 | 192 |
| 190 void StartWithFilePath(const base::FilePath& path) { | 193 void StartWithFilePath(const base::FilePath& path) { |
| 191 CHECK(!shell_client_); | 194 CHECK(!shell_client_); |
| 192 scoped_ptr<NativeRunner> runner = | 195 std::unique_ptr<NativeRunner> runner = |
| 193 shell_->native_runner_factory_->Create(path); | 196 shell_->native_runner_factory_->Create(path); |
| 194 bool start_sandboxed = false; | 197 bool start_sandboxed = false; |
| 195 mojom::ShellClientPtr client = runner->Start( | 198 mojom::ShellClientPtr client = runner->Start( |
| 196 path, identity_, start_sandboxed, | 199 path, identity_, start_sandboxed, |
| 197 base::Bind(&Instance::PIDAvailable, weak_factory_.GetWeakPtr()), | 200 base::Bind(&Instance::PIDAvailable, weak_factory_.GetWeakPtr()), |
| 198 base::Bind(&shell::Shell::CleanupRunner, | 201 base::Bind(&shell::Shell::CleanupRunner, |
| 199 shell_->weak_ptr_factory_.GetWeakPtr(), runner.get())); | 202 shell_->weak_ptr_factory_.GetWeakPtr(), runner.get())); |
| 200 shell_->native_runners_.push_back(std::move(runner)); | 203 shell_->native_runners_.push_back(std::move(runner)); |
| 201 StartWithClient(std::move(client)); | 204 StartWithClient(std::move(client)); |
| 202 } | 205 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 237 |
| 235 if (!ValidateIdentity(target, callback)) | 238 if (!ValidateIdentity(target, callback)) |
| 236 return; | 239 return; |
| 237 if (!ValidateClientProcessConnection(&client_process_connection, target, | 240 if (!ValidateClientProcessConnection(&client_process_connection, target, |
| 238 callback)) { | 241 callback)) { |
| 239 return; | 242 return; |
| 240 } | 243 } |
| 241 if (!ValidateCapabilities(target, callback)) | 244 if (!ValidateCapabilities(target, callback)) |
| 242 return; | 245 return; |
| 243 | 246 |
| 244 scoped_ptr<ConnectParams> params(new ConnectParams); | 247 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 245 params->set_source(identity_); | 248 params->set_source(identity_); |
| 246 params->set_target(target); | 249 params->set_target(target); |
| 247 params->set_remote_interfaces(std::move(remote_interfaces)); | 250 params->set_remote_interfaces(std::move(remote_interfaces)); |
| 248 params->set_local_interfaces(std::move(local_interfaces)); | 251 params->set_local_interfaces(std::move(local_interfaces)); |
| 249 params->set_client_process_connection(std::move(client_process_connection)); | 252 params->set_client_process_connection(std::move(client_process_connection)); |
| 250 params->set_connect_callback(callback); | 253 params->set_connect_callback(callback); |
| 251 shell_->Connect(std::move(params)); | 254 shell_->Connect(std::move(params)); |
| 252 } | 255 } |
| 253 | 256 |
| 254 void Clone(mojom::ConnectorRequest request) override { | 257 void Clone(mojom::ConnectorRequest request) override { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 shell::Shell* const shell_; | 408 shell::Shell* const shell_; |
| 406 | 409 |
| 407 // An id that identifies this instance. Distinct from pid, as a single process | 410 // An id that identifies this instance. Distinct from pid, as a single process |
| 408 // may vend multiple application instances, and this object may exist before a | 411 // may vend multiple application instances, and this object may exist before a |
| 409 // process is launched. | 412 // process is launched. |
| 410 const uint32_t id_; | 413 const uint32_t id_; |
| 411 const Identity identity_; | 414 const Identity identity_; |
| 412 const CapabilitySpec capability_spec_; | 415 const CapabilitySpec capability_spec_; |
| 413 const bool allow_any_application_; | 416 const bool allow_any_application_; |
| 414 mojom::ShellClientPtr shell_client_; | 417 mojom::ShellClientPtr shell_client_; |
| 415 Binding<mojom::PIDReceiver> pid_receiver_binding_; | 418 mojo::Binding<mojom::PIDReceiver> pid_receiver_binding_; |
| 416 BindingSet<mojom::Connector> connectors_; | 419 mojo::BindingSet<mojom::Connector> connectors_; |
| 417 BindingSet<mojom::Shell> shell_bindings_; | 420 mojo::BindingSet<mojom::Shell> shell_bindings_; |
| 418 NativeRunner* runner_ = nullptr; | 421 NativeRunner* runner_ = nullptr; |
| 419 base::ProcessId pid_ = base::kNullProcessId; | 422 base::ProcessId pid_ = base::kNullProcessId; |
| 420 Instance* parent_ = nullptr; | 423 Instance* parent_ = nullptr; |
| 421 std::set<Instance*> children_; | 424 std::set<Instance*> children_; |
| 422 base::WeakPtrFactory<Instance> weak_factory_; | 425 base::WeakPtrFactory<Instance> weak_factory_; |
| 423 | 426 |
| 424 DISALLOW_COPY_AND_ASSIGN(Instance); | 427 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 425 }; | 428 }; |
| 426 | 429 |
| 427 // static | 430 // static |
| 428 Shell::TestAPI::TestAPI(Shell* shell) : shell_(shell) {} | 431 Shell::TestAPI::TestAPI(Shell* shell) : shell_(shell) {} |
| 429 Shell::TestAPI::~TestAPI() {} | 432 Shell::TestAPI::~TestAPI() {} |
| 430 | 433 |
| 431 bool Shell::TestAPI::HasRunningInstanceForName(const std::string& name) const { | 434 bool Shell::TestAPI::HasRunningInstanceForName(const std::string& name) const { |
| 432 for (const auto& entry : shell_->identity_to_instance_) { | 435 for (const auto& entry : shell_->identity_to_instance_) { |
| 433 if (entry.first.name() == name) | 436 if (entry.first.name() == name) |
| 434 return true; | 437 return true; |
| 435 } | 438 } |
| 436 return false; | 439 return false; |
| 437 } | 440 } |
| 438 | 441 |
| 439 //////////////////////////////////////////////////////////////////////////////// | 442 //////////////////////////////////////////////////////////////////////////////// |
| 440 // Shell, public: | 443 // Shell, public: |
| 441 | 444 |
| 442 Shell::Shell(scoped_ptr<NativeRunnerFactory> native_runner_factory, | 445 Shell::Shell(std::unique_ptr<NativeRunnerFactory> native_runner_factory, |
| 443 mojom::ShellClientPtr catalog) | 446 mojom::ShellClientPtr catalog) |
| 444 : native_runner_factory_(std::move(native_runner_factory)), | 447 : native_runner_factory_(std::move(native_runner_factory)), |
| 445 weak_ptr_factory_(this) { | 448 weak_ptr_factory_(this) { |
| 446 mojom::ShellClientPtr client; | 449 mojom::ShellClientPtr client; |
| 447 mojom::ShellClientRequest request = GetProxy(&client); | 450 mojom::ShellClientRequest request = mojo::GetProxy(&client); |
| 448 Instance* instance = CreateInstance(Identity(), CreateShellIdentity(), | 451 Instance* instance = CreateInstance(Identity(), CreateShellIdentity(), |
| 449 GetPermissiveCapabilities()); | 452 GetPermissiveCapabilities()); |
| 450 instance->StartWithClient(std::move(client)); | 453 instance->StartWithClient(std::move(client)); |
| 451 singletons_.insert(kShellName); | 454 singletons_.insert(kShellName); |
| 452 shell_connection_.reset(new ShellConnection(this, std::move(request))); | 455 shell_connection_.reset(new ShellConnection(this, std::move(request))); |
| 453 | 456 |
| 454 if (catalog) | 457 if (catalog) |
| 455 InitCatalog(std::move(catalog)); | 458 InitCatalog(std::move(catalog)); |
| 456 } | 459 } |
| 457 | 460 |
| 458 Shell::~Shell() { | 461 Shell::~Shell() { |
| 459 TerminateShellConnections(); | 462 TerminateShellConnections(); |
| 460 STLDeleteValues(&name_to_loader_); | 463 STLDeleteValues(&name_to_loader_); |
| 461 for (auto& runner : native_runners_) | 464 for (auto& runner : native_runners_) |
| 462 runner.reset(); | 465 runner.reset(); |
| 463 } | 466 } |
| 464 | 467 |
| 465 void Shell::SetInstanceQuitCallback( | 468 void Shell::SetInstanceQuitCallback( |
| 466 base::Callback<void(const Identity&)> callback) { | 469 base::Callback<void(const Identity&)> callback) { |
| 467 instance_quit_callback_ = callback; | 470 instance_quit_callback_ = callback; |
| 468 } | 471 } |
| 469 | 472 |
| 470 void Shell::Connect(scoped_ptr<ConnectParams> params) { | 473 void Shell::Connect(std::unique_ptr<ConnectParams> params) { |
| 471 Connect(std::move(params), nullptr); | 474 Connect(std::move(params), nullptr); |
| 472 } | 475 } |
| 473 | 476 |
| 474 mojom::ShellClientRequest Shell::InitInstanceForEmbedder( | 477 mojom::ShellClientRequest Shell::InitInstanceForEmbedder( |
| 475 const std::string& name) { | 478 const std::string& name) { |
| 476 scoped_ptr<ConnectParams> params(new ConnectParams); | 479 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 477 | 480 |
| 478 Identity embedder_identity(name, mojom::kRootUserID); | 481 Identity embedder_identity(name, mojom::kRootUserID); |
| 479 params->set_source(embedder_identity); | 482 params->set_source(embedder_identity); |
| 480 params->set_target(embedder_identity); | 483 params->set_target(embedder_identity); |
| 481 | 484 |
| 482 mojom::ShellClientPtr client; | 485 mojom::ShellClientPtr client; |
| 483 mojom::ShellClientRequest request = GetProxy(&client); | 486 mojom::ShellClientRequest request = mojo::GetProxy(&client); |
| 484 Connect(std::move(params), std::move(client)); | 487 Connect(std::move(params), std::move(client)); |
| 485 | 488 |
| 486 return request; | 489 return request; |
| 487 } | 490 } |
| 488 | 491 |
| 489 void Shell::SetLoaderForName(scoped_ptr<Loader> loader, | 492 void Shell::SetLoaderForName(std::unique_ptr<Loader> loader, |
| 490 const std::string& name) { | 493 const std::string& name) { |
| 491 auto it = name_to_loader_.find(name); | 494 auto it = name_to_loader_.find(name); |
| 492 if (it != name_to_loader_.end()) | 495 if (it != name_to_loader_.end()) |
| 493 delete it->second; | 496 delete it->second; |
| 494 name_to_loader_[name] = loader.release(); | 497 name_to_loader_[name] = loader.release(); |
| 495 } | 498 } |
| 496 | 499 |
| 497 //////////////////////////////////////////////////////////////////////////////// | 500 //////////////////////////////////////////////////////////////////////////////// |
| 498 // Shell, ShellClient implementation: | 501 // Shell, ShellClient implementation: |
| 499 | 502 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 518 | 521 |
| 519 void Shell::InitCatalog(mojom::ShellClientPtr catalog) { | 522 void Shell::InitCatalog(mojom::ShellClientPtr catalog) { |
| 520 Instance* instance = CreateInstance(CreateShellIdentity(), | 523 Instance* instance = CreateInstance(CreateShellIdentity(), |
| 521 CreateCatalogIdentity(), | 524 CreateCatalogIdentity(), |
| 522 CapabilitySpec()); | 525 CapabilitySpec()); |
| 523 singletons_.insert(kCatalogName); | 526 singletons_.insert(kCatalogName); |
| 524 instance->StartWithClient(std::move(catalog)); | 527 instance->StartWithClient(std::move(catalog)); |
| 525 | 528 |
| 526 // TODO(beng): this doesn't work anymore. | 529 // TODO(beng): this doesn't work anymore. |
| 527 // Seed the catalog with manifest info for the shell & catalog. | 530 // Seed the catalog with manifest info for the shell & catalog. |
| 528 shell::mojom::ShellResolverPtr resolver; | 531 mojom::ShellResolverPtr resolver; |
| 529 shell_connection_->connector()->ConnectToInterface(kCatalogName, &resolver); | 532 shell_connection_->connector()->ConnectToInterface(kCatalogName, &resolver); |
| 530 resolver->ResolveMojoName(kCatalogName, base::Bind(&EmptyResolverCallback)); | 533 resolver->ResolveMojoName(kCatalogName, base::Bind(&EmptyResolverCallback)); |
| 531 resolver->ResolveMojoName(kShellName, base::Bind(&EmptyResolverCallback)); | 534 resolver->ResolveMojoName(kShellName, base::Bind(&EmptyResolverCallback)); |
| 532 } | 535 } |
| 533 | 536 |
| 534 void Shell::TerminateShellConnections() { | 537 void Shell::TerminateShellConnections() { |
| 535 Instance* instance = GetExistingInstance(CreateShellIdentity()); | 538 Instance* instance = GetExistingInstance(CreateShellIdentity()); |
| 536 DCHECK(instance); | 539 DCHECK(instance); |
| 537 OnInstanceError(instance); | 540 OnInstanceError(instance); |
| 538 } | 541 } |
| 539 | 542 |
| 540 void Shell::OnInstanceError(Instance* instance) { | 543 void Shell::OnInstanceError(Instance* instance) { |
| 541 const Identity identity = instance->identity(); | 544 const Identity identity = instance->identity(); |
| 542 // Remove the shell. | 545 // Remove the shell. |
| 543 auto it = identity_to_instance_.find(identity); | 546 auto it = identity_to_instance_.find(identity); |
| 544 DCHECK(it != identity_to_instance_.end()); | 547 DCHECK(it != identity_to_instance_.end()); |
| 545 int id = instance->id(); | 548 int id = instance->id(); |
| 546 delete it->second; | 549 delete it->second; |
| 547 identity_to_instance_.erase(it); | 550 identity_to_instance_.erase(it); |
| 548 instance_listeners_.ForAllPtrs([this, id](mojom::InstanceListener* listener) { | 551 instance_listeners_.ForAllPtrs([this, id](mojom::InstanceListener* listener) { |
| 549 listener->InstanceDestroyed(id); | 552 listener->InstanceDestroyed(id); |
| 550 }); | 553 }); |
| 551 if (!instance_quit_callback_.is_null()) | 554 if (!instance_quit_callback_.is_null()) |
| 552 instance_quit_callback_.Run(identity); | 555 instance_quit_callback_.Run(identity); |
| 553 } | 556 } |
| 554 | 557 |
| 555 void Shell::Connect(scoped_ptr<ConnectParams> params, | 558 void Shell::Connect(std::unique_ptr<ConnectParams> params, |
| 556 mojom::ShellClientPtr client) { | 559 mojom::ShellClientPtr client) { |
| 557 TRACE_EVENT_INSTANT1("mojo_shell", "Shell::Connect", | 560 TRACE_EVENT_INSTANT1("mojo_shell", "Shell::Connect", |
| 558 TRACE_EVENT_SCOPE_THREAD, "original_name", | 561 TRACE_EVENT_SCOPE_THREAD, "original_name", |
| 559 params->target().name()); | 562 params->target().name()); |
| 560 DCHECK(IsValidName(params->target().name())); | 563 DCHECK(IsValidName(params->target().name())); |
| 561 DCHECK(base::IsValidGUID(params->target().user_id())); | 564 DCHECK(base::IsValidGUID(params->target().user_id())); |
| 562 DCHECK_NE(mojom::kInheritUserID, params->target().user_id()); | 565 DCHECK_NE(mojom::kInheritUserID, params->target().user_id()); |
| 563 DCHECK(!client.is_bound() || !identity_to_instance_.count(params->target())); | 566 DCHECK(!client.is_bound() || !identity_to_instance_.count(params->target())); |
| 564 | 567 |
| 565 // Connect to an existing matching instance, if possible. | 568 // Connect to an existing matching instance, if possible. |
| 566 if (!client.is_bound() && ConnectToExistingInstance(¶ms)) | 569 if (!client.is_bound() && ConnectToExistingInstance(¶ms)) |
| 567 return; | 570 return; |
| 568 | 571 |
| 569 // The catalog needs to see the source identity as that of the originating | 572 // The catalog needs to see the source identity as that of the originating |
| 570 // app so it loads the correct store. Since the catalog is itself run as root | 573 // app so it loads the correct store. Since the catalog is itself run as root |
| 571 // when this re-enters Connect() it'll be handled by | 574 // when this re-enters Connect() it'll be handled by |
| 572 // ConnectToExistingInstance(). | 575 // ConnectToExistingInstance(). |
| 573 mojom::ShellResolverPtr resolver; | 576 mojom::ShellResolverPtr resolver; |
| 574 ConnectToInterface(this, Identity(kShellName, params->target().user_id()), | 577 ConnectToInterface(this, Identity(kShellName, params->target().user_id()), |
| 575 CreateCatalogIdentity(), &resolver); | 578 CreateCatalogIdentity(), &resolver); |
| 576 | 579 |
| 577 std::string name = params->target().name(); | 580 std::string name = params->target().name(); |
| 578 mojom::ShellResolver* resolver_raw = resolver.get(); | 581 mojom::ShellResolver* resolver_raw = resolver.get(); |
| 579 resolver_raw->ResolveMojoName( | 582 resolver_raw->ResolveMojoName( |
| 580 name, | 583 name, base::Bind(&shell::Shell::OnGotResolvedName, |
| 581 base::Bind(&Shell::OnGotResolvedName, weak_ptr_factory_.GetWeakPtr(), | 584 weak_ptr_factory_.GetWeakPtr(), |
| 582 base::Passed(std::move(resolver)), base::Passed(¶ms), | 585 base::Passed(std::move(resolver)), base::Passed(¶ms), |
| 583 base::Passed(&client))); | 586 base::Passed(&client))); |
| 584 } | 587 } |
| 585 | 588 |
| 586 Shell::Instance* Shell::GetExistingInstance(const Identity& identity) const { | 589 Shell::Instance* Shell::GetExistingInstance(const Identity& identity) const { |
| 587 const auto& it = identity_to_instance_.find(identity); | 590 const auto& it = identity_to_instance_.find(identity); |
| 588 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; | 591 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; |
| 589 if (instance) | 592 if (instance) |
| 590 return instance; | 593 return instance; |
| 591 | 594 |
| 592 if (singletons_.find(identity.name()) != singletons_.end()) { | 595 if (singletons_.find(identity.name()) != singletons_.end()) { |
| 593 for (auto entry : identity_to_instance_) { | 596 for (auto entry : identity_to_instance_) { |
| 594 if (entry.first.name() == identity.name() && | 597 if (entry.first.name() == identity.name() && |
| 595 entry.first.instance() == identity.instance()) { | 598 entry.first.instance() == identity.instance()) { |
| 596 return entry.second; | 599 return entry.second; |
| 597 } | 600 } |
| 598 } | 601 } |
| 599 } | 602 } |
| 600 return nullptr; | 603 return nullptr; |
| 601 } | 604 } |
| 602 | 605 |
| 603 void Shell::NotifyPIDAvailable(uint32_t id, base::ProcessId pid) { | 606 void Shell::NotifyPIDAvailable(uint32_t id, base::ProcessId pid) { |
| 604 instance_listeners_.ForAllPtrs([id, pid](mojom::InstanceListener* listener) { | 607 instance_listeners_.ForAllPtrs([id, pid](mojom::InstanceListener* listener) { |
| 605 listener->InstancePIDAvailable(id, pid); | 608 listener->InstancePIDAvailable(id, pid); |
| 606 }); | 609 }); |
| 607 } | 610 } |
| 608 | 611 |
| 609 bool Shell::ConnectToExistingInstance(scoped_ptr<ConnectParams>* params) { | 612 bool Shell::ConnectToExistingInstance(std::unique_ptr<ConnectParams>* params) { |
| 610 Instance* instance = GetExistingInstance((*params)->target()); | 613 Instance* instance = GetExistingInstance((*params)->target()); |
| 611 if (instance) | 614 if (instance) |
| 612 instance->ConnectToClient(std::move(*params)); | 615 instance->ConnectToClient(std::move(*params)); |
| 613 return !!instance; | 616 return !!instance; |
| 614 } | 617 } |
| 615 | 618 |
| 616 Shell::Instance* Shell::CreateInstance(const Identity& source, | 619 Shell::Instance* Shell::CreateInstance(const Identity& source, |
| 617 const Identity& target, | 620 const Identity& target, |
| 618 const CapabilitySpec& spec) { | 621 const CapabilitySpec& spec) { |
| 619 CHECK(target.user_id() != mojom::kInheritUserID); | 622 CHECK(target.user_id() != mojom::kInheritUserID); |
| 620 Instance* instance = new Instance(this, target, spec); | 623 Instance* instance = new Instance(this, target, spec); |
| 621 DCHECK(identity_to_instance_.find(target) == | 624 DCHECK(identity_to_instance_.find(target) == |
| 622 identity_to_instance_.end()); | 625 identity_to_instance_.end()); |
| 623 Instance* source_instance = GetExistingInstance(source); | 626 Instance* source_instance = GetExistingInstance(source); |
| 624 if (source_instance) | 627 if (source_instance) |
| 625 source_instance->AddChild(instance); | 628 source_instance->AddChild(instance); |
| 626 identity_to_instance_[target] = instance; | 629 identity_to_instance_[target] = instance; |
| 627 mojom::InstanceInfoPtr info = instance->CreateInstanceInfo(); | 630 mojom::InstanceInfoPtr info = instance->CreateInstanceInfo(); |
| 628 instance_listeners_.ForAllPtrs( | 631 instance_listeners_.ForAllPtrs( |
| 629 [this, &info](mojom::InstanceListener* listener) { | 632 [this, &info](mojom::InstanceListener* listener) { |
| 630 listener->InstanceCreated(info.Clone()); | 633 listener->InstanceCreated(info.Clone()); |
| 631 }); | 634 }); |
| 632 return instance; | 635 return instance; |
| 633 } | 636 } |
| 634 | 637 |
| 635 void Shell::AddInstanceListener(mojom::InstanceListenerPtr listener) { | 638 void Shell::AddInstanceListener(mojom::InstanceListenerPtr listener) { |
| 636 // TODO(beng): filter instances provided by those visible to this client. | 639 // TODO(beng): filter instances provided by those visible to this client. |
| 637 Array<mojom::InstanceInfoPtr> instances; | 640 mojo::Array<mojom::InstanceInfoPtr> instances; |
| 638 for (auto& instance : identity_to_instance_) | 641 for (auto& instance : identity_to_instance_) |
| 639 instances.push_back(instance.second->CreateInstanceInfo()); | 642 instances.push_back(instance.second->CreateInstanceInfo()); |
| 640 listener->SetExistingInstances(std::move(instances)); | 643 listener->SetExistingInstances(std::move(instances)); |
| 641 | 644 |
| 642 instance_listeners_.AddPtr(std::move(listener)); | 645 instance_listeners_.AddPtr(std::move(listener)); |
| 643 } | 646 } |
| 644 | 647 |
| 645 void Shell::CreateShellClientWithFactory(const Identity& shell_client_factory, | 648 void Shell::CreateShellClientWithFactory(const Identity& shell_client_factory, |
| 646 const std::string& name, | 649 const std::string& name, |
| 647 mojom::ShellClientRequest request) { | 650 mojom::ShellClientRequest request) { |
| 648 mojom::ShellClientFactory* factory = | 651 mojom::ShellClientFactory* factory = |
| 649 GetShellClientFactory(shell_client_factory); | 652 GetShellClientFactory(shell_client_factory); |
| 650 factory->CreateShellClient(std::move(request), name); | 653 factory->CreateShellClient(std::move(request), name); |
| 651 } | 654 } |
| 652 | 655 |
| 653 mojom::ShellClientFactory* Shell::GetShellClientFactory( | 656 mojom::ShellClientFactory* Shell::GetShellClientFactory( |
| 654 const Identity& shell_client_factory_identity) { | 657 const Identity& shell_client_factory_identity) { |
| 655 auto it = shell_client_factories_.find(shell_client_factory_identity); | 658 auto it = shell_client_factories_.find(shell_client_factory_identity); |
| 656 if (it != shell_client_factories_.end()) | 659 if (it != shell_client_factories_.end()) |
| 657 return it->second.get(); | 660 return it->second.get(); |
| 658 | 661 |
| 659 Identity source_identity(kShellName, mojom::kInheritUserID); | 662 Identity source_identity(kShellName, mojom::kInheritUserID); |
| 660 mojom::ShellClientFactoryPtr factory; | 663 mojom::ShellClientFactoryPtr factory; |
| 661 ConnectToInterface(this, source_identity, shell_client_factory_identity, | 664 ConnectToInterface(this, source_identity, shell_client_factory_identity, |
| 662 &factory); | 665 &factory); |
| 663 mojom::ShellClientFactory* factory_interface = factory.get(); | 666 mojom::ShellClientFactory* factory_interface = factory.get(); |
| 664 factory.set_connection_error_handler( | 667 factory.set_connection_error_handler(base::Bind( |
| 665 base::Bind(&Shell::OnShellClientFactoryLost, | 668 &shell::Shell::OnShellClientFactoryLost, weak_ptr_factory_.GetWeakPtr(), |
| 666 weak_ptr_factory_.GetWeakPtr(), | 669 shell_client_factory_identity)); |
| 667 shell_client_factory_identity)); | |
| 668 shell_client_factories_[shell_client_factory_identity] = std::move(factory); | 670 shell_client_factories_[shell_client_factory_identity] = std::move(factory); |
| 669 return factory_interface; | 671 return factory_interface; |
| 670 } | 672 } |
| 671 | 673 |
| 672 void Shell::OnShellClientFactoryLost(const Identity& which) { | 674 void Shell::OnShellClientFactoryLost(const Identity& which) { |
| 673 // Remove the mapping. | 675 // Remove the mapping. |
| 674 auto it = shell_client_factories_.find(which); | 676 auto it = shell_client_factories_.find(which); |
| 675 DCHECK(it != shell_client_factories_.end()); | 677 DCHECK(it != shell_client_factories_.end()); |
| 676 shell_client_factories_.erase(it); | 678 shell_client_factories_.erase(it); |
| 677 } | 679 } |
| 678 | 680 |
| 679 void Shell::OnGotResolvedName(mojom::ShellResolverPtr resolver, | 681 void Shell::OnGotResolvedName(mojom::ShellResolverPtr resolver, |
| 680 scoped_ptr<ConnectParams> params, | 682 std::unique_ptr<ConnectParams> params, |
| 681 mojom::ShellClientPtr client, | 683 mojom::ShellClientPtr client, |
| 682 mojom::ResolveResultPtr result) { | 684 mojom::ResolveResultPtr result) { |
| 683 std::string instance_name = params->target().instance(); | 685 std::string instance_name = params->target().instance(); |
| 684 if (instance_name == GetNamePath(params->target().name()) && | 686 if (instance_name == GetNamePath(params->target().name()) && |
| 685 result->qualifier != GetNamePath(result->resolved_name)) { | 687 result->qualifier != GetNamePath(result->resolved_name)) { |
| 686 instance_name = result->qualifier; | 688 instance_name = result->qualifier; |
| 687 } | 689 } |
| 688 Identity target(params->target().name(), params->target().user_id(), | 690 Identity target(params->target().name(), params->target().user_id(), |
| 689 instance_name); | 691 instance_name); |
| 690 params->set_target(target); | 692 params->set_target(target); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 CHECK(!result->package_url.is_null() && !result->capabilities.is_null()); | 743 CHECK(!result->package_url.is_null() && !result->capabilities.is_null()); |
| 742 | 744 |
| 743 if (target.name() != result->resolved_name) { | 745 if (target.name() != result->resolved_name) { |
| 744 instance->StartWithClient(std::move(client)); | 746 instance->StartWithClient(std::move(client)); |
| 745 Identity factory(result->resolved_name, target.user_id(), | 747 Identity factory(result->resolved_name, target.user_id(), |
| 746 instance_name); | 748 instance_name); |
| 747 CreateShellClientWithFactory(factory, target.name(), | 749 CreateShellClientWithFactory(factory, target.name(), |
| 748 std::move(request)); | 750 std::move(request)); |
| 749 } else { | 751 } else { |
| 750 instance->StartWithFilePath( | 752 instance->StartWithFilePath( |
| 751 util::UrlToFilePath(result->package_url.To<GURL>())); | 753 mojo::util::UrlToFilePath(result->package_url.To<GURL>())); |
| 752 } | 754 } |
| 753 } | 755 } |
| 754 } | 756 } |
| 755 | 757 |
| 756 // Now that the instance has a ShellClient, we can connect to it. | 758 // Now that the instance has a ShellClient, we can connect to it. |
| 757 instance->ConnectToClient(std::move(params)); | 759 instance->ConnectToClient(std::move(params)); |
| 758 } | 760 } |
| 759 | 761 |
| 760 bool Shell::LoadWithLoader(const Identity& target, | 762 bool Shell::LoadWithLoader(const Identity& target, |
| 761 mojom::ShellClientRequest* request) { | 763 mojom::ShellClientRequest* request) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 780 void Shell::CleanupRunner(NativeRunner* runner) { | 782 void Shell::CleanupRunner(NativeRunner* runner) { |
| 781 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { | 783 for (auto it = native_runners_.begin(); it != native_runners_.end(); ++it) { |
| 782 if (it->get() == runner) { | 784 if (it->get() == runner) { |
| 783 native_runners_.erase(it); | 785 native_runners_.erase(it); |
| 784 return; | 786 return; |
| 785 } | 787 } |
| 786 } | 788 } |
| 787 } | 789 } |
| 788 | 790 |
| 789 } // namespace shell | 791 } // namespace shell |
| 790 } // namespace mojo | |
| OLD | NEW |