OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "config.h" | |
6 #include "modules/serviceworkers/GlobalFetch.h" | |
7 | |
8 #include "core/dom/ActiveDOMObject.h" | |
9 #include "core/frame/LocalDOMWindow.h" | |
10 #include "core/workers/WorkerGlobalScope.h" | |
11 #include "modules/serviceworkers/FetchManager.h" | |
12 #include "modules/serviceworkers/Request.h" | |
13 #include "platform/Supplementable.h" | |
14 #include "platform/heap/Handle.h" | |
15 #include "wtf/OwnPtr.h" | |
16 | |
17 namespace blink { | |
18 | |
19 namespace { | |
20 | |
21 template <typename T> | |
22 class GlobalFetchImpl final : public NoBaseWillBeGarbageCollectedFinalized<Globa
lFetchImpl<T>>, public WillBeHeapSupplement<T> { | |
23 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(GlobalFetchImpl); | |
24 public: | |
25 static GlobalFetchImpl& from(T& supplementable, ExecutionContext* executionC
ontext) | |
26 { | |
27 GlobalFetchImpl* supplement = static_cast<GlobalFetchImpl*>(WillBeHeapSu
pplement<T>::from(supplementable, name())); | |
28 if (!supplement) { | |
29 supplement = new GlobalFetchImpl(executionContext); | |
30 WillBeHeapSupplement<T>::provideTo(supplementable, name(), adoptPtrW
illBeNoop(supplement)); | |
31 } | |
32 return *supplement; | |
33 } | |
34 | |
35 ScriptPromise fetch(ScriptState* scriptState, const RequestInfo& input, cons
t Dictionary& init, ExceptionState& exceptionState) | |
36 { | |
37 if (m_fetchManager.isStopped()) { | |
38 exceptionState.throwTypeError("The global scope is shutting down."); | |
39 return ScriptPromise(); | |
40 } | |
41 | |
42 // "Let |r| be the associated request of the result of invoking the | |
43 // initial value of Request as constructor with |input| and |init| as | |
44 // arguments. If this throws an exception, reject |p| with it." | |
45 Request* r = Request::create(m_stopDetector.executionContext(), input, i
nit, exceptionState); | |
46 if (exceptionState.hadException()) | |
47 return ScriptPromise(); | |
48 return m_fetchManager.fetch(scriptState, r->request()); | |
49 } | |
50 | |
51 void trace(Visitor* visitor) override | |
52 { | |
53 WillBeHeapSupplement<T>::trace(visitor); | |
54 } | |
55 | |
56 private: | |
57 class StopDetector : public ActiveDOMObject { | |
58 public: | |
59 StopDetector(ExecutionContext* executionContext, FetchManager* fetchMana
ger) | |
60 : ActiveDOMObject(executionContext) | |
61 , m_fetchManager(fetchManager) | |
62 { | |
63 suspendIfNeeded(); | |
64 } | |
65 void stop() override { m_fetchManager->stop(); } | |
66 | |
67 private: | |
68 // Having a raw pointer is safe, because |m_fetchManager| is owned by | |
69 // the owner of this object. | |
70 FetchManager* m_fetchManager; | |
71 }; | |
72 | |
73 explicit GlobalFetchImpl(ExecutionContext* executionContext) | |
74 : m_fetchManager(executionContext) | |
75 , m_stopDetector(executionContext, &m_fetchManager) | |
76 { | |
77 } | |
78 static const char* name() { return "GlobalFetch"; } | |
79 | |
80 FetchManager m_fetchManager; | |
81 StopDetector m_stopDetector; | |
82 }; | |
83 | |
84 } // namespace | |
85 | |
86 ScriptPromise GlobalFetch::fetch(ScriptState* scriptState, DOMWindow& window, co
nst RequestInfo& input, const Dictionary& init, ExceptionState& exceptionState) | |
87 { | |
88 return GlobalFetchImpl<LocalDOMWindow>::from(toLocalDOMWindow(window), windo
w.executionContext()).fetch(scriptState, input, init, exceptionState); | |
89 } | |
90 | |
91 ScriptPromise GlobalFetch::fetch(ScriptState* scriptState, WorkerGlobalScope& wo
rker, const RequestInfo& input, const Dictionary& init, ExceptionState& exceptio
nState) | |
92 { | |
93 return GlobalFetchImpl<WorkerGlobalScope>::from(worker, worker.executionCont
ext()).fetch(scriptState, input, init, exceptionState); | |
94 } | |
95 | |
96 } // namespace blink | |
OLD | NEW |