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/memory/weak_ptr.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "mojo/application/application_runner_chromium.h" |
| 14 #include "mojo/common/message_pump_mojo.h" |
| 15 #include "mojo/public/cpp/application/application_connection.h" |
| 16 #include "mojo/public/cpp/application/application_delegate.h" |
| 17 #include "mojo/public/cpp/application/application_impl.h" |
| 18 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 19 #include "mojo/public/cpp/bindings/strong_binding.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 InterfaceRequest<Application> application_request, |
| 32 URLResponsePtr response) |
| 33 : handler_thread_(handler_thread), |
| 34 termination_callback_(termination_callback), |
| 35 handler_delegate_(handler_delegate), |
| 36 application_request_(application_request.Pass()), |
| 37 response_(response.Pass()) {} |
| 38 |
| 39 private: |
| 40 void ThreadMain() override { |
| 41 handler_delegate_->RunApplication(application_request_.Pass(), |
| 42 response_.Pass()); |
| 43 handler_thread_->PostTask(FROM_HERE, |
| 44 base::Bind(termination_callback_, this)); |
| 45 } |
| 46 |
| 47 scoped_refptr<base::MessageLoopProxy> handler_thread_; |
| 48 base::Callback<void(ApplicationThread*)> termination_callback_; |
| 49 ContentHandlerFactory::Delegate* handler_delegate_; |
| 50 InterfaceRequest<Application> application_request_; |
| 51 URLResponsePtr response_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
| 54 }; |
| 55 |
| 56 class ContentHandlerImpl : public ContentHandler { |
| 57 public: |
| 58 ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate, |
| 59 InterfaceRequest<ContentHandler> request) |
| 60 : delegate_(delegate), |
| 61 binding_(this, request.Pass()), |
| 62 weak_factory_(this) {} |
| 63 ~ContentHandlerImpl() override { |
| 64 // We're shutting down and doing cleanup. Cleanup may trigger calls back to |
| 65 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in |
| 66 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any |
| 67 // calls to OnThreadEnd(). |
| 68 weak_factory_.InvalidateWeakPtrs(); |
| 69 for (auto thread : active_threads_) { |
| 70 base::PlatformThread::Join(thread.second); |
| 71 delete thread.first; |
| 72 } |
| 73 } |
| 74 |
| 75 private: |
| 76 // Overridden from ContentHandler: |
| 77 void StartApplication(InterfaceRequest<Application> application_request, |
| 78 URLResponsePtr response) override { |
| 79 ApplicationThread* thread = new ApplicationThread( |
| 80 base::MessageLoopProxy::current(), |
| 81 base::Bind(&ContentHandlerImpl::OnThreadEnd, |
| 82 weak_factory_.GetWeakPtr()), |
| 83 delegate_, application_request.Pass(), response.Pass()); |
| 84 base::PlatformThreadHandle handle; |
| 85 bool launched = base::PlatformThread::Create(0, thread, &handle); |
| 86 DCHECK(launched); |
| 87 active_threads_[thread] = handle; |
| 88 } |
| 89 |
| 90 void OnThreadEnd(ApplicationThread* thread) { |
| 91 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
| 92 base::PlatformThreadHandle handle = active_threads_[thread]; |
| 93 active_threads_.erase(thread); |
| 94 base::PlatformThread::Join(handle); |
| 95 delete thread; |
| 96 } |
| 97 |
| 98 ContentHandlerFactory::Delegate* delegate_; |
| 99 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; |
| 100 StrongBinding<ContentHandler> binding_; |
| 101 base::WeakPtrFactory<ContentHandlerImpl> weak_factory_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) |
| 109 : delegate_(delegate) { |
| 110 } |
| 111 |
| 112 ContentHandlerFactory::~ContentHandlerFactory() { |
| 113 } |
| 114 |
| 115 void ContentHandlerFactory::ManagedDelegate::RunApplication( |
| 116 InterfaceRequest<Application> application_request, |
| 117 URLResponsePtr response) { |
| 118 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 119 auto application = |
| 120 this->CreateApplication(application_request.Pass(), response.Pass()); |
| 121 if (application) |
| 122 loop.Run(); |
| 123 } |
| 124 |
| 125 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
| 126 InterfaceRequest<ContentHandler> request) { |
| 127 new ContentHandlerImpl(delegate_, request.Pass()); |
| 128 } |
| 129 |
| 130 } // namespace mojo |
OLD | NEW |