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