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/threadeddataprovider.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/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" | |
16 | |
17 namespace content { | |
18 | |
19 namespace { | |
20 | |
21 class DataProviderMessageFilter : public IPC::ChannelProxy::MessageFilter { | |
22 public: | |
23 DataProviderMessageFilter( | |
24 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
25 base::MessageLoop* main_thread_message_loop, | |
26 WebThreadImpl& background_thread, | |
darin (slow to review)
2014/03/19 03:51:28
nit: google c++ style forbids non-const reference
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Done.
| |
27 base::WeakPtr<ThreadedDataProvider> | |
darin (slow to review)
2014/03/19 03:51:28
nit: pass by |const base::WeakPtr<...>&| ?
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Done.
| |
28 background_thread_resource_provider, | |
29 base::WeakPtr<ThreadedDataProvider> | |
30 main_thread_resource_provider, | |
31 int request_id); | |
32 | |
33 // IPC::ChannelProxy::MessageFilter | |
34 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE FINAL; | |
35 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE FINAL; | |
36 | |
37 private: | |
38 virtual ~DataProviderMessageFilter(); | |
39 | |
40 void OnReceivedData(int request_id, int data_offset, int data_length, | |
41 int encoded_data_length); | |
42 | |
43 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | |
44 base::MessageLoop* main_thread_message_loop_; | |
45 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_; | |
53 }; | |
54 | |
55 DataProviderMessageFilter::DataProviderMessageFilter( | |
56 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
57 base::MessageLoop* main_thread_message_loop, | |
58 WebThreadImpl& background_thread, | |
59 base::WeakPtr<ThreadedDataProvider> | |
60 background_thread_resource_provider, | |
61 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); | |
71 } | |
72 | |
73 void DataProviderMessageFilter::OnFilterAdded(IPC::Channel* channel) { | |
74 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
75 | |
76 main_thread_message_loop_->PostTask(FROM_HERE, | |
77 base::Bind( | |
78 &ThreadedDataProvider::OnResourceMessageFilterAddedMainThread, | |
79 main_thread_resource_provider_)); | |
80 } | |
81 | |
82 bool DataProviderMessageFilter::OnMessageReceived( | |
83 const IPC::Message& message) { | |
84 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
85 | |
86 if (message.type() != ResourceMsg_DataReceived::ID) | |
87 return false; | |
88 | |
89 int request_id; | |
90 | |
91 PickleIterator iter(message); | |
92 if (!message.ReadInt(&iter, &request_id)) { | |
93 NOTREACHED() << "malformed resource message"; | |
94 return true; | |
95 } | |
96 | |
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; | |
102 } | |
103 } | |
104 | |
105 return false; | |
106 } | |
107 | |
108 DataProviderMessageFilter::~DataProviderMessageFilter() { | |
109 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
110 } | |
111 | |
112 void DataProviderMessageFilter::OnReceivedData(int request_id, | |
113 int data_offset, | |
darin (slow to review)
2014/03/19 03:51:28
nit: indentation
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Done.
| |
114 int data_length, | |
115 int encoded_data_length) { | |
116 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
117 background_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
118 &ThreadedDataProvider::OnReceivedDataOnBackgroundThread, | |
119 background_thread_resource_provider_, | |
120 data_offset, data_length, encoded_data_length)); | |
121 } | |
122 | |
123 } // anonymous namespace | |
124 | |
125 ThreadedDataProvider::ThreadedDataProvider( | |
126 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver, | |
127 linked_ptr<base::SharedMemory> shm_buffer, int shm_size) | |
128 : request_id_(request_id), | |
129 shm_buffer_(shm_buffer), | |
130 shm_size_(shm_size), | |
131 background_thread_weak_factory_(this), | |
132 main_thread_weak_factory_(this), | |
133 // Is this casting kosher, when we have a Chrome-constructed object sent | |
134 // back up from Blink? | |
135 background_thread_( | |
136 static_cast<WebThreadImpl&>( | |
137 *threaded_data_receiver->backgroundThread())), | |
138 ipc_channel_(ChildThread::current()->channel()), | |
139 threaded_data_receiver_(threaded_data_receiver), | |
140 resource_filter_active_(false), | |
141 main_thread_message_loop_(ChildThread::current()->message_loop()) { | |
142 DCHECK(ChildThread::current()); | |
143 DCHECK(ipc_channel_); | |
144 DCHECK(threaded_data_receiver_); | |
145 DCHECK(main_thread_message_loop_); | |
146 | |
147 filter_ = new DataProviderMessageFilter( | |
148 ChildProcess::current()->io_message_loop_proxy(), | |
149 main_thread_message_loop_, | |
150 background_thread_, | |
151 background_thread_weak_factory_.GetWeakPtr(), | |
152 main_thread_weak_factory_.GetWeakPtr(), | |
153 request_id); | |
154 | |
155 ChildThread::current()->channel()->AddFilter(filter_.get()); | |
156 } | |
157 | |
158 ThreadedDataProvider::~ThreadedDataProvider() { | |
159 DCHECK(ChildThread::current()); | |
160 | |
161 // Release the filter from our locally held member variable before | |
darin (slow to review)
2014/03/19 03:51:28
A better approach here is to use the traits templa
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Looking through this again it seems whatever reaso
| |
162 // posting a message to remove it from the I/O thread, to | |
163 // make sure there's no race conditions and it's always destructed | |
164 // on the I/O thread. | |
165 // Ideally we'd also assert on refcount = 2 here... | |
166 IPC::ChannelProxy::MessageFilter* filter = filter_.get(); | |
167 filter_ = nullptr; | |
darin (slow to review)
2014/03/19 03:51:28
i don't think we use nullptr in chromium code yet.
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Done.
| |
168 ChildThread::current()->channel()->RemoveFilter(filter); | |
169 | |
170 delete threaded_data_receiver_; | |
171 } | |
172 | |
173 void DestructOnMainThread(ThreadedDataProvider* dataProvider) { | |
darin (slow to review)
2014/03/19 03:51:28
nit: dataProvider -> data_provider
(please double
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Sorry about all of these; the Chrome<->Blink style
| |
174 DCHECK(ChildThread::current()); | |
175 | |
176 // The ThreadedDataProvider must be destructed on the main thread to | |
177 // be threadsafe when removing the message filter and releasing the shared | |
178 // memory buffer. | |
179 delete dataProvider; | |
180 } | |
181 | |
182 void ThreadedDataProvider::Stop() { | |
183 DCHECK(ChildThread::current()); | |
184 | |
185 // We can't destroy this instance directly; we need to bounce a message over | |
186 // to the background thread and back to make sure nothing else will access it | |
187 // there, before we can destruct it. | |
188 background_thread_.message_loop()->PostTask(FROM_HERE, | |
189 base::Bind(&ThreadedDataProvider::StopOnBackgroundThread, | |
190 base::Unretained(this))); | |
191 } | |
192 | |
193 void ThreadedDataProvider::StopOnBackgroundThread() { | |
194 DCHECK(background_thread_.isCurrentThread()); | |
195 | |
196 // When this happens, the provider should no longer be called on the | |
197 // background thread as it's about to be destroyed on the main | |
198 // thread. Invalidating the weak pointers means no callbacks from the filter | |
199 // will happen and nothing else will use this instance on the | |
200 // background thread. | |
201 background_thread_weak_factory_.InvalidateWeakPtrs(); | |
202 main_thread_message_loop_->PostTask(FROM_HERE, | |
203 base::Bind(&DestructOnMainThread, this)); | |
204 } | |
205 | |
206 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() { | |
207 DCHECK(ChildThread::current()); | |
208 | |
209 // We bounce this message from the I/O thread via the main thread and then | |
210 // to our background thread, following the same path as incoming data before | |
211 // our filter gets added, to make sure there's nothing still incoming. | |
212 background_thread_.message_loop()->PostTask(FROM_HERE, | |
213 base::Bind( | |
214 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread, | |
215 background_thread_weak_factory_.GetWeakPtr())); | |
216 } | |
217 | |
218 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() { | |
219 resource_filter_active_ = true; | |
darin (slow to review)
2014/03/19 03:51:28
nit: add DCHECK(background_thread_.isCurrentThread
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Done.
| |
220 | |
221 // At this point we know no more data is going to arrive from the main thread, | |
222 // so we can process any data we've received directly from the I/O thread | |
223 // in the meantime. | |
224 if (!queued_data_.empty()) { | |
225 std::vector<QueuedSharedMemoryData>::iterator iter = queued_data_.begin(); | |
226 for (; iter != queued_data_.end(); ++iter) | |
227 ForwardAndACKData(iter->data, iter->length); | |
228 | |
229 std::vector<QueuedSharedMemoryData> emptyList; | |
darin (slow to review)
2014/03/19 03:51:28
nit: emptyList -> empty_list
| |
230 // We'll never need this again, so free the memory. | |
231 queued_data_.swap(emptyList); | |
darin (slow to review)
2014/03/19 03:51:28
nit: you could also just call .clear() on the vect
oystein (OOO til 10th of July)
2014/03/21 19:26:15
AFAIK .clear() doesn't actually free any memory, b
| |
232 } | |
233 } | |
234 | |
235 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread( | |
236 int data_offset, int data_length, int encoded_data_length) { | |
237 DCHECK(background_thread_.isCurrentThread()); | |
238 DCHECK(shm_buffer_ != NULL); | |
239 | |
240 CHECK_GE(shm_size_, data_offset + data_length); | |
241 const char* data_ptr = static_cast<char*>(shm_buffer_->memory()); | |
242 CHECK(data_ptr); | |
243 CHECK(data_ptr + data_offset); | |
244 | |
245 if (resource_filter_active_) { | |
246 ForwardAndACKData(data_ptr + data_offset, data_length); | |
247 } else { | |
248 // There's a brief interval between the point where we know the filter | |
249 // has been installed on the I/O thread, and when we know for sure there's | |
250 // no more data coming in from the main thread (from before the filter | |
251 // got added). If we get any data during that interval, we need to queue | |
252 // it until we're certain we've processed all the main thread data to make | |
253 // sure we forward (and ACK) everything in the right order. | |
254 QueuedSharedMemoryData queuedData; | |
255 queuedData.data = data_ptr + data_offset; | |
darin (slow to review)
2014/03/19 03:51:28
nit: queuedData -> queued_data
| |
256 queuedData.length = data_length; | |
257 queued_data_.push_back(queuedData); | |
258 } | |
259 } | |
260 | |
261 void ThreadedDataProvider::OnReceivedDataOnForegroundThread( | |
262 const char* data, int data_length, int encoded_data_length) { | |
263 DCHECK(ChildThread::current()); | |
264 | |
265 background_thread_.message_loop()->PostTask(FROM_HERE, | |
266 base::Bind(&ThreadedDataProvider::ForwardAndACKData, | |
267 base::Unretained(this), | |
268 data, data_length)); | |
269 } | |
270 | |
271 void ThreadedDataProvider::ForwardAndACKData(const char* data, | |
272 int data_length) { | |
273 DCHECK(background_thread_.isCurrentThread()); | |
274 | |
275 // TODO(oysteine): SiteIsolationPolicy needs to be be checked | |
darin (slow to review)
2014/03/19 03:51:28
please make sure that creis@ is aware of this.
oystein (OOO til 10th of July)
2014/03/21 19:26:15
Yeah we've discussed it in the past, and I landed
| |
276 // here before we pass the data to the data provider | |
277 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does | |
278 // actual blocking as opposed to just UMA logging this will bypass it. | |
279 threaded_data_receiver_->acceptData(data, data_length); | |
280 ipc_channel_->Send(new ResourceHostMsg_DataReceived_ACK(request_id_)); | |
281 } | |
282 | |
283 } // namespace content | |
OLD | NEW |