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 // 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) { | |
nhiroki
2017/04/20 04:16:07
Can you add DCHECK(worker_global_scope->IsContextT
horo
2017/04/20 08:35:43
Done.
| |
57 WorkerClients* worker_clients = worker_global_scope.Clients(); | |
58 DCHECK(worker_clients); | |
59 WorkerFetchContextHolder* holder = | |
60 static_cast<WorkerFetchContextHolder*>(Supplement<WorkerClients>::From( | |
61 *worker_clients, WorkerFetchContextHolder::SupplementName())); | |
62 if (!holder) | |
63 return nullptr; | |
64 std::unique_ptr<WebWorkerFetchContext> web_context = holder->TakeContext(); | |
65 DCHECK(web_context); | |
66 return new WorkerFetchContext(std::move(web_context)); | |
67 } | |
68 | |
69 WorkerFetchContext::WorkerFetchContext( | |
70 std::unique_ptr<WebWorkerFetchContext> web_context) | |
71 : web_context_(std::move(web_context)) { | |
72 web_context_->InitializeOnWorkerThread(Platform::Current() | |
nhiroki
2017/04/20 04:16:07
The GlobalScopeScheduler patch was landed. Can you
nhiroki
2017/04/20 04:17:33
Hmm..., this was reverted. Please ignore this comm
| |
73 ->CurrentThread() | |
74 ->Scheduler() | |
75 ->LoadingTaskRunner() | |
76 ->ToSingleThreadTaskRunner()); | |
77 } | |
78 | |
79 WebURLLoader* WorkerFetchContext::CreateURLLoader() { | |
80 return web_context_->CreateURLLoader(); | |
81 } | |
82 | |
83 bool WorkerFetchContext::IsControlledByServiceWorker() const { | |
84 return web_context_->IsControlledByServiceWorker(); | |
85 } | |
86 | |
87 void WorkerFetchContext::PrepareRequest(ResourceRequest& request, | |
88 RedirectType) { | |
89 WrappedResourceRequest webreq(request); | |
90 web_context_->WillSendRequest(webreq); | |
91 } | |
92 | |
93 void ProvideWorkerFetchContextToWorker( | |
94 WorkerClients* clients, | |
95 std::unique_ptr<WebWorkerFetchContext> web_context) { | |
96 DCHECK(clients); | |
97 WorkerFetchContextHolder::ProvideTo( | |
98 *clients, WorkerFetchContextHolder::SupplementName(), | |
99 new WorkerFetchContextHolder(std::move(web_context))); | |
100 } | |
101 | |
102 } // namespace blink | |
OLD | NEW |