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/WorkerGlobalScope.h" | |
| 8 #include "platform/exported/WrappedResourceRequest.h" | |
| 9 #include "public/platform/Platform.h" | |
| 10 #include "public/platform/WebScheduler.h" | |
| 11 #include "public/platform/WebThread.h" | |
| 12 #include "public/platform/WebWorkerFetchContext.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class WorkerFetchContextInfo final | |
| 19 : public GarbageCollectedFinalized<WorkerFetchContextInfo>, | |
| 20 public Supplement<WorkerClients> { | |
| 21 USING_GARBAGE_COLLECTED_MIXIN(WorkerFetchContextInfo); | |
| 22 | |
| 23 public: | |
| 24 static WorkerFetchContextInfo* From(WorkerClients& clients) { | |
| 25 return static_cast<WorkerFetchContextInfo*>( | |
| 26 Supplement<WorkerClients>::From(clients, SupplementName())); | |
| 27 } | |
| 28 static const char* SupplementName() { return "WorkerFetchContextInfo"; } | |
| 29 | |
| 30 explicit WorkerFetchContextInfo( | |
| 31 std::unique_ptr<WebWorkerFetchContextInfo> info) | |
| 32 : info_(std::move(info)) {} | |
| 33 virtual ~WorkerFetchContextInfo() {} | |
| 34 | |
| 35 std::unique_ptr<WebWorkerFetchContext> CreateContext() { | |
| 36 DCHECK(info_); | |
| 37 DCHECK(!IsMainThread()); | |
| 38 std::unique_ptr<WebWorkerFetchContext> webContext = | |
| 39 info_->CreateContext(Platform::Current() | |
| 40 ->CurrentThread() | |
| 41 ->Scheduler() | |
| 42 ->LoadingTaskRunner() | |
| 43 ->ToSingleThreadTaskRunner()); | |
| 44 info_.reset(); | |
| 45 return webContext; | |
| 46 } | |
| 47 | |
| 48 DEFINE_INLINE_VIRTUAL_TRACE() { Supplement<WorkerClients>::Trace(visitor); } | |
| 49 | |
| 50 private: | |
| 51 std::unique_ptr<WebWorkerFetchContextInfo> info_; | |
| 52 }; | |
| 53 | |
| 54 class WorkerContextSupplement final | |
| 55 : public GarbageCollectedFinalized<WorkerContextSupplement>, | |
| 56 public Supplement<ExecutionContext> { | |
|
kinuko
2017/04/14 14:00:26
Not fully sure why we need this class? Can't Work
horo
2017/04/17 15:12:04
Done.
| |
| 57 USING_GARBAGE_COLLECTED_MIXIN(WorkerContextSupplement); | |
| 58 | |
| 59 public: | |
| 60 static WorkerContextSupplement* From(ExecutionContext& execution_context) { | |
| 61 DCHECK(execution_context.IsContextThread()); | |
| 62 if (!execution_context.IsWorkerGlobalScope()) | |
| 63 return nullptr; | |
| 64 WorkerContextSupplement* supplement = static_cast<WorkerContextSupplement*>( | |
| 65 Supplement<ExecutionContext>::From(execution_context, | |
| 66 SupplementName())); | |
| 67 if (supplement) | |
| 68 return supplement; | |
| 69 WorkerClients* clients = ToWorkerGlobalScope(execution_context).Clients(); | |
| 70 if (!clients) | |
| 71 return nullptr; | |
| 72 WorkerFetchContextInfo* context_info = | |
| 73 WorkerFetchContextInfo::From(*clients); | |
| 74 if (!context_info) | |
| 75 return nullptr; | |
| 76 WorkerFetchContext* worker_fetch_context = | |
| 77 new WorkerFetchContext(context_info->CreateContext()); | |
| 78 supplement = new WorkerContextSupplement(worker_fetch_context); | |
| 79 Supplement<ExecutionContext>::ProvideTo(execution_context, SupplementName(), | |
| 80 supplement); | |
| 81 return supplement; | |
| 82 } | |
| 83 WorkerFetchContext* GetContext() const { return worker_fetch_context_; } | |
| 84 | |
| 85 DEFINE_INLINE_VIRTUAL_TRACE() { | |
| 86 visitor->Trace(worker_fetch_context_); | |
| 87 Supplement<ExecutionContext>::Trace(visitor); | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 explicit WorkerContextSupplement(WorkerFetchContext* workerFetchContext) | |
| 92 : worker_fetch_context_(workerFetchContext) {} | |
| 93 static const char* SupplementName() { return "WorkerContextSupplement"; } | |
| 94 Member<WorkerFetchContext> worker_fetch_context_; | |
| 95 }; | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 WorkerFetchContext::~WorkerFetchContext() {} | |
| 100 | |
| 101 WorkerFetchContext* WorkerFetchContext::From( | |
| 102 ExecutionContext& executionContext) { | |
| 103 WorkerContextSupplement* supplement = | |
| 104 WorkerContextSupplement::From(executionContext); | |
| 105 if (!supplement) | |
| 106 return nullptr; | |
| 107 return supplement->GetContext(); | |
| 108 } | |
| 109 | |
| 110 WorkerFetchContext::WorkerFetchContext( | |
| 111 std::unique_ptr<WebWorkerFetchContext> context) | |
| 112 : context_(std::move(context)) {} | |
| 113 | |
| 114 void WorkerFetchContext::PrepareRequest(ResourceRequest& request, | |
| 115 RedirectType) { | |
| 116 WrappedResourceRequest webreq(request); | |
| 117 context_->WillSendRequest(webreq); | |
| 118 } | |
| 119 | |
| 120 WebURLLoader* WorkerFetchContext::CreateURLLoader() { | |
| 121 return context_->CreateURLLoader(); | |
| 122 } | |
| 123 | |
| 124 bool WorkerFetchContext::IsControlledByServiceWorker() const { | |
| 125 return context_->IsControlledByServiceWorker(); | |
| 126 } | |
| 127 | |
| 128 void ProvideWorkerFetchContextInfoToWorker( | |
| 129 WorkerClients* clients, | |
| 130 std::unique_ptr<WebWorkerFetchContextInfo> info) { | |
| 131 DCHECK(clients); | |
| 132 WorkerFetchContextInfo::ProvideTo( | |
| 133 *clients, WorkerFetchContextInfo::SupplementName(), | |
| 134 new WorkerFetchContextInfo(std::move(info))); | |
| 135 } | |
| 136 | |
| 137 } // namespace blink | |
| OLD | NEW |