Chromium Code Reviews| 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/NotImplemented.h" | 15 #include "platform/NotImplemented.h" |
| 16 #include "public/platform/WebServiceWorkerEventResult.h" | 16 #include "public/platform/WebServiceWorkerEventResult.h" |
| 17 #include "wtf/Assertions.h" | 17 #include "wtf/Assertions.h" |
| 18 #include "wtf/RefCounted.h" | 18 #include "wtf/RefCounted.h" |
| 19 #include "wtf/RefPtr.h" | 19 #include "wtf/RefPtr.h" |
| 20 #include <v8.h> | 20 #include <v8.h> |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 | 23 |
| 24 namespace { | |
| 25 | |
| 26 unsigned& windowInteractionTimeout() | |
| 27 { | |
| 28 DEFINE_STATIC_LOCAL(unsigned, windowInteractionTimeout, (ServiceWorkerGlobal Scope::kWindowInteractionTimeout)); | |
| 29 return windowInteractionTimeout; | |
| 30 } | |
| 31 | |
| 32 } // anonymous namespace | |
| 33 | |
| 24 class WaitUntilObserver::ThenFunction final : public ScriptFunction { | 34 class WaitUntilObserver::ThenFunction final : public ScriptFunction { |
| 25 public: | 35 public: |
| 26 enum ResolveType { | 36 enum ResolveType { |
| 27 Fulfilled, | 37 Fulfilled, |
| 28 Rejected, | 38 Rejected, |
| 29 }; | 39 }; |
| 30 | 40 |
| 31 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai tUntilObserver* observer, ResolveType type) | 41 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Wai tUntilObserver* observer, ResolveType type) |
| 32 { | 42 { |
| 33 ThenFunction* self = new ThenFunction(scriptState, observer, type); | 43 ThenFunction* self = new ThenFunction(scriptState, observer, type); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 63 ResolveType m_resolveType; | 73 ResolveType m_resolveType; |
| 64 }; | 74 }; |
| 65 | 75 |
| 66 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp e type, int eventID) | 76 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventTyp e type, int eventID) |
| 67 { | 77 { |
| 68 return new WaitUntilObserver(context, type, eventID); | 78 return new WaitUntilObserver(context, type, eventID); |
| 69 } | 79 } |
| 70 | 80 |
| 71 void WaitUntilObserver::willDispatchEvent() | 81 void WaitUntilObserver::willDispatchEvent() |
| 72 { | 82 { |
| 83 // When handling a notificationclick event, we want to allow one window to | |
| 84 // be focused or opened. These calls are allowed between the call to | |
| 85 // willDispatchEvent() and the last call to decrementPendingActivity(). If | |
| 86 // waitUntil() isn't called, that means between willDispatchEvent() and | |
| 87 // didDispatchEvent(). | |
| 88 if (m_type == NotificationClick) | |
| 89 executionContext()->allowWindowInteraction(); | |
|
dominicc (has gone to gerrit)
2015/02/05 18:58:13
Does it make sense to assert that this has been re
mlamouri (slow - plz ping)
2015/02/05 20:23:12
Not really. The callers are not aware of the numbe
| |
| 90 | |
| 73 incrementPendingActivity(); | 91 incrementPendingActivity(); |
| 74 } | 92 } |
| 75 | 93 |
| 76 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) | 94 void WaitUntilObserver::didDispatchEvent(bool errorOccurred) |
| 77 { | 95 { |
| 78 if (errorOccurred) | 96 if (errorOccurred) |
| 79 m_hasError = true; | 97 m_hasError = true; |
| 80 decrementPendingActivity(); | 98 decrementPendingActivity(); |
| 81 m_eventDispatched = true; | 99 m_eventDispatched = true; |
| 82 } | 100 } |
| 83 | 101 |
| 84 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v alue, ExceptionState& exceptionState) | 102 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& v alue, ExceptionState& exceptionState) |
| 85 { | 103 { |
| 86 if (m_eventDispatched) { | 104 if (m_eventDispatched) { |
| 87 exceptionState.throwDOMException(InvalidStateError, "The event handler i s already finished."); | 105 exceptionState.throwDOMException(InvalidStateError, "The event handler i s already finished."); |
| 88 return; | 106 return; |
| 89 } | 107 } |
| 90 | 108 |
| 91 if (!executionContext()) | 109 if (!executionContext()) |
| 92 return; | 110 return; |
| 93 | 111 |
| 94 // When handling a notificationclick event, we want to allow one window to | 112 // When handling a notificationclick event, we want to allow one window to |
| 95 // be focused or opened. Regardless of whether such action happened, | 113 // be focused or opened. See comments in ::willDispatchEvent(). When |
| 96 // |consumeWindowInteraction| will be called when all the pending activities | 114 // waitUntil() is being used, opening or closing a window must happen in a |
| 97 // will be resolved or after a | 115 // timeframe specified by windowInteractionTimeout(), otherwise the calls |
| 98 if (m_type == NotificationClick) { | 116 // will fail. |
| 99 executionContext()->allowWindowInteraction(); | 117 if (m_type == NotificationClick) |
| 100 m_consumeWindowInteractionTimer.startOneShot(ServiceWorkerGlobalScope::k WindowInteractionTimeout, FROM_HERE); | 118 m_consumeWindowInteractionTimer.startOneShot(windowInteractionTimeout(), FROM_HERE); |
| 101 } | |
| 102 | 119 |
| 103 incrementPendingActivity(); | 120 incrementPendingActivity(); |
| 104 ScriptPromise::cast(scriptState, value).then( | 121 ScriptPromise::cast(scriptState, value).then( |
| 105 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , | 122 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , |
| 106 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; | 123 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; |
| 107 } | 124 } |
| 108 | 125 |
| 109 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type, int eventID) | 126 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type, int eventID) |
| 110 : ContextLifecycleObserver(context) | 127 : ContextLifecycleObserver(context) |
| 111 , m_type(type) | 128 , m_type(type) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 observeContext(0); | 174 observeContext(0); |
| 158 } | 175 } |
| 159 | 176 |
| 160 void WaitUntilObserver::consumeWindowInteraction(Timer<WaitUntilObserver>*) | 177 void WaitUntilObserver::consumeWindowInteraction(Timer<WaitUntilObserver>*) |
| 161 { | 178 { |
| 162 if (!executionContext()) | 179 if (!executionContext()) |
| 163 return; | 180 return; |
| 164 executionContext()->consumeWindowInteraction(); | 181 executionContext()->consumeWindowInteraction(); |
| 165 } | 182 } |
| 166 | 183 |
| 184 // static | |
| 185 void WaitUntilObserver::overrideWindowInteractionTimeoutForTest(unsigned timeout ) | |
| 186 { | |
| 187 windowInteractionTimeout() = timeout; | |
| 188 } | |
| 189 | |
| 167 void WaitUntilObserver::trace(Visitor* visitor) | 190 void WaitUntilObserver::trace(Visitor* visitor) |
| 168 { | 191 { |
| 169 ContextLifecycleObserver::trace(visitor); | 192 ContextLifecycleObserver::trace(visitor); |
| 170 } | 193 } |
| 171 | 194 |
| 172 } // namespace blink | 195 } // namespace blink |
| OLD | NEW |