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" |
12 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
13 #include "mojo/application/application_runner_chromium.h" | 15 #include "mojo/application/application_runner_chromium.h" |
14 #include "mojo/common/message_pump_mojo.h" | 16 #include "mojo/common/message_pump_mojo.h" |
15 #include "mojo/public/cpp/application/application_connection.h" | 17 #include "mojo/public/cpp/application/application_connection.h" |
16 #include "mojo/public/cpp/application/application_delegate.h" | 18 #include "mojo/public/cpp/application/application_delegate.h" |
17 #include "mojo/public/cpp/application/application_impl.h" | 19 #include "mojo/public/cpp/application/application_impl.h" |
18 #include "mojo/public/cpp/application/interface_factory_impl.h" | 20 #include "mojo/public/cpp/application/interface_factory_impl.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 11 matching lines...) Expand all Loading... |
69 for (auto thread : active_threads_) { | 71 for (auto thread : active_threads_) { |
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 = |
80 base::MessageLoopProxy::current(), | 82 new ApplicationThread(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_, |
| 86 application_request.Pass(), |
| 87 response.Pass()); |
84 base::PlatformThreadHandle handle; | 88 base::PlatformThreadHandle handle; |
85 bool launched = base::PlatformThread::Create(0, thread, &handle); | 89 bool launched = base::PlatformThread::Create(0, thread, &handle); |
86 DCHECK(launched); | 90 DCHECK(launched); |
87 active_threads_[thread] = handle; | 91 active_threads_[thread] = handle; |
88 } | 92 } |
89 | 93 |
90 void OnThreadEnd(ApplicationThread* thread) { | 94 void OnThreadEnd(ApplicationThread* thread) { |
91 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 95 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
92 base::PlatformThreadHandle handle = active_threads_[thread]; | 96 base::PlatformThreadHandle handle = active_threads_[thread]; |
93 active_threads_.erase(thread); | 97 active_threads_.erase(thread); |
(...skipping 27 matching lines...) Expand all Loading... |
121 if (application) | 125 if (application) |
122 loop.Run(); | 126 loop.Run(); |
123 } | 127 } |
124 | 128 |
125 void ContentHandlerFactory::Create(ApplicationConnection* connection, | 129 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
126 InterfaceRequest<ContentHandler> request) { | 130 InterfaceRequest<ContentHandler> request) { |
127 new ContentHandlerImpl(delegate_, request.Pass()); | 131 new ContentHandlerImpl(delegate_, request.Pass()); |
128 } | 132 } |
129 | 133 |
130 } // namespace mojo | 134 } // namespace mojo |
OLD | NEW |