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.
6 #include "modules/serviceworkers/WaitUntilObserver.h"
8 #include "bindings/core/v8/ScriptFunction.h"
9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/dom/ExecutionContext.h"
13 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h"
14 #include "platform/NotImplemented.h"
15 #include "public/platform/WebServiceWorkerEventResult.h"
16 #include "wtf/Assertions.h"
17 #include "wtf/RefCounted.h"
18 #include "wtf/RefPtr.h"
23 class WaitUntilObserver::ThenFunction final : public ScriptFunction {
30 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, WaitUntilObserver* observer, ResolveType type)
32 ThenFunction* self = new ThenFunction(scriptState, observer, type);
33 return self->bindToV8Function();
36 virtual void trace(Visitor* visitor) override
38 visitor->trace(m_observer);
39 ScriptFunction::trace(visitor);
43 ThenFunction(ScriptState* scriptState, WaitUntilObserver* observer, ResolveType type)
44 : ScriptFunction(scriptState)
45 , m_observer(observer)
50 virtual ScriptValue call(ScriptValue value) override
53 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
54 if (m_resolveType == Rejected)
55 m_observer->reportError(value);
56 m_observer->decrementPendingActivity();
61 Member<WaitUntilObserver> m_observer;
62 ResolveType m_resolveType;
65 WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context, EventType type, int eventID)
67 return new WaitUntilObserver(context, type, eventID);
70 void WaitUntilObserver::willDispatchEvent()
72 incrementPendingActivity();
75 void WaitUntilObserver::didDispatchEvent(bool errorOccurred)
79 decrementPendingActivity();
82 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& value)
84 incrementPendingActivity();
85 ScriptPromise::cast(scriptState, value).then(
86 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled),
87 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected));
90 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type, int eventID)
91 : ContextLifecycleObserver(context)
94 , m_pendingActivity(0)
99 void WaitUntilObserver::reportError(const ScriptValue& value)
101 // FIXME: Propagate error message to the client for onerror handling.
107 void WaitUntilObserver::incrementPendingActivity()
112 void WaitUntilObserver::decrementPendingActivity()
114 ASSERT(m_pendingActivity > 0);
115 if (!executionContext() || (!m_hasError && --m_pendingActivity))
118 ServiceWorkerGlobalScopeClient* client = ServiceWorkerGlobalScopeClient::from(executionContext());
119 WebServiceWorkerEventResult result = m_hasError ? WebServiceWorkerEventResultRejected : WebServiceWorkerEventResultCompleted;
122 client->didHandleActivateEvent(m_eventID, result);
125 client->didHandleInstallEvent(m_eventID, result);