| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 WTF::MakeUnique<Vector<char>>(data_length); | 58 WTF::MakeUnique<Vector<char>>(data_length); |
| 59 memcpy(buffer->data(), data, data_length); | 59 memcpy(buffer->data(), data, data_length); |
| 60 return buffer; | 60 return buffer; |
| 61 } | 61 } |
| 62 | 62 |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 class WorkerThreadableLoader::AsyncTaskForwarder final | 65 class WorkerThreadableLoader::AsyncTaskForwarder final |
| 66 : public WorkerThreadableLoader::TaskForwarder { | 66 : public WorkerThreadableLoader::TaskForwarder { |
| 67 public: | 67 public: |
| 68 explicit AsyncTaskForwarder(PassRefPtr<WorkerLoaderProxy> loader_proxy) | 68 explicit AsyncTaskForwarder(RefPtr<WebTaskRunner> worker_loading_task_runner) |
| 69 : loader_proxy_(std::move(loader_proxy)) { | 69 : worker_loading_task_runner_(std::move(worker_loading_task_runner)) { |
| 70 DCHECK(IsMainThread()); | 70 DCHECK(IsMainThread()); |
| 71 } | 71 } |
| 72 ~AsyncTaskForwarder() override { DCHECK(IsMainThread()); } | 72 ~AsyncTaskForwarder() override { DCHECK(IsMainThread()); } |
| 73 | 73 |
| 74 void ForwardTask(const WebTraceLocation& location, | 74 void ForwardTask(const WebTraceLocation& location, |
| 75 std::unique_ptr<CrossThreadClosure> task) override { | 75 std::unique_ptr<CrossThreadClosure> task) override { |
| 76 DCHECK(IsMainThread()); | 76 DCHECK(IsMainThread()); |
| 77 loader_proxy_->PostTaskToWorkerGlobalScope(location, std::move(task)); | 77 worker_loading_task_runner_->PostTask(location, std::move(task)); |
| 78 } | 78 } |
| 79 void ForwardTaskWithDoneSignal( | 79 void ForwardTaskWithDoneSignal( |
| 80 const WebTraceLocation& location, | 80 const WebTraceLocation& location, |
| 81 std::unique_ptr<CrossThreadClosure> task) override { | 81 std::unique_ptr<CrossThreadClosure> task) override { |
| 82 DCHECK(IsMainThread()); | 82 DCHECK(IsMainThread()); |
| 83 loader_proxy_->PostTaskToWorkerGlobalScope(location, std::move(task)); | 83 worker_loading_task_runner_->PostTask(location, std::move(task)); |
| 84 } | 84 } |
| 85 void Abort() override { DCHECK(IsMainThread()); } | 85 void Abort() override { DCHECK(IsMainThread()); } |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 RefPtr<WorkerLoaderProxy> loader_proxy_; | 88 RefPtr<WebTaskRunner> worker_loading_task_runner_; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 struct WorkerThreadableLoader::TaskWithLocation final { | 91 struct WorkerThreadableLoader::TaskWithLocation final { |
| 92 TaskWithLocation(const WebTraceLocation& location, | 92 TaskWithLocation(const WebTraceLocation& location, |
| 93 std::unique_ptr<CrossThreadClosure> task) | 93 std::unique_ptr<CrossThreadClosure> task) |
| 94 : location_(location), task_(std::move(task)) {} | 94 : location_(location), task_(std::move(task)) {} |
| 95 TaskWithLocation(TaskWithLocation&& task) | 95 TaskWithLocation(TaskWithLocation&& task) |
| 96 : TaskWithLocation(task.location_, std::move(task.task_)) {} | 96 : TaskWithLocation(task.location_, std::move(task.task_)) {} |
| 97 ~TaskWithLocation() = default; | 97 ~TaskWithLocation() = default; |
| 98 | 98 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 192 |
| 193 WorkerThreadableLoader::WorkerThreadableLoader( | 193 WorkerThreadableLoader::WorkerThreadableLoader( |
| 194 WorkerGlobalScope& worker_global_scope, | 194 WorkerGlobalScope& worker_global_scope, |
| 195 ThreadableLoaderClient* client, | 195 ThreadableLoaderClient* client, |
| 196 const ThreadableLoaderOptions& options, | 196 const ThreadableLoaderOptions& options, |
| 197 const ResourceLoaderOptions& resource_loader_options, | 197 const ResourceLoaderOptions& resource_loader_options, |
| 198 BlockingBehavior blocking_behavior) | 198 BlockingBehavior blocking_behavior) |
| 199 : worker_global_scope_(&worker_global_scope), | 199 : worker_global_scope_(&worker_global_scope), |
| 200 worker_loader_proxy_( | 200 worker_loader_proxy_( |
| 201 worker_global_scope.GetThread()->GetWorkerLoaderProxy()), | 201 worker_global_scope.GetThread()->GetWorkerLoaderProxy()), |
| 202 parent_frame_task_runners_( |
| 203 worker_global_scope.GetThread()->GetParentFrameTaskRunners()), |
| 202 client_(client), | 204 client_(client), |
| 203 threadable_loader_options_(options), | 205 threadable_loader_options_(options), |
| 204 resource_loader_options_(resource_loader_options), | 206 resource_loader_options_(resource_loader_options), |
| 205 blocking_behavior_(blocking_behavior) { | 207 blocking_behavior_(blocking_behavior) { |
| 206 DCHECK(client); | 208 DCHECK(client); |
| 207 } | 209 } |
| 208 | 210 |
| 209 void WorkerThreadableLoader::LoadResourceSynchronously( | 211 void WorkerThreadableLoader::LoadResourceSynchronously( |
| 210 WorkerGlobalScope& worker_global_scope, | 212 WorkerGlobalScope& worker_global_scope, |
| 211 const ResourceRequest& request, | 213 const ResourceRequest& request, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 228 request.SetHTTPReferrer(SecurityPolicy::GenerateReferrer( | 230 request.SetHTTPReferrer(SecurityPolicy::GenerateReferrer( |
| 229 worker_global_scope_->GetReferrerPolicy(), request.Url(), | 231 worker_global_scope_->GetReferrerPolicy(), request.Url(), |
| 230 worker_global_scope_->OutgoingReferrer())); | 232 worker_global_scope_->OutgoingReferrer())); |
| 231 } | 233 } |
| 232 | 234 |
| 233 DCHECK(!IsMainThread()); | 235 DCHECK(!IsMainThread()); |
| 234 RefPtr<WaitableEventWithTasks> event_with_tasks; | 236 RefPtr<WaitableEventWithTasks> event_with_tasks; |
| 235 if (blocking_behavior_ == kLoadSynchronously) | 237 if (blocking_behavior_ == kLoadSynchronously) |
| 236 event_with_tasks = WaitableEventWithTasks::Create(); | 238 event_with_tasks = WaitableEventWithTasks::Create(); |
| 237 | 239 |
| 238 worker_loader_proxy_->PostTaskToLoader( | 240 RefPtr<WebTaskRunner> worker_loading_task_runner = TaskRunnerHelper::Get( |
| 239 BLINK_FROM_HERE, | 241 TaskType::kUnspecedLoading, worker_global_scope_.Get()); |
| 240 CrossThreadBind( | 242 parent_frame_task_runners_->Get(TaskType::kUnspecedLoading) |
| 241 &MainThreadLoaderHolder::CreateAndStart, | 243 ->PostTask( |
| 242 WrapCrossThreadPersistent(this), worker_loader_proxy_, | 244 BLINK_FROM_HERE, |
| 243 WrapCrossThreadPersistent(worker_global_scope_->GetThread() | 245 CrossThreadBind(&MainThreadLoaderHolder::CreateAndStart, |
| 244 ->GetWorkerThreadLifecycleContext()), | 246 WrapCrossThreadPersistent(this), worker_loader_proxy_, |
| 245 request, threadable_loader_options_, resource_loader_options_, | 247 std::move(worker_loading_task_runner), |
| 246 event_with_tasks)); | 248 WrapCrossThreadPersistent( |
| 249 worker_global_scope_->GetThread() |
| 250 ->GetWorkerThreadLifecycleContext()), |
| 251 request, threadable_loader_options_, |
| 252 resource_loader_options_, event_with_tasks)); |
| 247 | 253 |
| 248 if (blocking_behavior_ == kLoadAsynchronously) | 254 if (blocking_behavior_ == kLoadAsynchronously) |
| 249 return; | 255 return; |
| 250 | 256 |
| 251 event_with_tasks->Wait(); | 257 event_with_tasks->Wait(); |
| 252 | 258 |
| 253 if (event_with_tasks->IsAborted()) { | 259 if (event_with_tasks->IsAborted()) { |
| 254 // This thread is going to terminate. | 260 // This thread is going to terminate. |
| 255 Cancel(); | 261 Cancel(); |
| 256 return; | 262 return; |
| 257 } | 263 } |
| 258 | 264 |
| 259 for (const auto& task : event_with_tasks->Take()) { | 265 for (const auto& task : event_with_tasks->Take()) { |
| 260 // Store the program counter where the task is posted from, and alias | 266 // Store the program counter where the task is posted from, and alias |
| 261 // it to ensure it is stored in the crash dump. | 267 // it to ensure it is stored in the crash dump. |
| 262 const void* program_counter = task.location_.program_counter(); | 268 const void* program_counter = task.location_.program_counter(); |
| 263 WTF::debug::Alias(&program_counter); | 269 WTF::debug::Alias(&program_counter); |
| 264 | 270 |
| 265 (*task.task_)(); | 271 (*task.task_)(); |
| 266 } | 272 } |
| 267 } | 273 } |
| 268 | 274 |
| 269 void WorkerThreadableLoader::OverrideTimeout( | 275 void WorkerThreadableLoader::OverrideTimeout( |
| 270 unsigned long timeout_milliseconds) { | 276 unsigned long timeout_milliseconds) { |
| 271 DCHECK(!IsMainThread()); | 277 DCHECK(!IsMainThread()); |
| 272 if (!main_thread_loader_holder_) | 278 if (!main_thread_loader_holder_) |
| 273 return; | 279 return; |
| 274 worker_loader_proxy_->PostTaskToLoader( | 280 parent_frame_task_runners_->Get(TaskType::kUnspecedLoading) |
| 275 BLINK_FROM_HERE, | 281 ->PostTask( |
| 276 CrossThreadBind(&MainThreadLoaderHolder::OverrideTimeout, | 282 BLINK_FROM_HERE, |
| 277 main_thread_loader_holder_, timeout_milliseconds)); | 283 CrossThreadBind(&MainThreadLoaderHolder::OverrideTimeout, |
| 284 main_thread_loader_holder_, timeout_milliseconds)); |
| 278 } | 285 } |
| 279 | 286 |
| 280 void WorkerThreadableLoader::Cancel() { | 287 void WorkerThreadableLoader::Cancel() { |
| 281 DCHECK(!IsMainThread()); | 288 DCHECK(!IsMainThread()); |
| 282 if (main_thread_loader_holder_) { | 289 if (main_thread_loader_holder_) { |
| 283 worker_loader_proxy_->PostTaskToLoader( | 290 parent_frame_task_runners_->Get(TaskType::kUnspecedLoading) |
| 284 BLINK_FROM_HERE, CrossThreadBind(&MainThreadLoaderHolder::Cancel, | 291 ->PostTask(BLINK_FROM_HERE, |
| 285 main_thread_loader_holder_)); | 292 CrossThreadBind(&MainThreadLoaderHolder::Cancel, |
| 293 main_thread_loader_holder_)); |
| 286 main_thread_loader_holder_ = nullptr; | 294 main_thread_loader_holder_ = nullptr; |
| 287 } | 295 } |
| 288 | 296 |
| 289 if (!client_) | 297 if (!client_) |
| 290 return; | 298 return; |
| 291 | 299 |
| 292 // If the client hasn't reached a termination state, then transition it | 300 // If the client hasn't reached a termination state, then transition it |
| 293 // by sending a cancellation error. | 301 // by sending a cancellation error. |
| 294 // Note: no more client callbacks will be done after this method -- the | 302 // Note: no more client callbacks will be done after this method -- the |
| 295 // clearClient() call ensures that. | 303 // clearClient() call ensures that. |
| 296 ResourceError error(String(), 0, String(), String()); | 304 ResourceError error(String(), 0, String(), String()); |
| 297 error.SetIsCancellation(true); | 305 error.SetIsCancellation(true); |
| 298 DidFail(error); | 306 DidFail(error); |
| 299 DCHECK(!client_); | 307 DCHECK(!client_); |
| 300 } | 308 } |
| 301 | 309 |
| 302 void WorkerThreadableLoader::DidStart( | 310 void WorkerThreadableLoader::DidStart( |
| 303 MainThreadLoaderHolder* main_thread_loader_holder) { | 311 MainThreadLoaderHolder* main_thread_loader_holder) { |
| 304 DCHECK(!IsMainThread()); | 312 DCHECK(!IsMainThread()); |
| 305 DCHECK(!main_thread_loader_holder_); | 313 DCHECK(!main_thread_loader_holder_); |
| 306 DCHECK(main_thread_loader_holder); | 314 DCHECK(main_thread_loader_holder); |
| 307 if (!client_) { | 315 if (!client_) { |
| 308 // The thread is terminating. | 316 // The thread is terminating. |
| 309 worker_loader_proxy_->PostTaskToLoader( | 317 parent_frame_task_runners_->Get(TaskType::kUnspecedLoading) |
| 310 BLINK_FROM_HERE, | 318 ->PostTask(BLINK_FROM_HERE, |
| 311 CrossThreadBind(&MainThreadLoaderHolder::Cancel, | 319 CrossThreadBind( |
| 312 WrapCrossThreadPersistent(main_thread_loader_holder))); | 320 &MainThreadLoaderHolder::Cancel, |
| 321 WrapCrossThreadPersistent(main_thread_loader_holder))); |
| 313 return; | 322 return; |
| 314 } | 323 } |
| 315 | 324 |
| 316 main_thread_loader_holder_ = main_thread_loader_holder; | 325 main_thread_loader_holder_ = main_thread_loader_holder; |
| 317 } | 326 } |
| 318 | 327 |
| 319 void WorkerThreadableLoader::DidSendData( | 328 void WorkerThreadableLoader::DidSendData( |
| 320 unsigned long long bytes_sent, | 329 unsigned long long bytes_sent, |
| 321 unsigned long long total_bytes_to_be_sent) { | 330 unsigned long long total_bytes_to_be_sent) { |
| 322 DCHECK(!IsMainThread()); | 331 DCHECK(!IsMainThread()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 431 } |
| 423 | 432 |
| 424 DEFINE_TRACE(WorkerThreadableLoader) { | 433 DEFINE_TRACE(WorkerThreadableLoader) { |
| 425 visitor->Trace(worker_global_scope_); | 434 visitor->Trace(worker_global_scope_); |
| 426 ThreadableLoader::Trace(visitor); | 435 ThreadableLoader::Trace(visitor); |
| 427 } | 436 } |
| 428 | 437 |
| 429 void WorkerThreadableLoader::MainThreadLoaderHolder::CreateAndStart( | 438 void WorkerThreadableLoader::MainThreadLoaderHolder::CreateAndStart( |
| 430 WorkerThreadableLoader* worker_loader, | 439 WorkerThreadableLoader* worker_loader, |
| 431 RefPtr<WorkerLoaderProxy> loader_proxy, | 440 RefPtr<WorkerLoaderProxy> loader_proxy, |
| 441 RefPtr<WebTaskRunner> worker_loading_task_runner, |
| 432 WorkerThreadLifecycleContext* worker_thread_lifecycle_context, | 442 WorkerThreadLifecycleContext* worker_thread_lifecycle_context, |
| 433 std::unique_ptr<CrossThreadResourceRequestData> request, | 443 std::unique_ptr<CrossThreadResourceRequestData> request, |
| 434 const ThreadableLoaderOptions& options, | 444 const ThreadableLoaderOptions& options, |
| 435 const ResourceLoaderOptions& resource_loader_options, | 445 const ResourceLoaderOptions& resource_loader_options, |
| 436 PassRefPtr<WaitableEventWithTasks> event_with_tasks) { | 446 PassRefPtr<WaitableEventWithTasks> event_with_tasks) { |
| 437 DCHECK(IsMainThread()); | 447 DCHECK(IsMainThread()); |
| 438 TaskForwarder* forwarder; | 448 TaskForwarder* forwarder; |
| 439 ThreadableLoadingContext* loading_context = | 449 ThreadableLoadingContext* loading_context = |
| 440 loader_proxy->GetThreadableLoadingContext(); | 450 loader_proxy->GetThreadableLoadingContext(); |
| 441 if (!loading_context) | 451 if (!loading_context) |
| 442 return; | 452 return; |
| 443 if (event_with_tasks) | 453 if (event_with_tasks) |
| 444 forwarder = new SyncTaskForwarder(std::move(event_with_tasks)); | 454 forwarder = new SyncTaskForwarder(std::move(event_with_tasks)); |
| 445 else | 455 else |
| 446 forwarder = new AsyncTaskForwarder(std::move(loader_proxy)); | 456 forwarder = new AsyncTaskForwarder(std::move(worker_loading_task_runner)); |
| 447 | 457 |
| 448 MainThreadLoaderHolder* main_thread_loader_holder = | 458 MainThreadLoaderHolder* main_thread_loader_holder = |
| 449 new MainThreadLoaderHolder(forwarder, worker_thread_lifecycle_context); | 459 new MainThreadLoaderHolder(forwarder, worker_thread_lifecycle_context); |
| 450 if (main_thread_loader_holder->WasContextDestroyedBeforeObserverCreation()) { | 460 if (main_thread_loader_holder->WasContextDestroyedBeforeObserverCreation()) { |
| 451 // The thread is already terminating. | 461 // The thread is already terminating. |
| 452 forwarder->Abort(); | 462 forwarder->Abort(); |
| 453 main_thread_loader_holder->forwarder_ = nullptr; | 463 main_thread_loader_holder->forwarder_ = nullptr; |
| 454 return; | 464 return; |
| 455 } | 465 } |
| 456 main_thread_loader_holder->worker_loader_ = worker_loader; | 466 main_thread_loader_holder->worker_loader_ = worker_loader; |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 DCHECK(IsMainThread()); | 679 DCHECK(IsMainThread()); |
| 670 ResourceLoaderOptions resource_loader_options = | 680 ResourceLoaderOptions resource_loader_options = |
| 671 original_resource_loader_options; | 681 original_resource_loader_options; |
| 672 resource_loader_options.request_initiator_context = kWorkerContext; | 682 resource_loader_options.request_initiator_context = kWorkerContext; |
| 673 main_thread_loader_ = DocumentThreadableLoader::Create( | 683 main_thread_loader_ = DocumentThreadableLoader::Create( |
| 674 loading_context, this, options, resource_loader_options); | 684 loading_context, this, options, resource_loader_options); |
| 675 main_thread_loader_->Start(ResourceRequest(request.get())); | 685 main_thread_loader_->Start(ResourceRequest(request.get())); |
| 676 } | 686 } |
| 677 | 687 |
| 678 } // namespace blink | 688 } // namespace blink |
| OLD | NEW |