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