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