| 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 "content/common/service_manager/embedded_service_runner.h" | 5 #include "content/common/service_manager/embedded_service_runner.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "services/service_manager/public/cpp/service_context.h" | 16 #include "services/service_manager/public/cpp/service_context.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 class EmbeddedServiceRunner::Instance | 20 class EmbeddedServiceRunner::Instance |
| 21 : public base::RefCountedThreadSafe<Instance> { | 21 : public base::RefCountedThreadSafe<Instance> { |
| 22 public: | 22 public: |
| 23 Instance(const base::StringPiece& name, | 23 Instance(const base::StringPiece& name, |
| 24 const ServiceInfo& info, | 24 const ServiceInfo& info, |
| 25 const base::Closure& quit_closure) | 25 const base::Closure& quit_closure) |
| 26 : name_(name.as_string()), | 26 : name_(name.as_string()), |
| 27 factory_callback_(info.factory), | 27 factory_callback_(info.factory), |
| 28 use_own_thread_(!info.task_runner && info.use_own_thread), | 28 use_own_thread_(!info.task_runner && info.use_own_thread), |
| 29 service_owns_context_(info.service_owns_context), |
| 29 quit_closure_(quit_closure), | 30 quit_closure_(quit_closure), |
| 30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 31 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 31 task_runner_(info.task_runner) { | 32 task_runner_(info.task_runner) { |
| 32 if (!use_own_thread_ && !task_runner_) | 33 if (!use_own_thread_ && !task_runner_) |
| 33 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 34 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void BindServiceRequest(service_manager::mojom::ServiceRequest request) { | 37 void BindServiceRequest(service_manager::mojom::ServiceRequest request) { |
| 37 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 38 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 38 | 39 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 75 |
| 75 void BindServiceRequestOnApplicationThread( | 76 void BindServiceRequestOnApplicationThread( |
| 76 service_manager::mojom::ServiceRequest request) { | 77 service_manager::mojom::ServiceRequest request) { |
| 77 DCHECK(task_runner_->BelongsToCurrentThread()); | 78 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 78 | 79 |
| 79 if (!service_) { | 80 if (!service_) { |
| 80 service_ = factory_callback_.Run( | 81 service_ = factory_callback_.Run( |
| 81 base::Bind(&Instance::Quit, base::Unretained(this))); | 82 base::Bind(&Instance::Quit, base::Unretained(this))); |
| 82 } | 83 } |
| 83 | 84 |
| 84 service_manager::ServiceContext* new_connection = | 85 if (service_owns_context_) { |
| 85 new service_manager::ServiceContext(service_.get(), std::move(request)); | 86 service_->set_context(base::MakeUnique<service_manager::ServiceContext>( |
| 86 service_manager_connections_.push_back(base::WrapUnique(new_connection)); | 87 service_.get(), std::move(request))); |
| 87 new_connection->SetConnectionLostClosure( | 88 } else { |
| 88 base::Bind(&Instance::OnStop, base::Unretained(this), | 89 service_manager::ServiceContext* new_connection = |
| 89 new_connection)); | 90 new service_manager::ServiceContext(service_.get(), |
| 91 std::move(request)); |
| 92 new_connection->SetConnectionLostClosure( |
| 93 base::Bind(&Instance::OnStop, base::Unretained(this))); |
| 94 } |
| 90 } | 95 } |
| 91 | 96 |
| 92 void OnStop(service_manager::ServiceContext* connection) { | 97 void OnStop() { DCHECK(task_runner_->BelongsToCurrentThread()); } |
| 93 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 94 | |
| 95 for (auto it = service_manager_connections_.begin(); | |
| 96 it != service_manager_connections_.end(); ++it) { | |
| 97 if (it->get() == connection) { | |
| 98 service_manager_connections_.erase(it); | |
| 99 break; | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | 98 |
| 104 void Quit() { | 99 void Quit() { |
| 105 DCHECK(task_runner_->BelongsToCurrentThread()); | 100 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 106 | 101 |
| 107 service_manager_connections_.clear(); | |
| 108 service_.reset(); | 102 service_.reset(); |
| 109 if (quit_task_runner_->BelongsToCurrentThread()) { | 103 if (quit_task_runner_->BelongsToCurrentThread()) { |
| 110 QuitOnRunnerThread(); | 104 QuitOnRunnerThread(); |
| 111 } else { | 105 } else { |
| 112 quit_task_runner_->PostTask( | 106 quit_task_runner_->PostTask( |
| 113 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); | 107 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); |
| 114 } | 108 } |
| 115 } | 109 } |
| 116 | 110 |
| 117 void QuitOnRunnerThread() { | 111 void QuitOnRunnerThread() { |
| 118 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 112 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 119 if (thread_) { | 113 if (thread_) { |
| 120 thread_.reset(); | 114 thread_.reset(); |
| 121 task_runner_ = nullptr; | 115 task_runner_ = nullptr; |
| 122 } | 116 } |
| 123 quit_closure_.Run(); | 117 quit_closure_.Run(); |
| 124 } | 118 } |
| 125 | 119 |
| 126 const std::string name_; | 120 const std::string name_; |
| 127 const ServiceInfo::ServiceFactory factory_callback_; | 121 const ServiceInfo::ServiceFactory factory_callback_; |
| 128 const bool use_own_thread_; | 122 const bool use_own_thread_; |
| 123 const bool service_owns_context_; |
| 129 const base::Closure quit_closure_; | 124 const base::Closure quit_closure_; |
| 130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 125 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
| 131 | 126 |
| 132 // Thread checker used to ensure certain operations happen only on the | 127 // Thread checker used to ensure certain operations happen only on the |
| 133 // runner's (i.e. our owner's) thread. | 128 // runner's (i.e. our owner's) thread. |
| 134 base::ThreadChecker runner_thread_checker_; | 129 base::ThreadChecker runner_thread_checker_; |
| 135 | 130 |
| 136 // These fields must only be accessed from the runner's thread. | 131 // These fields must only be accessed from the runner's thread. |
| 137 std::unique_ptr<base::Thread> thread_; | 132 std::unique_ptr<base::Thread> thread_; |
| 138 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 133 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 139 | 134 |
| 140 // These fields must only be accessed from the application thread, except in | 135 // These fields must only be accessed from the application thread, except in |
| 141 // the destructor which may run on either the runner thread or the application | 136 // the destructor which may run on either the runner thread or the application |
| 142 // thread. | 137 // thread. |
| 143 std::unique_ptr<service_manager::Service> service_; | 138 std::unique_ptr<service_manager::Service> service_; |
| 144 std::vector<std::unique_ptr<service_manager::ServiceContext>> | |
| 145 service_manager_connections_; | |
| 146 | 139 |
| 147 DISALLOW_COPY_AND_ASSIGN(Instance); | 140 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 148 }; | 141 }; |
| 149 | 142 |
| 150 EmbeddedServiceRunner::EmbeddedServiceRunner(const base::StringPiece& name, | 143 EmbeddedServiceRunner::EmbeddedServiceRunner(const base::StringPiece& name, |
| 151 const ServiceInfo& info) | 144 const ServiceInfo& info) |
| 152 : weak_factory_(this) { | 145 : weak_factory_(this) { |
| 153 instance_ = new Instance(name, info, | 146 instance_ = new Instance(name, info, |
| 154 base::Bind(&EmbeddedServiceRunner::OnQuit, | 147 base::Bind(&EmbeddedServiceRunner::OnQuit, |
| 155 weak_factory_.GetWeakPtr())); | 148 weak_factory_.GetWeakPtr())); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 168 const base::Closure& quit_closure) { | 161 const base::Closure& quit_closure) { |
| 169 quit_closure_ = quit_closure; | 162 quit_closure_ = quit_closure; |
| 170 } | 163 } |
| 171 | 164 |
| 172 void EmbeddedServiceRunner::OnQuit() { | 165 void EmbeddedServiceRunner::OnQuit() { |
| 173 if (!quit_closure_.is_null()) | 166 if (!quit_closure_.is_null()) |
| 174 quit_closure_.Run(); | 167 quit_closure_.Run(); |
| 175 } | 168 } |
| 176 | 169 |
| 177 } // namespace content | 170 } // namespace content |
| OLD | NEW |