Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(789)

Side by Side Diff: mojo/shell/background/background_shell.cc

Issue 1877753003: Move mojo\shell to services\shell (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@62scan
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/shell/background/background_shell.h ('k') | mojo/shell/background/background_shell_main.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/shell/background/background_shell.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/simple_thread.h"
14 #include "mojo/message_pump/message_pump_mojo.h"
15 #include "mojo/services/catalog/store.h"
16 #include "mojo/shell/connect_params.h"
17 #include "mojo/shell/loader.h"
18 #include "mojo/shell/public/cpp/shell_client.h"
19 #include "mojo/shell/public/cpp/shell_connection.h"
20 #include "mojo/shell/shell.h"
21 #include "mojo/shell/standalone/context.h"
22
23 namespace mojo {
24 namespace shell {
25 namespace {
26
27 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() {
28 return make_scoped_ptr(new common::MessagePumpMojo);
29 }
30
31 // Used to obtain the ShellClientRequest for an application. When Loader::Load()
32 // is called a callback is run with the ShellClientRequest.
33 class BackgroundLoader : public Loader {
34 public:
35 using Callback = base::Callback<void(mojom::ShellClientRequest)>;
36
37 explicit BackgroundLoader(const Callback& callback) : callback_(callback) {}
38 ~BackgroundLoader() override {}
39
40 // Loader:
41 void Load(const std::string& name,
42 mojom::ShellClientRequest request) override {
43 DCHECK(!callback_.is_null()); // Callback should only be run once.
44 Callback callback = callback_;
45 callback_.Reset();
46 callback.Run(std::move(request));
47 }
48
49 private:
50 Callback callback_;
51
52 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader);
53 };
54
55 class MojoMessageLoop : public base::MessageLoop {
56 public:
57 MojoMessageLoop()
58 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM,
59 base::Bind(&CreateMessagePumpMojo)) {}
60 ~MojoMessageLoop() override {}
61
62 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); }
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop);
66 };
67
68 } // namespace
69
70 // Manages the thread to startup mojo.
71 class BackgroundShell::MojoThread : public base::SimpleThread {
72 public:
73 explicit MojoThread(scoped_ptr<BackgroundShell::InitParams> init_params)
74 : SimpleThread("mojo-background-shell"),
75 init_params_(std::move(init_params)) {}
76 ~MojoThread() override {}
77
78 void CreateShellClientRequest(base::WaitableEvent* signal,
79 scoped_ptr<ConnectParams> params,
80 mojom::ShellClientRequest* request) {
81 // Only valid to call this on the background thread.
82 DCHECK_EQ(message_loop_, base::MessageLoop::current());
83
84 // Ownership of |loader| passes to Shell.
85 const std::string name = params->target().name();
86 BackgroundLoader* loader = new BackgroundLoader(
87 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this),
88 name, signal, request));
89 context_->shell()->SetLoaderForName(make_scoped_ptr(loader), name);
90 context_->shell()->Connect(std::move(params));
91 // The request is asynchronously processed. When processed
92 // OnGotApplicationRequest() is called and we'll signal |signal|.
93 }
94
95 base::MessageLoop* message_loop() { return message_loop_; }
96
97 // Stops the background thread.
98 void Stop() {
99 DCHECK_NE(message_loop_, base::MessageLoop::current());
100 message_loop_->task_runner()->PostTask(
101 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
102 Join();
103 }
104
105 void RunShellCallback(const BackgroundShell::ShellThreadCallback& callback) {
106 DCHECK_EQ(message_loop_, base::MessageLoop::current());
107 callback.Run(context_->shell());
108 }
109
110 // base::SimpleThread:
111 void Start() override {
112 DCHECK(!message_loop_);
113 message_loop_ = new MojoMessageLoop;
114 base::SimpleThread::Start();
115 }
116 void Run() override {
117 // The construction/destruction order is very finicky and has to be done
118 // in the order here.
119 scoped_ptr<base::MessageLoop> message_loop(message_loop_);
120
121 scoped_ptr<mojo::shell::Context::InitParams> context_init_params(
122 new mojo::shell::Context::InitParams);
123 if (init_params_) {
124 context_init_params->catalog_store =
125 std::move(init_params_->catalog_store);
126 context_init_params->native_runner_delegate =
127 init_params_->native_runner_delegate;
128 context_init_params->init_edk = init_params_->init_edk;
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();
137 context_->Init(std::move(context_init_params));
138
139 message_loop_->Run();
140
141 // Has to happen after run, but while messageloop still valid.
142 context_->Shutdown();
143
144 // Context has to be destroyed after the MessageLoop has been destroyed.
145 message_loop.reset();
146 context_ = nullptr;
147 }
148
149 private:
150 void OnGotApplicationRequest(const std::string& name,
151 base::WaitableEvent* signal,
152 mojom::ShellClientRequest* request_result,
153 mojom::ShellClientRequest actual_request) {
154 *request_result = std::move(actual_request);
155 // Trigger destruction of the loader.
156 context_->shell()->SetLoaderForName(nullptr, name);
157 signal->Signal();
158 }
159
160 // We own this. It's created on the main thread, but destroyed on the
161 // background thread.
162 MojoMessageLoop* message_loop_ = nullptr;
163 // Created in Run() on the background thread.
164 Context* context_ = nullptr;
165
166 scoped_ptr<BackgroundShell::InitParams> init_params_;
167
168 DISALLOW_COPY_AND_ASSIGN(MojoThread);
169 };
170
171 BackgroundShell::InitParams::InitParams() {}
172 BackgroundShell::InitParams::~InitParams() {}
173
174 BackgroundShell::BackgroundShell() {}
175
176 BackgroundShell::~BackgroundShell() {
177 thread_->Stop();
178 }
179
180 void BackgroundShell::Init(scoped_ptr<InitParams> init_params) {
181 DCHECK(!thread_);
182 thread_.reset(new MojoThread(std::move(init_params)));
183 thread_->Start();
184 }
185
186 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest(
187 const std::string& name) {
188 scoped_ptr<ConnectParams> params(new ConnectParams);
189 params->set_source(CreateShellIdentity());
190 params->set_target(Identity(name, mojom::kRootUserID));
191 mojom::ShellClientRequest request;
192 base::WaitableEvent signal(true, false);
193 thread_->message_loop()->task_runner()->PostTask(
194 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest,
195 base::Unretained(thread_.get()), &signal,
196 base::Passed(&params), &request));
197 signal.Wait();
198 return request;
199 }
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
208 } // namespace shell
209 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/background/background_shell.h ('k') | mojo/shell/background/background_shell_main.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698