| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/base_switches.h" | 6 #include "base/base_switches.h" |
| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/process/process.h" | 11 #include "base/process/process.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "mojo/edk/embedder/embedder.h" | 13 #include "mojo/edk/embedder/embedder.h" |
| 14 #include "mojo/edk/embedder/platform_channel_pair.h" | 14 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" | 15 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "mojo/shell/public/cpp/identity.h" |
| 16 #include "mojo/shell/public/cpp/shell_test.h" | 17 #include "mojo/shell/public/cpp/shell_test.h" |
| 17 #include "mojo/shell/public/interfaces/shell.mojom.h" | 18 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 18 #include "mojo/shell/runner/common/switches.h" | 19 #include "mojo/shell/runner/common/switches.h" |
| 19 #include "mojo/shell/tests/lifecycle/lifecycle_unittest.mojom.h" | 20 #include "mojo/shell/tests/lifecycle/lifecycle_unittest.mojom.h" |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 namespace shell { | 23 namespace shell { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const char kTestAppName[] = "mojo:lifecycle_unittest_app"; | 26 const char kTestAppName[] = "mojo:lifecycle_unittest_app"; |
| 26 const char kTestExeName[] = "exe:lifecycle_unittest_exe"; | 27 const char kTestExeName[] = "exe:lifecycle_unittest_exe"; |
| 27 const char kTestPackageName[] = "mojo:lifecycle_unittest_package"; | 28 const char kTestPackageName[] = "mojo:lifecycle_unittest_package"; |
| 28 const char kTestPackageAppNameA[] = "mojo:lifecycle_unittest_package_app_a"; | 29 const char kTestPackageAppNameA[] = "mojo:lifecycle_unittest_package_app_a"; |
| 29 const char kTestPackageAppNameB[] = "mojo:lifecycle_unittest_package_app_b"; | 30 const char kTestPackageAppNameB[] = "mojo:lifecycle_unittest_package_app_b"; |
| 30 const char kTestName[] = "mojo:lifecycle_unittest"; | 31 const char kTestName[] = "mojo:lifecycle_unittest"; |
| 31 | 32 |
| 32 void QuitLoop(base::RunLoop* loop) { | 33 void QuitLoop(base::RunLoop* loop) { |
| 33 loop->Quit(); | 34 loop->Quit(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void DecrementCountAndQuitWhenZero(base::RunLoop* loop, size_t* count) { | 37 void DecrementCountAndQuitWhenZero(base::RunLoop* loop, size_t* count) { |
| 37 if (!--(*count)) | 38 if (!--(*count)) |
| 38 loop->Quit(); | 39 loop->Quit(); |
| 39 } | 40 } |
| 40 | 41 |
| 41 struct Instance { | 42 struct Instance { |
| 42 Instance() : id(shell::mojom::kInvalidInstanceID), pid(0) {} | 43 Instance() : id(shell::mojom::kInvalidInstanceID), pid(0) {} |
| 43 Instance(const std::string& name, const std::string& qualifier, uint32_t id, | 44 Instance(const Identity& identity, uint32_t id, uint32_t pid) |
| 44 uint32_t pid) | 45 : identity(identity), id(id), pid(pid) {} |
| 45 : name(name), qualifier(qualifier), id(id), pid(pid) {} | |
| 46 | 46 |
| 47 std::string name; | 47 Identity identity; |
| 48 std::string qualifier; | |
| 49 uint32_t id; | 48 uint32_t id; |
| 50 uint32_t pid; | 49 uint32_t pid; |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 class InstanceState : public mojom::InstanceListener { | 52 class InstanceState : public mojom::InstanceListener { |
| 54 public: | 53 public: |
| 55 InstanceState(mojom::InstanceListenerRequest request, base::RunLoop* loop) | 54 InstanceState(mojom::InstanceListenerRequest request, base::RunLoop* loop) |
| 56 : binding_(this, std::move(request)), loop_(loop) {} | 55 : binding_(this, std::move(request)), loop_(loop) {} |
| 57 ~InstanceState() override {} | 56 ~InstanceState() override {} |
| 58 | 57 |
| 59 bool HasInstanceForName(const std::string& name) const { | 58 bool HasInstanceForName(const std::string& name) const { |
| 60 return instances_.find(name) != instances_.end(); | 59 return instances_.find(name) != instances_.end(); |
| 61 } | 60 } |
| 62 size_t GetNewInstanceCount() const { | 61 size_t GetNewInstanceCount() const { |
| 63 return instances_.size() - initial_instances_.size(); | 62 return instances_.size() - initial_instances_.size(); |
| 64 } | 63 } |
| 65 void WaitForInstanceDestruction(base::RunLoop* loop) { | 64 void WaitForInstanceDestruction(base::RunLoop* loop) { |
| 66 DCHECK(!destruction_loop_); | 65 DCHECK(!destruction_loop_); |
| 67 destruction_loop_ = loop; | 66 destruction_loop_ = loop; |
| 68 // First of all check to see if we should be spinning this loop at all - | 67 // First of all check to see if we should be spinning this loop at all - |
| 69 // the app(s) we're waiting on quitting may already have quit. | 68 // the app(s) we're waiting on quitting may already have quit. |
| 70 TryToQuitDestructionLoop(); | 69 TryToQuitDestructionLoop(); |
| 71 } | 70 } |
| 72 | 71 |
| 73 private: | 72 private: |
| 74 // mojom::InstanceListener: | 73 // mojom::InstanceListener: |
| 75 void SetExistingInstances(Array<mojom::InstanceInfoPtr> instances) override { | 74 void SetExistingInstances(Array<mojom::InstanceInfoPtr> instances) override { |
| 76 for (const auto& instance : instances) { | 75 for (const auto& instance : instances) { |
| 77 Instance i(instance->name, instance->qualifier, instance->id, | 76 Instance i(instance->identity.To<Identity>(), instance->id, |
| 78 instance->pid); | 77 instance->pid); |
| 79 initial_instances_[i.name] = i; | 78 initial_instances_[i.identity.name()] = i; |
| 80 instances_[i.name] = i; | 79 instances_[i.identity.name()] = i; |
| 81 } | 80 } |
| 82 loop_->Quit(); | 81 loop_->Quit(); |
| 83 } | 82 } |
| 84 void InstanceCreated(mojom::InstanceInfoPtr instance) override { | 83 void InstanceCreated(mojom::InstanceInfoPtr instance) override { |
| 85 instances_[instance->name] = | 84 instances_[instance->identity->name] = |
| 86 Instance(instance->name, instance->qualifier, instance->id, | 85 Instance(instance->identity.To<Identity>(), instance->id, |
| 87 instance->pid); | 86 instance->pid); |
| 88 } | 87 } |
| 89 void InstanceDestroyed(uint32_t id) override { | 88 void InstanceDestroyed(uint32_t id) override { |
| 90 for (auto it = instances_.begin(); it != instances_.end(); ++it) { | 89 for (auto it = instances_.begin(); it != instances_.end(); ++it) { |
| 91 if (it->second.id == id) { | 90 if (it->second.id == id) { |
| 92 instances_.erase(it); | 91 instances_.erase(it); |
| 93 break; | 92 break; |
| 94 } | 93 } |
| 95 } | 94 } |
| 96 TryToQuitDestructionLoop(); | 95 TryToQuitDestructionLoop(); |
| 97 } | 96 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 test_interfaces.push_back("*"); | 209 test_interfaces.push_back("*"); |
| 211 filter->filter.insert("*", std::move(test_interfaces)); | 210 filter->filter.insert("*", std::move(test_interfaces)); |
| 212 | 211 |
| 213 mojo::shell::mojom::ShellPtr shell; | 212 mojo::shell::mojom::ShellPtr shell; |
| 214 connector()->ConnectToInterface("mojo:shell", &shell); | 213 connector()->ConnectToInterface("mojo:shell", &shell); |
| 215 | 214 |
| 216 mojo::shell::mojom::ShellClientFactoryPtr factory; | 215 mojo::shell::mojom::ShellClientFactoryPtr factory; |
| 217 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( | 216 factory.Bind(mojo::InterfacePtrInfo<mojo::shell::mojom::ShellClientFactory>( |
| 218 std::move(pipe), 0u)); | 217 std::move(pipe), 0u)); |
| 219 base::RunLoop loop; | 218 base::RunLoop loop; |
| 220 shell->CreateInstance(std::move(factory), kTestExeName, | 219 mojo::shell::mojom::IdentityPtr target(mojo::shell::mojom::Identity::New()); |
| 221 mojom::kInheritUserID, std::move(filter), | 220 target->name = kTestExeName; |
| 222 std::move(request), | 221 target->user_id = mojom::kInheritUserID; |
| 222 target->instance = ""; |
| 223 shell->CreateInstance(std::move(factory), std::move(target), |
| 224 std::move(filter), std::move(request), |
| 223 base::Bind(&LifecycleTest::OnConnectionCompleted, | 225 base::Bind(&LifecycleTest::OnConnectionCompleted, |
| 224 base::Unretained(this), &loop)); | 226 base::Unretained(this), &loop)); |
| 225 loop.Run(); | 227 loop.Run(); |
| 226 | 228 |
| 227 base::LaunchOptions options; | 229 base::LaunchOptions options; |
| 228 #if defined(OS_WIN) | 230 #if defined(OS_WIN) |
| 229 options.handles_to_inherit = &handle_passing_info; | 231 options.handles_to_inherit = &handle_passing_info; |
| 230 #elif defined(OS_POSIX) | 232 #elif defined(OS_POSIX) |
| 231 options.fds_to_remap = &handle_passing_info; | 233 options.fds_to_remap = &handle_passing_info; |
| 232 #endif | 234 #endif |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 loop.Run(); | 508 loop.Run(); |
| 507 | 509 |
| 508 WaitForInstanceDestruction(); | 510 WaitForInstanceDestruction(); |
| 509 EXPECT_FALSE(instances()->HasInstanceForName(kTestExeName)); | 511 EXPECT_FALSE(instances()->HasInstanceForName(kTestExeName)); |
| 510 EXPECT_EQ(0u, instances()->GetNewInstanceCount()); | 512 EXPECT_EQ(0u, instances()->GetNewInstanceCount()); |
| 511 } | 513 } |
| 512 | 514 |
| 513 | 515 |
| 514 } // namespace shell | 516 } // namespace shell |
| 515 } // namespace mojo | 517 } // namespace mojo |
| OLD | NEW |