| 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 <memory> | 5 #include <memory> |
| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 void QuitLoop(base::RunLoop* loop) { | 31 void QuitLoop(base::RunLoop* loop) { |
| 32 loop->Quit(); | 32 loop->Quit(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void DecrementCountAndQuitWhenZero(base::RunLoop* loop, size_t* count) { | 35 void DecrementCountAndQuitWhenZero(base::RunLoop* loop, size_t* count) { |
| 36 if (!--(*count)) | 36 if (!--(*count)) |
| 37 loop->Quit(); | 37 loop->Quit(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 struct Instance { | 40 struct Instance { |
| 41 Instance() : id(mojom::kInvalidInstanceID), pid(0) {} | 41 Instance() : pid(0) {} |
| 42 Instance(const Identity& identity, uint32_t id, uint32_t pid) | 42 Instance(const Identity& identity, uint32_t pid) |
| 43 : identity(identity), id(id), pid(pid) {} | 43 : identity(identity), pid(pid) {} |
| 44 | 44 |
| 45 Identity identity; | 45 Identity identity; |
| 46 uint32_t id; | |
| 47 uint32_t pid; | 46 uint32_t pid; |
| 48 }; | 47 }; |
| 49 | 48 |
| 50 class InstanceState : public mojom::ServiceManagerListener { | 49 class InstanceState : public mojom::ServiceManagerListener { |
| 51 public: | 50 public: |
| 52 InstanceState(mojom::ServiceManagerListenerRequest request, | 51 InstanceState(mojom::ServiceManagerListenerRequest request, |
| 53 base::RunLoop* loop) | 52 base::RunLoop* loop) |
| 54 : binding_(this, std::move(request)), loop_(loop) {} | 53 : binding_(this, std::move(request)), loop_(loop) {} |
| 55 ~InstanceState() override {} | 54 ~InstanceState() override {} |
| 56 | 55 |
| 57 bool HasInstanceForName(const std::string& name) const { | 56 bool HasInstanceForName(const std::string& name) const { |
| 58 return instances_.find(name) != instances_.end(); | 57 return instances_.find(name) != instances_.end(); |
| 59 } | 58 } |
| 60 size_t GetNewInstanceCount() const { | 59 size_t GetNewInstanceCount() const { |
| 61 return instances_.size() - initial_instances_.size(); | 60 return instances_.size() - initial_instances_.size(); |
| 62 } | 61 } |
| 63 void WaitForInstanceDestruction(base::RunLoop* loop) { | 62 void WaitForInstanceDestruction(base::RunLoop* loop) { |
| 64 DCHECK(!destruction_loop_); | 63 DCHECK(!destruction_loop_); |
| 65 destruction_loop_ = loop; | 64 destruction_loop_ = loop; |
| 66 // First of all check to see if we should be spinning this loop at all - | 65 // First of all check to see if we should be spinning this loop at all - |
| 67 // the app(s) we're waiting on quitting may already have quit. | 66 // the app(s) we're waiting on quitting may already have quit. |
| 68 TryToQuitDestructionLoop(); | 67 TryToQuitDestructionLoop(); |
| 69 } | 68 } |
| 70 | 69 |
| 71 private: | 70 private: |
| 72 // mojom::ServiceManagerListener: | 71 // mojom::ServiceManagerListener: |
| 73 void OnInit(mojo::Array<mojom::ServiceInfoPtr> instances) override { | 72 void OnInit(mojo::Array<mojom::ServiceInfoPtr> instances) override { |
| 74 for (const auto& instance : instances) { | 73 for (const auto& instance : instances) { |
| 75 Instance i(instance->identity.To<Identity>(), instance->id, | 74 Instance i(instance->identity.To<Identity>(), instance->pid); |
| 76 instance->pid); | |
| 77 initial_instances_[i.identity.name()] = i; | 75 initial_instances_[i.identity.name()] = i; |
| 78 instances_[i.identity.name()] = i; | 76 instances_[i.identity.name()] = i; |
| 79 } | 77 } |
| 80 loop_->Quit(); | 78 loop_->Quit(); |
| 81 } | 79 } |
| 82 void OnServiceCreated(mojom::ServiceInfoPtr instance) override { | 80 void OnServiceCreated(mojom::ServiceInfoPtr instance) override { |
| 83 instances_[instance->identity->name] = | 81 instances_[instance->identity->name] = |
| 84 Instance(instance->identity.To<Identity>(), instance->id, | 82 Instance(instance->identity.To<Identity>(), instance->pid); |
| 85 instance->pid); | |
| 86 } | 83 } |
| 87 void OnServiceStarted(uint32_t id, uint32_t pid) override { | 84 void OnServiceStarted(mojom::IdentityPtr identity_ptr, |
| 85 uint32_t pid) override { |
| 86 Identity identity = identity_ptr.To<Identity>(); |
| 88 for (auto& instance : instances_) { | 87 for (auto& instance : instances_) { |
| 89 if (instance.second.id == id) { | 88 if (instance.second.identity == identity) { |
| 90 instance.second.pid = pid; | 89 instance.second.pid = pid; |
| 91 break; | 90 break; |
| 92 } | 91 } |
| 93 } | 92 } |
| 94 } | 93 } |
| 95 void OnServiceStopped(uint32_t id) override { | 94 void OnServiceStopped(mojom::IdentityPtr identity_ptr) override { |
| 95 Identity identity = identity_ptr.To<Identity>(); |
| 96 for (auto it = instances_.begin(); it != instances_.end(); ++it) { | 96 for (auto it = instances_.begin(); it != instances_.end(); ++it) { |
| 97 if (it->second.id == id) { | 97 if (it->second.identity == identity) { |
| 98 instances_.erase(it); | 98 instances_.erase(it); |
| 99 break; | 99 break; |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 TryToQuitDestructionLoop(); | 102 TryToQuitDestructionLoop(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void TryToQuitDestructionLoop() { | 105 void TryToQuitDestructionLoop() { |
| 106 if (!GetNewInstanceCount() && destruction_loop_) { | 106 if (!GetNewInstanceCount() && destruction_loop_) { |
| 107 destruction_loop_->Quit(); | 107 destruction_loop_->Quit(); |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 parent->Quit(); | 445 parent->Quit(); |
| 446 | 446 |
| 447 // Quitting the parent should cascade-quit the child. | 447 // Quitting the parent should cascade-quit the child. |
| 448 WaitForInstanceDestruction(); | 448 WaitForInstanceDestruction(); |
| 449 EXPECT_EQ(0u, instances()->GetNewInstanceCount()); | 449 EXPECT_EQ(0u, instances()->GetNewInstanceCount()); |
| 450 EXPECT_FALSE(instances()->HasInstanceForName(kTestParentName)); | 450 EXPECT_FALSE(instances()->HasInstanceForName(kTestParentName)); |
| 451 EXPECT_FALSE(instances()->HasInstanceForName(kTestAppName)); | 451 EXPECT_FALSE(instances()->HasInstanceForName(kTestAppName)); |
| 452 } | 452 } |
| 453 | 453 |
| 454 } // namespace shell | 454 } // namespace shell |
| OLD | NEW |