OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/application/content_handler_factory.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/threading/platform_thread.h" |
| 12 #include "mojo/application/application_runner_chromium.h" |
| 13 #include "mojo/common/message_pump_mojo.h" |
| 14 #include "mojo/public/cpp/application/application_connection.h" |
| 15 #include "mojo/public/cpp/application/application_delegate.h" |
| 16 #include "mojo/public/cpp/application/application_impl.h" |
| 17 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 18 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 19 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" |
| 20 |
| 21 namespace mojo { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class ApplicationThread : public base::PlatformThread::Delegate { |
| 26 public: |
| 27 ApplicationThread( |
| 28 scoped_refptr<base::MessageLoopProxy> handler_thread, |
| 29 const base::Callback<void(ApplicationThread*)>& termination_callback, |
| 30 ContentHandlerFactory::Delegate* handler_delegate, |
| 31 ShellPtr shell, |
| 32 URLResponsePtr response) |
| 33 : handler_thread_(handler_thread), |
| 34 termination_callback_(termination_callback), |
| 35 handler_delegate_(handler_delegate), |
| 36 shell_(shell.Pass()), |
| 37 response_(response.Pass()) {} |
| 38 |
| 39 private: |
| 40 void ThreadMain() override { |
| 41 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 42 auto application = |
| 43 handler_delegate_->CreateApplication(shell_.Pass(), response_.Pass()); |
| 44 loop.Run(); |
| 45 handler_thread_->PostTask(FROM_HERE, |
| 46 base::Bind(termination_callback_, this)); |
| 47 } |
| 48 |
| 49 scoped_refptr<base::MessageLoopProxy> handler_thread_; |
| 50 base::Callback<void(ApplicationThread*)> termination_callback_; |
| 51 ContentHandlerFactory::Delegate* handler_delegate_; |
| 52 ShellPtr shell_; |
| 53 URLResponsePtr response_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
| 56 }; |
| 57 |
| 58 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
| 59 public: |
| 60 explicit ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate) |
| 61 : delegate_(delegate) {} |
| 62 ~ContentHandlerImpl() { |
| 63 for (auto thread : active_threads_) { |
| 64 base::PlatformThread::Join(thread.second); |
| 65 delete thread.first; |
| 66 } |
| 67 } |
| 68 |
| 69 private: |
| 70 // Overridden from ContentHandler: |
| 71 virtual void StartApplication(ShellPtr shell, |
| 72 URLResponsePtr response) override { |
| 73 ApplicationThread* thread = new ApplicationThread( |
| 74 base::MessageLoopProxy::current(), |
| 75 base::Bind(&ContentHandlerImpl::OnThreadEnd, base::Unretained(this)), |
| 76 delegate_, |
| 77 shell.Pass(), |
| 78 response.Pass()); |
| 79 base::PlatformThreadHandle handle; |
| 80 bool launched = base::PlatformThread::Create(0, thread, &handle); |
| 81 DCHECK(launched); |
| 82 active_threads_[thread] = handle; |
| 83 } |
| 84 |
| 85 void OnThreadEnd(ApplicationThread* thread) { |
| 86 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
| 87 base::PlatformThreadHandle handle = active_threads_[thread]; |
| 88 active_threads_.erase(thread); |
| 89 base::PlatformThread::Join(handle); |
| 90 delete thread; |
| 91 if (!active_threads_.size()) { |
| 92 ApplicationImpl::Terminate(); |
| 93 } |
| 94 } |
| 95 |
| 96 ContentHandlerFactory::Delegate* delegate_; |
| 97 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| 100 }; |
| 101 |
| 102 } // namespace |
| 103 |
| 104 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) |
| 105 : delegate_(delegate) { |
| 106 } |
| 107 |
| 108 ContentHandlerFactory::~ContentHandlerFactory() { |
| 109 } |
| 110 |
| 111 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
| 112 InterfaceRequest<ContentHandler> request) { |
| 113 BindToRequest(new ContentHandlerImpl(delegate_), &request); |
| 114 } |
| 115 |
| 116 } // namespace mojo |
OLD | NEW |