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/callback.h" | |
11 #include "base/threading/platform_thread.h" | |
12 #include "mojo/application/application_runner_chromium.h" | |
13 #include "mojo/common/message_pump_mojo.h" | |
14 #include "mojo/public/cpp/application/application_connection.h" | |
15 #include "mojo/public/cpp/application/application_delegate.h" | |
16 #include "mojo/public/cpp/application/application_impl.h" | |
17 #include "mojo/public/cpp/application/interface_factory_impl.h" | |
18 #include "mojo/public/cpp/bindings/interface_impl.h" | |
19 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom. h" | |
20 | |
21 namespace mojo { | |
22 | |
23 namespace { | |
24 | |
25 class ApplicationThread : public base::PlatformThread::Delegate { | |
26 public: | |
27 ApplicationThread( | |
28 scoped_refptr<base::MessageLoopProxy> handler_thread, | |
29 const base::Callback<void(ApplicationThread*)>& termination_callback, | |
30 ContentHandlerFactory::Delegate* handler_delegate, | |
31 ShellPtr shell, | |
32 URLResponsePtr response) | |
33 : handler_thread_(handler_thread), | |
34 termination_callback_(termination_callback), | |
35 handler_delegate_(handler_delegate), | |
36 shell_(shell.Pass()), | |
37 response_(response.Pass()) {} | |
38 | |
39 private: | |
40 void ThreadMain() override { | |
41 base::MessageLoop loop(common::MessagePumpMojo::Create()); | |
Aaron Boodman
2014/10/31 16:40:46
I thought you wanted to support content handlers t
| |
42 application_ = | |
43 handler_delegate_->CreateApplication(shell_.Pass(), response_.Pass()); | |
44 loop.Run(); | |
45 handler_thread_->PostTask(FROM_HERE, | |
46 base::Bind(termination_callback_, this)); | |
47 } | |
48 | |
49 scoped_refptr<base::MessageLoopProxy> handler_thread_; | |
50 base::Callback<void(ApplicationThread*)> termination_callback_; | |
51 ContentHandlerFactory::Delegate* handler_delegate_; | |
52 ShellPtr shell_; | |
53 URLResponsePtr response_; | |
54 scoped_ptr<ContentHandlerFactory::HandledApplicationHolder> application_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | |
57 }; | |
58 | |
59 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | |
60 public: | |
61 explicit ContentHandlerImpl(ContentHandlerFactory::Delegate* 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 if (!active_threads_.size()) { | |
93 ApplicationImpl::Terminate(); | |
94 } | |
95 } | |
96 | |
97 ContentHandlerFactory::Delegate* delegate_; | |
98 std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | |
101 }; | |
102 | |
103 } // namespace | |
104 | |
105 ContentHandlerFactory::ContentHandlerFactory(Delegate* delegate) | |
106 : delegate_(delegate) { | |
107 } | |
108 | |
109 ContentHandlerFactory::~ContentHandlerFactory() { | |
110 } | |
111 | |
112 void ContentHandlerFactory::Create(ApplicationConnection* connection, | |
113 InterfaceRequest<ContentHandler> request) { | |
114 BindToRequest(new ContentHandlerImpl(delegate_), &request); | |
115 } | |
116 | |
117 } // namespace mojo | |
OLD | NEW |