| 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 service_manager_connections_.push_back(base::WrapUnique(new_connection)); |
| 93 new_connection->SetConnectionLostClosure( |
| 94 base::Bind(&Instance::OnStop, base::Unretained(this), |
| 95 new_connection)); |
| 96 } |
| 90 } | 97 } |
| 91 | 98 |
| 92 void OnStop(service_manager::ServiceContext* connection) { | 99 void OnStop(service_manager::ServiceContext* connection) { |
| 93 DCHECK(task_runner_->BelongsToCurrentThread()); | 100 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 94 | 101 |
| 95 for (auto it = service_manager_connections_.begin(); | 102 for (auto it = service_manager_connections_.begin(); |
| 96 it != service_manager_connections_.end(); ++it) { | 103 it != service_manager_connections_.end(); ++it) { |
| 97 if (it->get() == connection) { | 104 if (it->get() == connection) { |
| 98 service_manager_connections_.erase(it); | 105 service_manager_connections_.erase(it); |
| 99 break; | 106 break; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 119 if (thread_) { | 126 if (thread_) { |
| 120 thread_.reset(); | 127 thread_.reset(); |
| 121 task_runner_ = nullptr; | 128 task_runner_ = nullptr; |
| 122 } | 129 } |
| 123 quit_closure_.Run(); | 130 quit_closure_.Run(); |
| 124 } | 131 } |
| 125 | 132 |
| 126 const std::string name_; | 133 const std::string name_; |
| 127 const ServiceInfo::ServiceFactory factory_callback_; | 134 const ServiceInfo::ServiceFactory factory_callback_; |
| 128 const bool use_own_thread_; | 135 const bool use_own_thread_; |
| 136 const bool service_owns_context_; |
| 129 const base::Closure quit_closure_; | 137 const base::Closure quit_closure_; |
| 130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 138 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
| 131 | 139 |
| 132 // Thread checker used to ensure certain operations happen only on the | 140 // Thread checker used to ensure certain operations happen only on the |
| 133 // runner's (i.e. our owner's) thread. | 141 // runner's (i.e. our owner's) thread. |
| 134 base::ThreadChecker runner_thread_checker_; | 142 base::ThreadChecker runner_thread_checker_; |
| 135 | 143 |
| 136 // These fields must only be accessed from the runner's thread. | 144 // These fields must only be accessed from the runner's thread. |
| 137 std::unique_ptr<base::Thread> thread_; | 145 std::unique_ptr<base::Thread> thread_; |
| 138 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 146 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 139 | 147 |
| 140 // These fields must only be accessed from the application thread, except in | 148 // 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 | 149 // the destructor which may run on either the runner thread or the application |
| 142 // thread. | 150 // thread. |
| 143 std::unique_ptr<service_manager::Service> service_; | 151 std::unique_ptr<service_manager::Service> service_; |
| 144 std::vector<std::unique_ptr<service_manager::ServiceContext>> | 152 std::vector<std::unique_ptr<service_manager::ServiceContext>> |
| 145 service_manager_connections_; | 153 service_manager_connections_; |
| 146 | 154 |
| 147 DISALLOW_COPY_AND_ASSIGN(Instance); | 155 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 148 }; | 156 }; |
| 149 | 157 |
| 150 EmbeddedServiceRunner::EmbeddedServiceRunner(const base::StringPiece& name, | 158 EmbeddedServiceRunner::EmbeddedServiceRunner(const base::StringPiece& name, |
| 151 const ServiceInfo& info) | 159 const ServiceInfo& info) |
| 152 : weak_factory_(this) { | 160 : weak_factory_(this) { |
| 153 instance_ = new Instance(name, info, | 161 instance_ = new Instance(name, info, |
| 154 base::Bind(&EmbeddedServiceRunner::OnQuit, | 162 base::Bind(&EmbeddedServiceRunner::OnQuit, |
| 155 weak_factory_.GetWeakPtr())); | 163 weak_factory_.GetWeakPtr())); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 168 const base::Closure& quit_closure) { | 176 const base::Closure& quit_closure) { |
| 169 quit_closure_ = quit_closure; | 177 quit_closure_ = quit_closure; |
| 170 } | 178 } |
| 171 | 179 |
| 172 void EmbeddedServiceRunner::OnQuit() { | 180 void EmbeddedServiceRunner::OnQuit() { |
| 173 if (!quit_closure_.is_null()) | 181 if (!quit_closure_.is_null()) |
| 174 quit_closure_.Run(); | 182 quit_closure_.Run(); |
| 175 } | 183 } |
| 176 | 184 |
| 177 } // namespace content | 185 } // namespace content |
| OLD | NEW |