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 "mojo/shell/background/background_shell.h" | 5 #include "mojo/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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 base::MessageLoop* message_loop() { return message_loop_; } | 95 base::MessageLoop* message_loop() { return message_loop_; } |
96 | 96 |
97 // Stops the background thread. | 97 // Stops the background thread. |
98 void Stop() { | 98 void Stop() { |
99 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 99 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
100 message_loop_->task_runner()->PostTask( | 100 message_loop_->task_runner()->PostTask( |
101 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 101 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
102 Join(); | 102 Join(); |
103 } | 103 } |
104 | 104 |
| 105 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) { |
| 106 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 107 callback.Run(context_->shell()); |
| 108 } |
| 109 |
105 // base::SimpleThread: | 110 // base::SimpleThread: |
106 void Start() override { | 111 void Start() override { |
107 DCHECK(!message_loop_); | 112 DCHECK(!message_loop_); |
108 message_loop_ = new MojoMessageLoop; | 113 message_loop_ = new MojoMessageLoop; |
109 base::SimpleThread::Start(); | 114 base::SimpleThread::Start(); |
110 } | 115 } |
111 void Run() override { | 116 void Run() override { |
112 // The construction/destruction order is very finicky and has to be done | 117 // The construction/destruction order is very finicky and has to be done |
113 // in the order here. | 118 // in the order here. |
114 scoped_ptr<base::MessageLoop> message_loop(message_loop_); | 119 scoped_ptr<base::MessageLoop> message_loop(message_loop_); |
115 | 120 |
116 Context::EnsureEmbedderIsInitialized(); | |
117 | |
118 message_loop_->BindToCurrentThread(); | |
119 | |
120 scoped_ptr<Context> context(new Context); | |
121 context_ = context.get(); | |
122 scoped_ptr<mojo::shell::Context::InitParams> context_init_params( | 121 scoped_ptr<mojo::shell::Context::InitParams> context_init_params( |
123 new mojo::shell::Context::InitParams); | 122 new mojo::shell::Context::InitParams); |
124 if (init_params_) { | 123 if (init_params_) { |
125 context_init_params->catalog_store = | 124 context_init_params->catalog_store = |
126 std::move(init_params_->catalog_store); | 125 std::move(init_params_->catalog_store); |
127 context_init_params->native_runner_delegate = | 126 context_init_params->native_runner_delegate = |
128 init_params_->native_runner_delegate; | 127 init_params_->native_runner_delegate; |
| 128 context_init_params->init_edk = init_params_->init_edk; |
129 } | 129 } |
| 130 if (context_init_params->init_edk) |
| 131 Context::EnsureEmbedderIsInitialized(); |
| 132 |
| 133 message_loop_->BindToCurrentThread(); |
| 134 |
| 135 scoped_ptr<Context> context(new Context); |
| 136 context_ = context.get(); |
130 context_->Init(std::move(context_init_params)); | 137 context_->Init(std::move(context_init_params)); |
131 | 138 |
132 message_loop_->Run(); | 139 message_loop_->Run(); |
133 | 140 |
134 // Has to happen after run, but while messageloop still valid. | 141 // Has to happen after run, but while messageloop still valid. |
135 context_->Shutdown(); | 142 context_->Shutdown(); |
136 | 143 |
137 // Context has to be destroyed after the MessageLoop has been destroyed. | 144 // Context has to be destroyed after the MessageLoop has been destroyed. |
138 message_loop.reset(); | 145 message_loop.reset(); |
139 context_ = nullptr; | 146 context_ = nullptr; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 mojom::ShellClientRequest request; | 191 mojom::ShellClientRequest request; |
185 base::WaitableEvent signal(true, false); | 192 base::WaitableEvent signal(true, false); |
186 thread_->message_loop()->task_runner()->PostTask( | 193 thread_->message_loop()->task_runner()->PostTask( |
187 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, | 194 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, |
188 base::Unretained(thread_.get()), &signal, | 195 base::Unretained(thread_.get()), &signal, |
189 base::Passed(¶ms), &request)); | 196 base::Passed(¶ms), &request)); |
190 signal.Wait(); | 197 signal.Wait(); |
191 return request; | 198 return request; |
192 } | 199 } |
193 | 200 |
| 201 void BackgroundShell::ExecuteOnShellThread( |
| 202 const ShellThreadCallback& callback) { |
| 203 thread_->message_loop()->task_runner()->PostTask( |
| 204 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, |
| 205 base::Unretained(thread_.get()), callback)); |
| 206 } |
| 207 |
194 } // namespace shell | 208 } // namespace shell |
195 } // namespace mojo | 209 } // namespace mojo |
OLD | NEW |