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.
4
5#ifndef WaitUntilObserver_h
6#define WaitUntilObserver_h
7
8#include "core/dom/ContextLifecycleObserver.h"
9#include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
10#include "wtf/Forward.h"
11#include "wtf/RefCounted.h"
12
13namespace blink {
14
15class ExecutionContext;
16class ScriptState;
17class ScriptValue;
18
19// Created for each ExtendableEvent instance.
20class WaitUntilObserver FINAL : public GarbageCollectedFinalized<WaitUntilObserver>, public ContextLifecycleObserver {
21public:
22    enum EventType {
23        Activate,
24        Install
25    };
26
27    static WaitUntilObserver* create(ExecutionContext*, EventType, int eventID);
28
29    // Must be called before and after dispatching the event.
30    void willDispatchEvent();
31    void didDispatchEvent();
32
33    // Observes the promise and delays calling the continuation until
34    // the given promise is resolved or rejected.
35    void waitUntil(ScriptState*, const ScriptValue&);
36
37    void trace(Visitor*) { }
38
39private:
40    class ThenFunction;
41
42    WaitUntilObserver(ExecutionContext*, EventType, int eventID);
43
44    void reportError(const ScriptValue&);
45
46    void incrementPendingActivity();
47    void decrementPendingActivity();
48
49    EventType m_type;
50    int m_eventID;
51    int m_pendingActivity;
52    bool m_hasError;
53};
54
55} // namespace blink
56
57#endif // WaitUntilObserver_h
58