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