OLD | NEW |
(Empty) | |
| 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 AbstractScriptPromiseResolver_h |
| 6 #define AbstractScriptPromiseResolver_h |
| 7 |
| 8 #include "bindings/common/ScriptPromise.h" |
| 9 #include "bindings/common/ScriptState.h" |
| 10 #include "bindings/core/v8/ScopedPersistent.h" |
| 11 #include "bindings/core/v8/V8Binding.h" |
| 12 #include "core/dom/ActiveDOMObject.h" |
| 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "platform/Timer.h" |
| 15 #include "wtf/RefCounted.h" |
| 16 #include "wtf/Vector.h" |
| 17 #include <v8.h> |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 class BatteryManager; |
| 22 class Blob; |
| 23 class BooleanValue; |
| 24 class Cache; |
| 25 class Credential; |
| 26 class CryptoKey; |
| 27 class DOMError; |
| 28 class DOMException; |
| 29 class ExecutionContext; |
| 30 class FontFace; |
| 31 class FontFaceSet; |
| 32 class GeofencingRegion; |
| 33 class ImageBitmap; |
| 34 class MIDIAccess; |
| 35 class MediaKeySession; |
| 36 class MediaKeys; |
| 37 class PushRegistration; |
| 38 class Response; |
| 39 class Request; |
| 40 class ServiceWorker; |
| 41 class ServiceWorkerClient; |
| 42 class ServiceWorkerRegistration; |
| 43 class StorageInfo; |
| 44 |
| 45 #define PROMISE_RESOLUTION_TYPES_LIST(V) \ |
| 46 V(AtomicString) \ |
| 47 V(BatteryManager*) \ |
| 48 V(Cache*) \ |
| 49 V(Credential*) \ |
| 50 V(CryptoKey*) \ |
| 51 V(DOMError*) \ |
| 52 V(DOMException*) \ |
| 53 V(HeapVector<Member<GeofencingRegion> >) \ |
| 54 V(HeapVector<Member<ServiceWorkerClient> >) \ |
| 55 V(MIDIAccess*) \ |
| 56 V(MediaKeys*) \ |
| 57 V(PassRefPtr<ArrayBuffer>) \ |
| 58 V(PassRefPtr<BatteryManager>) \ |
| 59 V(PassRefPtr<MediaKeySession>) \ |
| 60 V(PassRefPtrWillBeRawPtr<Blob>) \ |
| 61 V(PassRefPtrWillBeRawPtr<DOMError>) \ |
| 62 V(PassRefPtrWillBeRawPtr<DOMException>) \ |
| 63 V(PassRefPtrWillBeRawPtr<FontFace>) \ |
| 64 V(PassRefPtrWillBeRawPtr<FontFaceSet>) \ |
| 65 V(PassRefPtrWillBeRawPtr<ImageBitmap>) \ |
| 66 V(PassRefPtrWillBeRawPtr<MIDIAccess>) \ |
| 67 V(PassRefPtrWillBeRawPtr<ServiceWorker>) \ |
| 68 V(PassRefPtrWillBeRawPtr<ServiceWorkerRegistration>) \ |
| 69 V(PushRegistration*) \ |
| 70 V(Response*) \ |
| 71 V(ScriptValue) \ |
| 72 V(ServiceWorker*) \ |
| 73 V(ServiceWorkerRegistration*) \ |
| 74 V(StorageInfo*) \ |
| 75 V(String) \ |
| 76 V(V8BoolType) \ |
| 77 V(V8UndefinedType) \ |
| 78 V(Vector<String>) \ |
| 79 V(WillBeHeapVector<RefPtrWillBeMember<FontFace> >) \ |
| 80 V(const char*) \ |
| 81 V(v8::Handle<v8::Value>) \ |
| 82 V(HeapVector<Member<Response> >) \ |
| 83 V(HeapVector<Member<Request> >) \ |
| 84 |
| 85 // This class wraps v8::Promise::Resolver and provides the following |
| 86 // functionalities. |
| 87 // - A ScriptPromiseResolver retains a ScriptState. A caller |
| 88 // can call resolve or reject from outside of a V8 context. |
| 89 // - This class is an ActiveDOMObject and keeps track of the associated |
| 90 // ExecutionContext state. When the ExecutionContext is suspended, |
| 91 // resolve or reject will be delayed. When it is stopped, resolve or reject |
| 92 // will be ignored. |
| 93 class AbstractScriptPromiseResolver { |
| 94 WTF_MAKE_NONCOPYABLE(AbstractScriptPromiseResolver); |
| 95 public: |
| 96 virtual ~AbstractScriptPromiseResolver() { } |
| 97 |
| 98 // Note that an empty ScriptPromise will be returned after resolve or |
| 99 // reject is called. |
| 100 virtual PassRefPtr<AbstractScriptPromise> promise() = 0; |
| 101 |
| 102 virtual void resolve() = 0; |
| 103 virtual void reject() = 0; |
| 104 |
| 105 template <typename T> |
| 106 void resolve(T value) { resolveVirtual(value); } |
| 107 |
| 108 template <typename T> |
| 109 void reject(T error) { rejectVirtual(error); } |
| 110 |
| 111 // Once this function is called this resolver stays alive while the |
| 112 // promise is pending and the associated ExecutionContext isn't stopped. |
| 113 virtual void keepAliveWhilePending() = 0; |
| 114 |
| 115 virtual ScriptState* scriptState() = 0; |
| 116 virtual ScriptState* scriptState() const = 0; |
| 117 |
| 118 // Forwarded ActiveDOMObject implementation. |
| 119 virtual void suspend() = 0; |
| 120 virtual void resume() = 0; |
| 121 virtual void stop() = 0; |
| 122 |
| 123 protected: |
| 124 // You need to call suspendIfNeeded after the construction because |
| 125 // this is an ActiveDOMObject. |
| 126 AbstractScriptPromiseResolver() { } |
| 127 |
| 128 private: |
| 129 // It would be preferable to simply expose these virtual methods as the |
| 130 // public methods for resolve/reject. But we need to expose a resolve(bool), |
| 131 // and if exposed this way the implicit conversion from a pointer type to |
| 132 // bool interfers with conversions of RefPtr<X> to PassRefPtr<X> and can |
| 133 // lead to subtle bugs when a new type needs to be added to the macro |
| 134 // PROMISE_RESOLUTION_TYPES_LIST. Instead we use template specialization to |
| 135 // expose a resolve(bool) that is not subject to implicit conversions. |
| 136 #define DECLARE_RESOLUTION_METHODS(type) \ |
| 137 virtual void resolveVirtual(type) = 0; \ |
| 138 virtual void rejectVirtual(type) = 0; |
| 139 PROMISE_RESOLUTION_TYPES_LIST(DECLARE_RESOLUTION_METHODS); |
| 140 #undef DECLARE_RESOLUTION_METHODS |
| 141 }; |
| 142 |
| 143 template <> |
| 144 inline void AbstractScriptPromiseResolver::resolve<bool>(bool value) |
| 145 { |
| 146 resolveVirtual(V8BoolType(value)); |
| 147 } |
| 148 |
| 149 template <> |
| 150 inline void AbstractScriptPromiseResolver::reject<bool>(bool error) |
| 151 { |
| 152 rejectVirtual(V8BoolType(error)); |
| 153 } |
| 154 |
| 155 } // namespace blink |
| 156 |
| 157 #endif // #ifndef AbstractScriptPromiseResolver_h |
OLD | NEW |