| 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/shell/background/background_shell.h" | 5 #include "services/shell/background/background_shell.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_pump_default.h" | 12 #include "base/message_loop/message_pump_default.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/simple_thread.h" | 16 #include "base/threading/simple_thread.h" |
| 16 #include "services/catalog/store.h" | 17 #include "services/catalog/store.h" |
| 17 #include "services/shell/connect_params.h" | 18 #include "services/shell/connect_params.h" |
| 18 #include "services/shell/public/cpp/shell_client.h" | 19 #include "services/shell/public/cpp/shell_client.h" |
| 19 #include "services/shell/public/cpp/shell_connection.h" | 20 #include "services/shell/public/cpp/shell_connection.h" |
| 20 #include "services/shell/shell.h" | 21 #include "services/shell/shell.h" |
| 21 #include "services/shell/standalone/context.h" | 22 #include "services/shell/standalone/context.h" |
| 22 | 23 |
| 23 namespace shell { | 24 namespace shell { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 48 public: | 49 public: |
| 49 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) | 50 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) |
| 50 : SimpleThread("mojo-background-shell"), | 51 : SimpleThread("mojo-background-shell"), |
| 51 init_params_(std::move(init_params)) {} | 52 init_params_(std::move(init_params)) {} |
| 52 ~MojoThread() override {} | 53 ~MojoThread() override {} |
| 53 | 54 |
| 54 void CreateShellClientRequest(base::WaitableEvent* signal, | 55 void CreateShellClientRequest(base::WaitableEvent* signal, |
| 55 const std::string& name, | 56 const std::string& name, |
| 56 mojom::ShellClientRequest* request) { | 57 mojom::ShellClientRequest* request) { |
| 57 // Only valid to call this on the background thread. | 58 // Only valid to call this on the background thread. |
| 58 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 59 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 59 *request = context_->shell()->InitInstanceForEmbedder(name); | 60 *request = context_->shell()->InitInstanceForEmbedder(name); |
| 60 signal->Signal(); | 61 signal->Signal(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void Connect(std::unique_ptr<ConnectParams> params) { | 64 void Connect(std::unique_ptr<ConnectParams> params) { |
| 64 context_->shell()->Connect(std::move(params)); | 65 context_->shell()->Connect(std::move(params)); |
| 65 } | 66 } |
| 66 | 67 |
| 67 base::MessageLoop* message_loop() { return message_loop_; } | 68 base::MessageLoop* message_loop() { return message_loop_; } |
| 68 | 69 |
| 69 // Stops the background thread. | 70 // Stops the background thread. |
| 70 void Stop() { | 71 void Stop() { |
| 71 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 72 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
| 72 message_loop_->task_runner()->PostTask( | 73 message_loop_->task_runner()->PostTask( |
| 73 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 74 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 74 Join(); | 75 Join(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) { | 78 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) { |
| 78 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 79 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 79 callback.Run(context_->shell()); | 80 callback.Run(context_->shell()); |
| 80 } | 81 } |
| 81 | 82 |
| 82 // base::SimpleThread: | 83 // base::SimpleThread: |
| 83 void Start() override { | 84 void Start() override { |
| 84 DCHECK(!message_loop_); | 85 DCHECK(!message_loop_); |
| 85 message_loop_ = new MojoMessageLoop; | 86 message_loop_ = new MojoMessageLoop; |
| 86 base::SimpleThread::Start(); | 87 base::SimpleThread::Start(); |
| 87 } | 88 } |
| 88 void Run() override { | 89 void Run() override { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 148 |
| 148 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( | 149 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( |
| 149 const std::string& name) { | 150 const std::string& name) { |
| 150 std::unique_ptr<ConnectParams> params(new ConnectParams); | 151 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 151 params->set_source(CreateShellIdentity()); | 152 params->set_source(CreateShellIdentity()); |
| 152 params->set_target(Identity(name, mojom::kRootUserID)); | 153 params->set_target(Identity(name, mojom::kRootUserID)); |
| 153 mojom::ShellClientRequest request; | 154 mojom::ShellClientRequest request; |
| 154 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | 155 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, |
| 155 base::WaitableEvent::InitialState::NOT_SIGNALED); | 156 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 156 thread_->message_loop()->task_runner()->PostTask( | 157 thread_->message_loop()->task_runner()->PostTask( |
| 157 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, | 158 FROM_HERE, |
| 158 base::Unretained(thread_.get()), &signal, name, | 159 base::Bind(&MojoThread::CreateShellClientRequest, |
| 159 &request)); | 160 base::Unretained(thread_.get()), &signal, name, &request)); |
| 160 signal.Wait(); | 161 signal.Wait(); |
| 161 thread_->message_loop()->task_runner()->PostTask( | 162 thread_->message_loop()->task_runner()->PostTask( |
| 162 FROM_HERE, base::Bind(&MojoThread::Connect, | 163 FROM_HERE, |
| 163 base::Unretained(thread_.get()), | 164 base::Bind(&MojoThread::Connect, base::Unretained(thread_.get()), |
| 164 base::Passed(¶ms))); | 165 base::Passed(¶ms))); |
| 165 return request; | 166 return request; |
| 166 } | 167 } |
| 167 | 168 |
| 168 void BackgroundShell::ExecuteOnShellThread( | 169 void BackgroundShell::ExecuteOnShellThread( |
| 169 const ShellThreadCallback& callback) { | 170 const ShellThreadCallback& callback) { |
| 170 thread_->message_loop()->task_runner()->PostTask( | 171 thread_->message_loop()->task_runner()->PostTask( |
| 171 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, | 172 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, |
| 172 base::Unretained(thread_.get()), callback)); | 173 base::Unretained(thread_.get()), callback)); |
| 173 } | 174 } |
| 174 | 175 |
| 175 } // namespace shell | 176 } // namespace shell |
| OLD | NEW |