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/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/simple_thread.h" | 14 #include "base/threading/simple_thread.h" |
14 #include "mojo/message_pump/message_pump_mojo.h" | 15 #include "mojo/message_pump/message_pump_mojo.h" |
15 #include "services/catalog/store.h" | 16 #include "services/catalog/store.h" |
16 #include "services/shell/connect_params.h" | 17 #include "services/shell/connect_params.h" |
17 #include "services/shell/loader.h" | 18 #include "services/shell/loader.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 mojo { | |
24 namespace shell { | 24 namespace shell { |
| 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { | 28 std::unique_ptr<base::MessagePump> CreateMessagePumpMojo() { |
28 return make_scoped_ptr(new common::MessagePumpMojo); | 29 return base::WrapUnique(new mojo::common::MessagePumpMojo); |
29 } | 30 } |
30 | 31 |
31 // Used to obtain the ShellClientRequest for an application. When Loader::Load() | 32 // Used to obtain the ShellClientRequest for an application. When Loader::Load() |
32 // is called a callback is run with the ShellClientRequest. | 33 // is called a callback is run with the ShellClientRequest. |
33 class BackgroundLoader : public Loader { | 34 class BackgroundLoader : public Loader { |
34 public: | 35 public: |
35 using Callback = base::Callback<void(mojom::ShellClientRequest)>; | 36 using Callback = base::Callback<void(mojom::ShellClientRequest)>; |
36 | 37 |
37 explicit BackgroundLoader(const Callback& callback) : callback_(callback) {} | 38 explicit BackgroundLoader(const Callback& callback) : callback_(callback) {} |
38 ~BackgroundLoader() override {} | 39 ~BackgroundLoader() override {} |
(...skipping 24 matching lines...) Expand all Loading... |
63 | 64 |
64 private: | 65 private: |
65 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); | 66 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); |
66 }; | 67 }; |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 // Manages the thread to startup mojo. | 71 // Manages the thread to startup mojo. |
71 class BackgroundShell::MojoThread : public base::SimpleThread { | 72 class BackgroundShell::MojoThread : public base::SimpleThread { |
72 public: | 73 public: |
73 explicit MojoThread(scoped_ptr<BackgroundShell::InitParams> init_params) | 74 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) |
74 : SimpleThread("mojo-background-shell"), | 75 : SimpleThread("mojo-background-shell"), |
75 init_params_(std::move(init_params)) {} | 76 init_params_(std::move(init_params)) {} |
76 ~MojoThread() override {} | 77 ~MojoThread() override {} |
77 | 78 |
78 void CreateShellClientRequest(base::WaitableEvent* signal, | 79 void CreateShellClientRequest(base::WaitableEvent* signal, |
79 scoped_ptr<ConnectParams> params, | 80 std::unique_ptr<ConnectParams> params, |
80 mojom::ShellClientRequest* request) { | 81 mojom::ShellClientRequest* request) { |
81 // Only valid to call this on the background thread. | 82 // Only valid to call this on the background thread. |
82 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 83 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
83 | 84 |
84 // Ownership of |loader| passes to Shell. | 85 // Ownership of |loader| passes to Shell. |
85 const std::string name = params->target().name(); | 86 const std::string name = params->target().name(); |
86 BackgroundLoader* loader = new BackgroundLoader( | 87 BackgroundLoader* loader = new BackgroundLoader( |
87 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this), | 88 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this), |
88 name, signal, request)); | 89 name, signal, request)); |
89 context_->shell()->SetLoaderForName(make_scoped_ptr(loader), name); | 90 context_->shell()->SetLoaderForName(base::WrapUnique(loader), name); |
90 context_->shell()->Connect(std::move(params)); | 91 context_->shell()->Connect(std::move(params)); |
91 // The request is asynchronously processed. When processed | 92 // The request is asynchronously processed. When processed |
92 // OnGotApplicationRequest() is called and we'll signal |signal|. | 93 // OnGotApplicationRequest() is called and we'll signal |signal|. |
93 } | 94 } |
94 | 95 |
95 base::MessageLoop* message_loop() { return message_loop_; } | 96 base::MessageLoop* message_loop() { return message_loop_; } |
96 | 97 |
97 // Stops the background thread. | 98 // Stops the background thread. |
98 void Stop() { | 99 void Stop() { |
99 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 100 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
100 message_loop_->task_runner()->PostTask( | 101 message_loop_->task_runner()->PostTask( |
101 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 102 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
102 Join(); | 103 Join(); |
103 } | 104 } |
104 | 105 |
105 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) { | 106 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) { |
106 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 107 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
107 callback.Run(context_->shell()); | 108 callback.Run(context_->shell()); |
108 } | 109 } |
109 | 110 |
110 // base::SimpleThread: | 111 // base::SimpleThread: |
111 void Start() override { | 112 void Start() override { |
112 DCHECK(!message_loop_); | 113 DCHECK(!message_loop_); |
113 message_loop_ = new MojoMessageLoop; | 114 message_loop_ = new MojoMessageLoop; |
114 base::SimpleThread::Start(); | 115 base::SimpleThread::Start(); |
115 } | 116 } |
116 void Run() override { | 117 void Run() override { |
117 // The construction/destruction order is very finicky and has to be done | 118 // The construction/destruction order is very finicky and has to be done |
118 // in the order here. | 119 // in the order here. |
119 scoped_ptr<base::MessageLoop> message_loop(message_loop_); | 120 std::unique_ptr<base::MessageLoop> message_loop(message_loop_); |
120 | 121 |
121 scoped_ptr<mojo::shell::Context::InitParams> context_init_params( | 122 std::unique_ptr<Context::InitParams> context_init_params( |
122 new mojo::shell::Context::InitParams); | 123 new Context::InitParams); |
123 if (init_params_) { | 124 if (init_params_) { |
124 context_init_params->catalog_store = | 125 context_init_params->catalog_store = |
125 std::move(init_params_->catalog_store); | 126 std::move(init_params_->catalog_store); |
126 context_init_params->native_runner_delegate = | 127 context_init_params->native_runner_delegate = |
127 init_params_->native_runner_delegate; | 128 init_params_->native_runner_delegate; |
128 context_init_params->init_edk = init_params_->init_edk; | 129 context_init_params->init_edk = init_params_->init_edk; |
129 } | 130 } |
130 if (context_init_params->init_edk) | 131 if (context_init_params->init_edk) |
131 Context::EnsureEmbedderIsInitialized(); | 132 Context::EnsureEmbedderIsInitialized(); |
132 | 133 |
133 message_loop_->BindToCurrentThread(); | 134 message_loop_->BindToCurrentThread(); |
134 | 135 |
135 scoped_ptr<Context> context(new Context); | 136 std::unique_ptr<Context> context(new Context); |
136 context_ = context.get(); | 137 context_ = context.get(); |
137 context_->Init(std::move(context_init_params)); | 138 context_->Init(std::move(context_init_params)); |
138 | 139 |
139 message_loop_->Run(); | 140 message_loop_->Run(); |
140 | 141 |
141 // Has to happen after run, but while messageloop still valid. | 142 // Has to happen after run, but while messageloop still valid. |
142 context_->Shutdown(); | 143 context_->Shutdown(); |
143 | 144 |
144 // Context has to be destroyed after the MessageLoop has been destroyed. | 145 // Context has to be destroyed after the MessageLoop has been destroyed. |
145 message_loop.reset(); | 146 message_loop.reset(); |
(...skipping 10 matching lines...) Expand all Loading... |
156 context_->shell()->SetLoaderForName(nullptr, name); | 157 context_->shell()->SetLoaderForName(nullptr, name); |
157 signal->Signal(); | 158 signal->Signal(); |
158 } | 159 } |
159 | 160 |
160 // We own this. It's created on the main thread, but destroyed on the | 161 // We own this. It's created on the main thread, but destroyed on the |
161 // background thread. | 162 // background thread. |
162 MojoMessageLoop* message_loop_ = nullptr; | 163 MojoMessageLoop* message_loop_ = nullptr; |
163 // Created in Run() on the background thread. | 164 // Created in Run() on the background thread. |
164 Context* context_ = nullptr; | 165 Context* context_ = nullptr; |
165 | 166 |
166 scoped_ptr<BackgroundShell::InitParams> init_params_; | 167 std::unique_ptr<BackgroundShell::InitParams> init_params_; |
167 | 168 |
168 DISALLOW_COPY_AND_ASSIGN(MojoThread); | 169 DISALLOW_COPY_AND_ASSIGN(MojoThread); |
169 }; | 170 }; |
170 | 171 |
171 BackgroundShell::InitParams::InitParams() {} | 172 BackgroundShell::InitParams::InitParams() {} |
172 BackgroundShell::InitParams::~InitParams() {} | 173 BackgroundShell::InitParams::~InitParams() {} |
173 | 174 |
174 BackgroundShell::BackgroundShell() {} | 175 BackgroundShell::BackgroundShell() {} |
175 | 176 |
176 BackgroundShell::~BackgroundShell() { | 177 BackgroundShell::~BackgroundShell() { |
177 thread_->Stop(); | 178 thread_->Stop(); |
178 } | 179 } |
179 | 180 |
180 void BackgroundShell::Init(scoped_ptr<InitParams> init_params) { | 181 void BackgroundShell::Init(std::unique_ptr<InitParams> init_params) { |
181 DCHECK(!thread_); | 182 DCHECK(!thread_); |
182 thread_.reset(new MojoThread(std::move(init_params))); | 183 thread_.reset(new MojoThread(std::move(init_params))); |
183 thread_->Start(); | 184 thread_->Start(); |
184 } | 185 } |
185 | 186 |
186 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( | 187 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( |
187 const std::string& name) { | 188 const std::string& name) { |
188 scoped_ptr<ConnectParams> params(new ConnectParams); | 189 std::unique_ptr<ConnectParams> params(new ConnectParams); |
189 params->set_source(CreateShellIdentity()); | 190 params->set_source(CreateShellIdentity()); |
190 params->set_target(Identity(name, mojom::kRootUserID)); | 191 params->set_target(Identity(name, mojom::kRootUserID)); |
191 mojom::ShellClientRequest request; | 192 mojom::ShellClientRequest request; |
192 base::WaitableEvent signal(true, false); | 193 base::WaitableEvent signal(true, false); |
193 thread_->message_loop()->task_runner()->PostTask( | 194 thread_->message_loop()->task_runner()->PostTask( |
194 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, | 195 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, |
195 base::Unretained(thread_.get()), &signal, | 196 base::Unretained(thread_.get()), &signal, |
196 base::Passed(¶ms), &request)); | 197 base::Passed(¶ms), &request)); |
197 signal.Wait(); | 198 signal.Wait(); |
198 return request; | 199 return request; |
199 } | 200 } |
200 | 201 |
201 void BackgroundShell::ExecuteOnShellThread( | 202 void BackgroundShell::ExecuteOnShellThread( |
202 const ShellThreadCallback& callback) { | 203 const ShellThreadCallback& callback) { |
203 thread_->message_loop()->task_runner()->PostTask( | 204 thread_->message_loop()->task_runner()->PostTask( |
204 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, | 205 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, |
205 base::Unretained(thread_.get()), callback)); | 206 base::Unretained(thread_.get()), callback)); |
206 } | 207 } |
207 | 208 |
208 } // namespace shell | 209 } // namespace shell |
209 } // namespace mojo | |
OLD | NEW |