Index: third_party/WebKit/Source/core/loader/WorkerFetchContext.cpp |
diff --git a/third_party/WebKit/Source/core/loader/WorkerFetchContext.cpp b/third_party/WebKit/Source/core/loader/WorkerFetchContext.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..790b780d148f7849af3f9fb0564bba7cbd902b69 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/loader/WorkerFetchContext.cpp |
@@ -0,0 +1,136 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/loader/WorkerFetchContext.h" |
+ |
+#include "core/workers/WorkerGlobalScope.h" |
+#include "platform/exported/WrappedResourceRequest.h" |
+#include "public/platform/Platform.h" |
+#include "public/platform/WebScheduler.h" |
+#include "public/platform/WebThread.h" |
+#include "public/platform/WebWorkerFetchContext.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+class WorkerFetchContextInfo final |
+ : public GarbageCollectedFinalized<WorkerFetchContextInfo>, |
+ public Supplement<WorkerClients> { |
+ USING_GARBAGE_COLLECTED_MIXIN(WorkerFetchContextInfo); |
+ |
+ public: |
+ static WorkerFetchContextInfo* From(WorkerClients& clients) { |
+ return static_cast<WorkerFetchContextInfo*>( |
+ Supplement<WorkerClients>::From(clients, SupplementName())); |
+ } |
+ static const char* SupplementName() { return "WorkerFetchContextInfo"; } |
+ |
+ explicit WorkerFetchContextInfo( |
+ std::unique_ptr<WebWorkerFetchContextInfo> info) |
+ : info_(std::move(info)) {} |
+ virtual ~WorkerFetchContextInfo() {} |
+ |
+ std::unique_ptr<WebWorkerFetchContext> CreateContext() { |
+ DCHECK(info_); |
+ DCHECK(!IsMainThread()); |
+ std::unique_ptr<WebWorkerFetchContext> webContext = |
+ info_->CreateContext(Platform::Current() |
+ ->CurrentThread() |
+ ->Scheduler() |
+ ->LoadingTaskRunner() |
+ ->ToSingleThreadTaskRunner()); |
nhiroki
2017/04/14 04:09:24
FYI: CurrentThread()->Scheduler()->LoadingTaskRunn
horo
2017/04/14 07:22:45
Acknowledged.
Thank you for letting me know.
|
+ info_.reset(); |
+ return webContext; |
+ } |
+ |
+ DEFINE_INLINE_VIRTUAL_TRACE() { Supplement<WorkerClients>::Trace(visitor); } |
+ |
+ private: |
+ std::unique_ptr<WebWorkerFetchContextInfo> info_; |
+}; |
+ |
+class WorkerContextSupplement final |
+ : public GarbageCollectedFinalized<WorkerContextSupplement>, |
+ public Supplement<ExecutionContext> { |
+ USING_GARBAGE_COLLECTED_MIXIN(WorkerContextSupplement); |
+ |
+ public: |
+ static WorkerContextSupplement* From(ExecutionContext& execution_context) { |
nhiroki
2017/04/14 04:09:24
Since ExecutionContext must not be accessed from t
horo
2017/04/14 07:22:45
Done.
|
+ if (!execution_context.IsWorkerGlobalScope()) |
+ return nullptr; |
+ WorkerContextSupplement* supplement = static_cast<WorkerContextSupplement*>( |
+ Supplement<ExecutionContext>::From(execution_context, |
+ SupplementName())); |
+ if (supplement) |
+ return supplement; |
+ WorkerClients* clients = ToWorkerGlobalScope(execution_context).Clients(); |
+ if (!clients) |
+ return nullptr; |
+ WorkerFetchContextInfo* context_info = |
+ WorkerFetchContextInfo::From(*clients); |
+ if (!context_info) |
+ return nullptr; |
+ WorkerFetchContext* worker_fetch_context = |
+ new WorkerFetchContext(context_info->CreateContext()); |
+ supplement = new WorkerContextSupplement(worker_fetch_context); |
+ Supplement<ExecutionContext>::ProvideTo(execution_context, SupplementName(), |
+ supplement); |
+ return supplement; |
+ } |
+ WorkerFetchContext* GetContext() const { return worker_fetch_context_; } |
+ |
+ DEFINE_INLINE_VIRTUAL_TRACE() { |
+ visitor->Trace(worker_fetch_context_); |
+ Supplement<ExecutionContext>::Trace(visitor); |
+ } |
+ |
+ private: |
+ explicit WorkerContextSupplement(WorkerFetchContext* workerFetchContext) |
+ : worker_fetch_context_(workerFetchContext) {} |
+ static const char* SupplementName() { return "WorkerContextSupplement"; } |
+ Member<WorkerFetchContext> worker_fetch_context_; |
+}; |
+ |
+} // namespace |
+ |
+WorkerFetchContext::~WorkerFetchContext() {} |
+ |
+WorkerFetchContext* WorkerFetchContext::From( |
+ ExecutionContext& executionContext) { |
+ WorkerContextSupplement* supplement = |
+ WorkerContextSupplement::From(executionContext); |
+ if (!supplement) |
+ return nullptr; |
+ return supplement->GetContext(); |
+} |
+ |
+WorkerFetchContext::WorkerFetchContext( |
+ std::unique_ptr<WebWorkerFetchContext> context) |
+ : context_(std::move(context)) {} |
+ |
+void WorkerFetchContext::PrepareRequest(ResourceRequest& request, |
+ RedirectType) { |
+ WrappedResourceRequest webreq(request); |
+ context_->WillSendRequest(webreq); |
+} |
+ |
+WebURLLoader* WorkerFetchContext::CreateURLLoader() { |
+ return context_->CreateURLLoader(); |
+} |
+ |
+bool WorkerFetchContext::IsControlledByServiceWorker() const { |
+ return context_->IsControlledByServiceWorker(); |
+} |
+ |
+void ProvideWorkerFetchContextInfoToWorker( |
+ WorkerClients* clients, |
+ std::unique_ptr<WebWorkerFetchContextInfo> info) { |
+ DCHECK(clients); |
+ WorkerFetchContextInfo::ProvideTo( |
+ *clients, WorkerFetchContextInfo::SupplementName(), |
+ new WorkerFetchContextInfo(std::move(info))); |
+} |
+ |
+} // namespace blink |