Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: third_party/WebKit/Source/core/loader/WorkerFetchContext.cpp

Issue 2804843005: Implement the infrastructure of creating WorkerFetchContext in worker global scope. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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());
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.
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> {
57 USING_GARBAGE_COLLECTED_MIXIN(WorkerContextSupplement);
58
59 public:
60 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.
61 if (!execution_context.IsWorkerGlobalScope())
62 return nullptr;
63 WorkerContextSupplement* supplement = static_cast<WorkerContextSupplement*>(
64 Supplement<ExecutionContext>::From(execution_context,
65 SupplementName()));
66 if (supplement)
67 return supplement;
68 WorkerClients* clients = ToWorkerGlobalScope(execution_context).Clients();
69 if (!clients)
70 return nullptr;
71 WorkerFetchContextInfo* context_info =
72 WorkerFetchContextInfo::From(*clients);
73 if (!context_info)
74 return nullptr;
75 WorkerFetchContext* worker_fetch_context =
76 new WorkerFetchContext(context_info->CreateContext());
77 supplement = new WorkerContextSupplement(worker_fetch_context);
78 Supplement<ExecutionContext>::ProvideTo(execution_context, SupplementName(),
79 supplement);
80 return supplement;
81 }
82 WorkerFetchContext* GetContext() const { return worker_fetch_context_; }
83
84 DEFINE_INLINE_VIRTUAL_TRACE() {
85 visitor->Trace(worker_fetch_context_);
86 Supplement<ExecutionContext>::Trace(visitor);
87 }
88
89 private:
90 explicit WorkerContextSupplement(WorkerFetchContext* workerFetchContext)
91 : worker_fetch_context_(workerFetchContext) {}
92 static const char* SupplementName() { return "WorkerContextSupplement"; }
93 Member<WorkerFetchContext> worker_fetch_context_;
94 };
95
96 } // namespace
97
98 WorkerFetchContext::~WorkerFetchContext() {}
99
100 WorkerFetchContext* WorkerFetchContext::From(
101 ExecutionContext& executionContext) {
102 WorkerContextSupplement* supplement =
103 WorkerContextSupplement::From(executionContext);
104 if (!supplement)
105 return nullptr;
106 return supplement->GetContext();
107 }
108
109 WorkerFetchContext::WorkerFetchContext(
110 std::unique_ptr<WebWorkerFetchContext> context)
111 : context_(std::move(context)) {}
112
113 void WorkerFetchContext::PrepareRequest(ResourceRequest& request,
114 RedirectType) {
115 WrappedResourceRequest webreq(request);
116 context_->WillSendRequest(webreq);
117 }
118
119 WebURLLoader* WorkerFetchContext::CreateURLLoader() {
120 return context_->CreateURLLoader();
121 }
122
123 bool WorkerFetchContext::IsControlledByServiceWorker() const {
124 return context_->IsControlledByServiceWorker();
125 }
126
127 void ProvideWorkerFetchContextInfoToWorker(
128 WorkerClients* clients,
129 std::unique_ptr<WebWorkerFetchContextInfo> info) {
130 DCHECK(clients);
131 WorkerFetchContextInfo::ProvideTo(
132 *clients, WorkerFetchContextInfo::SupplementName(),
133 new WorkerFetchContextInfo(std::move(info)));
134 }
135
136 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698