Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / content / child / threaded_data_provider.cc
blob64daeebb8e3f8f3bf793450d3081642f9aecf429
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"
17 namespace content {
19 namespace {
21 class DataProviderMessageFilter : public IPC::MessageFilter {
22 public:
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,
30 int request_id);
32 // IPC::ChannelProxy::MessageFilter
33 void OnFilterAdded(IPC::Sender* sender) final;
34 bool OnMessageReceived(const IPC::Message& message) final;
36 private:
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_;
51 int request_id_;
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,
61 int request_id)
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(
75 FROM_HERE,
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)
85 return false;
87 int request_id;
89 PickleIterator iter(message);
90 if (!iter.ReadInt(&request_id)) {
91 NOTREACHED() << "malformed resource message";
92 return true;
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));
99 return true;
103 return false;
106 void DataProviderMessageFilter::OnReceivedData(int request_id,
107 int data_offset,
108 int data_length,
109 int encoded_data_length) {
110 DCHECK(io_message_loop_->BelongsToCurrentThread());
111 background_thread_.TaskRunner()->PostTask(
112 FROM_HERE,
113 base::Bind(&ThreadedDataProvider::OnReceivedDataOnBackgroundThread,
114 background_thread_resource_provider_, data_offset, data_length,
115 encoded_data_length));
118 } // anonymous namespace
120 ThreadedDataProvider::ThreadedDataProvider(
121 int request_id,
122 blink::WebThreadedDataReceiver* threaded_data_receiver,
123 linked_ptr<base::SharedMemory> shm_buffer,
124 int shm_size,
125 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner)
126 : request_id_(request_id),
127 shm_buffer_(shm_buffer),
128 shm_size_(shm_size),
129 background_thread_(static_cast<WebThreadImpl&>(
130 *threaded_data_receiver->backgroundThread())),
131 ipc_channel_(ChildThreadImpl::current()->channel()),
132 threaded_data_receiver_(threaded_data_receiver),
133 resource_filter_active_(false),
134 main_thread_task_runner_(main_thread_task_runner),
135 main_thread_weak_factory_(this) {
136 DCHECK(ChildThreadImpl::current());
137 DCHECK(ipc_channel_);
138 DCHECK(threaded_data_receiver_);
139 DCHECK(main_thread_task_runner_.get());
141 background_thread_weak_factory_.reset(
142 new base::WeakPtrFactory<ThreadedDataProvider>(this));
144 filter_ = new DataProviderMessageFilter(
145 ChildProcess::current()->io_message_loop_proxy(),
146 main_thread_task_runner_, background_thread_,
147 background_thread_weak_factory_->GetWeakPtr(),
148 main_thread_weak_factory_.GetWeakPtr(), request_id);
150 ChildThreadImpl::current()->channel()->AddFilter(filter_.get());
153 ThreadedDataProvider::~ThreadedDataProvider() {
154 DCHECK(ChildThreadImpl::current());
156 ChildThreadImpl::current()->channel()->RemoveFilter(filter_.get());
158 delete threaded_data_receiver_;
161 void ThreadedDataProvider::DestructOnMainThread() {
162 DCHECK(ChildThreadImpl::current());
164 // The ThreadedDataProvider must be destructed on the main thread to
165 // be threadsafe when removing the message filter and releasing the shared
166 // memory buffer.
167 delete this;
170 void ThreadedDataProvider::Stop() {
171 DCHECK(ChildThreadImpl::current());
173 // Make sure we don't get called by on the main thread anymore via weak
174 // pointers we've passed to the filter.
175 main_thread_weak_factory_.InvalidateWeakPtrs();
177 blink::WebThread* current_background_thread =
178 threaded_data_receiver_->backgroundThread();
180 // We can't destroy this instance directly; we need to bounce a message over
181 // to the background thread and back to make sure nothing else will access it
182 // there, before we can destruct it. We also need to make sure the background
183 // thread is still alive, since Blink could have shut down at this point
184 // and freed the thread.
185 if (current_background_thread) {
186 // We should never end up with a different parser thread than from when the
187 // ThreadedDataProvider gets created.
188 DCHECK(current_background_thread ==
189 static_cast<WebThreadImpl*>(&background_thread_));
190 background_thread_.TaskRunner()->PostTask(
191 FROM_HERE, base::Bind(&ThreadedDataProvider::StopOnBackgroundThread,
192 base::Unretained(this)));
196 void ThreadedDataProvider::StopOnBackgroundThread() {
197 DCHECK(background_thread_.isCurrentThread());
198 DCHECK(background_thread_weak_factory_);
200 // When this happens, the provider should no longer be called on the
201 // background thread as it's about to be destroyed on the main thread.
202 // Destructing the weak pointer factory means invalidating the weak pointers
203 // which means no callbacks from the filter will happen and nothing else will
204 // use this instance on the background thread.
205 background_thread_weak_factory_.reset(NULL);
206 main_thread_task_runner_->PostTask(FROM_HERE,
207 base::Bind(&ThreadedDataProvider::DestructOnMainThread,
208 base::Unretained(this)));
211 void ThreadedDataProvider::OnRequestCompleteForegroundThread(
212 base::WeakPtr<ResourceDispatcher> resource_dispatcher,
213 const ResourceMsg_RequestCompleteData& request_complete_data,
214 const base::TimeTicks& renderer_completion_time) {
215 DCHECK(ChildThreadImpl::current());
217 background_thread_.TaskRunner()->PostTask(
218 FROM_HERE,
219 base::Bind(&ThreadedDataProvider::OnRequestCompleteBackgroundThread,
220 base::Unretained(this), resource_dispatcher,
221 request_complete_data, renderer_completion_time));
224 void ThreadedDataProvider::OnRequestCompleteBackgroundThread(
225 base::WeakPtr<ResourceDispatcher> resource_dispatcher,
226 const ResourceMsg_RequestCompleteData& request_complete_data,
227 const base::TimeTicks& renderer_completion_time) {
228 DCHECK(background_thread_.isCurrentThread());
230 main_thread_task_runner_->PostTask(FROM_HERE,
231 base::Bind(
232 &ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush,
233 resource_dispatcher,
234 request_id_,
235 request_complete_data,
236 renderer_completion_time));
239 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() {
240 DCHECK(ChildThreadImpl::current());
241 DCHECK(background_thread_weak_factory_);
243 // We bounce this message from the I/O thread via the main thread and then
244 // to our background thread, following the same path as incoming data before
245 // our filter gets added, to make sure there's nothing still incoming.
246 background_thread_.TaskRunner()->PostTask(
247 FROM_HERE,
248 base::Bind(
249 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread,
250 background_thread_weak_factory_->GetWeakPtr()));
253 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() {
254 DCHECK(background_thread_.isCurrentThread());
255 resource_filter_active_ = true;
257 // At this point we know no more data is going to arrive from the main thread,
258 // so we can process any data we've received directly from the I/O thread
259 // in the meantime.
260 if (!queued_data_.empty()) {
261 std::vector<QueuedSharedMemoryData>::iterator iter = queued_data_.begin();
262 for (; iter != queued_data_.end(); ++iter) {
263 ForwardAndACKData(iter->data, iter->length, iter->encoded_length);
266 queued_data_.clear();
270 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread(
271 int data_offset, int data_length, int encoded_data_length) {
272 DCHECK(background_thread_.isCurrentThread());
273 DCHECK(shm_buffer_ != NULL);
275 CHECK_GE(shm_size_, data_offset + data_length);
276 const char* data_ptr = static_cast<char*>(shm_buffer_->memory());
277 CHECK(data_ptr);
278 CHECK(data_ptr + data_offset);
280 if (resource_filter_active_) {
281 ForwardAndACKData(data_ptr + data_offset, data_length, encoded_data_length);
282 } else {
283 // There's a brief interval between the point where we know the filter
284 // has been installed on the I/O thread, and when we know for sure there's
285 // no more data coming in from the main thread (from before the filter
286 // got added). If we get any data during that interval, we need to queue
287 // it until we're certain we've processed all the main thread data to make
288 // sure we forward (and ACK) everything in the right order.
289 QueuedSharedMemoryData queued_data;
290 queued_data.data = data_ptr + data_offset;
291 queued_data.length = data_length;
292 queued_data.encoded_length = encoded_data_length;
293 queued_data_.push_back(queued_data);
297 void ThreadedDataProvider::OnReceivedDataOnForegroundThread(
298 const char* data, int data_length, int encoded_data_length) {
299 DCHECK(ChildThreadImpl::current());
301 background_thread_.TaskRunner()->PostTask(
302 FROM_HERE, base::Bind(&ThreadedDataProvider::ForwardAndACKData,
303 base::Unretained(this), data, data_length,
304 encoded_data_length));
307 void ThreadedDataProvider::ForwardAndACKData(const char* data,
308 int data_length,
309 int encoded_data_length) {
310 DCHECK(background_thread_.isCurrentThread());
312 // TODO(oysteine): SiteIsolationPolicy needs to be be checked
313 // here before we pass the data to the data provider
314 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does
315 // actual blocking as opposed to just UMA logging this will bypass it.
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),
327 data_length,
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,
335 int data_length,
336 int encoded_data_length) {
337 if (data_copy) {
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