| 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/mojo/embedded_application_runner.h" | 5 #include "content/common/mojo/embedded_application_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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 : name_(name.as_string()), | 26 : name_(name.as_string()), |
| 27 factory_callback_(info.application_factory), | 27 factory_callback_(info.application_factory), |
| 28 use_own_thread_(!info.application_task_runner && info.use_own_thread), | 28 use_own_thread_(!info.application_task_runner && info.use_own_thread), |
| 29 quit_closure_(quit_closure), | 29 quit_closure_(quit_closure), |
| 30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 31 application_task_runner_(info.application_task_runner) { | 31 application_task_runner_(info.application_task_runner) { |
| 32 if (!use_own_thread_ && !application_task_runner_) | 32 if (!use_own_thread_ && !application_task_runner_) |
| 33 application_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 33 application_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void BindShellClientRequest(shell::mojom::ShellClientRequest request) { | 36 void BindServiceRequest(shell::mojom::ServiceRequest request) { |
| 37 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 37 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 38 | 38 |
| 39 if (use_own_thread_ && !thread_) { | 39 if (use_own_thread_ && !thread_) { |
| 40 // Start a new thread if necessary. | 40 // Start a new thread if necessary. |
| 41 thread_.reset(new base::Thread(name_)); | 41 thread_.reset(new base::Thread(name_)); |
| 42 thread_->Start(); | 42 thread_->Start(); |
| 43 application_task_runner_ = thread_->task_runner(); | 43 application_task_runner_ = thread_->task_runner(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 DCHECK(application_task_runner_); | 46 DCHECK(application_task_runner_); |
| 47 application_task_runner_->PostTask( | 47 application_task_runner_->PostTask( |
| 48 FROM_HERE, | 48 FROM_HERE, |
| 49 base::Bind(&Instance::BindShellClientRequestOnApplicationThread, this, | 49 base::Bind(&Instance::BindServiceRequestOnApplicationThread, this, |
| 50 base::Passed(&request))); | 50 base::Passed(&request))); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void ShutDown() { | 53 void ShutDown() { |
| 54 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 54 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 55 if (thread_) { | 55 if (thread_) { |
| 56 application_task_runner_ = nullptr; | 56 application_task_runner_ = nullptr; |
| 57 thread_.reset(); | 57 thread_.reset(); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 void BindShellClientRequestOnApplicationThread( | 62 void BindServiceRequestOnApplicationThread( |
| 63 shell::mojom::ShellClientRequest request) { | 63 shell::mojom::ServiceRequest request) { |
| 64 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 64 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 65 | 65 |
| 66 if (!shell_client_) { | 66 if (!service_) { |
| 67 shell_client_ = factory_callback_.Run( | 67 service_ = factory_callback_.Run( |
| 68 base::Bind(&Instance::Quit, base::Unretained(this))); | 68 base::Bind(&Instance::Quit, base::Unretained(this))); |
| 69 } | 69 } |
| 70 | 70 |
| 71 shell::ShellConnection* new_connection = | 71 shell::ShellConnection* new_connection = |
| 72 new shell::ShellConnection(shell_client_.get(), std::move(request)); | 72 new shell::ShellConnection(service_.get(), std::move(request)); |
| 73 shell_connections_.push_back(base::WrapUnique(new_connection)); | 73 shell_connections_.push_back(base::WrapUnique(new_connection)); |
| 74 new_connection->SetConnectionLostClosure( | 74 new_connection->SetConnectionLostClosure( |
| 75 base::Bind(&Instance::OnShellConnectionLost, base::Unretained(this), | 75 base::Bind(&Instance::OnStop, base::Unretained(this), |
| 76 new_connection)); | 76 new_connection)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 friend class base::RefCountedThreadSafe<Instance>; | 80 friend class base::RefCountedThreadSafe<Instance>; |
| 81 | 81 |
| 82 ~Instance() { | 82 ~Instance() { |
| 83 // If this instance had its own thread, it MUST be explicitly destroyed by | 83 // If this instance had its own thread, it MUST be explicitly destroyed by |
| 84 // ShutDown() on the runner's thread by the time this destructor is run. | 84 // ShutDown() on the runner's thread by the time this destructor is run. |
| 85 DCHECK(!thread_); | 85 DCHECK(!thread_); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void OnShellConnectionLost(shell::ShellConnection* connection) { | 88 void OnStop(shell::ShellConnection* connection) { |
| 89 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 89 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 90 | 90 |
| 91 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); | 91 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
| 92 ++it) { | 92 ++it) { |
| 93 if (it->get() == connection) { | 93 if (it->get() == connection) { |
| 94 shell_connections_.erase(it); | 94 shell_connections_.erase(it); |
| 95 break; | 95 break; |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 void Quit() { | 100 void Quit() { |
| 101 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 101 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 102 | 102 |
| 103 shell_connections_.clear(); | 103 shell_connections_.clear(); |
| 104 shell_client_.reset(); | 104 service_.reset(); |
| 105 quit_task_runner_->PostTask( | 105 quit_task_runner_->PostTask( |
| 106 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); | 106 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void QuitOnRunnerThread() { | 109 void QuitOnRunnerThread() { |
| 110 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 110 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 111 ShutDown(); | 111 ShutDown(); |
| 112 quit_closure_.Run(); | 112 quit_closure_.Run(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 const std::string name_; | 115 const std::string name_; |
| 116 const MojoApplicationInfo::ApplicationFactory factory_callback_; | 116 const MojoApplicationInfo::ApplicationFactory factory_callback_; |
| 117 const bool use_own_thread_; | 117 const bool use_own_thread_; |
| 118 const base::Closure quit_closure_; | 118 const base::Closure quit_closure_; |
| 119 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 119 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
| 120 | 120 |
| 121 // Thread checker used to ensure certain operations happen only on the | 121 // Thread checker used to ensure certain operations happen only on the |
| 122 // runner's (i.e. our owner's) thread. | 122 // runner's (i.e. our owner's) thread. |
| 123 base::ThreadChecker runner_thread_checker_; | 123 base::ThreadChecker runner_thread_checker_; |
| 124 | 124 |
| 125 // These fields must only be accessed from the runner's thread. | 125 // These fields must only be accessed from the runner's thread. |
| 126 std::unique_ptr<base::Thread> thread_; | 126 std::unique_ptr<base::Thread> thread_; |
| 127 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_; | 127 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_; |
| 128 | 128 |
| 129 // These fields must only be accessed from the application thread, except in | 129 // These fields must only be accessed from the application thread, except in |
| 130 // the destructor which may run on either the runner thread or the application | 130 // the destructor which may run on either the runner thread or the application |
| 131 // thread. | 131 // thread. |
| 132 std::unique_ptr<shell::ShellClient> shell_client_; | 132 std::unique_ptr<shell::Service> service_; |
| 133 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; | 133 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; |
| 134 | 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(Instance); | 135 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 EmbeddedApplicationRunner::EmbeddedApplicationRunner( | 138 EmbeddedApplicationRunner::EmbeddedApplicationRunner( |
| 139 const base::StringPiece& name, | 139 const base::StringPiece& name, |
| 140 const MojoApplicationInfo& info) | 140 const MojoApplicationInfo& info) |
| 141 : weak_factory_(this) { | 141 : weak_factory_(this) { |
| 142 instance_ = new Instance(name, info, | 142 instance_ = new Instance(name, info, |
| 143 base::Bind(&EmbeddedApplicationRunner::OnQuit, | 143 base::Bind(&EmbeddedApplicationRunner::OnQuit, |
| 144 weak_factory_.GetWeakPtr())); | 144 weak_factory_.GetWeakPtr())); |
| 145 } | 145 } |
| 146 | 146 |
| 147 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { | 147 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { |
| 148 instance_->ShutDown(); | 148 instance_->ShutDown(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void EmbeddedApplicationRunner::BindShellClientRequest( | 151 void EmbeddedApplicationRunner::BindServiceRequest( |
| 152 shell::mojom::ShellClientRequest request) { | 152 shell::mojom::ServiceRequest request) { |
| 153 instance_->BindShellClientRequest(std::move(request)); | 153 instance_->BindServiceRequest(std::move(request)); |
| 154 } | 154 } |
| 155 | 155 |
| 156 void EmbeddedApplicationRunner::SetQuitClosure( | 156 void EmbeddedApplicationRunner::SetQuitClosure( |
| 157 const base::Closure& quit_closure) { | 157 const base::Closure& quit_closure) { |
| 158 quit_closure_ = quit_closure; | 158 quit_closure_ = quit_closure; |
| 159 } | 159 } |
| 160 | 160 |
| 161 void EmbeddedApplicationRunner::OnQuit() { | 161 void EmbeddedApplicationRunner::OnQuit() { |
| 162 if (!quit_closure_.is_null()) | 162 if (!quit_closure_.is_null()) |
| 163 quit_closure_.Run(); | 163 quit_closure_.Run(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace content | 166 } // namespace content |
| OLD | NEW |