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/v8/ScriptFunction.h"
9 #include "bindings/v8/ScriptPromise.h"
10 #include "bindings/v8/ScriptValue.h"
11 #include "core/dom/ExecutionContext.h"
12 #include "platform/NotImplemented.h"
13 #include "wtf/Assertions.h"
14 #include "wtf/RefCounted.h"
15 #include "wtf/RefPtr.h"
19 class WaitUntilObserver::ThenFunction FINAL : public ScriptFunction {
26 static PassOwnPtr<ScriptFunction> create(PassRefPtr<WaitUntilObserver> observer, ResolveType type)
28 ExecutionContext* executionContext = observer->executionContext();
29 return adoptPtr(new ThenFunction(toIsolate(executionContext), observer, type));
33 ThenFunction(v8::Isolate* isolate, PassRefPtr<WaitUntilObserver> observer, ResolveType type)
34 : ScriptFunction(isolate)
35 , m_observer(observer)
40 virtual ScriptValue call(ScriptValue value) OVERRIDE
43 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
44 if (m_resolveType == Rejected)
45 m_observer->reportError(value);
46 m_observer->decrementPendingActivity();
51 RefPtr<WaitUntilObserver> m_observer;
52 ResolveType m_resolveType;
55 PassRefPtr<WaitUntilObserver> WaitUntilObserver::create(ExecutionContext* context, int eventID)
57 return adoptRef(new WaitUntilObserver(context, eventID));
60 WaitUntilObserver::~WaitUntilObserver()
62 ASSERT(!m_pendingActivity);
65 void WaitUntilObserver::willDispatchEvent()
67 incrementPendingActivity();
70 void WaitUntilObserver::didDispatchEvent()
72 decrementPendingActivity();
75 void WaitUntilObserver::waitUntil(const ScriptValue& value)
77 incrementPendingActivity();
78 ScriptPromise(value).then(
79 ThenFunction::create(this, ThenFunction::Fulfilled),
80 ThenFunction::create(this, ThenFunction::Rejected));
83 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, int eventID)
84 : ContextLifecycleObserver(context)
86 , m_pendingActivity(0)
90 void WaitUntilObserver::reportError(const ScriptValue& value)
92 // FIXME: Propagate error message to the client for onerror handling.
96 void WaitUntilObserver::incrementPendingActivity()
101 void WaitUntilObserver::decrementPendingActivity()
103 ASSERT(m_pendingActivity > 0);
104 if (--m_pendingActivity || !executionContext())
107 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleInstallEvent(m_eventID);
111 } // namespace WebCore