18abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)/*
28abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
38abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
48abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
58abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * modification, are permitted provided that the following conditions are
68abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * met:
78abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
88abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
98abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * in the documentation and/or other materials provided with the
138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * distribution.
148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * this software without specific prior written permission.
178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) */
308abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
318abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#ifndef ScriptPromise_h
328abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#define ScriptPromise_h
338abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ScriptFunction.h"
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ScriptValue.h"
36e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "bindings/core/v8/V8ThrowException.h"
37e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "core/dom/ExceptionCode.h"
38d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "platform/heap/Handle.h"
396f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch#include "wtf/PassOwnPtr.h"
40d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "wtf/PassRefPtr.h"
41e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "wtf/text/WTFString.h"
428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include <v8.h>
438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
44c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
458abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
46d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)class DOMException;
47e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)class ExceptionState;
48bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
498abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)// ScriptPromise is the class for representing Promise values in C++ world.
508abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)// ScriptPromise holds a Promise.
518abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)// So holding a ScriptPromise as a member variable in DOM object causes
528abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)// memory leaks since it has a reference from C++ to V8.
538abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)//
54197021e6b966cfb06891637935ef33fff06433d1Ben Murdochclass ScriptPromise FINAL {
558abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)public:
56bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    // Constructs an empty promise.
5709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ScriptPromise() { }
588abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
59d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Constructs a ScriptPromise from |promise|.
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // If |promise| is not a Promise object, throws a v8 TypeError.
61323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    ScriptPromise(ScriptState*, v8::Handle<v8::Value> promise);
628abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
637242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    ScriptPromise then(v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected = v8::Handle<v8::Function>());
6409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
658abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool isObject() const
668abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
678abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return m_promise.isObject();
688abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
698abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool isNull() const
718abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
728abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return m_promise.isNull();
738abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
748abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
758abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool isUndefinedOrNull() const
768abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
778abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return m_promise.isUndefined() || m_promise.isNull();
788abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
798abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
808abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    v8::Handle<v8::Value> v8Value() const
818abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
828abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return m_promise.v8Value();
838abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
848abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
85a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    v8::Isolate* isolate() const
86a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    {
87a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        return m_promise.isolate();
88a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
9010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    bool isEmpty() const
918abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
9210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        return m_promise.isEmpty();
938abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
948abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
958abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    void clear()
968abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
978abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        m_promise.clear();
988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
998abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
100197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool operator==(const ScriptPromise& value) const
101197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
102197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return m_promise == value.m_promise;
103197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
104197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
105197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool operator!=(const ScriptPromise& value) const
106197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    {
107197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return !operator==(value);
108197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
109197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Constructs and returns a ScriptPromise from |value|.
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // if |value| is not a Promise object, returns a Promise object
112d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // resolved with |value|.
113d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // Returns |value| itself if it is a Promise.
1145d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    static ScriptPromise cast(ScriptState*, const ScriptValue& /*value*/);
115d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    static ScriptPromise cast(ScriptState*, v8::Handle<v8::Value> /*value*/);
116d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
1175d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    static ScriptPromise reject(ScriptState*, const ScriptValue&);
118d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    static ScriptPromise reject(ScriptState*, v8::Handle<v8::Value>);
119d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
120d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    static ScriptPromise rejectWithDOMException(ScriptState*, PassRefPtrWillBeRawPtr<DOMException>);
121d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
122e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    static v8::Local<v8::Promise> rejectRaw(v8::Isolate*, v8::Handle<v8::Value>);
123e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
124197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // This is a utility class intended to be used internally.
125197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // ScriptPromiseResolver is for general purpose.
126197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    class InternalResolver FINAL {
127197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    public:
128197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        explicit InternalResolver(ScriptState*);
129197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        v8::Local<v8::Promise> v8Promise() const;
130197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        ScriptPromise promise() const;
131197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        void resolve(v8::Local<v8::Value>);
132197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        void reject(v8::Local<v8::Value>);
133197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        void clear() { m_resolver.clear(); }
134197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
135197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    private:
136197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        ScriptValue m_resolver;
137197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    };
138197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
139d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)private:
140323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    RefPtr<ScriptState> m_scriptState;
1418abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    ScriptValue m_promise;
1428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)};
1438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
144c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1458abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1468abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1478abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#endif // ScriptPromise_h
148