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 "content/child/child_process.h"
8 #include "content/child/child_thread_impl.h"
9 #include "content/child/resource_dispatcher.h"
10 #include "content/child/thread_safe_sender.h"
11 #include "content/child/webthread_impl.h"
12 #include "content/common/resource_messages.h"
13 #include "ipc/ipc_sync_channel.h"
14 #include "third_party/WebKit/public/platform/WebThread.h"
15 #include "third_party/WebKit/public/platform/WebThreadedDataReceiver.h"
21 class DataProviderMessageFilter
: public IPC::MessageFilter
{
23 DataProviderMessageFilter(
24 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
,
25 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
26 const WebThreadImpl
& background_thread
,
27 const base::WeakPtr
<ThreadedDataProvider
>&
28 background_thread_resource_provider
,
29 const base::WeakPtr
<ThreadedDataProvider
>& main_thread_resource_provider
,
32 // IPC::ChannelProxy::MessageFilter
33 void OnFilterAdded(IPC::Sender
* sender
) final
;
34 bool OnMessageReceived(const IPC::Message
& message
) final
;
37 ~DataProviderMessageFilter() override
{}
39 void OnReceivedData(int request_id
, int data_offset
, int data_length
,
40 int encoded_data_length
);
42 const scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
43 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
44 const WebThreadImpl
& background_thread_
;
45 // This weakptr can only be dereferenced on the background thread.
46 base::WeakPtr
<ThreadedDataProvider
>
47 background_thread_resource_provider_
;
48 // This weakptr can only be dereferenced on the main thread.
49 base::WeakPtr
<ThreadedDataProvider
>
50 main_thread_resource_provider_
;
54 DataProviderMessageFilter::DataProviderMessageFilter(
55 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
,
56 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
57 const WebThreadImpl
& background_thread
,
58 const base::WeakPtr
<ThreadedDataProvider
>&
59 background_thread_resource_provider
,
60 const base::WeakPtr
<ThreadedDataProvider
>& main_thread_resource_provider
,
62 : io_message_loop_(io_message_loop
),
63 main_thread_task_runner_(main_thread_task_runner
),
64 background_thread_(background_thread
),
65 background_thread_resource_provider_(background_thread_resource_provider
),
66 main_thread_resource_provider_(main_thread_resource_provider
),
67 request_id_(request_id
) {
68 DCHECK(main_thread_task_runner_
.get());
71 void DataProviderMessageFilter::OnFilterAdded(IPC::Sender
* sender
) {
72 DCHECK(io_message_loop_
->BelongsToCurrentThread());
74 main_thread_task_runner_
->PostTask(
76 base::Bind(&ThreadedDataProvider::OnResourceMessageFilterAddedMainThread
,
77 main_thread_resource_provider_
));
80 bool DataProviderMessageFilter::OnMessageReceived(
81 const IPC::Message
& message
) {
82 DCHECK(io_message_loop_
->BelongsToCurrentThread());
84 if (message
.type() != ResourceMsg_DataReceived::ID
)
89 PickleIterator
iter(message
);
90 if (!iter
.ReadInt(&request_id
)) {
91 NOTREACHED() << "malformed resource message";
95 if (request_id
== request_id_
) {
96 ResourceMsg_DataReceived::Schema::Param arg
;
97 if (ResourceMsg_DataReceived::Read(&message
, &arg
)) {
98 OnReceivedData(get
<0>(arg
), get
<1>(arg
), get
<2>(arg
), get
<3>(arg
));
106 void DataProviderMessageFilter::OnReceivedData(int request_id
,
109 int encoded_data_length
) {
110 DCHECK(io_message_loop_
->BelongsToCurrentThread());
111 background_thread_
.message_loop()->PostTask(FROM_HERE
, base::Bind(
112 &ThreadedDataProvider::OnReceivedDataOnBackgroundThread
,
113 background_thread_resource_provider_
,
114 data_offset
, data_length
, encoded_data_length
));
117 } // anonymous namespace
119 ThreadedDataProvider::ThreadedDataProvider(
121 blink::WebThreadedDataReceiver
* threaded_data_receiver
,
122 linked_ptr
<base::SharedMemory
> shm_buffer
,
124 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
)
125 : request_id_(request_id
),
126 shm_buffer_(shm_buffer
),
128 background_thread_(static_cast<WebThreadImpl
&>(
129 *threaded_data_receiver
->backgroundThread())),
130 ipc_channel_(ChildThreadImpl::current()->channel()),
131 threaded_data_receiver_(threaded_data_receiver
),
132 resource_filter_active_(false),
133 main_thread_task_runner_(main_thread_task_runner
),
134 main_thread_weak_factory_(this) {
135 DCHECK(ChildThreadImpl::current());
136 DCHECK(ipc_channel_
);
137 DCHECK(threaded_data_receiver_
);
138 DCHECK(main_thread_task_runner_
.get());
140 background_thread_weak_factory_
.reset(
141 new base::WeakPtrFactory
<ThreadedDataProvider
>(this));
143 filter_
= new DataProviderMessageFilter(
144 ChildProcess::current()->io_message_loop_proxy(),
145 main_thread_task_runner_
, background_thread_
,
146 background_thread_weak_factory_
->GetWeakPtr(),
147 main_thread_weak_factory_
.GetWeakPtr(), request_id
);
149 ChildThreadImpl::current()->channel()->AddFilter(filter_
.get());
152 ThreadedDataProvider::~ThreadedDataProvider() {
153 DCHECK(ChildThreadImpl::current());
155 ChildThreadImpl::current()->channel()->RemoveFilter(filter_
.get());
157 delete threaded_data_receiver_
;
160 void ThreadedDataProvider::DestructOnMainThread() {
161 DCHECK(ChildThreadImpl::current());
163 // The ThreadedDataProvider must be destructed on the main thread to
164 // be threadsafe when removing the message filter and releasing the shared
169 void ThreadedDataProvider::Stop() {
170 DCHECK(ChildThreadImpl::current());
172 // Make sure we don't get called by on the main thread anymore via weak
173 // pointers we've passed to the filter.
174 main_thread_weak_factory_
.InvalidateWeakPtrs();
176 blink::WebThread
* current_background_thread
=
177 threaded_data_receiver_
->backgroundThread();
179 // We can't destroy this instance directly; we need to bounce a message over
180 // to the background thread and back to make sure nothing else will access it
181 // there, before we can destruct it. We also need to make sure the background
182 // thread is still alive, since Blink could have shut down at this point
183 // and freed the thread.
184 if (current_background_thread
) {
185 // We should never end up with a different parser thread than from when the
186 // ThreadedDataProvider gets created.
187 DCHECK(current_background_thread
==
188 static_cast<WebThreadImpl
*>(&background_thread_
));
189 background_thread_
.message_loop()->PostTask(FROM_HERE
,
190 base::Bind(&ThreadedDataProvider::StopOnBackgroundThread
,
191 base::Unretained(this)));
195 void ThreadedDataProvider::StopOnBackgroundThread() {
196 DCHECK(background_thread_
.isCurrentThread());
197 DCHECK(background_thread_weak_factory_
);
199 // When this happens, the provider should no longer be called on the
200 // background thread as it's about to be destroyed on the main thread.
201 // Destructing the weak pointer factory means invalidating the weak pointers
202 // which means no callbacks from the filter will happen and nothing else will
203 // use this instance on the background thread.
204 background_thread_weak_factory_
.reset(NULL
);
205 main_thread_task_runner_
->PostTask(FROM_HERE
,
206 base::Bind(&ThreadedDataProvider::DestructOnMainThread
,
207 base::Unretained(this)));
210 void ThreadedDataProvider::OnRequestCompleteForegroundThread(
211 base::WeakPtr
<ResourceDispatcher
> resource_dispatcher
,
212 const ResourceMsg_RequestCompleteData
& request_complete_data
,
213 const base::TimeTicks
& renderer_completion_time
) {
214 DCHECK(ChildThreadImpl::current());
216 background_thread_
.message_loop()->PostTask(FROM_HERE
,
217 base::Bind(&ThreadedDataProvider::OnRequestCompleteBackgroundThread
,
218 base::Unretained(this), resource_dispatcher
,
219 request_complete_data
, renderer_completion_time
));
222 void ThreadedDataProvider::OnRequestCompleteBackgroundThread(
223 base::WeakPtr
<ResourceDispatcher
> resource_dispatcher
,
224 const ResourceMsg_RequestCompleteData
& request_complete_data
,
225 const base::TimeTicks
& renderer_completion_time
) {
226 DCHECK(background_thread_
.isCurrentThread());
228 main_thread_task_runner_
->PostTask(FROM_HERE
,
230 &ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush
,
233 request_complete_data
,
234 renderer_completion_time
));
237 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() {
238 DCHECK(ChildThreadImpl::current());
239 DCHECK(background_thread_weak_factory_
);
241 // We bounce this message from the I/O thread via the main thread and then
242 // to our background thread, following the same path as incoming data before
243 // our filter gets added, to make sure there's nothing still incoming.
244 background_thread_
.message_loop()->PostTask(FROM_HERE
,
246 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread
,
247 background_thread_weak_factory_
->GetWeakPtr()));
250 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() {
251 DCHECK(background_thread_
.isCurrentThread());
252 resource_filter_active_
= true;
254 // At this point we know no more data is going to arrive from the main thread,
255 // so we can process any data we've received directly from the I/O thread
257 if (!queued_data_
.empty()) {
258 std::vector
<QueuedSharedMemoryData
>::iterator iter
= queued_data_
.begin();
259 for (; iter
!= queued_data_
.end(); ++iter
) {
260 ForwardAndACKData(iter
->data
, iter
->length
, iter
->encoded_length
);
263 queued_data_
.clear();
267 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread(
268 int data_offset
, int data_length
, int encoded_data_length
) {
269 DCHECK(background_thread_
.isCurrentThread());
270 DCHECK(shm_buffer_
!= NULL
);
272 CHECK_GE(shm_size_
, data_offset
+ data_length
);
273 const char* data_ptr
= static_cast<char*>(shm_buffer_
->memory());
275 CHECK(data_ptr
+ data_offset
);
277 if (resource_filter_active_
) {
278 ForwardAndACKData(data_ptr
+ data_offset
, data_length
, encoded_data_length
);
280 // There's a brief interval between the point where we know the filter
281 // has been installed on the I/O thread, and when we know for sure there's
282 // no more data coming in from the main thread (from before the filter
283 // got added). If we get any data during that interval, we need to queue
284 // it until we're certain we've processed all the main thread data to make
285 // sure we forward (and ACK) everything in the right order.
286 QueuedSharedMemoryData queued_data
;
287 queued_data
.data
= data_ptr
+ data_offset
;
288 queued_data
.length
= data_length
;
289 queued_data
.encoded_length
= encoded_data_length
;
290 queued_data_
.push_back(queued_data
);
294 void ThreadedDataProvider::OnReceivedDataOnForegroundThread(
295 const char* data
, int data_length
, int encoded_data_length
) {
296 DCHECK(ChildThreadImpl::current());
298 background_thread_
.message_loop()->PostTask(FROM_HERE
,
299 base::Bind(&ThreadedDataProvider::ForwardAndACKData
,
300 base::Unretained(this),
301 data
, data_length
, encoded_data_length
));
304 void ThreadedDataProvider::ForwardAndACKData(const char* data
,
306 int encoded_data_length
) {
307 DCHECK(background_thread_
.isCurrentThread());
309 // TODO(oysteine): SiteIsolationPolicy needs to be be checked
310 // here before we pass the data to the data provider
311 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does
312 // actual blocking as opposed to just UMA logging this will bypass it.
313 threaded_data_receiver_
->acceptData(data
, data_length
);
315 scoped_ptr
<std::vector
<char>> data_copy
;
316 if (threaded_data_receiver_
->needsMainthreadDataCopy()) {
317 data_copy
.reset(new std::vector
<char>(data
, data
+ data_length
));
320 main_thread_task_runner_
->PostTask(FROM_HERE
,
321 base::Bind(&ThreadedDataProvider::DataNotifyForegroundThread
,
322 base::Unretained(this),
323 base::Passed(&data_copy
),
325 encoded_data_length
));
327 ipc_channel_
->Send(new ResourceHostMsg_DataReceived_ACK(request_id_
));
330 void ThreadedDataProvider::DataNotifyForegroundThread(
331 scoped_ptr
<std::vector
<char> > data_copy
,
333 int encoded_data_length
) {
335 DCHECK(threaded_data_receiver_
->needsMainthreadDataCopy());
336 DCHECK_EQ((size_t)data_length
, data_copy
->size());
339 threaded_data_receiver_
->acceptMainthreadDataNotification(
340 (data_copy
&& !data_copy
->empty()) ? &data_copy
->front() : NULL
,
341 data_length
, encoded_data_length
);
344 } // namespace content