| 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 "services/service_manager/embedder/embedded_service_runner.h" | 5 #include "services/service_manager/embedder/embedded_service_runner.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 FROM_HERE, | 57 FROM_HERE, |
| 58 base::Bind(&InstanceManager::BindServiceRequestOnServiceSequence, this, | 58 base::Bind(&InstanceManager::BindServiceRequestOnServiceSequence, this, |
| 59 base::Passed(&request))); | 59 base::Passed(&request))); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void ShutDown() { | 62 void ShutDown() { |
| 63 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 63 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 64 if (!service_task_runner_) | 64 if (!service_task_runner_) |
| 65 return; | 65 return; |
| 66 // Any extant ServiceContexts must be destroyed on the application thread. | 66 // Any extant ServiceContexts must be destroyed on the application thread. |
| 67 if (service_task_runner_->RunsTasksOnCurrentThread()) { | 67 if (service_task_runner_->RunsTasksInCurrentSequence()) { |
| 68 QuitOnServiceSequence(); | 68 QuitOnServiceSequence(); |
| 69 } else { | 69 } else { |
| 70 service_task_runner_->PostTask( | 70 service_task_runner_->PostTask( |
| 71 FROM_HERE, base::Bind(&InstanceManager::QuitOnServiceSequence, this)); | 71 FROM_HERE, base::Bind(&InstanceManager::QuitOnServiceSequence, this)); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 friend class base::RefCountedThreadSafe<InstanceManager>; | 76 friend class base::RefCountedThreadSafe<InstanceManager>; |
| 77 | 77 |
| 78 ~InstanceManager() { | 78 ~InstanceManager() { |
| 79 // If this instance had its own thread, it MUST be explicitly destroyed by | 79 // If this instance had its own thread, it MUST be explicitly destroyed by |
| 80 // QuitOnRunnerThread() by the time this destructor is run. | 80 // QuitOnRunnerThread() by the time this destructor is run. |
| 81 DCHECK(!thread_); | 81 DCHECK(!thread_); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void BindServiceRequestOnServiceSequence( | 84 void BindServiceRequestOnServiceSequence( |
| 85 service_manager::mojom::ServiceRequest request) { | 85 service_manager::mojom::ServiceRequest request) { |
| 86 DCHECK(service_task_runner_->RunsTasksOnCurrentThread()); | 86 DCHECK(service_task_runner_->RunsTasksInCurrentSequence()); |
| 87 | 87 |
| 88 int instance_id = next_instance_id_++; | 88 int instance_id = next_instance_id_++; |
| 89 | 89 |
| 90 std::unique_ptr<service_manager::ServiceContext> context = | 90 std::unique_ptr<service_manager::ServiceContext> context = |
| 91 base::MakeUnique<service_manager::ServiceContext>( | 91 base::MakeUnique<service_manager::ServiceContext>( |
| 92 factory_callback_.Run(), std::move(request)); | 92 factory_callback_.Run(), std::move(request)); |
| 93 | 93 |
| 94 service_manager::ServiceContext* raw_context = context.get(); | 94 service_manager::ServiceContext* raw_context = context.get(); |
| 95 context->SetQuitClosure( | 95 context->SetQuitClosure( |
| 96 base::Bind(&InstanceManager::OnInstanceLost, this, instance_id)); | 96 base::Bind(&InstanceManager::OnInstanceLost, this, instance_id)); |
| 97 contexts_.insert(std::make_pair(raw_context, std::move(context))); | 97 contexts_.insert(std::make_pair(raw_context, std::move(context))); |
| 98 id_to_context_map_.insert(std::make_pair(instance_id, raw_context)); | 98 id_to_context_map_.insert(std::make_pair(instance_id, raw_context)); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void OnInstanceLost(int instance_id) { | 101 void OnInstanceLost(int instance_id) { |
| 102 DCHECK(service_task_runner_->RunsTasksOnCurrentThread()); | 102 DCHECK(service_task_runner_->RunsTasksInCurrentSequence()); |
| 103 | 103 |
| 104 auto id_iter = id_to_context_map_.find(instance_id); | 104 auto id_iter = id_to_context_map_.find(instance_id); |
| 105 CHECK(id_iter != id_to_context_map_.end()); | 105 CHECK(id_iter != id_to_context_map_.end()); |
| 106 | 106 |
| 107 auto context_iter = contexts_.find(id_iter->second); | 107 auto context_iter = contexts_.find(id_iter->second); |
| 108 CHECK(context_iter != contexts_.end()); | 108 CHECK(context_iter != contexts_.end()); |
| 109 contexts_.erase(context_iter); | 109 contexts_.erase(context_iter); |
| 110 id_to_context_map_.erase(id_iter); | 110 id_to_context_map_.erase(id_iter); |
| 111 | 111 |
| 112 // If we've lost the last instance, run the quit closure. | 112 // If we've lost the last instance, run the quit closure. |
| 113 if (contexts_.empty()) | 113 if (contexts_.empty()) |
| 114 QuitOnServiceSequence(); | 114 QuitOnServiceSequence(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void QuitOnServiceSequence() { | 117 void QuitOnServiceSequence() { |
| 118 DCHECK(service_task_runner_->RunsTasksOnCurrentThread()); | 118 DCHECK(service_task_runner_->RunsTasksInCurrentSequence()); |
| 119 | 119 |
| 120 contexts_.clear(); | 120 contexts_.clear(); |
| 121 if (quit_task_runner_->RunsTasksOnCurrentThread()) { | 121 if (quit_task_runner_->RunsTasksInCurrentSequence()) { |
| 122 QuitOnRunnerThread(); | 122 QuitOnRunnerThread(); |
| 123 } else { | 123 } else { |
| 124 quit_task_runner_->PostTask( | 124 quit_task_runner_->PostTask( |
| 125 FROM_HERE, base::Bind(&InstanceManager::QuitOnRunnerThread, this)); | 125 FROM_HERE, base::Bind(&InstanceManager::QuitOnRunnerThread, this)); |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 void QuitOnRunnerThread() { | 129 void QuitOnRunnerThread() { |
| 130 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 130 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 131 if (thread_) { | 131 if (thread_) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 void EmbeddedServiceRunner::SetQuitClosure(const base::Closure& quit_closure) { | 192 void EmbeddedServiceRunner::SetQuitClosure(const base::Closure& quit_closure) { |
| 193 quit_closure_ = quit_closure; | 193 quit_closure_ = quit_closure; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void EmbeddedServiceRunner::OnQuit() { | 196 void EmbeddedServiceRunner::OnQuit() { |
| 197 if (!quit_closure_.is_null()) | 197 if (!quit_closure_.is_null()) |
| 198 quit_closure_.Run(); | 198 quit_closure_.Run(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 } // namespace service_manager | 201 } // namespace service_manager |
| OLD | NEW |