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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace mojo { | 22 namespace mojo { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class ApplicationThread : public base::PlatformThread::Delegate { | 26 class ApplicationThread : public base::PlatformThread::Delegate { |
27 public: | 27 public: |
28 ApplicationThread( | 28 ApplicationThread( |
29 scoped_refptr<base::MessageLoopProxy> handler_thread, | 29 scoped_refptr<base::MessageLoopProxy> handler_thread, |
30 const base::Callback<void(ApplicationThread*)>& termination_callback, | 30 const base::Callback<void(ApplicationThread*)>& termination_callback, |
31 ContentHandlerFactory::Delegate* handler_delegate, | 31 ContentHandlerFactory::Delegate* handler_delegate, |
32 ShellPtr shell, | 32 InterfaceRequest<Application> application_request, |
33 URLResponsePtr response) | 33 URLResponsePtr response) |
34 : handler_thread_(handler_thread), | 34 : handler_thread_(handler_thread), |
35 termination_callback_(termination_callback), | 35 termination_callback_(termination_callback), |
36 handler_delegate_(handler_delegate), | 36 handler_delegate_(handler_delegate), |
37 shell_(shell.Pass()), | 37 application_request_(application_request.Pass()), |
38 response_(response.Pass()) {} | 38 response_(response.Pass()) {} |
39 | 39 |
40 private: | 40 private: |
41 void ThreadMain() override { | 41 void ThreadMain() override { |
42 handler_delegate_->RunApplication(shell_.Pass(), response_.Pass()); | 42 handler_delegate_->RunApplication(application_request_.Pass(), |
| 43 response_.Pass()); |
43 handler_thread_->PostTask(FROM_HERE, | 44 handler_thread_->PostTask(FROM_HERE, |
44 base::Bind(termination_callback_, this)); | 45 base::Bind(termination_callback_, this)); |
45 } | 46 } |
46 | 47 |
47 scoped_refptr<base::MessageLoopProxy> handler_thread_; | 48 scoped_refptr<base::MessageLoopProxy> handler_thread_; |
48 base::Callback<void(ApplicationThread*)> termination_callback_; | 49 base::Callback<void(ApplicationThread*)> termination_callback_; |
49 ContentHandlerFactory::Delegate* handler_delegate_; | 50 ContentHandlerFactory::Delegate* handler_delegate_; |
50 ShellPtr shell_; | 51 InterfaceRequest<Application> application_request_; |
51 URLResponsePtr response_; | 52 URLResponsePtr response_; |
52 | 53 |
53 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | 54 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
54 }; | 55 }; |
55 | 56 |
56 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | 57 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
57 public: | 58 public: |
58 explicit ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate) | 59 explicit ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate) |
59 : delegate_(delegate), weak_factory_(this) {} | 60 : delegate_(delegate), weak_factory_(this) {} |
60 ~ContentHandlerImpl() { | 61 ~ContentHandlerImpl() { |
61 // We're shutting down and doing cleanup. Cleanup may trigger calls back to | 62 // We're shutting down and doing cleanup. Cleanup may trigger calls back to |
62 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in | 63 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in |
63 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any | 64 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any |
64 // calls to OnThreadEnd(). | 65 // calls to OnThreadEnd(). |
65 weak_factory_.InvalidateWeakPtrs(); | 66 weak_factory_.InvalidateWeakPtrs(); |
66 for (auto thread : active_threads_) { | 67 for (auto thread : active_threads_) { |
67 base::PlatformThread::Join(thread.second); | 68 base::PlatformThread::Join(thread.second); |
68 delete thread.first; | 69 delete thread.first; |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 private: | 73 private: |
73 // Overridden from ContentHandler: | 74 // Overridden from ContentHandler: |
74 virtual void StartApplication(ShellPtr shell, | 75 virtual void StartApplication( |
75 URLResponsePtr response) override { | 76 InterfaceRequest<Application> application_request, |
| 77 URLResponsePtr response) override { |
76 ApplicationThread* thread = new ApplicationThread( | 78 ApplicationThread* thread = new ApplicationThread( |
77 base::MessageLoopProxy::current(), | 79 base::MessageLoopProxy::current(), |
78 base::Bind(&ContentHandlerImpl::OnThreadEnd, | 80 base::Bind(&ContentHandlerImpl::OnThreadEnd, |
79 weak_factory_.GetWeakPtr()), | 81 weak_factory_.GetWeakPtr()), |
80 delegate_, | 82 delegate_, application_request.Pass(), response.Pass()); |
81 shell.Pass(), | |
82 response.Pass()); | |
83 base::PlatformThreadHandle handle; | 83 base::PlatformThreadHandle handle; |
84 bool launched = base::PlatformThread::Create(0, thread, &handle); | 84 bool launched = base::PlatformThread::Create(0, thread, &handle); |
85 DCHECK(launched); | 85 DCHECK(launched); |
86 active_threads_[thread] = handle; | 86 active_threads_[thread] = handle; |
87 } | 87 } |
88 | 88 |
89 void OnThreadEnd(ApplicationThread* thread) { | 89 void OnThreadEnd(ApplicationThread* thread) { |
90 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 90 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
91 base::PlatformThreadHandle handle = active_threads_[thread]; | 91 base::PlatformThreadHandle handle = active_threads_[thread]; |
92 active_threads_.erase(thread); | 92 active_threads_.erase(thread); |
(...skipping 11 matching lines...) Expand all Loading... |
104 } // namespace | 104 } // namespace |
105 | 105 |
106 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) | 106 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) |
107 : delegate_(delegate) { | 107 : delegate_(delegate) { |
108 } | 108 } |
109 | 109 |
110 ContentHandlerFactory::~ContentHandlerFactory() { | 110 ContentHandlerFactory::~ContentHandlerFactory() { |
111 } | 111 } |
112 | 112 |
113 void ContentHandlerFactory::ManagedDelegate::RunApplication( | 113 void ContentHandlerFactory::ManagedDelegate::RunApplication( |
114 ShellPtr shell, | 114 InterfaceRequest<Application> application_request, |
115 URLResponsePtr response) { | 115 URLResponsePtr response) { |
116 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 116 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
117 auto application = this->CreateApplication(shell.Pass(), response.Pass()); | 117 auto application = |
| 118 this->CreateApplication(application_request.Pass(), response.Pass()); |
118 if (application) | 119 if (application) |
119 loop.Run(); | 120 loop.Run(); |
120 } | 121 } |
121 | 122 |
122 void ContentHandlerFactory::Create(ApplicationConnection* connection, | 123 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
123 InterfaceRequest<ContentHandler> request) { | 124 InterfaceRequest<ContentHandler> request) { |
124 BindToRequest(new ContentHandlerImpl(delegate_), &request); | 125 BindToRequest(new ContentHandlerImpl(delegate_), &request); |
125 } | 126 } |
126 | 127 |
127 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |