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.h" | 5 #include "mojo/application/content_handler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "mojo/application/application_runner_chromium.h" | 11 #include "mojo/application/application_runner_chromium.h" |
12 #include "mojo/common/message_pump_mojo.h" | 12 #include "mojo/common/message_pump_mojo.h" |
13 #include "mojo/public/cpp/application/application_connection.h" | 13 #include "mojo/public/cpp/application/application_connection.h" |
14 #include "mojo/public/cpp/application/application_delegate.h" | 14 #include "mojo/public/cpp/application/application_delegate.h" |
15 #include "mojo/public/cpp/application/application_impl.h" | 15 #include "mojo/public/cpp/application/application_impl.h" |
16 #include "mojo/public/cpp/application/interface_factory_impl.h" | |
17 #include "mojo/public/cpp/bindings/interface_impl.h" | 16 #include "mojo/public/cpp/bindings/interface_impl.h" |
18 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" | |
19 | 17 |
20 namespace mojo { | 18 namespace mojo { |
21 | 19 |
22 namespace { | 20 namespace { |
23 | 21 |
24 class ApplicationThread : public base::PlatformThread::Delegate { | 22 class ApplicationThread : public base::PlatformThread::Delegate { |
25 public: | 23 public: |
26 ApplicationThread( | 24 ApplicationThread( |
27 scoped_refptr<base::MessageLoopProxy> handler_thread, | 25 scoped_refptr<base::MessageLoopProxy> handler_thread, |
28 const base::Callback<void(ApplicationThread*)>& termination_callback, | 26 const base::Callback<void(ApplicationThread*)>& termination_callback, |
29 mojo::ContentHandlerDelegate* handler_delegate, | 27 ContentHandlerFactory::Delegate* delegate, |
30 ShellPtr shell, | 28 ShellPtr shell, |
31 URLResponsePtr response) | 29 URLResponsePtr response) |
32 : handler_thread_(handler_thread), | 30 : handler_thread_(handler_thread), |
33 termination_callback_(termination_callback), | 31 termination_callback_(termination_callback), |
34 handler_delegate_(handler_delegate), | 32 delegate_(delegate), |
35 shell_(shell.Pass()), | 33 shell_(shell.Pass()), |
36 response_(response.Pass()) {} | 34 response_(response.Pass()) {} |
37 | 35 |
38 private: | 36 private: |
39 void ThreadMain() override { | 37 void ThreadMain() override { |
40 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 38 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
41 application_ = | 39 application_.reset( |
42 handler_delegate_->CreateApplication(shell_.Pass(), response_.Pass()); | 40 new ApplicationImpl(delegate_->CreateApplication(response_.Pass()), |
| 41 shell_.PassMessagePipe())); |
43 loop.Run(); | 42 loop.Run(); |
44 handler_thread_->PostTask(FROM_HERE, | 43 handler_thread_->PostTask(FROM_HERE, |
45 base::Bind(termination_callback_, this)); | 44 base::Bind(termination_callback_, this)); |
46 } | 45 } |
47 | 46 |
48 scoped_refptr<base::MessageLoopProxy> handler_thread_; | 47 scoped_refptr<base::MessageLoopProxy> handler_thread_; |
49 base::Callback<void(ApplicationThread*)> termination_callback_; | 48 base::Callback<void(ApplicationThread*)> termination_callback_; |
50 mojo::ContentHandlerDelegate* handler_delegate_; | 49 ContentHandlerFactory::Delegate* delegate_; |
51 ShellPtr shell_; | 50 ShellPtr shell_; |
52 URLResponsePtr response_; | 51 URLResponsePtr response_; |
53 scoped_ptr<ApplicationDelegate> application_delegate_; | |
54 scoped_ptr<mojo::InterfaceImpl<mojo::Application>> application_; | 52 scoped_ptr<mojo::InterfaceImpl<mojo::Application>> application_; |
55 | 53 |
56 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | 54 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
57 }; | 55 }; |
58 | 56 |
59 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | 57 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
60 public: | 58 public: |
61 explicit ContentHandlerImpl(mojo::ContentHandlerDelegate* delegate) | 59 explicit ContentHandlerImpl(ContentHandlerFactory::Delegate* delegate) |
62 : delegate_(delegate) {} | 60 : delegate_(delegate) {} |
63 ~ContentHandlerImpl() { | 61 ~ContentHandlerImpl() { |
64 for (auto thread : active_threads_) { | 62 for (auto thread : active_threads_) { |
65 base::PlatformThread::Join(thread.second); | 63 base::PlatformThread::Join(thread.second); |
66 delete thread.first; | 64 delete thread.first; |
67 } | 65 } |
68 } | 66 } |
69 | 67 |
70 private: | 68 private: |
71 // Overridden from ContentHandler: | 69 // Overridden from ContentHandler: |
(...skipping 15 matching lines...) Expand all Loading... |
87 DCHECK(active_threads_.find(thread) != active_threads_.end()); | 85 DCHECK(active_threads_.find(thread) != active_threads_.end()); |
88 base::PlatformThreadHandle handle = active_threads_[thread]; | 86 base::PlatformThreadHandle handle = active_threads_[thread]; |
89 active_threads_.erase(thread); | 87 active_threads_.erase(thread); |
90 base::PlatformThread::Join(handle); | 88 base::PlatformThread::Join(handle); |
91 delete thread; | 89 delete thread; |
92 // TODO(qsr): If the last active thread exited, quit this application. Not | 90 // TODO(qsr): If the last active thread exited, quit this application. Not |
93 // possible at the moment as the shell doesn't support the content handler | 91 // possible at the moment as the shell doesn't support the content handler |
94 // quitting. | 92 // quitting. |
95 } | 93 } |
96 | 94 |
97 mojo::ContentHandlerDelegate* delegate_; | 95 ContentHandlerFactory::Delegate* delegate_; |
98 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; | 96 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; |
99 | 97 |
100 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 98 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
101 }; | 99 }; |
102 | 100 |
103 class ContentHandlerApp : public ApplicationDelegate { | |
104 public: | |
105 explicit ContentHandlerApp(scoped_ptr<ContentHandlerDelegate> delegate) | |
106 : delegate_(delegate.Pass()), content_handler_factory_(delegate_.get()) {} | |
107 | |
108 private: | |
109 // Overridden from ApplicationDelegate: | |
110 virtual void Initialize(ApplicationImpl* app) override { | |
111 delegate_->Initialize(app); | |
112 } | |
113 | |
114 virtual bool ConfigureIncomingConnection( | |
115 ApplicationConnection* connection) override { | |
116 connection->AddService(&content_handler_factory_); | |
117 return delegate_->ConfigureIncomingConnection(connection); | |
118 } | |
119 | |
120 virtual bool ConfigureOutgoingConnection( | |
121 ApplicationConnection* connection) override { | |
122 return delegate_->ConfigureOutgoingConnection(connection); | |
123 } | |
124 | |
125 scoped_ptr<ContentHandlerDelegate> delegate_; | |
126 InterfaceFactoryImplWithContext<ContentHandlerImpl, | |
127 mojo::ContentHandlerDelegate> | |
128 content_handler_factory_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(ContentHandlerApp); | |
131 }; | |
132 | |
133 } // namespace | 101 } // namespace |
134 | 102 |
135 // static | 103 ContentHandlerFactory::ContentHandlerFactory( |
136 MojoResult ContentHandlerRunner::Run( | 104 ContentHandlerFactory::Delegate* delegate) |
137 MojoHandle shell_handle, | 105 : delegate_(delegate) { |
138 scoped_ptr<mojo::ContentHandlerDelegate> delegate) { | 106 } |
139 mojo::ApplicationRunnerChromium runner( | 107 |
140 new ContentHandlerApp(delegate.Pass())); | 108 ContentHandlerFactory::~ContentHandlerFactory() { |
141 return runner.Run(shell_handle); | 109 } |
| 110 |
| 111 void ContentHandlerFactory::Create(ApplicationConnection* connection, |
| 112 InterfaceRequest<ContentHandler> request) { |
| 113 BindToRequest(new ContentHandlerImpl(delegate_), &request); |
142 } | 114 } |
143 | 115 |
144 } // namespace mojo | 116 } // namespace mojo |
OLD | NEW |