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