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 RespondWithObserver_h
6#define RespondWithObserver_h
7
8#include "core/dom/ContextLifecycleObserver.h"
9#include "platform/heap/Handle.h"
10#include "wtf/Forward.h"
11#include "wtf/RefCounted.h"
12
13namespace blink {
14
15class ExceptionState;
16class ExecutionContext;
17class Response;
18class ScriptState;
19class ScriptValue;
20
21// This class observes the service worker's handling of a FetchEvent and
22// notifies the client.
23class RespondWithObserver FINAL : public GarbageCollectedFinalized<RespondWithObserver>, public ContextLifecycleObserver {
24public:
25    static RespondWithObserver* create(ExecutionContext*, int eventID);
26
27    virtual void contextDestroyed() OVERRIDE;
28
29    void didDispatchEvent();
30
31    // Observes the promise and delays calling didHandleFetchEvent() until the
32    // given promise is resolved or rejected.
33    void respondWith(ScriptState*, const ScriptValue&, ExceptionState&);
34
35    void responseWasRejected();
36    void responseWasFulfilled(const ScriptValue&);
37
38    void trace(Visitor*) { }
39
40private:
41    class ThenFunction;
42
43    RespondWithObserver(ExecutionContext*, int eventID);
44
45    int m_eventID;
46
47    enum State { Initial, Pending, Done };
48    State m_state;
49};
50
51} // namespace blink
52
53#endif // RespondWithObserver_h
54