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.
5 #include "content/child/threaded_data_provider.h"
7 #include "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h"
10 #include "content/child/child_process.h"
11 #include "content/child/child_thread_impl.h"
12 #include "content/child/resource_dispatcher.h"
13 #include "content/child/thread_safe_sender.h"
14 #include "content/common/resource_messages.h"
15 #include "ipc/ipc_sync_channel.h"
16 #include "third_party/WebKit/public/platform/WebThread.h"
17 #include "third_party/WebKit/public/platform/WebThreadedDataReceiver.h"
23 class DataProviderMessageFilter
: public IPC::MessageFilter
{
25 DataProviderMessageFilter(
26 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
27 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
28 const scheduler::WebThreadImplForWorkerScheduler
& background_thread
,
29 const base::WeakPtr
<ThreadedDataProvider
>&
30 background_thread_resource_provider
,
31 const base::WeakPtr
<ThreadedDataProvider
>& main_thread_resource_provider
,
34 // IPC::ChannelProxy::MessageFilter
35 void OnFilterAdded(IPC::Sender
* sender
) final
;
36 bool OnMessageReceived(const IPC::Message
& message
) final
;
39 ~DataProviderMessageFilter() override
{}
41 void OnReceivedData(int request_id
, int data_offset
, int data_length
,
42 int encoded_data_length
);
44 const scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
45 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
46 const scheduler::WebThreadImplForWorkerScheduler
& background_thread_
;
47 // This weakptr can only be dereferenced on the background thread.
48 base::WeakPtr
<ThreadedDataProvider
>
49 background_thread_resource_provider_
;
50 // This weakptr can only be dereferenced on the main thread.
51 base::WeakPtr
<ThreadedDataProvider
>
52 main_thread_resource_provider_
;
56 DataProviderMessageFilter::DataProviderMessageFilter(
57 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
58 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
59 const scheduler::WebThreadImplForWorkerScheduler
& background_thread
,
60 const base::WeakPtr
<ThreadedDataProvider
>&
61 background_thread_resource_provider
,
62 const base::WeakPtr
<ThreadedDataProvider
>& main_thread_resource_provider
,
64 : io_task_runner_(io_task_runner
),
65 main_thread_task_runner_(main_thread_task_runner
),
66 background_thread_(background_thread
),
67 background_thread_resource_provider_(background_thread_resource_provider
),
68 main_thread_resource_provider_(main_thread_resource_provider
),
69 request_id_(request_id
) {
70 DCHECK(main_thread_task_runner_
.get());
73 void DataProviderMessageFilter::OnFilterAdded(IPC::Sender
* sender
) {
74 DCHECK(io_task_runner_
->BelongsToCurrentThread());
76 main_thread_task_runner_
->PostTask(
78 base::Bind(&ThreadedDataProvider::OnResourceMessageFilterAddedMainThread
,
79 main_thread_resource_provider_
));
82 bool DataProviderMessageFilter::OnMessageReceived(
83 const IPC::Message
& message
) {
84 DCHECK(io_task_runner_
->BelongsToCurrentThread());
86 if (message
.type() != ResourceMsg_DataReceived::ID
)
91 base::PickleIterator
iter(message
);
92 if (!iter
.ReadInt(&request_id
)) {
93 NOTREACHED() << "malformed resource message";
97 if (request_id
== request_id_
) {
98 ResourceMsg_DataReceived::Schema::Param arg
;
99 if (ResourceMsg_DataReceived::Read(&message
, &arg
)) {
100 OnReceivedData(base::get
<0>(arg
), base::get
<1>(arg
),
101 base::get
<2>(arg
), base::get
<3>(arg
));
109 void DataProviderMessageFilter::OnReceivedData(int request_id
,
112 int encoded_data_length
) {
113 DCHECK(io_task_runner_
->BelongsToCurrentThread());
114 background_thread_
.TaskRunner()->PostTask(
116 base::Bind(&ThreadedDataProvider::OnReceivedDataOnBackgroundThread
,
117 background_thread_resource_provider_
, data_offset
, data_length
,
118 encoded_data_length
));
121 } // anonymous namespace
123 ThreadedDataProvider::ThreadedDataProvider(
125 blink::WebThreadedDataReceiver
* threaded_data_receiver
,
126 linked_ptr
<base::SharedMemory
> shm_buffer
,
128 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
)
129 : request_id_(request_id
),
130 shm_buffer_(shm_buffer
),
133 static_cast<scheduler::WebThreadImplForWorkerScheduler
&>(
134 *threaded_data_receiver
->backgroundThread())),
135 ipc_channel_(ChildThreadImpl::current()->channel()),
136 threaded_data_receiver_(threaded_data_receiver
),
137 resource_filter_active_(false),
138 main_thread_task_runner_(main_thread_task_runner
),
139 main_thread_weak_factory_(this) {
140 DCHECK(ChildThreadImpl::current());
141 DCHECK(ipc_channel_
);
142 DCHECK(threaded_data_receiver_
);
143 DCHECK(main_thread_task_runner_
.get());
145 background_thread_weak_factory_
.reset(
146 new base::WeakPtrFactory
<ThreadedDataProvider
>(this));
148 filter_
= new DataProviderMessageFilter(
149 ChildProcess::current()->io_task_runner(), main_thread_task_runner_
,
150 background_thread_
, background_thread_weak_factory_
->GetWeakPtr(),
151 main_thread_weak_factory_
.GetWeakPtr(), request_id
);
153 ChildThreadImpl::current()->channel()->AddFilter(filter_
.get());
156 ThreadedDataProvider::~ThreadedDataProvider() {
157 DCHECK(ChildThreadImpl::current());
159 ChildThreadImpl::current()->channel()->RemoveFilter(filter_
.get());
161 delete threaded_data_receiver_
;
164 void ThreadedDataProvider::DestructOnMainThread() {
165 DCHECK(ChildThreadImpl::current());
167 // The ThreadedDataProvider must be destructed on the main thread to
168 // be threadsafe when removing the message filter and releasing the shared
173 void ThreadedDataProvider::Stop() {
174 DCHECK(ChildThreadImpl::current());
176 // Make sure we don't get called by on the main thread anymore via weak
177 // pointers we've passed to the filter.
178 main_thread_weak_factory_
.InvalidateWeakPtrs();
180 blink::WebThread
* current_background_thread
=
181 threaded_data_receiver_
->backgroundThread();
183 // We can't destroy this instance directly; we need to bounce a message over
184 // to the background thread and back to make sure nothing else will access it
185 // there, before we can destruct it. We also need to make sure the background
186 // thread is still alive, since Blink could have shut down at this point
187 // and freed the thread.
188 if (current_background_thread
) {
189 // We should never end up with a different parser thread than from when the
190 // ThreadedDataProvider gets created.
191 DCHECK(current_background_thread
==
192 static_cast<scheduler::WebThreadImplForWorkerScheduler
*>(
193 &background_thread_
));
194 background_thread_
.TaskRunner()->PostTask(
195 FROM_HERE
, base::Bind(&ThreadedDataProvider::StopOnBackgroundThread
,
196 base::Unretained(this)));
200 void ThreadedDataProvider::StopOnBackgroundThread() {
201 DCHECK(background_thread_
.isCurrentThread());
202 DCHECK(background_thread_weak_factory_
);
204 // When this happens, the provider should no longer be called on the
205 // background thread as it's about to be destroyed on the main thread.
206 // Destructing the weak pointer factory means invalidating the weak pointers
207 // which means no callbacks from the filter will happen and nothing else will
208 // use this instance on the background thread.
209 background_thread_weak_factory_
.reset(NULL
);
210 main_thread_task_runner_
->PostTask(FROM_HERE
,
211 base::Bind(&ThreadedDataProvider::DestructOnMainThread
,
212 base::Unretained(this)));
215 void ThreadedDataProvider::OnRequestCompleteForegroundThread(
216 base::WeakPtr
<ResourceDispatcher
> resource_dispatcher
,
217 const ResourceMsg_RequestCompleteData
& request_complete_data
,
218 const base::TimeTicks
& renderer_completion_time
) {
219 DCHECK(ChildThreadImpl::current());
221 background_thread_
.TaskRunner()->PostTask(
223 base::Bind(&ThreadedDataProvider::OnRequestCompleteBackgroundThread
,
224 base::Unretained(this), resource_dispatcher
,
225 request_complete_data
, renderer_completion_time
));
228 void ThreadedDataProvider::OnRequestCompleteBackgroundThread(
229 base::WeakPtr
<ResourceDispatcher
> resource_dispatcher
,
230 const ResourceMsg_RequestCompleteData
& request_complete_data
,
231 const base::TimeTicks
& renderer_completion_time
) {
232 DCHECK(background_thread_
.isCurrentThread());
234 main_thread_task_runner_
->PostTask(FROM_HERE
,
236 &ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush
,
239 request_complete_data
,
240 renderer_completion_time
));
243 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() {
244 DCHECK(ChildThreadImpl::current());
245 DCHECK(background_thread_weak_factory_
);
247 // We bounce this message from the I/O thread via the main thread and then
248 // to our background thread, following the same path as incoming data before
249 // our filter gets added, to make sure there's nothing still incoming.
250 background_thread_
.TaskRunner()->PostTask(
253 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread
,
254 background_thread_weak_factory_
->GetWeakPtr()));
257 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() {
258 DCHECK(background_thread_
.isCurrentThread());
259 resource_filter_active_
= true;
261 // At this point we know no more data is going to arrive from the main thread,
262 // so we can process any data we've received directly from the I/O thread
264 if (!queued_data_
.empty()) {
265 std::vector
<QueuedSharedMemoryData
>::iterator iter
= queued_data_
.begin();
266 for (; iter
!= queued_data_
.end(); ++iter
) {
267 ForwardAndACKData(iter
->data
, iter
->length
, iter
->encoded_length
);
270 queued_data_
.clear();
274 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread(
275 int data_offset
, int data_length
, int encoded_data_length
) {
276 DCHECK(background_thread_
.isCurrentThread());
277 DCHECK(shm_buffer_
!= NULL
);
279 CHECK_GE(shm_size_
, data_offset
+ data_length
);
280 const char* data_ptr
= static_cast<char*>(shm_buffer_
->memory());
282 CHECK(data_ptr
+ data_offset
);
284 if (resource_filter_active_
) {
285 ForwardAndACKData(data_ptr
+ data_offset
, data_length
, encoded_data_length
);
287 // There's a brief interval between the point where we know the filter
288 // has been installed on the I/O thread, and when we know for sure there's
289 // no more data coming in from the main thread (from before the filter
290 // got added). If we get any data during that interval, we need to queue
291 // it until we're certain we've processed all the main thread data to make
292 // sure we forward (and ACK) everything in the right order.
293 QueuedSharedMemoryData queued_data
;
294 queued_data
.data
= data_ptr
+ data_offset
;
295 queued_data
.length
= data_length
;
296 queued_data
.encoded_length
= encoded_data_length
;
297 queued_data_
.push_back(queued_data
);
301 void ThreadedDataProvider::OnReceivedDataOnForegroundThread(
302 const char* data
, int data_length
, int encoded_data_length
) {
303 DCHECK(ChildThreadImpl::current());
305 background_thread_
.TaskRunner()->PostTask(
306 FROM_HERE
, base::Bind(&ThreadedDataProvider::ForwardAndACKData
,
307 base::Unretained(this), data
, data_length
,
308 encoded_data_length
));
311 void ThreadedDataProvider::ForwardAndACKData(const char* data
,
313 int encoded_data_length
) {
314 DCHECK(background_thread_
.isCurrentThread());
316 threaded_data_receiver_
->acceptData(data
, data_length
);
318 scoped_ptr
<std::vector
<char>> data_copy
;
319 if (threaded_data_receiver_
->needsMainthreadDataCopy()) {
320 data_copy
.reset(new std::vector
<char>(data
, data
+ data_length
));
323 main_thread_task_runner_
->PostTask(FROM_HERE
,
324 base::Bind(&ThreadedDataProvider::DataNotifyForegroundThread
,
325 base::Unretained(this),
326 base::Passed(&data_copy
),
328 encoded_data_length
));
330 ipc_channel_
->Send(new ResourceHostMsg_DataReceived_ACK(request_id_
));
333 void ThreadedDataProvider::DataNotifyForegroundThread(
334 scoped_ptr
<std::vector
<char> > data_copy
,
336 int encoded_data_length
) {
338 DCHECK(threaded_data_receiver_
->needsMainthreadDataCopy());
339 DCHECK_EQ((size_t)data_length
, data_copy
->size());
342 threaded_data_receiver_
->acceptMainthreadDataNotification(
343 (data_copy
&& !data_copy
->empty()) ? &data_copy
->front() : NULL
,
344 data_length
, encoded_data_length
);
347 } // namespace content