| 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/content_handler_factory.h" | 5 #include "mojo/application/content_handler_factory.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/weak_ptr.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" | 12 #include "base/threading/platform_thread.h" |
| 15 #include "mojo/application/application_runner_chromium.h" | 13 #include "mojo/application/application_runner_chromium.h" |
| 16 #include "mojo/common/message_pump_mojo.h" | 14 #include "mojo/common/message_pump_mojo.h" |
| 17 #include "mojo/public/cpp/application/application_connection.h" | 15 #include "mojo/public/cpp/application/application_connection.h" |
| 18 #include "mojo/public/cpp/application/application_delegate.h" | 16 #include "mojo/public/cpp/application/application_delegate.h" |
| 19 #include "mojo/public/cpp/application/application_impl.h" | 17 #include "mojo/public/cpp/application/application_impl.h" |
| 20 #include "mojo/public/cpp/application/interface_factory_impl.h" | 18 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 21 #include "mojo/public/cpp/bindings/strong_binding.h" | 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 22 | 20 |
| 23 namespace mojo { | 21 namespace mojo { |
| 24 | 22 |
| 25 namespace { | 23 namespace { |
| 26 | 24 |
| 27 class ApplicationThread : public base::PlatformThread::Delegate { | 25 class ApplicationThread : public base::PlatformThread::Delegate { |
| 28 public: | 26 public: |
| 29 ApplicationThread( | 27 ApplicationThread( |
| 30 scoped_refptr<base::SingleThreadTaskRunner> handler_thread, | 28 scoped_refptr<base::MessageLoopProxy> handler_thread, |
| 31 const base::Callback<void(ApplicationThread*)>& termination_callback, | 29 const base::Callback<void(ApplicationThread*)>& termination_callback, |
| 32 ContentHandlerFactory::Delegate* handler_delegate, | 30 ContentHandlerFactory::Delegate* handler_delegate, |
| 33 InterfaceRequest<Application> application_request, | 31 InterfaceRequest<Application> application_request, |
| 34 URLResponsePtr response) | 32 URLResponsePtr response) |
| 35 : handler_thread_(handler_thread), | 33 : handler_thread_(handler_thread), |
| 36 termination_callback_(termination_callback), | 34 termination_callback_(termination_callback), |
| 37 handler_delegate_(handler_delegate), | 35 handler_delegate_(handler_delegate), |
| 38 application_request_(application_request.Pass()), | 36 application_request_(application_request.Pass()), |
| 39 response_(response.Pass()) {} | 37 response_(response.Pass()) {} |
| 40 | 38 |
| 41 private: | 39 private: |
| 42 void ThreadMain() override { | 40 void ThreadMain() override { |
| 43 handler_delegate_->RunApplication(application_request_.Pass(), | 41 handler_delegate_->RunApplication(application_request_.Pass(), |
| 44 response_.Pass()); | 42 response_.Pass()); |
| 45 handler_thread_->PostTask(FROM_HERE, | 43 handler_thread_->PostTask(FROM_HERE, |
| 46 base::Bind(termination_callback_, this)); | 44 base::Bind(termination_callback_, this)); |
| 47 } | 45 } |
| 48 | 46 |
| 49 scoped_refptr<base::SingleThreadTaskRunner> handler_thread_; | 47 scoped_refptr<base::MessageLoopProxy> handler_thread_; |
| 50 base::Callback<void(ApplicationThread*)> termination_callback_; | 48 base::Callback<void(ApplicationThread*)> termination_callback_; |
| 51 ContentHandlerFactory::Delegate* handler_delegate_; | 49 ContentHandlerFactory::Delegate* handler_delegate_; |
| 52 InterfaceRequest<Application> application_request_; | 50 InterfaceRequest<Application> application_request_; |
| 53 URLResponsePtr response_; | 51 URLResponsePtr response_; |
| 54 | 52 |
| 55 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | 53 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
| 56 }; | 54 }; |
| 57 | 55 |
| 58 class ContentHandlerImpl : public ContentHandler { | 56 class ContentHandlerImpl : public ContentHandler { |
| 59 public: | 57 public: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 71 for (auto thread : active_threads_) { | 69 for (auto thread : active_threads_) { |
| 72 base::PlatformThread::Join(thread.second); | 70 base::PlatformThread::Join(thread.second); |
| 73 delete thread.first; | 71 delete thread.first; |
| 74 } | 72 } |
| 75 } | 73 } |
| 76 | 74 |
| 77 private: | 75 private: |
| 78 // Overridden from ContentHandler: | 76 // Overridden from ContentHandler: |
| 79 void StartApplication(InterfaceRequest<Application> application_request, | 77 void StartApplication(InterfaceRequest<Application> application_request, |
| 80 URLResponsePtr response) override { | 78 URLResponsePtr response) override { |
| 81 ApplicationThread* thread = | 79 ApplicationThread* thread = new ApplicationThread( |
| 82 new ApplicationThread(base::ThreadTaskRunnerHandle::Get(), | 80 base::MessageLoopProxy::current(), |
| 83 base::Bind(&ContentHandlerImpl::OnThreadEnd, | 81 base::Bind(&ContentHandlerImpl::OnThreadEnd, |
| 84 weak_factory_.GetWeakPtr()), | 82 weak_factory_.GetWeakPtr()), |
| 85 delegate_, | 83 delegate_, application_request.Pass(), response.Pass()); |
| 86 application_request.Pass(), | |
| 87 response.Pass()); | |
| 88 base::PlatformThreadHandle handle; | 84 base::PlatformThreadHandle handle; |
| 89 bool launched = base::PlatformThread::Create(0, thread, &handle); | 85 bool launched = base::PlatformThread::Create(0, thread, &handle); |
| 90 DCHECK(launched); | 86 DCHECK(launched); |
| 91 active_threads_[thread] = handle; | 87 active_threads_[thread] = handle; |
| 92 } | 88 } |
| 93 | 89 |
| 94 void OnThreadEnd(ApplicationThread* thread) { | 90 void OnThreadEnd(ApplicationThread* thread) { |
| 95 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 91 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
| 96 base::PlatformThreadHandle handle = active_threads_[thread]; | 92 base::PlatformThreadHandle handle = active_threads_[thread]; |
| 97 active_threads_.erase(thread); | 93 active_threads_.erase(thread); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 125 if (application) | 121 if (application) |
| 126 loop.Run(); | 122 loop.Run(); |
| 127 } | 123 } |
| 128 | 124 |
| 129 void ContentHandlerFactory::Create(ApplicationConnection* connection, | 125 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
| 130 InterfaceRequest<ContentHandler> request) { | 126 InterfaceRequest<ContentHandler> request) { |
| 131 new ContentHandlerImpl(delegate_, request.Pass()); | 127 new ContentHandlerImpl(delegate_, request.Pass()); |
| 132 } | 128 } |
| 133 | 129 |
| 134 } // namespace mojo | 130 } // namespace mojo |
| OLD | NEW |