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