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

Side by Side Diff: services/service_manager/background/background_service_manager.cc

Issue 2645973006: [Service Manager] Get rid of dynamic service discovery (Closed)
Patch Set: . Created 3 years, 11 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
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/service_manager/background/background_service_manager.h" 5 #include "services/service_manager/background/background_service_manager.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/memory/ref_counted.h"
12 #include "base/message_loop/message_pump_default.h" 13 #include "base/message_loop/message_pump_default.h"
13 #include "base/path_service.h" 14 #include "base/path_service.h"
14 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
16 #include "base/synchronization/waitable_event.h" 18 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/simple_thread.h" 19 #include "base/threading/simple_thread.h"
18 #include "services/catalog/store.h" 20 #include "services/catalog/store.h"
19 #include "services/service_manager/connect_params.h" 21 #include "services/service_manager/connect_params.h"
20 #include "services/service_manager/public/cpp/service.h" 22 #include "services/service_manager/public/cpp/service.h"
21 #include "services/service_manager/public/cpp/service_context.h" 23 #include "services/service_manager/public/cpp/service_context.h"
22 #include "services/service_manager/service_manager.h" 24 #include "services/service_manager/service_manager.h"
23 #include "services/service_manager/standalone/context.h" 25 #include "services/service_manager/standalone/context.h"
24 26
25 namespace service_manager { 27 namespace service_manager {
26 28
27 namespace { 29 BackgroundServiceManager::BackgroundServiceManager(
28 30 service_manager::ServiceProcessLauncher::Delegate* launcher_delegate,
29 std::unique_ptr<base::MessagePump> CreateDefaultMessagePump() { 31 std::unique_ptr<base::Value> catalog_contents)
30 return base::WrapUnique(new base::MessagePumpDefault); 32 : background_thread_("service_manager") {
33 background_thread_.Start();
34 background_thread_.task_runner()->PostTask(
35 FROM_HERE,
36 base::Bind(&BackgroundServiceManager::InitializeOnBackgroundThread,
37 base::Unretained(this), launcher_delegate,
38 base::Passed(&catalog_contents)));
31 } 39 }
32 40
33 class MojoMessageLoop : public base::MessageLoop {
34 public:
35 MojoMessageLoop()
36 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM,
37 base::Bind(&CreateDefaultMessagePump)) {}
38 ~MojoMessageLoop() override {}
39
40 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); }
41
42 private:
43 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop);
44 };
45
46 } // namespace
47
48 // Manages the thread to startup mojo.
49 class BackgroundServiceManager::MojoThread : public base::SimpleThread {
50 public:
51 explicit MojoThread(
52 std::unique_ptr<BackgroundServiceManager::InitParams> init_params)
53 : SimpleThread("background-service-manager"),
54 init_params_(std::move(init_params)) {}
55 ~MojoThread() override {}
56
57 void CreateServiceRequest(base::WaitableEvent* signal,
58 const std::string& name,
59 mojom::ServiceRequest* request) {
60 // Only valid to call this on the background thread.
61 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread());
62 *request = context_->service_manager()->StartEmbedderService(name);
63 signal->Signal();
64 }
65
66 void Connect(std::unique_ptr<ConnectParams> params) {
67 context_->service_manager()->Connect(std::move(params));
68 }
69
70 base::MessageLoop* message_loop() { return message_loop_; }
71
72 // Stops the background thread.
73 void Stop() {
74 DCHECK_NE(message_loop_, base::MessageLoop::current());
75 message_loop_->task_runner()->PostTask(
76 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
77 Join();
78 }
79
80 void RunServiceManagerCallback(
81 const BackgroundServiceManager::ServiceManagerThreadCallback& callback) {
82 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread());
83 callback.Run(context_->service_manager());
84 }
85
86 // base::SimpleThread:
87 void Start() override {
88 DCHECK(!message_loop_);
89 message_loop_ = new MojoMessageLoop;
90 base::SimpleThread::Start();
91 }
92 void Run() override {
93 // The construction/destruction order is very finicky and has to be done
94 // in the order here.
95 std::unique_ptr<base::MessageLoop> message_loop(message_loop_);
96
97 std::unique_ptr<Context::InitParams> context_init_params(
98 new Context::InitParams);
99 if (init_params_) {
100 context_init_params->service_process_launcher_delegate =
101 init_params_->service_process_launcher_delegate;
102 context_init_params->init_edk = init_params_->init_edk;
103 }
104 if (context_init_params->init_edk)
105 Context::EnsureEmbedderIsInitialized();
106
107 message_loop_->BindToCurrentThread();
108
109 std::unique_ptr<Context> context(new Context);
110 context_ = context.get();
111 context_->Init(std::move(context_init_params));
112
113 base::RunLoop().Run();
114
115 // Has to happen after run, but while messageloop still valid.
116 context_->Shutdown();
117
118 // Context has to be destroyed after the MessageLoop has been destroyed.
119 message_loop.reset();
120 context_ = nullptr;
121 }
122
123 private:
124 // We own this. It's created on the main thread, but destroyed on the
125 // background thread.
126 MojoMessageLoop* message_loop_ = nullptr;
127 // Created in Run() on the background thread.
128 Context* context_ = nullptr;
129
130 std::unique_ptr<BackgroundServiceManager::InitParams> init_params_;
131
132 DISALLOW_COPY_AND_ASSIGN(MojoThread);
133 };
134
135 BackgroundServiceManager::InitParams::InitParams() {}
136 BackgroundServiceManager::InitParams::~InitParams() {}
137
138 BackgroundServiceManager::BackgroundServiceManager() {}
139
140 BackgroundServiceManager::~BackgroundServiceManager() { 41 BackgroundServiceManager::~BackgroundServiceManager() {
141 thread_->Stop(); 42 base::WaitableEvent done_event(
43 base::WaitableEvent::ResetPolicy::MANUAL,
44 base::WaitableEvent::InitialState::NOT_SIGNALED);
45 background_thread_.task_runner()->PostTask(
46 FROM_HERE,
47 base::Bind(&BackgroundServiceManager::ShutDownOnBackgroundThread,
48 base::Unretained(this), &done_event));
49 done_event.Wait();
50 DCHECK(!context_);
142 } 51 }
143 52
144 void BackgroundServiceManager::Init(std::unique_ptr<InitParams> init_params) { 53 void BackgroundServiceManager::RegisterService(
145 DCHECK(!thread_); 54 const Identity& identity,
146 thread_.reset(new MojoThread(std::move(init_params))); 55 mojom::ServicePtr service,
147 thread_->Start(); 56 mojom::PIDReceiverRequest pid_receiver_request) {
57 mojom::ServicePtrInfo service_info = service.PassInterface();
58 background_thread_.task_runner()->PostTask(
59 FROM_HERE,
60 base::Bind(&BackgroundServiceManager::RegisterServiceOnBackgroundThread,
61 base::Unretained(this), identity, base::Passed(&service_info),
62 base::Passed(&pid_receiver_request)));
148 } 63 }
149 64
150 mojom::ServiceRequest BackgroundServiceManager::CreateServiceRequest( 65 void BackgroundServiceManager::InitializeOnBackgroundThread(
151 const std::string& name) { 66 service_manager::ServiceProcessLauncher::Delegate* launcher_delegate,
152 std::unique_ptr<ConnectParams> params(new ConnectParams); 67 std::unique_ptr<base::Value> catalog_contents) {
153 params->set_source(CreateServiceManagerIdentity()); 68 context_ =
154 params->set_target(Identity(name, mojom::kRootUserID)); 69 base::MakeUnique<Context>(launcher_delegate, std::move(catalog_contents));
155 mojom::ServiceRequest request;
156 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL,
157 base::WaitableEvent::InitialState::NOT_SIGNALED);
158 thread_->message_loop()->task_runner()->PostTask(
159 FROM_HERE,
160 base::Bind(&MojoThread::CreateServiceRequest,
161 base::Unretained(thread_.get()), &signal, name, &request));
162 signal.Wait();
163 thread_->message_loop()->task_runner()->PostTask(
164 FROM_HERE,
165 base::Bind(&MojoThread::Connect, base::Unretained(thread_.get()),
166 base::Passed(&params)));
167 return request;
168 } 70 }
169 71
170 void BackgroundServiceManager::ExecuteOnServiceManagerThread( 72 void BackgroundServiceManager::ShutDownOnBackgroundThread(
171 const ServiceManagerThreadCallback& callback) { 73 base::WaitableEvent* done_event) {
172 thread_->message_loop()->task_runner()->PostTask( 74 context_.reset();
173 FROM_HERE, base::Bind(&MojoThread::RunServiceManagerCallback, 75 done_event->Signal();
174 base::Unretained(thread_.get()), callback)); 76 }
77
78 void BackgroundServiceManager::RegisterServiceOnBackgroundThread(
79 const Identity& identity,
80 mojom::ServicePtrInfo service_info,
81 mojom::PIDReceiverRequest pid_receiver_request) {
82 mojom::ServicePtr service;
83 service.Bind(std::move(service_info));
84 context_->service_manager()->RegisterService(
85 identity, std::move(service), std::move(pid_receiver_request));
175 } 86 }
176 87
177 } // namespace service_manager 88 } // namespace service_manager
OLDNEW
« no previous file with comments | « services/service_manager/background/background_service_manager.h ('k') | services/service_manager/background/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698