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 // Timeout before a service worker that was given window interaction |
| 28 // permission loses them. The unit is seconds. |
| 29 const unsigned kWindowInteractionTimeout = 10; |
| 30 const unsigned kWindowInteractionTimeoutForTest = 1; |
| 31 |
| 32 unsigned windowInteractionTimeout() |
| 33 { |
| 34 return LayoutTestSupport::isRunningLayoutTest() |
| 35 ? kWindowInteractionTimeoutForTest : kWindowInteractionTimeout; |
| 36 } |
| 37 |
| 38 } // anonymous namespace |
| 39 |
24 class WaitUntilObserver::ThenFunction final : public ScriptFunction { | 40 class WaitUntilObserver::ThenFunction final : public ScriptFunction { |
25 public: | 41 public: |
26 enum ResolveType { | 42 enum ResolveType { |
27 Fulfilled, | 43 Fulfilled, |
28 Rejected, | 44 Rejected, |
29 }; | 45 }; |
30 | 46 |
31 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai
tUntilObserver* observer, ResolveType type) | 47 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai
tUntilObserver* observer, ResolveType type) |
32 { | 48 { |
33 ThenFunction* self = new ThenFunction(scriptState, observer, type); | 49 ThenFunction* self = new ThenFunction(scriptState, observer, type); |
(...skipping 29 matching lines...) Expand all Loading... |
63 ResolveType m_resolveType; | 79 ResolveType m_resolveType; |
64 }; | 80 }; |
65 | 81 |
66 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp
e type, int eventID) | 82 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp
e type, int eventID) |
67 { | 83 { |
68 return new WaitUntilObserver(context, type, eventID); | 84 return new WaitUntilObserver(context, type, eventID); |
69 } | 85 } |
70 | 86 |
71 void WaitUntilObserver::willDispatchEvent() | 87 void WaitUntilObserver::willDispatchEvent() |
72 { | 88 { |
| 89 // When handling a notificationclick event, we want to allow one window to |
| 90 // be focused or opened. These calls are allowed between the call to |
| 91 // willDispatchEvent() and the last call to decrementPendingActivity(). If |
| 92 // waitUntil() isn't called, that means between willDispatchEvent() and |
| 93 // didDispatchEvent(). |
| 94 if (m_type == NotificationClick) |
| 95 executionContext()->allowWindowInteraction(); |
| 96 |
73 incrementPendingActivity(); | 97 incrementPendingActivity(); |
74 } | 98 } |
75 | 99 |
76 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) | 100 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) |
77 { | 101 { |
78 if (errorOccurred) | 102 if (errorOccurred) |
79 m_hasError = true; | 103 m_hasError = true; |
80 decrementPendingActivity(); | 104 decrementPendingActivity(); |
81 m_eventDispatched = true; | 105 m_eventDispatched = true; |
82 } | 106 } |
83 | 107 |
84 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v
alue, ExceptionState& exceptionState) | 108 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v
alue, ExceptionState& exceptionState) |
85 { | 109 { |
86 if (m_eventDispatched) { | 110 if (m_eventDispatched) { |
87 exceptionState.throwDOMException(InvalidStateError, "The event handler i
s already finished."); | 111 exceptionState.throwDOMException(InvalidStateError, "The event handler i
s already finished."); |
88 return; | 112 return; |
89 } | 113 } |
90 | 114 |
91 if (!executionContext()) | 115 if (!executionContext()) |
92 return; | 116 return; |
93 | 117 |
94 // When handling a notificationclick event, we want to allow one window to | 118 // When handling a notificationclick event, we want to allow one window to |
95 // be focused or opened. Regardless of whether such action happened, | 119 // be focused or opened. See comments in ::willDispatchEvent(). When |
96 // |consumeWindowInteraction| will be called when all the pending activities | 120 // waitUntil() is being used, opening or closing a window must happen in a |
97 // will be resolved or after a | 121 // timeframe specified by windowInteractionTimeout(), otherwise the calls |
98 if (m_type == NotificationClick) { | 122 // will fail. |
99 executionContext()->allowWindowInteraction(); | 123 if (m_type == NotificationClick) |
100 m_consumeWindowInteractionTimer.startOneShot(ServiceWorkerGlobalScope::k
WindowInteractionTimeout, FROM_HERE); | 124 m_consumeWindowInteractionTimer.startOneShot(windowInteractionTimeout(),
FROM_HERE); |
101 } | |
102 | 125 |
103 incrementPendingActivity(); | 126 incrementPendingActivity(); |
104 ScriptPromise::cast(scriptState, value).then( | 127 ScriptPromise::cast(scriptState, value).then( |
105 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 128 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
106 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 129 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
107 } | 130 } |
108 | 131 |
109 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type,
int eventID) | 132 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type,
int eventID) |
110 : ContextLifecycleObserver(context) | 133 : ContextLifecycleObserver(context) |
111 , m_type(type) | 134 , m_type(type) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 return; | 186 return; |
164 executionContext()->consumeWindowInteraction(); | 187 executionContext()->consumeWindowInteraction(); |
165 } | 188 } |
166 | 189 |
167 DEFINE_TRACE(WaitUntilObserver) | 190 DEFINE_TRACE(WaitUntilObserver) |
168 { | 191 { |
169 ContextLifecycleObserver::trace(visitor); | 192 ContextLifecycleObserver::trace(visitor); |
170 } | 193 } |
171 | 194 |
172 } // namespace blink | 195 } // namespace blink |
OLD | NEW |