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/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" | |
13 #include "base/message_loop/message_pump_default.h" | 12 #include "base/message_loop/message_pump_default.h" |
14 #include "base/path_service.h" | 13 #include "base/path_service.h" |
15 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
16 #include "base/sequenced_task_runner.h" | |
17 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
18 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
19 #include "base/threading/simple_thread.h" | 17 #include "base/threading/simple_thread.h" |
20 #include "services/catalog/store.h" | 18 #include "services/catalog/store.h" |
21 #include "services/service_manager/connect_params.h" | 19 #include "services/service_manager/connect_params.h" |
22 #include "services/service_manager/public/cpp/service.h" | 20 #include "services/service_manager/public/cpp/service.h" |
23 #include "services/service_manager/public/cpp/service_context.h" | 21 #include "services/service_manager/public/cpp/service_context.h" |
24 #include "services/service_manager/service_manager.h" | 22 #include "services/service_manager/service_manager.h" |
25 #include "services/service_manager/standalone/context.h" | 23 #include "services/service_manager/standalone/context.h" |
26 | 24 |
27 namespace service_manager { | 25 namespace service_manager { |
28 | 26 |
29 BackgroundServiceManager::BackgroundServiceManager( | 27 namespace { |
30 service_manager::ServiceProcessLauncher::Delegate* launcher_delegate, | 28 |
31 std::unique_ptr<base::Value> catalog_contents) | 29 std::unique_ptr<base::MessagePump> CreateDefaultMessagePump() { |
32 : background_thread_("service_manager") { | 30 return base::WrapUnique(new base::MessagePumpDefault); |
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))); | |
39 } | 31 } |
40 | 32 |
| 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 |
41 BackgroundServiceManager::~BackgroundServiceManager() { | 140 BackgroundServiceManager::~BackgroundServiceManager() { |
42 base::WaitableEvent done_event( | 141 thread_->Stop(); |
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_); | |
51 } | 142 } |
52 | 143 |
53 void BackgroundServiceManager::RegisterService( | 144 void BackgroundServiceManager::Init(std::unique_ptr<InitParams> init_params) { |
54 const Identity& identity, | 145 DCHECK(!thread_); |
55 mojom::ServicePtr service, | 146 thread_.reset(new MojoThread(std::move(init_params))); |
56 mojom::PIDReceiverRequest pid_receiver_request) { | 147 thread_->Start(); |
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))); | |
63 } | 148 } |
64 | 149 |
65 void BackgroundServiceManager::InitializeOnBackgroundThread( | 150 mojom::ServiceRequest BackgroundServiceManager::CreateServiceRequest( |
66 service_manager::ServiceProcessLauncher::Delegate* launcher_delegate, | 151 const std::string& name) { |
67 std::unique_ptr<base::Value> catalog_contents) { | 152 std::unique_ptr<ConnectParams> params(new ConnectParams); |
68 context_ = | 153 params->set_source(CreateServiceManagerIdentity()); |
69 base::MakeUnique<Context>(launcher_delegate, std::move(catalog_contents)); | 154 params->set_target(Identity(name, mojom::kRootUserID)); |
| 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(¶ms))); |
| 167 return request; |
70 } | 168 } |
71 | 169 |
72 void BackgroundServiceManager::ShutDownOnBackgroundThread( | 170 void BackgroundServiceManager::ExecuteOnServiceManagerThread( |
73 base::WaitableEvent* done_event) { | 171 const ServiceManagerThreadCallback& callback) { |
74 context_.reset(); | 172 thread_->message_loop()->task_runner()->PostTask( |
75 done_event->Signal(); | 173 FROM_HERE, base::Bind(&MojoThread::RunServiceManagerCallback, |
76 } | 174 base::Unretained(thread_.get()), callback)); |
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)); | |
86 } | 175 } |
87 | 176 |
88 } // namespace service_manager | 177 } // namespace service_manager |
OLD | NEW |