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 "mojo/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/path_service.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/simple_thread.h" |
| 14 #include "mojo/message_pump/message_pump_mojo.h" |
| 15 #include "mojo/shell/application_loader.h" |
| 16 #include "mojo/shell/application_manager.h" |
| 17 #include "mojo/shell/capability_filter.h" |
| 18 #include "mojo/shell/connect_to_application_params.h" |
| 19 #include "mojo/shell/public/cpp/application_impl.h" |
| 20 #include "mojo/shell/public/cpp/shell_client.h" |
| 21 #include "mojo/shell/standalone/context.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace mojo { |
| 25 namespace shell { |
| 26 namespace { |
| 27 |
| 28 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { |
| 29 return make_scoped_ptr(new common::MessagePumpMojo); |
| 30 } |
| 31 |
| 32 // Used to obtain the InterfaceRequest for an application. |
| 33 class BackgroundApplicationLoader : public ApplicationLoader { |
| 34 public: |
| 35 BackgroundApplicationLoader() {} |
| 36 ~BackgroundApplicationLoader() override {} |
| 37 |
| 38 bool got_request() const { return got_request_; } |
| 39 InterfaceRequest<mojom::Application> TakeApplicationRequest() { |
| 40 return std::move(application_request_); |
| 41 } |
| 42 |
| 43 // ApplicationLoader: |
| 44 void Load(const GURL& url, |
| 45 InterfaceRequest<mojom::Application> application_request) override { |
| 46 got_request_ = true; |
| 47 application_request_ = std::move(application_request); |
| 48 } |
| 49 |
| 50 private: |
| 51 bool got_request_ = false; |
| 52 InterfaceRequest<mojom::Application> application_request_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationLoader); |
| 55 }; |
| 56 |
| 57 class MojoMessageLoop : public base::MessageLoop { |
| 58 public: |
| 59 MojoMessageLoop() |
| 60 : base::MessageLoop(base::MessageLoop::TYPE_CUSTOM, |
| 61 base::Bind(&CreateMessagePumpMojo)) {} |
| 62 ~MojoMessageLoop() override {} |
| 63 |
| 64 void BindToCurrentThread() { base::MessageLoop::BindToCurrentThread(); } |
| 65 |
| 66 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(MojoMessageLoop); |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 72 // Manages the thread to startup mojo. |
| 73 class BackgroundShell::MojoThread : public base::SimpleThread { |
| 74 public: |
| 75 MojoThread() : SimpleThread("mojo-background-shell") {} |
| 76 ~MojoThread() override {} |
| 77 |
| 78 void CreateApplicationImpl(base::WaitableEvent* signal, |
| 79 scoped_ptr<ConnectToApplicationParams> params, |
| 80 InterfaceRequest<mojom::Application>* request) { |
| 81 // Only valid to call this on the background thread. |
| 82 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 83 |
| 84 // Ownership of |loader| passes to ApplicationManager. |
| 85 BackgroundApplicationLoader* loader = new BackgroundApplicationLoader; |
| 86 const GURL url = params->target().url(); |
| 87 context_->application_manager()->SetLoaderForURL(make_scoped_ptr(loader), |
| 88 url); |
| 89 context_->application_manager()->ConnectToApplication(std::move(params)); |
| 90 DCHECK(loader->got_request()); |
| 91 *request = loader->TakeApplicationRequest(); |
| 92 // Trigger destruction of the loader. |
| 93 context_->application_manager()->SetLoaderForURL(nullptr, url); |
| 94 signal->Signal(); |
| 95 } |
| 96 |
| 97 base::MessageLoop* message_loop() { return message_loop_; } |
| 98 |
| 99 // Stops the background thread. |
| 100 void Stop() { |
| 101 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
| 102 message_loop_->task_runner()->PostTask( |
| 103 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 104 Join(); |
| 105 } |
| 106 |
| 107 // base::SimpleThread: |
| 108 void Start() override { |
| 109 DCHECK(!message_loop_); |
| 110 message_loop_ = new MojoMessageLoop; |
| 111 base::SimpleThread::Start(); |
| 112 } |
| 113 void Run() override { |
| 114 // The construction/destruction order is very finicky and has to be done |
| 115 // in the order here. |
| 116 scoped_ptr<base::MessageLoop> message_loop(message_loop_); |
| 117 |
| 118 Context::EnsureEmbedderIsInitialized(); |
| 119 |
| 120 message_loop_->BindToCurrentThread(); |
| 121 |
| 122 base::FilePath shell_dir; |
| 123 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 124 |
| 125 scoped_ptr<Context> context(new Context); |
| 126 context_ = context.get(); |
| 127 context_->Init(shell_dir); |
| 128 |
| 129 message_loop_->Run(); |
| 130 |
| 131 // Has to happen after run, but while messageloop still valid. |
| 132 context_->Shutdown(); |
| 133 |
| 134 // Context has to be destroyed after the MessageLoop has been destroyed. |
| 135 message_loop.reset(); |
| 136 context_ = nullptr; |
| 137 } |
| 138 |
| 139 private: |
| 140 // We own this. It's created on the main thread, but destroyed on the |
| 141 // background thread. |
| 142 MojoMessageLoop* message_loop_ = nullptr; |
| 143 // Created in Run() on the background thread. |
| 144 Context* context_ = nullptr; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(MojoThread); |
| 147 }; |
| 148 |
| 149 BackgroundShell::BackgroundShell() {} |
| 150 |
| 151 BackgroundShell::~BackgroundShell() { |
| 152 thread_->Stop(); |
| 153 } |
| 154 |
| 155 void BackgroundShell::Init() { |
| 156 DCHECK(!thread_); |
| 157 thread_.reset(new MojoThread); |
| 158 thread_->Start(); |
| 159 } |
| 160 |
| 161 InterfaceRequest<mojom::Application> BackgroundShell::CreateApplication( |
| 162 const GURL& url) { |
| 163 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); |
| 164 params->SetTarget( |
| 165 Identity(url, std::string(), GetPermissiveCapabilityFilter())); |
| 166 InterfaceRequest<mojom::Application> request; |
| 167 base::WaitableEvent signal(true, false); |
| 168 thread_->message_loop()->task_runner()->PostTask( |
| 169 FROM_HERE, base::Bind(&MojoThread::CreateApplicationImpl, |
| 170 base::Unretained(thread_.get()), &signal, |
| 171 base::Passed(¶ms), &request)); |
| 172 signal.Wait(); |
| 173 return request; |
| 174 } |
| 175 |
| 176 void RegisterLocalAliases(PackageManagerImpl* manager) {} |
| 177 |
| 178 } // namespace shell |
| 179 } // namespace mojo |
OLD | NEW |