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