| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/threaded_data_provider.h" | 5 #include "content/child/threaded_data_provider.h" |
| 6 | 6 |
| 7 #include "content/child/child_process.h" | 7 #include "content/child/child_process.h" |
| 8 #include "content/child/child_thread_impl.h" | 8 #include "content/child/child_thread_impl.h" |
| 9 #include "content/child/resource_dispatcher.h" | 9 #include "content/child/resource_dispatcher.h" |
| 10 #include "content/child/thread_safe_sender.h" | 10 #include "content/child/thread_safe_sender.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 } | 101 } |
| 102 | 102 |
| 103 return false; | 103 return false; |
| 104 } | 104 } |
| 105 | 105 |
| 106 void DataProviderMessageFilter::OnReceivedData(int request_id, | 106 void DataProviderMessageFilter::OnReceivedData(int request_id, |
| 107 int data_offset, | 107 int data_offset, |
| 108 int data_length, | 108 int data_length, |
| 109 int encoded_data_length) { | 109 int encoded_data_length) { |
| 110 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 110 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 111 background_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | 111 background_thread_.TaskRunner()->PostTask( |
| 112 &ThreadedDataProvider::OnReceivedDataOnBackgroundThread, | 112 FROM_HERE, |
| 113 background_thread_resource_provider_, | 113 base::Bind(&ThreadedDataProvider::OnReceivedDataOnBackgroundThread, |
| 114 data_offset, data_length, encoded_data_length)); | 114 background_thread_resource_provider_, data_offset, data_length, |
| 115 encoded_data_length)); |
| 115 } | 116 } |
| 116 | 117 |
| 117 } // anonymous namespace | 118 } // anonymous namespace |
| 118 | 119 |
| 119 ThreadedDataProvider::ThreadedDataProvider( | 120 ThreadedDataProvider::ThreadedDataProvider( |
| 120 int request_id, | 121 int request_id, |
| 121 blink::WebThreadedDataReceiver* threaded_data_receiver, | 122 blink::WebThreadedDataReceiver* threaded_data_receiver, |
| 122 linked_ptr<base::SharedMemory> shm_buffer, | 123 linked_ptr<base::SharedMemory> shm_buffer, |
| 123 int shm_size, | 124 int shm_size, |
| 124 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 125 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // We can't destroy this instance directly; we need to bounce a message over | 180 // 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 // 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 // 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 // thread is still alive, since Blink could have shut down at this point |
| 183 // and freed the thread. | 184 // and freed the thread. |
| 184 if (current_background_thread) { | 185 if (current_background_thread) { |
| 185 // We should never end up with a different parser thread than from when the | 186 // We should never end up with a different parser thread than from when the |
| 186 // ThreadedDataProvider gets created. | 187 // ThreadedDataProvider gets created. |
| 187 DCHECK(current_background_thread == | 188 DCHECK(current_background_thread == |
| 188 static_cast<WebThreadImpl*>(&background_thread_)); | 189 static_cast<WebThreadImpl*>(&background_thread_)); |
| 189 background_thread_.message_loop()->PostTask(FROM_HERE, | 190 background_thread_.TaskRunner()->PostTask( |
| 190 base::Bind(&ThreadedDataProvider::StopOnBackgroundThread, | 191 FROM_HERE, base::Bind(&ThreadedDataProvider::StopOnBackgroundThread, |
| 191 base::Unretained(this))); | 192 base::Unretained(this))); |
| 192 } | 193 } |
| 193 } | 194 } |
| 194 | 195 |
| 195 void ThreadedDataProvider::StopOnBackgroundThread() { | 196 void ThreadedDataProvider::StopOnBackgroundThread() { |
| 196 DCHECK(background_thread_.isCurrentThread()); | 197 DCHECK(background_thread_.isCurrentThread()); |
| 197 DCHECK(background_thread_weak_factory_); | 198 DCHECK(background_thread_weak_factory_); |
| 198 | 199 |
| 199 // When this happens, the provider should no longer be called on the | 200 // 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 // 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 // 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 // which means no callbacks from the filter will happen and nothing else will |
| 203 // use this instance on the background thread. | 204 // use this instance on the background thread. |
| 204 background_thread_weak_factory_.reset(NULL); | 205 background_thread_weak_factory_.reset(NULL); |
| 205 main_thread_task_runner_->PostTask(FROM_HERE, | 206 main_thread_task_runner_->PostTask(FROM_HERE, |
| 206 base::Bind(&ThreadedDataProvider::DestructOnMainThread, | 207 base::Bind(&ThreadedDataProvider::DestructOnMainThread, |
| 207 base::Unretained(this))); | 208 base::Unretained(this))); |
| 208 } | 209 } |
| 209 | 210 |
| 210 void ThreadedDataProvider::OnRequestCompleteForegroundThread( | 211 void ThreadedDataProvider::OnRequestCompleteForegroundThread( |
| 211 base::WeakPtr<ResourceDispatcher> resource_dispatcher, | 212 base::WeakPtr<ResourceDispatcher> resource_dispatcher, |
| 212 const ResourceMsg_RequestCompleteData& request_complete_data, | 213 const ResourceMsg_RequestCompleteData& request_complete_data, |
| 213 const base::TimeTicks& renderer_completion_time) { | 214 const base::TimeTicks& renderer_completion_time) { |
| 214 DCHECK(ChildThreadImpl::current()); | 215 DCHECK(ChildThreadImpl::current()); |
| 215 | 216 |
| 216 background_thread_.message_loop()->PostTask(FROM_HERE, | 217 background_thread_.TaskRunner()->PostTask( |
| 218 FROM_HERE, |
| 217 base::Bind(&ThreadedDataProvider::OnRequestCompleteBackgroundThread, | 219 base::Bind(&ThreadedDataProvider::OnRequestCompleteBackgroundThread, |
| 218 base::Unretained(this), resource_dispatcher, | 220 base::Unretained(this), resource_dispatcher, |
| 219 request_complete_data, renderer_completion_time)); | 221 request_complete_data, renderer_completion_time)); |
| 220 } | 222 } |
| 221 | 223 |
| 222 void ThreadedDataProvider::OnRequestCompleteBackgroundThread( | 224 void ThreadedDataProvider::OnRequestCompleteBackgroundThread( |
| 223 base::WeakPtr<ResourceDispatcher> resource_dispatcher, | 225 base::WeakPtr<ResourceDispatcher> resource_dispatcher, |
| 224 const ResourceMsg_RequestCompleteData& request_complete_data, | 226 const ResourceMsg_RequestCompleteData& request_complete_data, |
| 225 const base::TimeTicks& renderer_completion_time) { | 227 const base::TimeTicks& renderer_completion_time) { |
| 226 DCHECK(background_thread_.isCurrentThread()); | 228 DCHECK(background_thread_.isCurrentThread()); |
| 227 | 229 |
| 228 main_thread_task_runner_->PostTask(FROM_HERE, | 230 main_thread_task_runner_->PostTask(FROM_HERE, |
| 229 base::Bind( | 231 base::Bind( |
| 230 &ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush, | 232 &ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush, |
| 231 resource_dispatcher, | 233 resource_dispatcher, |
| 232 request_id_, | 234 request_id_, |
| 233 request_complete_data, | 235 request_complete_data, |
| 234 renderer_completion_time)); | 236 renderer_completion_time)); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() { | 239 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() { |
| 238 DCHECK(ChildThreadImpl::current()); | 240 DCHECK(ChildThreadImpl::current()); |
| 239 DCHECK(background_thread_weak_factory_); | 241 DCHECK(background_thread_weak_factory_); |
| 240 | 242 |
| 241 // We bounce this message from the I/O thread via the main thread and then | 243 // 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 | 244 // 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. | 245 // our filter gets added, to make sure there's nothing still incoming. |
| 244 background_thread_.message_loop()->PostTask(FROM_HERE, | 246 background_thread_.TaskRunner()->PostTask( |
| 247 FROM_HERE, |
| 245 base::Bind( | 248 base::Bind( |
| 246 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread, | 249 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread, |
| 247 background_thread_weak_factory_->GetWeakPtr())); | 250 background_thread_weak_factory_->GetWeakPtr())); |
| 248 } | 251 } |
| 249 | 252 |
| 250 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() { | 253 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() { |
| 251 DCHECK(background_thread_.isCurrentThread()); | 254 DCHECK(background_thread_.isCurrentThread()); |
| 252 resource_filter_active_ = true; | 255 resource_filter_active_ = true; |
| 253 | 256 |
| 254 // At this point we know no more data is going to arrive from the main thread, | 257 // At this point we know no more data is going to arrive from the main thread, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 queued_data.length = data_length; | 291 queued_data.length = data_length; |
| 289 queued_data.encoded_length = encoded_data_length; | 292 queued_data.encoded_length = encoded_data_length; |
| 290 queued_data_.push_back(queued_data); | 293 queued_data_.push_back(queued_data); |
| 291 } | 294 } |
| 292 } | 295 } |
| 293 | 296 |
| 294 void ThreadedDataProvider::OnReceivedDataOnForegroundThread( | 297 void ThreadedDataProvider::OnReceivedDataOnForegroundThread( |
| 295 const char* data, int data_length, int encoded_data_length) { | 298 const char* data, int data_length, int encoded_data_length) { |
| 296 DCHECK(ChildThreadImpl::current()); | 299 DCHECK(ChildThreadImpl::current()); |
| 297 | 300 |
| 298 background_thread_.message_loop()->PostTask(FROM_HERE, | 301 background_thread_.TaskRunner()->PostTask( |
| 299 base::Bind(&ThreadedDataProvider::ForwardAndACKData, | 302 FROM_HERE, base::Bind(&ThreadedDataProvider::ForwardAndACKData, |
| 300 base::Unretained(this), | 303 base::Unretained(this), data, data_length, |
| 301 data, data_length, encoded_data_length)); | 304 encoded_data_length)); |
| 302 } | 305 } |
| 303 | 306 |
| 304 void ThreadedDataProvider::ForwardAndACKData(const char* data, | 307 void ThreadedDataProvider::ForwardAndACKData(const char* data, |
| 305 int data_length, | 308 int data_length, |
| 306 int encoded_data_length) { | 309 int encoded_data_length) { |
| 307 DCHECK(background_thread_.isCurrentThread()); | 310 DCHECK(background_thread_.isCurrentThread()); |
| 308 | 311 |
| 309 // TODO(oysteine): SiteIsolationPolicy needs to be be checked | 312 // TODO(oysteine): SiteIsolationPolicy needs to be be checked |
| 310 // here before we pass the data to the data provider | 313 // here before we pass the data to the data provider |
| 311 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does | 314 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does |
| (...skipping 23 matching lines...) Expand all Loading... |
| 335 DCHECK(threaded_data_receiver_->needsMainthreadDataCopy()); | 338 DCHECK(threaded_data_receiver_->needsMainthreadDataCopy()); |
| 336 DCHECK_EQ((size_t)data_length, data_copy->size()); | 339 DCHECK_EQ((size_t)data_length, data_copy->size()); |
| 337 } | 340 } |
| 338 | 341 |
| 339 threaded_data_receiver_->acceptMainthreadDataNotification( | 342 threaded_data_receiver_->acceptMainthreadDataNotification( |
| 340 (data_copy && !data_copy->empty()) ? &data_copy->front() : NULL, | 343 (data_copy && !data_copy->empty()) ? &data_copy->front() : NULL, |
| 341 data_length, encoded_data_length); | 344 data_length, encoded_data_length); |
| 342 } | 345 } |
| 343 | 346 |
| 344 } // namespace content | 347 } // namespace content |
| OLD | NEW |