Chromium Code Reviews| 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 "public/platform/Platform.h" | |
| 13 #include "public/platform/WebScheduler.h" | |
| 14 #include "public/platform/WebThread.h" | |
| 15 #include "public/platform/WebWorkerFetchContext.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // WorkerFetchContextInfo 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 WorkerFetchContextInfo final | |
|
kinuko
2017/04/19 11:08:14
nit: WorkerFetchContextHolder maybe?
horo
2017/04/19 13:52:27
Done.
| |
| 25 : public GarbageCollectedFinalized<WorkerFetchContextInfo>, | |
| 26 public Supplement<WorkerClients> { | |
| 27 USING_GARBAGE_COLLECTED_MIXIN(WorkerFetchContextInfo); | |
| 28 | |
| 29 public: | |
| 30 static WorkerFetchContextInfo* From(WorkerClients& clients) { | |
| 31 return static_cast<WorkerFetchContextInfo*>( | |
| 32 Supplement<WorkerClients>::From(clients, SupplementName())); | |
| 33 } | |
| 34 static const char* SupplementName() { return "WorkerFetchContextInfo"; } | |
| 35 | |
| 36 explicit WorkerFetchContextInfo( | |
| 37 std::unique_ptr<WebWorkerFetchContext> web_context) | |
| 38 : web_context_(std::move(web_context)) {} | |
| 39 virtual ~WorkerFetchContextInfo() {} | |
| 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 WorkerClients* worker_clients = worker_global_scope.Clients(); | |
| 58 DCHECK(worker_clients); | |
| 59 WorkerFetchContextInfo* context_info = | |
| 60 static_cast<WorkerFetchContextInfo*>(Supplement<WorkerClients>::From( | |
| 61 *worker_clients, WorkerFetchContextInfo::SupplementName())); | |
| 62 if (!context_info) | |
| 63 return nullptr; | |
| 64 std::unique_ptr<WebWorkerFetchContext> web_context = | |
| 65 context_info->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 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 WorkerFetchContextInfo::ProvideTo( | |
| 99 *clients, WorkerFetchContextInfo::SupplementName(), | |
| 100 new WorkerFetchContextInfo(std::move(web_context))); | |
| 101 } | |
| 102 | |
| 103 } // namespace blink | |
| OLD | NEW |