Added unit test for DevTools' ephemeral port support.
[chromium-blink-merge.git] / content / child / threaded_data_provider.cc
blob84fa65402594b1a9b6d63ab7650a35809f3d41df
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.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 base::MessageLoop* main_thread_message_loop,
26 const WebThreadImpl& background_thread,
27 const base::WeakPtr<ThreadedDataProvider>&
28 background_thread_resource_provider,
29 const base::WeakPtr<ThreadedDataProvider>&
30 main_thread_resource_provider,
31 int request_id);
33 // IPC::ChannelProxy::MessageFilter
34 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE FINAL;
35 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE FINAL;
37 private:
38 virtual ~DataProviderMessageFilter() { }
40 void OnReceivedData(int request_id, int data_offset, int data_length,
41 int encoded_data_length);
43 const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
44 base::MessageLoop* main_thread_message_loop_;
45 const WebThreadImpl& background_thread_;
46 // This weakptr can only be dereferenced on the background thread.
47 base::WeakPtr<ThreadedDataProvider>
48 background_thread_resource_provider_;
49 // This weakptr can only be dereferenced on the main thread.
50 base::WeakPtr<ThreadedDataProvider>
51 main_thread_resource_provider_;
52 int request_id_;
55 DataProviderMessageFilter::DataProviderMessageFilter(
56 const scoped_refptr<base::MessageLoopProxy>& io_message_loop,
57 base::MessageLoop* main_thread_message_loop,
58 const WebThreadImpl& background_thread,
59 const base::WeakPtr<ThreadedDataProvider>&
60 background_thread_resource_provider,
61 const base::WeakPtr<ThreadedDataProvider>&
62 main_thread_resource_provider,
63 int request_id)
64 : io_message_loop_(io_message_loop),
65 main_thread_message_loop_(main_thread_message_loop),
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_message_loop != NULL);
73 void DataProviderMessageFilter::OnFilterAdded(IPC::Channel* channel) {
74 DCHECK(io_message_loop_->BelongsToCurrentThread());
76 main_thread_message_loop_->PostTask(FROM_HERE,
77 base::Bind(
78 &ThreadedDataProvider::OnResourceMessageFilterAddedMainThread,
79 main_thread_resource_provider_));
82 bool DataProviderMessageFilter::OnMessageReceived(
83 const IPC::Message& message) {
84 DCHECK(io_message_loop_->BelongsToCurrentThread());
86 if (message.type() != ResourceMsg_DataReceived::ID)
87 return false;
89 int request_id;
91 PickleIterator iter(message);
92 if (!message.ReadInt(&iter, &request_id)) {
93 NOTREACHED() << "malformed resource message";
94 return true;
97 if (request_id == request_id_) {
98 ResourceMsg_DataReceived::Schema::Param arg;
99 if (ResourceMsg_DataReceived::Read(&message, &arg)) {
100 OnReceivedData(arg.a, arg.b, arg.c, arg.d);
101 return true;
105 return false;
108 void DataProviderMessageFilter::OnReceivedData(int request_id,
109 int data_offset,
110 int data_length,
111 int encoded_data_length) {
112 DCHECK(io_message_loop_->BelongsToCurrentThread());
113 background_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
114 &ThreadedDataProvider::OnReceivedDataOnBackgroundThread,
115 background_thread_resource_provider_,
116 data_offset, data_length, encoded_data_length));
119 } // anonymous namespace
121 ThreadedDataProvider::ThreadedDataProvider(
122 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver,
123 linked_ptr<base::SharedMemory> shm_buffer, int shm_size)
124 : request_id_(request_id),
125 shm_buffer_(shm_buffer),
126 shm_size_(shm_size),
127 main_thread_weak_factory_(this),
128 background_thread_(
129 static_cast<WebThreadImpl&>(
130 *threaded_data_receiver->backgroundThread())),
131 ipc_channel_(ChildThread::current()->channel()),
132 threaded_data_receiver_(threaded_data_receiver),
133 resource_filter_active_(false),
134 main_thread_message_loop_(ChildThread::current()->message_loop()) {
135 DCHECK(ChildThread::current());
136 DCHECK(ipc_channel_);
137 DCHECK(threaded_data_receiver_);
138 DCHECK(main_thread_message_loop_);
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_message_loop_,
146 background_thread_,
147 background_thread_weak_factory_->GetWeakPtr(),
148 main_thread_weak_factory_.GetWeakPtr(),
149 request_id);
151 ChildThread::current()->channel()->AddFilter(filter_);
154 ThreadedDataProvider::~ThreadedDataProvider() {
155 DCHECK(ChildThread::current());
157 ChildThread::current()->channel()->RemoveFilter(filter_);
159 delete threaded_data_receiver_;
162 void DestructOnMainThread(ThreadedDataProvider* data_provider) {
163 DCHECK(ChildThread::current());
165 // The ThreadedDataProvider must be destructed on the main thread to
166 // be threadsafe when removing the message filter and releasing the shared
167 // memory buffer.
168 delete data_provider;
171 void ThreadedDataProvider::Stop() {
172 DCHECK(ChildThread::current());
174 // Make sure we don't get called by on the main thread anymore via weak
175 // pointers we've passed to the filter.
176 main_thread_weak_factory_.InvalidateWeakPtrs();
178 // We can't destroy this instance directly; we need to bounce a message over
179 // to the background thread and back to make sure nothing else will access it
180 // there, before we can destruct it.
181 background_thread_.message_loop()->PostTask(FROM_HERE,
182 base::Bind(&ThreadedDataProvider::StopOnBackgroundThread,
183 base::Unretained(this)));
186 void ThreadedDataProvider::StopOnBackgroundThread() {
187 DCHECK(background_thread_.isCurrentThread());
188 DCHECK(background_thread_weak_factory_);
190 // When this happens, the provider should no longer be called on the
191 // background thread as it's about to be destroyed on the main thread.
192 // Destructing the weak pointer factory means invalidating the weak pointers
193 // which means no callbacks from the filter will happen and nothing else will
194 // use this instance on the background thread.
195 background_thread_weak_factory_.reset(NULL);
196 main_thread_message_loop_->PostTask(FROM_HERE,
197 base::Bind(&DestructOnMainThread, this));
200 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() {
201 DCHECK(ChildThread::current());
202 DCHECK(background_thread_weak_factory_);
204 // We bounce this message from the I/O thread via the main thread and then
205 // to our background thread, following the same path as incoming data before
206 // our filter gets added, to make sure there's nothing still incoming.
207 background_thread_.message_loop()->PostTask(FROM_HERE,
208 base::Bind(
209 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread,
210 background_thread_weak_factory_->GetWeakPtr()));
213 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() {
214 DCHECK(background_thread_.isCurrentThread());
215 resource_filter_active_ = true;
217 // At this point we know no more data is going to arrive from the main thread,
218 // so we can process any data we've received directly from the I/O thread
219 // in the meantime.
220 if (!queued_data_.empty()) {
221 std::vector<QueuedSharedMemoryData>::iterator iter = queued_data_.begin();
222 for (; iter != queued_data_.end(); ++iter) {
223 ForwardAndACKData(iter->data, iter->length);
226 queued_data_.clear();
230 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread(
231 int data_offset, int data_length, int encoded_data_length) {
232 DCHECK(background_thread_.isCurrentThread());
233 DCHECK(shm_buffer_ != NULL);
235 CHECK_GE(shm_size_, data_offset + data_length);
236 const char* data_ptr = static_cast<char*>(shm_buffer_->memory());
237 CHECK(data_ptr);
238 CHECK(data_ptr + data_offset);
240 if (resource_filter_active_) {
241 ForwardAndACKData(data_ptr + data_offset, data_length);
242 } else {
243 // There's a brief interval between the point where we know the filter
244 // has been installed on the I/O thread, and when we know for sure there's
245 // no more data coming in from the main thread (from before the filter
246 // got added). If we get any data during that interval, we need to queue
247 // it until we're certain we've processed all the main thread data to make
248 // sure we forward (and ACK) everything in the right order.
249 QueuedSharedMemoryData queued_data;
250 queued_data.data = data_ptr + data_offset;
251 queued_data.length = data_length;
252 queued_data_.push_back(queued_data);
256 void ThreadedDataProvider::OnReceivedDataOnForegroundThread(
257 const char* data, int data_length, int encoded_data_length) {
258 DCHECK(ChildThread::current());
260 background_thread_.message_loop()->PostTask(FROM_HERE,
261 base::Bind(&ThreadedDataProvider::ForwardAndACKData,
262 base::Unretained(this),
263 data, data_length));
266 void ThreadedDataProvider::ForwardAndACKData(const char* data,
267 int data_length) {
268 DCHECK(background_thread_.isCurrentThread());
270 // TODO(oysteine): SiteIsolationPolicy needs to be be checked
271 // here before we pass the data to the data provider
272 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does
273 // actual blocking as opposed to just UMA logging this will bypass it.
274 threaded_data_receiver_->acceptData(data, data_length);
275 ipc_channel_->Send(new ResourceHostMsg_DataReceived_ACK(request_id_));
278 } // namespace content