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

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

Issue 1899583002: Revert of Remove shell::Loader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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 | « services/shell/BUILD.gn ('k') | services/shell/loader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
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 {
24 25
25 namespace { 26 namespace {
26 27
27 std::unique_ptr<base::MessagePump> CreateMessagePumpMojo() { 28 std::unique_ptr<base::MessagePump> CreateMessagePumpMojo() {
28 return base::WrapUnique(new mojo::common::MessagePumpMojo); 29 return base::WrapUnique(new mojo::common::MessagePumpMojo);
29 } 30 }
30 31
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
31 class MojoMessageLoop : public base::MessageLoop { 56 class MojoMessageLoop : public base::MessageLoop {
32 public: 57 public:
33 MojoMessageLoop() 58 MojoMessageLoop()
34 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM, 59 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM,
35 base::Bind(&CreateMessagePumpMojo)) {} 60 base::Bind(&CreateMessagePumpMojo)) {}
36 ~MojoMessageLoop() override {} 61 ~MojoMessageLoop() override {}
37 62
38 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } 63 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); }
39 64
40 private: 65 private:
41 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); 66 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop);
42 }; 67 };
43 68
44 } // namespace 69 } // namespace
45 70
46 // Manages the thread to startup mojo. 71 // Manages the thread to startup mojo.
47 class BackgroundShell::MojoThread : public base::SimpleThread { 72 class BackgroundShell::MojoThread : public base::SimpleThread {
48 public: 73 public:
49 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) 74 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params)
50 : SimpleThread("mojo-background-shell"), 75 : SimpleThread("mojo-background-shell"),
51 init_params_(std::move(init_params)) {} 76 init_params_(std::move(init_params)) {}
52 ~MojoThread() override {} 77 ~MojoThread() override {}
53 78
54 void CreateShellClientRequest(base::WaitableEvent* signal, 79 void CreateShellClientRequest(base::WaitableEvent* signal,
55 const std::string& name, 80 std::unique_ptr<ConnectParams> params,
56 mojom::ShellClientRequest* request) { 81 mojom::ShellClientRequest* request) {
57 // Only valid to call this on the background thread. 82 // Only valid to call this on the background thread.
58 DCHECK_EQ(message_loop_, base::MessageLoop::current()); 83 DCHECK_EQ(message_loop_, base::MessageLoop::current());
59 *request = context_->shell()->InitInstanceForEmbedder(name);
60 signal->Signal();
61 }
62 84
63 void Connect(std::unique_ptr<ConnectParams> params) { 85 // Ownership of |loader| passes to Shell.
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);
64 context_->shell()->Connect(std::move(params)); 91 context_->shell()->Connect(std::move(params));
92 // The request is asynchronously processed. When processed
93 // OnGotApplicationRequest() is called and we'll signal |signal|.
65 } 94 }
66 95
67 base::MessageLoop* message_loop() { return message_loop_; } 96 base::MessageLoop* message_loop() { return message_loop_; }
68 97
69 // Stops the background thread. 98 // Stops the background thread.
70 void Stop() { 99 void Stop() {
71 DCHECK_NE(message_loop_, base::MessageLoop::current()); 100 DCHECK_NE(message_loop_, base::MessageLoop::current());
72 message_loop_->task_runner()->PostTask( 101 message_loop_->task_runner()->PostTask(
73 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); 102 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
74 Join(); 103 Join();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 141
113 // Has to happen after run, but while messageloop still valid. 142 // Has to happen after run, but while messageloop still valid.
114 context_->Shutdown(); 143 context_->Shutdown();
115 144
116 // Context has to be destroyed after the MessageLoop has been destroyed. 145 // Context has to be destroyed after the MessageLoop has been destroyed.
117 message_loop.reset(); 146 message_loop.reset();
118 context_ = nullptr; 147 context_ = nullptr;
119 } 148 }
120 149
121 private: 150 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
122 // 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
123 // background thread. 162 // background thread.
124 MojoMessageLoop* message_loop_ = nullptr; 163 MojoMessageLoop* message_loop_ = nullptr;
125 // Created in Run() on the background thread. 164 // Created in Run() on the background thread.
126 Context* context_ = nullptr; 165 Context* context_ = nullptr;
127 166
128 std::unique_ptr<BackgroundShell::InitParams> init_params_; 167 std::unique_ptr<BackgroundShell::InitParams> init_params_;
129 168
130 DISALLOW_COPY_AND_ASSIGN(MojoThread); 169 DISALLOW_COPY_AND_ASSIGN(MojoThread);
131 }; 170 };
(...skipping 15 matching lines...) Expand all
147 186
148 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( 187 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest(
149 const std::string& name) { 188 const std::string& name) {
150 std::unique_ptr<ConnectParams> params(new ConnectParams); 189 std::unique_ptr<ConnectParams> params(new ConnectParams);
151 params->set_source(CreateShellIdentity()); 190 params->set_source(CreateShellIdentity());
152 params->set_target(Identity(name, mojom::kRootUserID)); 191 params->set_target(Identity(name, mojom::kRootUserID));
153 mojom::ShellClientRequest request; 192 mojom::ShellClientRequest request;
154 base::WaitableEvent signal(true, false); 193 base::WaitableEvent signal(true, false);
155 thread_->message_loop()->task_runner()->PostTask( 194 thread_->message_loop()->task_runner()->PostTask(
156 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, 195 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest,
157 base::Unretained(thread_.get()), &signal, name, 196 base::Unretained(thread_.get()), &signal,
158 &request)); 197 base::Passed(&params), &request));
159 signal.Wait(); 198 signal.Wait();
160 thread_->message_loop()->task_runner()->PostTask(
161 FROM_HERE, base::Bind(&MojoThread::Connect,
162 base::Unretained(thread_.get()),
163 base::Passed(&params)));
164 return request; 199 return request;
165 } 200 }
166 201
167 void BackgroundShell::ExecuteOnShellThread( 202 void BackgroundShell::ExecuteOnShellThread(
168 const ShellThreadCallback& callback) { 203 const ShellThreadCallback& callback) {
169 thread_->message_loop()->task_runner()->PostTask( 204 thread_->message_loop()->task_runner()->PostTask(
170 FROM_HERE, base::Bind(&MojoThread::RunShellCallback, 205 FROM_HERE, base::Bind(&MojoThread::RunShellCallback,
171 base::Unretained(thread_.get()), callback)); 206 base::Unretained(thread_.get()), callback));
172 } 207 }
173 208
174 } // namespace shell 209 } // namespace shell
OLDNEW
« no previous file with comments | « services/shell/BUILD.gn ('k') | services/shell/loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698