OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/application/content_handler.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/threading/platform_thread.h" | |
11 #include "mojo/application/application_runner_chromium.h" | |
12 #include "mojo/common/message_pump_mojo.h" | |
13 #include "mojo/public/cpp/application/application_connection.h" | |
14 #include "mojo/public/cpp/application/application_delegate.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" | |
18 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h" | |
19 | |
20 namespace mojo { | |
21 | |
22 namespace { | |
23 | |
24 class ApplicationThread : public base::PlatformThread::Delegate { | |
25 public: | |
26 ApplicationThread( | |
27 scoped_refptr<base::MessageLoopProxy> handler_thread, | |
28 const base::Callback<void(ApplicationThread*)>& termination_callback, | |
29 mojo::ContentHandlerDelegate* handler_delegate, | |
30 ShellPtr shell, | |
31 URLResponsePtr response) | |
32 : handler_thread_(handler_thread), | |
33 termination_callback_(termination_callback), | |
34 handler_delegate_(handler_delegate), | |
35 shell_(shell.Pass()), | |
36 response_(response.Pass()) {} | |
37 | |
38 private: | |
39 void ThreadMain() override { | |
40 base::MessageLoop loop(common::MessagePumpMojo::Create()); | |
41 application_ = | |
42 handler_delegate_->CreateApplication(shell_.Pass(), response_.Pass()); | |
43 loop.Run(); | |
44 handler_thread_->PostTask(FROM_HERE, | |
45 base::Bind(termination_callback_, this)); | |
46 } | |
47 | |
48 scoped_refptr<base::MessageLoopProxy> handler_thread_; | |
49 base::Callback<void(ApplicationThread*)> termination_callback_; | |
50 mojo::ContentHandlerDelegate* handler_delegate_; | |
51 ShellPtr shell_; | |
52 URLResponsePtr response_; | |
53 scoped_ptr<ApplicationDelegate> application_delegate_; | |
Aaron Boodman
2014/10/31 08:24:01
This is not used.
qsr
2014/10/31 12:10:44
Done.
| |
54 scoped_ptr<mojo::InterfaceImpl<mojo::Application>> application_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | |
57 }; | |
58 | |
59 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | |
60 public: | |
61 explicit ContentHandlerImpl(mojo::ContentHandlerDelegate* delegate) | |
62 : delegate_(delegate) {} | |
63 ~ContentHandlerImpl() { | |
64 for (auto thread : active_threads_) { | |
65 base::PlatformThread::Join(thread.second); | |
66 delete thread.first; | |
67 } | |
68 } | |
69 | |
70 private: | |
71 // Overridden from ContentHandler: | |
72 virtual void StartApplication(ShellPtr shell, | |
73 URLResponsePtr response) override { | |
74 ApplicationThread* thread = new ApplicationThread( | |
75 base::MessageLoopProxy::current(), | |
76 base::Bind(&ContentHandlerImpl::OnThreadEnd, base::Unretained(this)), | |
77 delegate_, | |
78 shell.Pass(), | |
79 response.Pass()); | |
80 base::PlatformThreadHandle handle; | |
81 bool launched = base::PlatformThread::Create(0, thread, &handle); | |
82 DCHECK(launched); | |
83 active_threads_[thread] = handle; | |
84 } | |
85 | |
86 void OnThreadEnd(ApplicationThread* thread) { | |
87 DCHECK(active_threads_.find(thread) != active_threads_.end()); | |
88 base::PlatformThreadHandle handle = active_threads_[thread]; | |
89 active_threads_.erase(thread); | |
90 base::PlatformThread::Join(handle); | |
91 delete thread; | |
92 // TODO(qsr): If the last active thread exited, quit this application. Not | |
Aaron Boodman
2014/10/31 08:24:01
Ah, because we never remove the entry from url_to_
qsr
2014/10/31 12:10:44
I tried, and the simplest thing resulted in crashe
| |
93 // possible at the moment as the shell doesn't support the content handler | |
94 // quitting. | |
95 } | |
96 | |
97 mojo::ContentHandlerDelegate* delegate_; | |
98 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | |
101 }; | |
102 | |
103 class ContentHandlerApp : public ApplicationDelegate { | |
Aaron Boodman
2014/10/31 08:24:01
I like the naming better, thanks.
qsr
2014/10/31 12:10:44
Acknowledged.
| |
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 | |
134 | |
135 // static | |
136 MojoResult ContentHandlerRunner::Run( | |
137 MojoHandle shell_handle, | |
138 scoped_ptr<mojo::ContentHandlerDelegate> delegate) { | |
139 mojo::ApplicationRunnerChromium runner( | |
140 new ContentHandlerApp(delegate.Pass())); | |
141 return runner.Run(shell_handle); | |
142 } | |
143 | |
144 } // namespace mojo | |
OLD | NEW |