OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "modules/serviceworkers/WaitUntilObserver.h" | 6 #include "modules/serviceworkers/WaitUntilObserver.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptValue.h" | 10 #include "bindings/core/v8/ScriptValue.h" |
11 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
14 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 14 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
| 15 #include "platform/LayoutTestSupport.h" |
15 #include "platform/NotImplemented.h" | 16 #include "platform/NotImplemented.h" |
16 #include "public/platform/WebServiceWorkerEventResult.h" | 17 #include "public/platform/WebServiceWorkerEventResult.h" |
17 #include "wtf/Assertions.h" | 18 #include "wtf/Assertions.h" |
18 #include "wtf/RefCounted.h" | 19 #include "wtf/RefCounted.h" |
19 #include "wtf/RefPtr.h" | 20 #include "wtf/RefPtr.h" |
20 #include <v8.h> | 21 #include <v8.h> |
21 | 22 |
22 namespace blink { | 23 namespace blink { |
23 | 24 |
| 25 namespace { |
| 26 |
| 27 unsigned windowInteractionTimeout() |
| 28 { |
| 29 return LayoutTestSupport::isRunningLayoutTest() |
| 30 ? ServiceWorkerGlobalScope::kWindowInteractionTimeoutForTest |
| 31 : ServiceWorkerGlobalScope::kWindowInteractionTimeout; |
| 32 } |
| 33 |
| 34 } // anonymous namespace |
| 35 |
24 class WaitUntilObserver::ThenFunction final : public ScriptFunction { | 36 class WaitUntilObserver::ThenFunction final : public ScriptFunction { |
25 public: | 37 public: |
26 enum ResolveType { | 38 enum ResolveType { |
27 Fulfilled, | 39 Fulfilled, |
28 Rejected, | 40 Rejected, |
29 }; | 41 }; |
30 | 42 |
31 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai
tUntilObserver* observer, ResolveType type) | 43 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai
tUntilObserver* observer, ResolveType type) |
32 { | 44 { |
33 ThenFunction* self = new ThenFunction(scriptState, observer, type); | 45 ThenFunction* self = new ThenFunction(scriptState, observer, type); |
(...skipping 29 matching lines...) Expand all Loading... |
63 ResolveType m_resolveType; | 75 ResolveType m_resolveType; |
64 }; | 76 }; |
65 | 77 |
66 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp
e type, int eventID) | 78 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp
e type, int eventID) |
67 { | 79 { |
68 return new WaitUntilObserver(context, type, eventID); | 80 return new WaitUntilObserver(context, type, eventID); |
69 } | 81 } |
70 | 82 |
71 void WaitUntilObserver::willDispatchEvent() | 83 void WaitUntilObserver::willDispatchEvent() |
72 { | 84 { |
| 85 // When handling a notificationclick event, we want to allow one window to |
| 86 // be focused or opened. These calls are allowed between the call to |
| 87 // willDispatchEvent() and the last call to decrementPendingActivity(). If |
| 88 // waitUntil() isn't called, that means between willDispatchEvent() and |
| 89 // didDispatchEvent(). |
| 90 if (m_type == NotificationClick) |
| 91 executionContext()->allowWindowInteraction(); |
| 92 |
73 incrementPendingActivity(); | 93 incrementPendingActivity(); |
74 } | 94 } |
75 | 95 |
76 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) | 96 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) |
77 { | 97 { |
78 if (errorOccurred) | 98 if (errorOccurred) |
79 m_hasError = true; | 99 m_hasError = true; |
80 decrementPendingActivity(); | 100 decrementPendingActivity(); |
81 m_eventDispatched = true; | 101 m_eventDispatched = true; |
82 } | 102 } |
83 | 103 |
84 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v
alue, ExceptionState& exceptionState) | 104 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v
alue, ExceptionState& exceptionState) |
85 { | 105 { |
86 if (m_eventDispatched) { | 106 if (m_eventDispatched) { |
87 exceptionState.throwDOMException(InvalidStateError, "The event handler i
s already finished."); | 107 exceptionState.throwDOMException(InvalidStateError, "The event handler i
s already finished."); |
88 return; | 108 return; |
89 } | 109 } |
90 | 110 |
91 if (!executionContext()) | 111 if (!executionContext()) |
92 return; | 112 return; |
93 | 113 |
94 // When handling a notificationclick event, we want to allow one window to | 114 // When handling a notificationclick event, we want to allow one window to |
95 // be focused or opened. Regardless of whether such action happened, | 115 // be focused or opened. See comments in ::willDispatchEvent(). When |
96 // |consumeWindowInteraction| will be called when all the pending activities | 116 // waitUntil() is being used, opening or closing a window must happen in a |
97 // will be resolved or after a | 117 // timeframe specified by windowInteractionTimeout(), otherwise the calls |
98 if (m_type == NotificationClick) { | 118 // will fail. |
99 executionContext()->allowWindowInteraction(); | 119 if (m_type == NotificationClick) |
100 m_consumeWindowInteractionTimer.startOneShot(ServiceWorkerGlobalScope::k
WindowInteractionTimeout, FROM_HERE); | 120 m_consumeWindowInteractionTimer.startOneShot(windowInteractionTimeout(),
FROM_HERE); |
101 } | |
102 | 121 |
103 incrementPendingActivity(); | 122 incrementPendingActivity(); |
104 ScriptPromise::cast(scriptState, value).then( | 123 ScriptPromise::cast(scriptState, value).then( |
105 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 124 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
106 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 125 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
107 } | 126 } |
108 | 127 |
109 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type,
int eventID) | 128 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type,
int eventID) |
110 : ContextLifecycleObserver(context) | 129 : ContextLifecycleObserver(context) |
111 , m_type(type) | 130 , m_type(type) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 return; | 182 return; |
164 executionContext()->consumeWindowInteraction(); | 183 executionContext()->consumeWindowInteraction(); |
165 } | 184 } |
166 | 185 |
167 void WaitUntilObserver::trace(Visitor* visitor) | 186 void WaitUntilObserver::trace(Visitor* visitor) |
168 { | 187 { |
169 ContextLifecycleObserver::trace(visitor); | 188 ContextLifecycleObserver::trace(visitor); |
170 } | 189 } |
171 | 190 |
172 } // namespace blink | 191 } // namespace blink |
OLD | NEW |