| 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 11 matching lines...) Expand all Loading... |
| 22 public: | 22 public: |
| 23 Instance(const base::StringPiece& name, | 23 Instance(const base::StringPiece& name, |
| 24 const MojoApplicationInfo& info, | 24 const MojoApplicationInfo& 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.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 application_thread_checker_.DetachFromThread(); | |
| 33 | |
| 34 if (!use_own_thread_ && !application_task_runner_) | 32 if (!use_own_thread_ && !application_task_runner_) |
| 35 application_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 33 application_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 36 } | 34 } |
| 37 | 35 |
| 38 void BindShellClientRequest(shell::mojom::ShellClientRequest request) { | 36 void BindShellClientRequest(shell::mojom::ShellClientRequest request) { |
| 39 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 37 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 40 | 38 |
| 41 if (use_own_thread_ && !thread_) { | 39 if (use_own_thread_ && !thread_) { |
| 42 // Start a new thread if necessary. | 40 // Start a new thread if necessary. |
| 43 thread_.reset(new base::Thread(name_)); | 41 thread_.reset(new base::Thread(name_)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 56 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 54 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 57 if (thread_) { | 55 if (thread_) { |
| 58 application_task_runner_ = nullptr; | 56 application_task_runner_ = nullptr; |
| 59 thread_.reset(); | 57 thread_.reset(); |
| 60 } | 58 } |
| 61 } | 59 } |
| 62 | 60 |
| 63 private: | 61 private: |
| 64 void BindShellClientRequestOnApplicationThread( | 62 void BindShellClientRequestOnApplicationThread( |
| 65 shell::mojom::ShellClientRequest request) { | 63 shell::mojom::ShellClientRequest request) { |
| 66 DCHECK(application_thread_checker_.CalledOnValidThread()); | 64 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 67 | 65 |
| 68 if (!shell_client_) { | 66 if (!shell_client_) { |
| 69 shell_client_ = factory_callback_.Run( | 67 shell_client_ = factory_callback_.Run( |
| 70 base::Bind(&Instance::Quit, base::Unretained(this))); | 68 base::Bind(&Instance::Quit, base::Unretained(this))); |
| 71 } | 69 } |
| 72 | 70 |
| 73 shell::ShellConnection* new_connection = | 71 shell::ShellConnection* new_connection = |
| 74 new shell::ShellConnection(shell_client_.get(), std::move(request)); | 72 new shell::ShellConnection(shell_client_.get(), std::move(request)); |
| 75 shell_connections_.push_back(base::WrapUnique(new_connection)); | 73 shell_connections_.push_back(base::WrapUnique(new_connection)); |
| 76 new_connection->SetConnectionLostClosure( | 74 new_connection->SetConnectionLostClosure( |
| 77 base::Bind(&Instance::OnShellConnectionLost, base::Unretained(this), | 75 base::Bind(&Instance::OnShellConnectionLost, base::Unretained(this), |
| 78 new_connection)); | 76 new_connection)); |
| 79 } | 77 } |
| 80 | 78 |
| 81 private: | 79 private: |
| 82 friend class base::RefCountedThreadSafe<Instance>; | 80 friend class base::RefCountedThreadSafe<Instance>; |
| 83 | 81 |
| 84 ~Instance() { | 82 ~Instance() { |
| 85 // 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 |
| 86 // 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. |
| 87 DCHECK(!thread_); | 85 DCHECK(!thread_); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void OnShellConnectionLost(shell::ShellConnection* connection) { | 88 void OnShellConnectionLost(shell::ShellConnection* connection) { |
| 91 DCHECK(application_thread_checker_.CalledOnValidThread()); | 89 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 92 | 90 |
| 93 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); | 91 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
| 94 ++it) { | 92 ++it) { |
| 95 if (it->get() == connection) { | 93 if (it->get() == connection) { |
| 96 shell_connections_.erase(it); | 94 shell_connections_.erase(it); |
| 97 break; | 95 break; |
| 98 } | 96 } |
| 99 } | 97 } |
| 100 } | 98 } |
| 101 | 99 |
| 102 void Quit() { | 100 void Quit() { |
| 103 DCHECK(application_thread_checker_.CalledOnValidThread()); | 101 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
| 104 | 102 |
| 105 shell_connections_.clear(); | 103 shell_connections_.clear(); |
| 106 shell_client_.reset(); | 104 shell_client_.reset(); |
| 107 quit_task_runner_->PostTask( | 105 quit_task_runner_->PostTask( |
| 108 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); | 106 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); |
| 109 } | 107 } |
| 110 | 108 |
| 111 void QuitOnRunnerThread() { | 109 void QuitOnRunnerThread() { |
| 112 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 110 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
| 113 ShutDown(); | 111 ShutDown(); |
| 114 quit_closure_.Run(); | 112 quit_closure_.Run(); |
| 115 } | 113 } |
| 116 | 114 |
| 117 const std::string name_; | 115 const std::string name_; |
| 118 const MojoApplicationInfo::ApplicationFactory factory_callback_; | 116 const MojoApplicationInfo::ApplicationFactory factory_callback_; |
| 119 const bool use_own_thread_; | 117 const bool use_own_thread_; |
| 120 const base::Closure quit_closure_; | 118 const base::Closure quit_closure_; |
| 121 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 119 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
| 122 | 120 |
| 123 // Thread checker used to ensure certain operations happen only on the | 121 // Thread checker used to ensure certain operations happen only on the |
| 124 // runner's (i.e. our owner's) thread. | 122 // runner's (i.e. our owner's) thread. |
| 125 base::ThreadChecker runner_thread_checker_; | 123 base::ThreadChecker runner_thread_checker_; |
| 126 | 124 |
| 127 // Thread checker used to ensure certain operations happen only on the | |
| 128 // application task runner's thread. | |
| 129 base::ThreadChecker application_thread_checker_; | |
| 130 | |
| 131 // These fields must only be accessed from the runner's thread. | 125 // These fields must only be accessed from the runner's thread. |
| 132 std::unique_ptr<base::Thread> thread_; | 126 std::unique_ptr<base::Thread> thread_; |
| 133 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_; | 127 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_; |
| 134 | 128 |
| 135 // 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 |
| 136 // 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 |
| 137 // thread. | 131 // thread. |
| 138 std::unique_ptr<shell::ShellClient> shell_client_; | 132 std::unique_ptr<shell::ShellClient> shell_client_; |
| 139 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; | 133 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; |
| 140 | 134 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 163 const base::Closure& quit_closure) { | 157 const base::Closure& quit_closure) { |
| 164 quit_closure_ = quit_closure; | 158 quit_closure_ = quit_closure; |
| 165 } | 159 } |
| 166 | 160 |
| 167 void EmbeddedApplicationRunner::OnQuit() { | 161 void EmbeddedApplicationRunner::OnQuit() { |
| 168 if (!quit_closure_.is_null()) | 162 if (!quit_closure_.is_null()) |
| 169 quit_closure_.Run(); | 163 quit_closure_.Run(); |
| 170 } | 164 } |
| 171 | 165 |
| 172 } // namespace content | 166 } // namespace content |
| OLD | NEW |