OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "core/loader/WorkerFetchContext.h" |
| 6 |
| 7 #include "core/workers/WorkerClients.h" |
| 8 #include "core/workers/WorkerGlobalScope.h" |
| 9 #include "platform/Supplementable.h" |
| 10 #include "platform/WebTaskRunner.h" |
| 11 #include "platform/exported/WrappedResourceRequest.h" |
| 12 #include "platform/scheduler/child/web_scheduler.h" |
| 13 #include "public/platform/Platform.h" |
| 14 #include "public/platform/WebThread.h" |
| 15 #include "public/platform/WebWorkerFetchContext.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // WorkerFetchContextHolder is used to pass the WebWorkerFetchContext from the |
| 22 // main thread to the worker thread by attaching to the WorkerClients as a |
| 23 // Supplement. |
| 24 class WorkerFetchContextHolder final |
| 25 : public GarbageCollectedFinalized<WorkerFetchContextHolder>, |
| 26 public Supplement<WorkerClients> { |
| 27 USING_GARBAGE_COLLECTED_MIXIN(WorkerFetchContextHolder); |
| 28 |
| 29 public: |
| 30 static WorkerFetchContextHolder* From(WorkerClients& clients) { |
| 31 return static_cast<WorkerFetchContextHolder*>( |
| 32 Supplement<WorkerClients>::From(clients, SupplementName())); |
| 33 } |
| 34 static const char* SupplementName() { return "WorkerFetchContextHolder"; } |
| 35 |
| 36 explicit WorkerFetchContextHolder( |
| 37 std::unique_ptr<WebWorkerFetchContext> web_context) |
| 38 : web_context_(std::move(web_context)) {} |
| 39 virtual ~WorkerFetchContextHolder() {} |
| 40 |
| 41 std::unique_ptr<WebWorkerFetchContext> TakeContext() { |
| 42 return std::move(web_context_); |
| 43 } |
| 44 |
| 45 DEFINE_INLINE_VIRTUAL_TRACE() { Supplement<WorkerClients>::Trace(visitor); } |
| 46 |
| 47 private: |
| 48 std::unique_ptr<WebWorkerFetchContext> web_context_; |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 WorkerFetchContext::~WorkerFetchContext() {} |
| 54 |
| 55 WorkerFetchContext* WorkerFetchContext::Create( |
| 56 WorkerGlobalScope& worker_global_scope) { |
| 57 DCHECK(worker_global_scope.IsContextThread()); |
| 58 WorkerClients* worker_clients = worker_global_scope.Clients(); |
| 59 DCHECK(worker_clients); |
| 60 WorkerFetchContextHolder* holder = |
| 61 static_cast<WorkerFetchContextHolder*>(Supplement<WorkerClients>::From( |
| 62 *worker_clients, WorkerFetchContextHolder::SupplementName())); |
| 63 if (!holder) |
| 64 return nullptr; |
| 65 std::unique_ptr<WebWorkerFetchContext> web_context = holder->TakeContext(); |
| 66 DCHECK(web_context); |
| 67 return new WorkerFetchContext(std::move(web_context)); |
| 68 } |
| 69 |
| 70 WorkerFetchContext::WorkerFetchContext( |
| 71 std::unique_ptr<WebWorkerFetchContext> web_context) |
| 72 : web_context_(std::move(web_context)) { |
| 73 web_context_->InitializeOnWorkerThread(Platform::Current() |
| 74 ->CurrentThread() |
| 75 ->Scheduler() |
| 76 ->LoadingTaskRunner() |
| 77 ->ToSingleThreadTaskRunner()); |
| 78 } |
| 79 |
| 80 std::unique_ptr<WebURLLoader> WorkerFetchContext::CreateURLLoader() { |
| 81 return web_context_->CreateURLLoader(); |
| 82 } |
| 83 |
| 84 bool WorkerFetchContext::IsControlledByServiceWorker() const { |
| 85 return web_context_->IsControlledByServiceWorker(); |
| 86 } |
| 87 |
| 88 void WorkerFetchContext::PrepareRequest(ResourceRequest& request, |
| 89 RedirectType) { |
| 90 WrappedResourceRequest webreq(request); |
| 91 web_context_->WillSendRequest(webreq); |
| 92 } |
| 93 |
| 94 void ProvideWorkerFetchContextToWorker( |
| 95 WorkerClients* clients, |
| 96 std::unique_ptr<WebWorkerFetchContext> web_context) { |
| 97 DCHECK(clients); |
| 98 WorkerFetchContextHolder::ProvideTo( |
| 99 *clients, WorkerFetchContextHolder::SupplementName(), |
| 100 new WorkerFetchContextHolder(std::move(web_context))); |
| 101 } |
| 102 |
| 103 } // namespace blink |
OLD | NEW |