| 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_shell.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/message_loop/message_pump_default.h" | 12 #include "base/message_loop/message_pump_default.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } | 40 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); | 43 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 // Manages the thread to startup mojo. | 48 // Manages the thread to startup mojo. |
| 49 class BackgroundShell::MojoThread : public base::SimpleThread { | 49 class BackgroundServiceManager::MojoThread : public base::SimpleThread { |
| 50 public: | 50 public: |
| 51 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) | 51 explicit MojoThread( |
| 52 : SimpleThread("mojo-background-shell"), | 52 std::unique_ptr<BackgroundServiceManager::InitParams> init_params) |
| 53 : SimpleThread("background-service-manager"), |
| 53 init_params_(std::move(init_params)) {} | 54 init_params_(std::move(init_params)) {} |
| 54 ~MojoThread() override {} | 55 ~MojoThread() override {} |
| 55 | 56 |
| 56 void CreateServiceRequest(base::WaitableEvent* signal, | 57 void CreateServiceRequest(base::WaitableEvent* signal, |
| 57 const std::string& name, | 58 const std::string& name, |
| 58 mojom::ServiceRequest* request) { | 59 mojom::ServiceRequest* request) { |
| 59 // Only valid to call this on the background thread. | 60 // Only valid to call this on the background thread. |
| 60 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 61 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 61 *request = context_->service_manager()->StartEmbedderService(name); | 62 *request = context_->service_manager()->StartEmbedderService(name); |
| 62 signal->Signal(); | 63 signal->Signal(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void Connect(std::unique_ptr<ConnectParams> params) { | 66 void Connect(std::unique_ptr<ConnectParams> params) { |
| 66 context_->service_manager()->Connect(std::move(params)); | 67 context_->service_manager()->Connect(std::move(params)); |
| 67 } | 68 } |
| 68 | 69 |
| 69 base::MessageLoop* message_loop() { return message_loop_; } | 70 base::MessageLoop* message_loop() { return message_loop_; } |
| 70 | 71 |
| 71 // Stops the background thread. | 72 // Stops the background thread. |
| 72 void Stop() { | 73 void Stop() { |
| 73 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 74 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
| 74 message_loop_->task_runner()->PostTask( | 75 message_loop_->task_runner()->PostTask( |
| 75 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 76 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 76 Join(); | 77 Join(); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void RunServiceManagerCallback( | 80 void RunServiceManagerCallback( |
| 80 const BackgroundShell::ServiceManagerThreadCallback& callback) { | 81 const BackgroundServiceManager::ServiceManagerThreadCallback& callback) { |
| 81 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 82 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 82 callback.Run(context_->service_manager()); | 83 callback.Run(context_->service_manager()); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // base::SimpleThread: | 86 // base::SimpleThread: |
| 86 void Start() override { | 87 void Start() override { |
| 87 DCHECK(!message_loop_); | 88 DCHECK(!message_loop_); |
| 88 message_loop_ = new MojoMessageLoop; | 89 message_loop_ = new MojoMessageLoop; |
| 89 base::SimpleThread::Start(); | 90 base::SimpleThread::Start(); |
| 90 } | 91 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 121 context_ = nullptr; | 122 context_ = nullptr; |
| 122 } | 123 } |
| 123 | 124 |
| 124 private: | 125 private: |
| 125 // We own this. It's created on the main thread, but destroyed on the | 126 // We own this. It's created on the main thread, but destroyed on the |
| 126 // background thread. | 127 // background thread. |
| 127 MojoMessageLoop* message_loop_ = nullptr; | 128 MojoMessageLoop* message_loop_ = nullptr; |
| 128 // Created in Run() on the background thread. | 129 // Created in Run() on the background thread. |
| 129 Context* context_ = nullptr; | 130 Context* context_ = nullptr; |
| 130 | 131 |
| 131 std::unique_ptr<BackgroundShell::InitParams> init_params_; | 132 std::unique_ptr<BackgroundServiceManager::InitParams> init_params_; |
| 132 | 133 |
| 133 DISALLOW_COPY_AND_ASSIGN(MojoThread); | 134 DISALLOW_COPY_AND_ASSIGN(MojoThread); |
| 134 }; | 135 }; |
| 135 | 136 |
| 136 BackgroundShell::InitParams::InitParams() {} | 137 BackgroundServiceManager::InitParams::InitParams() {} |
| 137 BackgroundShell::InitParams::~InitParams() {} | 138 BackgroundServiceManager::InitParams::~InitParams() {} |
| 138 | 139 |
| 139 BackgroundShell::BackgroundShell() {} | 140 BackgroundServiceManager::BackgroundServiceManager() {} |
| 140 | 141 |
| 141 BackgroundShell::~BackgroundShell() { | 142 BackgroundServiceManager::~BackgroundServiceManager() { |
| 142 thread_->Stop(); | 143 thread_->Stop(); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void BackgroundShell::Init(std::unique_ptr<InitParams> init_params) { | 146 void BackgroundServiceManager::Init(std::unique_ptr<InitParams> init_params) { |
| 146 DCHECK(!thread_); | 147 DCHECK(!thread_); |
| 147 thread_.reset(new MojoThread(std::move(init_params))); | 148 thread_.reset(new MojoThread(std::move(init_params))); |
| 148 thread_->Start(); | 149 thread_->Start(); |
| 149 } | 150 } |
| 150 | 151 |
| 151 mojom::ServiceRequest BackgroundShell::CreateServiceRequest( | 152 mojom::ServiceRequest BackgroundServiceManager::CreateServiceRequest( |
| 152 const std::string& name) { | 153 const std::string& name) { |
| 153 std::unique_ptr<ConnectParams> params(new ConnectParams); | 154 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 154 params->set_source(CreateServiceManagerIdentity()); | 155 params->set_source(CreateServiceManagerIdentity()); |
| 155 params->set_target(Identity(name, mojom::kRootUserID)); | 156 params->set_target(Identity(name, mojom::kRootUserID)); |
| 156 mojom::ServiceRequest request; | 157 mojom::ServiceRequest request; |
| 157 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | 158 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, |
| 158 base::WaitableEvent::InitialState::NOT_SIGNALED); | 159 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 159 thread_->message_loop()->task_runner()->PostTask( | 160 thread_->message_loop()->task_runner()->PostTask( |
| 160 FROM_HERE, | 161 FROM_HERE, |
| 161 base::Bind(&MojoThread::CreateServiceRequest, | 162 base::Bind(&MojoThread::CreateServiceRequest, |
| 162 base::Unretained(thread_.get()), &signal, name, &request)); | 163 base::Unretained(thread_.get()), &signal, name, &request)); |
| 163 signal.Wait(); | 164 signal.Wait(); |
| 164 thread_->message_loop()->task_runner()->PostTask( | 165 thread_->message_loop()->task_runner()->PostTask( |
| 165 FROM_HERE, | 166 FROM_HERE, |
| 166 base::Bind(&MojoThread::Connect, base::Unretained(thread_.get()), | 167 base::Bind(&MojoThread::Connect, base::Unretained(thread_.get()), |
| 167 base::Passed(¶ms))); | 168 base::Passed(¶ms))); |
| 168 return request; | 169 return request; |
| 169 } | 170 } |
| 170 | 171 |
| 171 void BackgroundShell::ExecuteOnServiceManagerThread( | 172 void BackgroundServiceManager::ExecuteOnServiceManagerThread( |
| 172 const ServiceManagerThreadCallback& callback) { | 173 const ServiceManagerThreadCallback& callback) { |
| 173 thread_->message_loop()->task_runner()->PostTask( | 174 thread_->message_loop()->task_runner()->PostTask( |
| 174 FROM_HERE, base::Bind(&MojoThread::RunServiceManagerCallback, | 175 FROM_HERE, base::Bind(&MojoThread::RunServiceManagerCallback, |
| 175 base::Unretained(thread_.get()), callback)); | 176 base::Unretained(thread_.get()), callback)); |
| 176 } | 177 } |
| 177 | 178 |
| 178 } // namespace service_manager | 179 } // namespace service_manager |
| OLD | NEW |