| OLD | NEW |
| (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 "services/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/memory/ptr_util.h" | |
| 12 #include "base/message_loop/message_pump_default.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/synchronization/waitable_event.h" | |
| 17 #include "base/threading/simple_thread.h" | |
| 18 #include "services/catalog/store.h" | |
| 19 #include "services/shell/connect_params.h" | |
| 20 #include "services/shell/public/cpp/service.h" | |
| 21 #include "services/shell/public/cpp/service_context.h" | |
| 22 #include "services/shell/service_manager.h" | |
| 23 #include "services/shell/standalone/context.h" | |
| 24 | |
| 25 namespace shell { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 std::unique_ptr<base::MessagePump> CreateDefaultMessagePump() { | |
| 30 return base::WrapUnique(new base::MessagePumpDefault); | |
| 31 } | |
| 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 BackgroundShell::MojoThread : public base::SimpleThread { | |
| 50 public: | |
| 51 explicit MojoThread(std::unique_ptr<BackgroundShell::InitParams> init_params) | |
| 52 : SimpleThread("mojo-background-shell"), | |
| 53 init_params_(std::move(init_params)) {} | |
| 54 ~MojoThread() override {} | |
| 55 | |
| 56 void CreateServiceRequest(base::WaitableEvent* signal, | |
| 57 const std::string& name, | |
| 58 mojom::ServiceRequest* request) { | |
| 59 // Only valid to call this on the background thread. | |
| 60 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | |
| 61 *request = context_->service_manager()->StartEmbedderService(name); | |
| 62 signal->Signal(); | |
| 63 } | |
| 64 | |
| 65 void Connect(std::unique_ptr<ConnectParams> params) { | |
| 66 context_->service_manager()->Connect(std::move(params)); | |
| 67 } | |
| 68 | |
| 69 base::MessageLoop* message_loop() { return message_loop_; } | |
| 70 | |
| 71 // Stops the background thread. | |
| 72 void Stop() { | |
| 73 DCHECK_NE(message_loop_, base::MessageLoop::current()); | |
| 74 message_loop_->task_runner()->PostTask( | |
| 75 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
| 76 Join(); | |
| 77 } | |
| 78 | |
| 79 void RunServiceManagerCallback( | |
| 80 const BackgroundShell::ServiceManagerThreadCallback& callback) { | |
| 81 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | |
| 82 callback.Run(context_->service_manager()); | |
| 83 } | |
| 84 | |
| 85 // base::SimpleThread: | |
| 86 void Start() override { | |
| 87 DCHECK(!message_loop_); | |
| 88 message_loop_ = new MojoMessageLoop; | |
| 89 base::SimpleThread::Start(); | |
| 90 } | |
| 91 void Run() override { | |
| 92 // The construction/destruction order is very finicky and has to be done | |
| 93 // in the order here. | |
| 94 std::unique_ptr<base::MessageLoop> message_loop(message_loop_); | |
| 95 | |
| 96 std::unique_ptr<Context::InitParams> context_init_params( | |
| 97 new Context::InitParams); | |
| 98 if (init_params_) { | |
| 99 context_init_params->catalog_store = | |
| 100 std::move(init_params_->catalog_store); | |
| 101 context_init_params->native_runner_delegate = | |
| 102 init_params_->native_runner_delegate; | |
| 103 context_init_params->init_edk = init_params_->init_edk; | |
| 104 } | |
| 105 if (context_init_params->init_edk) | |
| 106 Context::EnsureEmbedderIsInitialized(); | |
| 107 | |
| 108 message_loop_->BindToCurrentThread(); | |
| 109 | |
| 110 std::unique_ptr<Context> context(new Context); | |
| 111 context_ = context.get(); | |
| 112 context_->Init(std::move(context_init_params)); | |
| 113 | |
| 114 base::RunLoop().Run(); | |
| 115 | |
| 116 // Has to happen after run, but while messageloop still valid. | |
| 117 context_->Shutdown(); | |
| 118 | |
| 119 // Context has to be destroyed after the MessageLoop has been destroyed. | |
| 120 message_loop.reset(); | |
| 121 context_ = nullptr; | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 // We own this. It's created on the main thread, but destroyed on the | |
| 126 // background thread. | |
| 127 MojoMessageLoop* message_loop_ = nullptr; | |
| 128 // Created in Run() on the background thread. | |
| 129 Context* context_ = nullptr; | |
| 130 | |
| 131 std::unique_ptr<BackgroundShell::InitParams> init_params_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(MojoThread); | |
| 134 }; | |
| 135 | |
| 136 BackgroundShell::InitParams::InitParams() {} | |
| 137 BackgroundShell::InitParams::~InitParams() {} | |
| 138 | |
| 139 BackgroundShell::BackgroundShell() {} | |
| 140 | |
| 141 BackgroundShell::~BackgroundShell() { | |
| 142 thread_->Stop(); | |
| 143 } | |
| 144 | |
| 145 void BackgroundShell::Init(std::unique_ptr<InitParams> init_params) { | |
| 146 DCHECK(!thread_); | |
| 147 thread_.reset(new MojoThread(std::move(init_params))); | |
| 148 thread_->Start(); | |
| 149 } | |
| 150 | |
| 151 mojom::ServiceRequest BackgroundShell::CreateServiceRequest( | |
| 152 const std::string& name) { | |
| 153 std::unique_ptr<ConnectParams> params(new ConnectParams); | |
| 154 params->set_source(CreateServiceManagerIdentity()); | |
| 155 params->set_target(Identity(name, mojom::kRootUserID)); | |
| 156 mojom::ServiceRequest request; | |
| 157 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 158 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 159 thread_->message_loop()->task_runner()->PostTask( | |
| 160 FROM_HERE, | |
| 161 base::Bind(&MojoThread::CreateServiceRequest, | |
| 162 base::Unretained(thread_.get()), &signal, name, &request)); | |
| 163 signal.Wait(); | |
| 164 thread_->message_loop()->task_runner()->PostTask( | |
| 165 FROM_HERE, | |
| 166 base::Bind(&MojoThread::Connect, base::Unretained(thread_.get()), | |
| 167 base::Passed(¶ms))); | |
| 168 return request; | |
| 169 } | |
| 170 | |
| 171 void BackgroundShell::ExecuteOnServiceManagerThread( | |
| 172 const ServiceManagerThreadCallback& callback) { | |
| 173 thread_->message_loop()->task_runner()->PostTask( | |
| 174 FROM_HERE, base::Bind(&MojoThread::RunServiceManagerCallback, | |
| 175 base::Unretained(thread_.get()), callback)); | |
| 176 } | |
| 177 | |
| 178 } // namespace shell | |
| OLD | NEW |