1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef ScriptPromise_h 32 #define ScriptPromise_h 33 34 #include "bindings/v8/ScriptFunction.h" 35 #include "bindings/v8/ScriptValue.h" 36 #include "platform/heap/Handle.h" 37 #include "wtf/PassOwnPtr.h" 38 #include "wtf/PassRefPtr.h" 39 #include <v8.h> 40 41 namespace WebCore { 42 43 class DOMException; 44 45 // ScriptPromise is the class for representing Promise values in C++ world. 46 // ScriptPromise holds a Promise. 47 // So holding a ScriptPromise as a member variable in DOM object causes 48 // memory leaks since it has a reference from C++ to V8. 49 // 50 class ScriptPromise { 51 public: 52 // Constructs an empty promise. ScriptPromise()53 ScriptPromise() { } 54 55 // Constructs a ScriptPromise from |promise|. 56 // If |promise| is not a Promise object, throws a v8 TypeError. 57 ScriptPromise(ScriptState*, v8::Handle<v8::Value> promise); 58 59 ScriptPromise then(PassOwnPtr<ScriptFunction> onFulfilled, PassOwnPtr<ScriptFunction> onRejected = PassOwnPtr<ScriptFunction>()); 60 isObject()61 bool isObject() const 62 { 63 return m_promise.isObject(); 64 } 65 isNull()66 bool isNull() const 67 { 68 return m_promise.isNull(); 69 } 70 isUndefinedOrNull()71 bool isUndefinedOrNull() const 72 { 73 return m_promise.isUndefined() || m_promise.isNull(); 74 } 75 v8Value()76 v8::Handle<v8::Value> v8Value() const 77 { 78 return m_promise.v8Value(); 79 } 80 isolate()81 v8::Isolate* isolate() const 82 { 83 return m_promise.isolate(); 84 } 85 isEmpty()86 bool isEmpty() const 87 { 88 return m_promise.isEmpty(); 89 } 90 clear()91 void clear() 92 { 93 m_promise.clear(); 94 } 95 96 // Constructs and returns a ScriptPromise from |value|. 97 // if |value| is not a Promise object, returns a Promise object 98 // resolved with |value|. 99 // Returns |value| itself if it is a Promise. 100 static ScriptPromise cast(ScriptState*, const ScriptValue& /*value*/); 101 static ScriptPromise cast(ScriptState*, v8::Handle<v8::Value> /*value*/); 102 103 static ScriptPromise reject(ScriptState*, const ScriptValue&); 104 static ScriptPromise reject(ScriptState*, v8::Handle<v8::Value>); 105 106 static ScriptPromise rejectWithDOMException(ScriptState*, PassRefPtrWillBeRawPtr<DOMException>); 107 108 private: 109 RefPtr<ScriptState> m_scriptState; 110 ScriptValue m_promise; 111 }; 112 113 } // namespace WebCore 114 115 116 #endif // ScriptPromise_h 117