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" |
(...skipping 13 matching lines...) Expand all Loading... |
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 : 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()), |
| 41 destruct_callback_(destruct_callback) {} |
| 42 |
| 43 ~ApplicationThread() override { |
| 44 destruct_callback_.Run(); |
| 45 } |
40 | 46 |
41 private: | 47 private: |
42 void ThreadMain() override { | 48 void ThreadMain() override { |
43 handler_delegate_->RunApplication(application_request_.Pass(), | 49 handler_delegate_->RunApplication(application_request_.Pass(), |
44 response_.Pass()); | 50 response_.Pass()); |
45 handler_thread_->PostTask(FROM_HERE, | 51 handler_thread_->PostTask(FROM_HERE, |
46 base::Bind(termination_callback_, this)); | 52 base::Bind(termination_callback_, this)); |
47 } | 53 } |
48 | 54 |
49 scoped_refptr<base::SingleThreadTaskRunner> handler_thread_; | 55 scoped_refptr<base::SingleThreadTaskRunner> handler_thread_; |
50 base::Callback<void(ApplicationThread*)> termination_callback_; | 56 base::Callback<void(ApplicationThread*)> termination_callback_; |
51 ContentHandlerFactory::Delegate* handler_delegate_; | 57 ContentHandlerFactory::Delegate* handler_delegate_; |
52 InterfaceRequest<Application> application_request_; | 58 InterfaceRequest<Application> application_request_; |
53 URLResponsePtr response_; | 59 URLResponsePtr response_; |
| 60 Callback<void()> destruct_callback_; |
54 | 61 |
55 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | 62 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
56 }; | 63 }; |
57 | 64 |
58 class ContentHandlerImpl : public ContentHandler { | 65 class ContentHandlerImpl : public ContentHandler { |
59 public: | 66 public: |
60 ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate, | 67 ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate, |
61 InterfaceRequest<ContentHandler> request) | 68 InterfaceRequest<ContentHandler> request) |
62 : delegate_(delegate), | 69 : delegate_(delegate), |
63 binding_(this, request.Pass()), | 70 binding_(this, request.Pass()), |
64 weak_factory_(this) {} | 71 weak_factory_(this) {} |
65 ~ContentHandlerImpl() override { | 72 ~ContentHandlerImpl() override { |
66 // 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 |
67 // 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 |
68 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any | 75 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any |
69 // calls to OnThreadEnd(). | 76 // calls to OnThreadEnd(). |
70 weak_factory_.InvalidateWeakPtrs(); | 77 weak_factory_.InvalidateWeakPtrs(); |
71 for (auto thread : active_threads_) { | 78 for (auto thread : active_threads_) { |
72 base::PlatformThread::Join(thread.second); | 79 base::PlatformThread::Join(thread.second); |
73 delete thread.first; | 80 delete thread.first; |
74 } | 81 } |
75 } | 82 } |
76 | 83 |
77 private: | 84 private: |
78 // Overridden from ContentHandler: | 85 // Overridden from ContentHandler: |
79 void StartApplication(InterfaceRequest<Application> application_request, | 86 void StartApplication( |
80 URLResponsePtr response) override { | 87 InterfaceRequest<Application> application_request, |
| 88 URLResponsePtr response, |
| 89 const Callback<void()>& destruct_callback) override { |
81 ApplicationThread* thread = new ApplicationThread( | 90 ApplicationThread* thread = new ApplicationThread( |
82 base::ThreadTaskRunnerHandle::Get(), | 91 base::ThreadTaskRunnerHandle::Get(), |
83 base::Bind(&ContentHandlerImpl::OnThreadEnd, | 92 base::Bind(&ContentHandlerImpl::OnThreadEnd, |
84 weak_factory_.GetWeakPtr()), | 93 weak_factory_.GetWeakPtr()), |
85 delegate_, application_request.Pass(), response.Pass()); | 94 delegate_, application_request.Pass(), response.Pass(), |
| 95 destruct_callback); |
86 base::PlatformThreadHandle handle; | 96 base::PlatformThreadHandle handle; |
87 bool launched = base::PlatformThread::Create(0, thread, &handle); | 97 bool launched = base::PlatformThread::Create(0, thread, &handle); |
88 DCHECK(launched); | 98 DCHECK(launched); |
89 active_threads_[thread] = handle; | 99 active_threads_[thread] = handle; |
90 } | 100 } |
91 | 101 |
92 void OnThreadEnd(ApplicationThread* thread) { | 102 void OnThreadEnd(ApplicationThread* thread) { |
93 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 103 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
94 base::PlatformThreadHandle handle = active_threads_[thread]; | 104 base::PlatformThreadHandle handle = active_threads_[thread]; |
95 active_threads_.erase(thread); | 105 active_threads_.erase(thread); |
(...skipping 27 matching lines...) Expand all Loading... |
123 if (application) | 133 if (application) |
124 loop.Run(); | 134 loop.Run(); |
125 } | 135 } |
126 | 136 |
127 void ContentHandlerFactory::Create(ApplicationConnection* connection, | 137 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
128 InterfaceRequest<ContentHandler> request) { | 138 InterfaceRequest<ContentHandler> request) { |
129 new ContentHandlerImpl(delegate_, request.Pass()); | 139 new ContentHandlerImpl(delegate_, request.Pass()); |
130 } | 140 } |
131 | 141 |
132 } // namespace mojo | 142 } // namespace mojo |
OLD | NEW |