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/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/simple_thread.h" | 14 #include "base/threading/simple_thread.h" |
15 #include "mojo/message_pump/message_pump_mojo.h" | 15 #include "mojo/message_pump/message_pump_mojo.h" |
16 #include "services/catalog/store.h" | 16 #include "services/catalog/store.h" |
17 #include "services/shell/connect_params.h" | 17 #include "services/shell/connect_params.h" |
18 #include "services/shell/loader.h" | |
19 #include "services/shell/public/cpp/shell_client.h" | 18 #include "services/shell/public/cpp/shell_client.h" |
20 #include "services/shell/public/cpp/shell_connection.h" | 19 #include "services/shell/public/cpp/shell_connection.h" |
21 #include "services/shell/shell.h" | 20 #include "services/shell/shell.h" |
22 #include "services/shell/standalone/context.h" | 21 #include "services/shell/standalone/context.h" |
23 | 22 |
24 namespace shell { | 23 namespace shell { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 std::unique_ptr<base::MessagePump> CreateMessagePumpMojo() { | 27 std::unique_ptr<base::MessagePump> CreateMessagePumpMojo() { |
29 return base::WrapUnique(new mojo::common::MessagePumpMojo); | 28 return base::WrapUnique(new mojo::common::MessagePumpMojo); |
30 } | 29 } |
31 | 30 |
32 // Used to obtain the ShellClientRequest for an application. When Loader::Load() | |
33 // is called a callback is run with the ShellClientRequest. | |
34 class BackgroundLoader : public Loader { | |
35 public: | |
36 using Callback = base::Callback<void(mojom::ShellClientRequest)>; | |
37 | |
38 explicit BackgroundLoader(const Callback& callback) : callback_(callback) {} | |
39 ~BackgroundLoader() override {} | |
40 | |
41 // Loader: | |
42 void Load(const std::string& name, | |
43 mojom::ShellClientRequest request) override { | |
44 DCHECK(!callback_.is_null()); // Callback should only be run once. | |
45 Callback callback = callback_; | |
46 callback_.Reset(); | |
47 callback.Run(std::move(request)); | |
48 } | |
49 | |
50 private: | |
51 Callback callback_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader); | |
54 }; | |
55 | |
56 class MojoMessageLoop : public base::MessageLoop { | 31 class MojoMessageLoop : public base::MessageLoop { |
57 public: | 32 public: |
58 MojoMessageLoop() | 33 MojoMessageLoop() |
59 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM, | 34 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM, |
60 base::Bind(&CreateMessagePumpMojo)) {} | 35 base::Bind(&CreateMessagePumpMojo)) {} |
61 ~MojoMessageLoop() override {} | 36 ~MojoMessageLoop() override {} |
62 | 37 |
63 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } | 38 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } |
64 | 39 |
65 private: | 40 private: |
66 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); | 41 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); |
67 }; | 42 }; |
68 | 43 |
69 } // namespace | 44 } // namespace |
70 | 45 |
71 // Manages the thread to startup mojo. | 46 // Manages the thread to startup mojo. |
72 class BackgroundShell::MojoThread : public base::SimpleThread { | 47 class BackgroundShell::MojoThread : public base::SimpleThread { |
73 public: | 48 public: |
74 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) | 49 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) |
75 : SimpleThread("mojo-background-shell"), | 50 : SimpleThread("mojo-background-shell"), |
76 init_params_(std::move(init_params)) {} | 51 init_params_(std::move(init_params)) {} |
77 ~MojoThread() override {} | 52 ~MojoThread() override {} |
78 | 53 |
79 void CreateShellClientRequest(base::WaitableEvent* signal, | 54 void CreateShellClientRequest(base::WaitableEvent* signal, |
80 std::unique_ptr<ConnectParams> params, | 55 const std::string& name, |
81 mojom::ShellClientRequest* request) { | 56 mojom::ShellClientRequest* request) { |
82 // Only valid to call this on the background thread. | 57 // Only valid to call this on the background thread. |
83 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 58 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 59 *request = context_->shell()->InitInstanceForEmbedder(name); |
| 60 signal->Signal(); |
| 61 } |
84 | 62 |
85 // Ownership of |loader| passes to Shell. | 63 void Connect(std::unique_ptr<ConnectParams> params) { |
86 const std::string name = params->target().name(); | |
87 BackgroundLoader* loader = new BackgroundLoader( | |
88 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this), | |
89 name, signal, request)); | |
90 context_->shell()->SetLoaderForName(base::WrapUnique(loader), name); | |
91 context_->shell()->Connect(std::move(params)); | 64 context_->shell()->Connect(std::move(params)); |
92 // The request is asynchronously processed. When processed | |
93 // OnGotApplicationRequest() is called and we'll signal |signal|. | |
94 } | 65 } |
95 | 66 |
96 base::MessageLoop* message_loop() { return message_loop_; } | 67 base::MessageLoop* message_loop() { return message_loop_; } |
97 | 68 |
98 // Stops the background thread. | 69 // Stops the background thread. |
99 void Stop() { | 70 void Stop() { |
100 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 71 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
101 message_loop_->task_runner()->PostTask( | 72 message_loop_->task_runner()->PostTask( |
102 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 73 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
103 Join(); | 74 Join(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 112 |
142 // Has to happen after run, but while messageloop still valid. | 113 // Has to happen after run, but while messageloop still valid. |
143 context_->Shutdown(); | 114 context_->Shutdown(); |
144 | 115 |
145 // Context has to be destroyed after the MessageLoop has been destroyed. | 116 // Context has to be destroyed after the MessageLoop has been destroyed. |
146 message_loop.reset(); | 117 message_loop.reset(); |
147 context_ = nullptr; | 118 context_ = nullptr; |
148 } | 119 } |
149 | 120 |
150 private: | 121 private: |
151 void OnGotApplicationRequest(const std::string& name, | |
152 base::WaitableEvent* signal, | |
153 mojom::ShellClientRequest* request_result, | |
154 mojom::ShellClientRequest actual_request) { | |
155 *request_result = std::move(actual_request); | |
156 // Trigger destruction of the loader. | |
157 context_->shell()->SetLoaderForName(nullptr, name); | |
158 signal->Signal(); | |
159 } | |
160 | |
161 // We own this. It's created on the main thread, but destroyed on the | 122 // We own this. It's created on the main thread, but destroyed on the |
162 // background thread. | 123 // background thread. |
163 MojoMessageLoop* message_loop_ = nullptr; | 124 MojoMessageLoop* message_loop_ = nullptr; |
164 // Created in Run() on the background thread. | 125 // Created in Run() on the background thread. |
165 Context* context_ = nullptr; | 126 Context* context_ = nullptr; |
166 | 127 |
167 std::unique_ptr<BackgroundShell::InitParams> init_params_; | 128 std::unique_ptr<BackgroundShell::InitParams> init_params_; |
168 | 129 |
169 DISALLOW_COPY_AND_ASSIGN(MojoThread); | 130 DISALLOW_COPY_AND_ASSIGN(MojoThread); |
170 }; | 131 }; |
(...skipping 15 matching lines...) Expand all Loading... |
186 | 147 |
187 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( | 148 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( |
188 const std::string& name) { | 149 const std::string& name) { |
189 std::unique_ptr<ConnectParams> params(new ConnectParams); | 150 std::unique_ptr<ConnectParams> params(new ConnectParams); |
190 params->set_source(CreateShellIdentity()); | 151 params->set_source(CreateShellIdentity()); |
191 params->set_target(Identity(name, mojom::kRootUserID)); | 152 params->set_target(Identity(name, mojom::kRootUserID)); |
192 mojom::ShellClientRequest request; | 153 mojom::ShellClientRequest request; |
193 base::WaitableEvent signal(true, false); | 154 base::WaitableEvent signal(true, false); |
194 thread_->message_loop()->task_runner()->PostTask( | 155 thread_->message_loop()->task_runner()->PostTask( |
195 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, | 156 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, |
196 base::Unretained(thread_.get()), &signal, | 157 base::Unretained(thread_.get()), &signal, name, |
197 base::Passed(¶ms), &request)); | 158 &request)); |
198 signal.Wait(); | 159 signal.Wait(); |
| 160 thread_->message_loop()->task_runner()->PostTask( |
| 161 FROM_HERE, base::Bind(&MojoThread::Connect, |
| 162 base::Unretained(thread_.get()), |
| 163 base::Passed(¶ms))); |
199 return request; | 164 return request; |
200 } | 165 } |
201 | 166 |
202 void BackgroundShell::ExecuteOnShellThread( | 167 void BackgroundShell::ExecuteOnShellThread( |
203 const ShellThreadCallback& callback) { | 168 const ShellThreadCallback& callback) { |
204 thread_->message_loop()->task_runner()->PostTask( | 169 thread_->message_loop()->task_runner()->PostTask( |
205 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, | 170 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, |
206 base::Unretained(thread_.get()), callback)); | 171 base::Unretained(thread_.get()), callback)); |
207 } | 172 } |
208 | 173 |
209 } // namespace shell | 174 } // namespace shell |
OLD | NEW |