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 <set> | 5 #include <set> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 namespace mojo { | 24 namespace mojo { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 class ApplicationThread : public base::PlatformThread::Delegate { | 28 class ApplicationThread : public base::PlatformThread::Delegate { |
29 public: | 29 public: |
30 ApplicationThread( | 30 ApplicationThread( |
31 scoped_refptr<base::SingleThreadTaskRunner> handler_thread, | 31 scoped_refptr<base::SingleThreadTaskRunner> handler_thread, |
32 const base::Callback<void(ApplicationThread*)>& termination_callback, | 32 const base::Callback<void(ApplicationThread*)>& termination_callback, |
33 ContentHandlerFactory::Delegate* handler_delegate, | 33 ContentHandlerFactory::Delegate* handler_delegate, |
34 InterfaceRequest<Application> application_request, | 34 InterfaceRequest<shell::mojom::Application> application_request, |
35 URLResponsePtr response, | 35 URLResponsePtr response, |
36 const Callback<void()>& destruct_callback) | 36 const Callback<void()>& destruct_callback) |
37 : handler_thread_(handler_thread), | 37 : handler_thread_(handler_thread), |
38 termination_callback_(termination_callback), | 38 termination_callback_(termination_callback), |
39 handler_delegate_(handler_delegate), | 39 handler_delegate_(handler_delegate), |
40 application_request_(std::move(application_request)), | 40 application_request_(std::move(application_request)), |
41 response_(std::move(response)), | 41 response_(std::move(response)), |
42 destruct_callback_(destruct_callback) {} | 42 destruct_callback_(destruct_callback) {} |
43 | 43 |
44 ~ApplicationThread() override { | 44 ~ApplicationThread() override { |
45 destruct_callback_.Run(); | 45 destruct_callback_.Run(); |
46 } | 46 } |
47 | 47 |
48 private: | 48 private: |
49 void ThreadMain() override { | 49 void ThreadMain() override { |
50 handler_delegate_->RunApplication(std::move(application_request_), | 50 handler_delegate_->RunApplication(std::move(application_request_), |
51 std::move(response_)); | 51 std::move(response_)); |
52 handler_thread_->PostTask(FROM_HERE, | 52 handler_thread_->PostTask(FROM_HERE, |
53 base::Bind(termination_callback_, this)); | 53 base::Bind(termination_callback_, this)); |
54 } | 54 } |
55 | 55 |
56 scoped_refptr<base::SingleThreadTaskRunner> handler_thread_; | 56 scoped_refptr<base::SingleThreadTaskRunner> handler_thread_; |
57 base::Callback<void(ApplicationThread*)> termination_callback_; | 57 base::Callback<void(ApplicationThread*)> termination_callback_; |
58 ContentHandlerFactory::Delegate* handler_delegate_; | 58 ContentHandlerFactory::Delegate* handler_delegate_; |
59 InterfaceRequest<Application> application_request_; | 59 InterfaceRequest<shell::mojom::Application> application_request_; |
60 URLResponsePtr response_; | 60 URLResponsePtr response_; |
61 Callback<void()> destruct_callback_; | 61 Callback<void()> destruct_callback_; |
62 | 62 |
63 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | 63 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
64 }; | 64 }; |
65 | 65 |
66 class ContentHandlerImpl : public ContentHandler { | 66 class ContentHandlerImpl : public shell::mojom::ContentHandler { |
67 public: | 67 public: |
68 ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate, | 68 ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate, |
69 InterfaceRequest<ContentHandler> request) | 69 InterfaceRequest<shell::mojom::ContentHandler> request) |
70 : delegate_(delegate), | 70 : delegate_(delegate), |
71 binding_(this, std::move(request)), | 71 binding_(this, std::move(request)), |
72 weak_factory_(this) {} | 72 weak_factory_(this) {} |
73 ~ContentHandlerImpl() override { | 73 ~ContentHandlerImpl() override { |
74 // We're shutting down and doing cleanup. Cleanup may trigger calls back to | 74 // We're shutting down and doing cleanup. Cleanup may trigger calls back to |
75 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in | 75 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in |
76 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any | 76 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any |
77 // calls to OnThreadEnd(). | 77 // calls to OnThreadEnd(). |
78 weak_factory_.InvalidateWeakPtrs(); | 78 weak_factory_.InvalidateWeakPtrs(); |
79 for (auto thread : active_threads_) { | 79 for (auto thread : active_threads_) { |
80 base::PlatformThread::Join(thread.second); | 80 base::PlatformThread::Join(thread.second); |
81 delete thread.first; | 81 delete thread.first; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 private: | 85 private: |
86 // Overridden from ContentHandler: | 86 // Overridden from ContentHandler: |
87 void StartApplication( | 87 void StartApplication( |
88 InterfaceRequest<Application> application_request, | 88 InterfaceRequest<shell::mojom::Application> application_request, |
89 URLResponsePtr response, | 89 URLResponsePtr response, |
90 const Callback<void()>& destruct_callback) override { | 90 const Callback<void()>& destruct_callback) override { |
91 ApplicationThread* thread = | 91 ApplicationThread* thread = |
92 new ApplicationThread(base::ThreadTaskRunnerHandle::Get(), | 92 new ApplicationThread(base::ThreadTaskRunnerHandle::Get(), |
93 base::Bind(&ContentHandlerImpl::OnThreadEnd, | 93 base::Bind(&ContentHandlerImpl::OnThreadEnd, |
94 weak_factory_.GetWeakPtr()), | 94 weak_factory_.GetWeakPtr()), |
95 delegate_, std::move(application_request), | 95 delegate_, std::move(application_request), |
96 std::move(response), destruct_callback); | 96 std::move(response), destruct_callback); |
97 base::PlatformThreadHandle handle; | 97 base::PlatformThreadHandle handle; |
98 bool launched = base::PlatformThread::Create(0, thread, &handle); | 98 bool launched = base::PlatformThread::Create(0, thread, &handle); |
99 DCHECK(launched); | 99 DCHECK(launched); |
100 active_threads_[thread] = handle; | 100 active_threads_[thread] = handle; |
101 } | 101 } |
102 | 102 |
103 void OnThreadEnd(ApplicationThread* thread) { | 103 void OnThreadEnd(ApplicationThread* thread) { |
104 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 104 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
105 base::PlatformThreadHandle handle = active_threads_[thread]; | 105 base::PlatformThreadHandle handle = active_threads_[thread]; |
106 active_threads_.erase(thread); | 106 active_threads_.erase(thread); |
107 base::PlatformThread::Join(handle); | 107 base::PlatformThread::Join(handle); |
108 delete thread; | 108 delete thread; |
109 } | 109 } |
110 | 110 |
111 ContentHandlerFactory::Delegate* delegate_; | 111 ContentHandlerFactory::Delegate* delegate_; |
112 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; | 112 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; |
113 StrongBinding<ContentHandler> binding_; | 113 StrongBinding<shell::mojom::ContentHandler> binding_; |
114 base::WeakPtrFactory<ContentHandlerImpl> weak_factory_; | 114 base::WeakPtrFactory<ContentHandlerImpl> weak_factory_; |
115 | 115 |
116 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 116 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
117 }; | 117 }; |
118 | 118 |
119 } // namespace | 119 } // namespace |
120 | 120 |
121 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) | 121 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) |
122 : delegate_(delegate) { | 122 : delegate_(delegate) { |
123 } | 123 } |
124 | 124 |
125 ContentHandlerFactory::~ContentHandlerFactory() { | 125 ContentHandlerFactory::~ContentHandlerFactory() { |
126 } | 126 } |
127 | 127 |
128 void ContentHandlerFactory::ManagedDelegate::RunApplication( | 128 void ContentHandlerFactory::ManagedDelegate::RunApplication( |
129 InterfaceRequest<Application> application_request, | 129 InterfaceRequest<shell::mojom::Application> application_request, |
130 URLResponsePtr response) { | 130 URLResponsePtr response) { |
131 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 131 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
132 auto application = this->CreateApplication(std::move(application_request), | 132 auto application = this->CreateApplication(std::move(application_request), |
133 std::move(response)); | 133 std::move(response)); |
134 if (application) | 134 if (application) |
135 loop.Run(); | 135 loop.Run(); |
136 } | 136 } |
137 | 137 |
138 void ContentHandlerFactory::Create(ApplicationConnection* connection, | 138 void ContentHandlerFactory::Create( |
139 InterfaceRequest<ContentHandler> request) { | 139 ApplicationConnection* connection, |
| 140 InterfaceRequest<shell::mojom::ContentHandler> request) { |
140 new ContentHandlerImpl(delegate_, std::move(request)); | 141 new ContentHandlerImpl(delegate_, std::move(request)); |
141 } | 142 } |
142 | 143 |
143 } // namespace mojo | 144 } // namespace mojo |
OLD | NEW |