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 ScriptPromisePropertyBase_h
6#define ScriptPromisePropertyBase_h
7
8#include "bindings/core/v8/ScopedPersistent.h"
9#include "bindings/core/v8/ScriptPromise.h"
10#include "bindings/core/v8/ScriptPromiseProperties.h"
11#include "core/dom/ContextLifecycleObserver.h"
12#include "wtf/OwnPtr.h"
13#include "wtf/RefCounted.h"
14#include "wtf/Vector.h"
15#include <v8.h>
16
17namespace blink {
18
19class DOMWrapperWorld;
20class ExecutionContext;
21class ScriptState;
22
23class ScriptPromisePropertyBase : public GarbageCollectedFinalized<ScriptPromisePropertyBase>, public ContextLifecycleObserver {
24public:
25    virtual ~ScriptPromisePropertyBase();
26
27    enum Name {
28#define P(Name) Name,
29        SCRIPT_PROMISE_PROPERTIES(P)
30#undef P
31    };
32
33    enum State {
34        Pending,
35        Resolved,
36        Rejected,
37    };
38    State state() const { return m_state; }
39
40    ScriptPromise promise(DOMWrapperWorld&);
41
42    virtual void trace(Visitor*) { }
43
44protected:
45    ScriptPromisePropertyBase(ExecutionContext*, Name);
46
47    void resolveOrReject(State targetState);
48
49    // ScriptPromiseProperty overrides these to wrap the holder,
50    // rejected value and resolved value. The
51    // ScriptPromisePropertyBase caller will enter the V8Context for
52    // the property's execution context and the world it is
53    // creating/settling promises in; the implementation should use
54    // this context.
55    virtual v8::Handle<v8::Object> holder(v8::Handle<v8::Object> creationContext, v8::Isolate*) = 0;
56    virtual v8::Handle<v8::Value> resolvedValue(v8::Handle<v8::Object> creationContext, v8::Isolate*) = 0;
57    virtual v8::Handle<v8::Value> rejectedValue(v8::Handle<v8::Object> creationContext, v8::Isolate*) = 0;
58
59    void resetBase();
60
61private:
62    typedef Vector<OwnPtr<ScopedPersistent<v8::Object> > > WeakPersistentSet;
63
64    void resolveOrRejectInternal(v8::Handle<v8::Promise::Resolver>);
65    v8::Local<v8::Object> ensureHolderWrapper(ScriptState*);
66    void clearWrappers();
67
68    v8::Handle<v8::String> promiseName();
69    v8::Handle<v8::String> resolverName();
70
71    v8::Isolate* m_isolate;
72    Name m_name;
73    State m_state;
74
75    WeakPersistentSet m_wrappers;
76};
77
78} // namespace blink
79
80#endif // ScriptPromisePropertyBase_h
81