OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "content/child/webthreadedresourceprovider_impl.h" | |
6 | |
7 #include "content/child/child_process.h" | |
8 #include "content/child/child_thread.h" | |
9 #include "content/child/resource_dispatcher.h" | |
10 #include "content/child/thread_safe_sender.h" | |
11 #include "content/common/resource_messages.h" | |
12 #include "ipc/ipc_sync_channel.h" | |
13 #include "third_party/WebKit/public/platform/WebThread.h" | |
14 #include "webkit/child/webthread_impl.h" | |
15 | |
16 namespace content { | |
17 | |
18 namespace { | |
19 | |
20 static webkit_glue::WebThreadImpl* thread_ = NULL; | |
21 | |
22 class ParserResourceMessageFilter : public IPC::ChannelProxy::MessageFilter { | |
darin (slow to review)
2014/02/11 07:14:23
I think I would avoid mentioning the parser in thi
oystein (OOO til 10th of July)
2014/02/11 21:46:44
Done! Yeah this has become less and less parser sp
| |
23 public: | |
24 ParserResourceMessageFilter( | |
25 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
26 base::MessageLoop* main_thread_message_loop, | |
27 webkit_glue::WebThreadImpl& parser_thread, | |
28 base::WeakPtr<WebThreadedResourceProviderImpl> | |
29 parser_thread_resource_provider, | |
30 base::WeakPtr<WebThreadedResourceProviderImpl> | |
31 main_thread_resource_provider, | |
32 int request_id); | |
33 | |
34 // IPC::ChannelProxy::MessageFilter | |
35 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE; | |
36 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
37 | |
38 private: | |
39 virtual ~ParserResourceMessageFilter(); | |
40 | |
41 void OnReceivedData(int request_id, int data_offset, int data_length, | |
42 int encoded_data_length); | |
43 | |
44 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | |
45 base::MessageLoop* main_thread_message_loop_; | |
46 webkit_glue::WebThreadImpl& parser_thread_; | |
47 // This weakptr can only be dereferenced on the parser thread. | |
48 base::WeakPtr<WebThreadedResourceProviderImpl> | |
49 parser_thread_resource_provider_; | |
50 // This weakptr can only be dereferenced on the main thread. | |
51 base::WeakPtr<WebThreadedResourceProviderImpl> | |
52 main_thread_resource_provider_; | |
53 int request_id_; | |
54 }; | |
55 | |
56 ParserResourceMessageFilter::ParserResourceMessageFilter( | |
57 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
58 base::MessageLoop* main_thread_message_loop, | |
59 webkit_glue::WebThreadImpl& parser_thread, | |
60 base::WeakPtr<WebThreadedResourceProviderImpl> | |
61 parser_thread_resource_provider, | |
62 base::WeakPtr<WebThreadedResourceProviderImpl> | |
63 main_thread_resource_provider, | |
64 int request_id) | |
65 : io_message_loop_(io_message_loop), | |
66 main_thread_message_loop_(main_thread_message_loop), | |
67 parser_thread_(parser_thread), | |
68 parser_thread_resource_provider_(parser_thread_resource_provider), | |
69 main_thread_resource_provider_(main_thread_resource_provider), | |
70 request_id_(request_id) { | |
71 DCHECK(main_thread_message_loop != NULL); | |
72 } | |
73 | |
74 void ParserResourceMessageFilter::OnFilterAdded(IPC::Channel* channel) { | |
75 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
76 | |
77 main_thread_message_loop_->PostTask(FROM_HERE, | |
78 base::Bind( | |
79 &WebThreadedResourceProviderImpl::OnParserResourceMessageFilterAdded, | |
80 main_thread_resource_provider_)); | |
81 } | |
82 | |
83 bool ParserResourceMessageFilter::OnMessageReceived( | |
84 const IPC::Message& message) { | |
85 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
86 | |
87 if (message.type() != ResourceMsg_DataReceived::ID) | |
88 return false; | |
89 | |
90 int request_id; | |
91 | |
92 PickleIterator iter(message); | |
93 if (!message.ReadInt(&iter, &request_id)) { | |
94 NOTREACHED() << "malformed resource message"; | |
95 return true; | |
96 } | |
97 | |
98 if (request_id == request_id_) { | |
99 ResourceMsg_DataReceived::Schema::Param arg; | |
100 if (ResourceMsg_DataReceived::Read(&message, &arg)) | |
101 OnReceivedData(arg.a, arg.b, arg.c, arg.d); | |
102 } | |
103 | |
104 // Even if the parser thread wants the message (i.e. the request ID matches), | |
105 // the main thread currently still expects to get it as well. Each chunk will | |
106 // still only be parsed once. | |
107 return false; | |
108 } | |
109 | |
110 ParserResourceMessageFilter::~ParserResourceMessageFilter() { | |
111 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
112 } | |
113 | |
114 void ParserResourceMessageFilter::OnReceivedData(int request_id, | |
115 int data_offset, | |
116 int data_length, | |
117 int encoded_data_length) { | |
118 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
119 parser_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
120 &WebThreadedResourceProviderImpl::OnReceivedData, | |
121 parser_thread_resource_provider_, | |
122 data_offset, data_length, encoded_data_length)); | |
123 } | |
124 | |
125 } // anonymous namespace | |
126 | |
127 webkit_glue::WebThreadImpl& parser_thread() { | |
128 if (thread_ == NULL) | |
129 thread_ = new webkit_glue::WebThreadImpl("HTMLParserThread"); | |
darin (slow to review)
2014/02/11 07:14:23
This code doesn't seem specific to the parser.
oystein (OOO til 10th of July)
2014/02/11 21:46:44
Done.
| |
130 | |
131 return *thread_; | |
132 } | |
133 | |
134 void WebThreadedResourceProviderImpl::Cleanup() { | |
135 delete thread_; | |
136 thread_ = NULL; | |
137 } | |
138 | |
139 WebThreadedResourceProviderImpl::WebThreadedResourceProviderImpl( | |
140 int request_id, base::WeakPtr<ResourceDispatcher> resource_dispatcher, | |
141 linked_ptr<base::SharedMemory> shm_buffer, int shm_size) | |
142 : backgroundClient_(NULL), | |
143 foregroundClient_(NULL), | |
144 request_id_(request_id), | |
145 resource_dispatcher_(resource_dispatcher), | |
146 shm_buffer_(shm_buffer), | |
147 shm_size_(shm_size), | |
148 parser_thread_weak_factory_(this), | |
149 main_thread_weak_factory_(this), | |
150 main_thread_message_loop(ChildThread::current()->message_loop()) { | |
151 DCHECK(ChildThread::current()); | |
152 DCHECK(main_thread_message_loop); | |
153 | |
154 filter_ = new ParserResourceMessageFilter( | |
155 ChildProcess::current()->io_message_loop_proxy(), | |
156 main_thread_message_loop, | |
157 parser_thread(), | |
158 parser_thread_weak_factory_.GetWeakPtr(), | |
159 main_thread_weak_factory_.GetWeakPtr(), | |
160 request_id); | |
161 | |
162 ChildThread::current()->channel()->AddFilter(filter_.get()); | |
163 } | |
164 | |
165 WebThreadedResourceProviderImpl::~WebThreadedResourceProviderImpl() { | |
166 DCHECK(ChildThread::current()); | |
167 | |
168 // Release it from our locally held member variable before | |
169 // posting a message to remove it from the I/O thread, to | |
170 // make sure there's no race conditions and it's always destructed | |
171 // on the I/O thread. | |
172 // Ideally we'd also assert on refcount = 2 here... | |
173 IPC::ChannelProxy::MessageFilter* filter = filter_.get(); | |
174 filter_ = scoped_refptr<IPC::ChannelProxy::MessageFilter>(); | |
175 ChildThread::current()->channel()->RemoveFilter(filter); | |
176 | |
177 delete foregroundClient_; | |
darin (slow to review)
2014/02/11 07:14:23
nit: google variable naming style is foreground_cl
oystein (OOO til 10th of July)
2014/02/11 21:46:44
Done.
| |
178 } | |
179 | |
180 blink::WebThread* WebThreadedResourceProviderImpl::resourceThread() { | |
181 return &parser_thread(); | |
darin (slow to review)
2014/02/11 07:14:23
It is not clear why resourceThread() is a method s
oystein (OOO til 10th of July)
2014/02/11 21:46:44
resourceProviderThread() (renamed now) is implemen
| |
182 } | |
183 | |
184 void WebThreadedResourceProviderImpl::setBackgroundClient( | |
185 blink::WebThreadedResourceBackgroundClient* backgroundClient) { | |
186 DCHECK(parser_thread().isCurrentThread()); | |
187 backgroundClient_ = backgroundClient; | |
188 | |
189 if (!backgroundClient_) { | |
190 // When this happens, the provider should no longer be called on the | |
191 // parser thread as it's about to be destroyed on the main thread. | |
192 // Invalidating the weak pointers means no callbacks from the filter | |
193 // will happen. | |
194 parser_thread_weak_factory_.InvalidateWeakPtrs(); | |
195 } | |
196 } | |
197 | |
198 void WebThreadedResourceProviderImpl::setForegroundClient( | |
199 blink::WebThreadedResourceForegroundClient* foregroundClient) { | |
200 DCHECK(ChildThread::current()); | |
201 DCHECK(!foregroundClient_ && foregroundClient); | |
202 foregroundClient_ = foregroundClient; | |
203 } | |
204 | |
205 void WebThreadedResourceProviderImpl::OnParserResourceMessageFilterAdded() { | |
206 DCHECK(ChildThread::current()); | |
207 DCHECK(foregroundClient_); | |
208 DCHECK(resource_dispatcher_); | |
209 foregroundClient_->didSwitchedToBackgroundClient(); | |
210 resource_dispatcher_->StopSendingDataACKs(request_id_); | |
211 } | |
212 | |
213 void WebThreadedResourceProviderImpl::OnReceivedData(int data_offset, | |
214 int data_length, | |
215 int encoded_data_length) { | |
216 DCHECK(parser_thread().isCurrentThread()); | |
217 DCHECK(shm_buffer_ != NULL); | |
218 DCHECK(backgroundClient_); | |
219 | |
220 CHECK_GE(shm_size_, data_offset + data_length); | |
221 const char* data_ptr = static_cast<char*>(shm_buffer_->memory()); | |
222 CHECK(data_ptr); | |
223 CHECK(data_ptr + data_offset); | |
224 | |
225 // TODO: XSS validation and other stuff needs to happen to happen | |
darin (slow to review)
2014/02/11 07:14:23
nit: TODO(username)
oystein (OOO til 10th of July)
2014/02/11 21:46:44
Done.
| |
226 // here before we pass it to the parser (or earlier on the I/O thread) | |
227 backgroundClient_->didReceivedData(data_ptr + data_offset, data_length); | |
228 | |
229 // Sending a message via the main thread here that it can ACK the received | |
230 // data guarantees that the shared memory buffer won't be released by | |
231 // the browser process until we're done with it. | |
darin (slow to review)
2014/02/11 07:14:23
true, but why does this message need to be sent fr
oystein (OOO til 10th of July)
2014/02/11 21:46:44
Clarified this in the comment a bit; it's because
| |
232 main_thread_message_loop->PostTask(FROM_HERE, base::Bind( | |
233 &ResourceDispatcher::SendDataACK, | |
234 resource_dispatcher_, | |
235 request_id_)); | |
236 } | |
237 | |
238 } // namespace content | |
OLD | NEW |