Chromium Code Reviews| 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 ScriptPromiseProperty_h | |
| 6 #define ScriptPromiseProperty_h | |
| 7 | |
| 8 #include "bindings/core/v8/ScriptPromise.h" | |
| 9 #include "bindings/core/v8/ScriptPromisePropertyBase.h" | |
| 10 #include "bindings/core/v8/V8Binding.h" | |
| 11 #include "wtf/Noncopyable.h" | |
| 12 #include "wtf/PassRefPtr.h" | |
| 13 | |
| 14 namespace WebCore { | |
| 15 | |
| 16 class ExecutionContext; | |
| 17 | |
| 18 // ScriptPromiseProperty is a helper for implementing a DOM method or | |
| 19 // attribute whose value is a Promise, and the same Promise must be | |
|
haraken
2014/07/02 09:04:31
whose value is a Promise => that returns a Promise
| |
| 20 // returned each time. | |
| 21 // | |
| 22 // ScriptPromiseProperty does not keep Promises or worlds alive to | |
| 23 // deliver Promise resolution/rejection to them; the Promise | |
| 24 // resolution/rejections are delivered if the wrapper for the object | |
| 25 // with the property is alive. This is achieved by keeping a weak | |
|
haraken
2014/07/02 09:04:31
the wrapper for the object with the property => th
| |
| 26 // reference from ScriptPromiseProperty to the holder's wrapper, and | |
| 27 // references in hidden values between the wrapper, promise and | |
| 28 // resolver (coincidentally the Resolver and Promise may be the same | |
| 29 // object, but that is an implementation detail of v8.) | |
| 30 // | |
| 31 // -----> Resolver | |
| 32 // / | |
| 33 // ScriptPromiseProperty - - -> Holder Wrapper <------. | |
| 34 // \ | | |
| 35 // -----> Promise | |
| 36 // | |
| 37 // To avoid exposing the action of the garbage collector to script, | |
| 38 // you should keep the wrapper alive as long as a promise may be | |
| 39 // settled. | |
| 40 // | |
| 41 // ScriptPromiseProperty only supports the main world. | |
| 42 // FIXME: Implement support for isolated worlds. | |
| 43 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 44 class ScriptPromiseProperty : public ScriptPromisePropertyBase { | |
| 45 WTF_MAKE_NONCOPYABLE(ScriptPromiseProperty); | |
| 46 public: | |
| 47 // Creates a ScriptPromiseProperty that will create Promises in | |
| 48 // the specified ExecutionContext for a property of 'holder' | |
| 49 // (typically ScriptPromiseProperty should be a member of the | |
| 50 // property holder). 'name' must be a unique name for the hidden | |
| 51 // value backing the property. | |
| 52 template<typename PassHolderType> | |
| 53 static PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType , RejectedType> > create(ExecutionContext*, PassHolderType, const char* name); | |
| 54 | |
| 55 virtual ~ScriptPromiseProperty() { } | |
| 56 | |
| 57 template<typename PassResolvedType> | |
| 58 void resolve(PassResolvedType); | |
| 59 | |
| 60 template<typename PassRejectedType> | |
| 61 void reject(PassRejectedType); | |
| 62 | |
| 63 private: | |
| 64 template<typename PassHolderType> | |
| 65 ScriptPromiseProperty(ExecutionContext*, PassHolderType, const char* name); | |
| 66 | |
| 67 virtual v8::Handle<v8::Object> holder(v8::Handle<v8::Object> creationContext , v8::Isolate*) OVERRIDE; | |
| 68 virtual v8::Handle<v8::Value> resolvedValue(v8::Handle<v8::Object> creationC ontext, v8::Isolate*) OVERRIDE; | |
| 69 virtual v8::Handle<v8::Value> rejectedValue(v8::Handle<v8::Object> creationC ontext, v8::Isolate*) OVERRIDE; | |
| 70 | |
| 71 HolderType m_holder; | |
| 72 ResolvedType m_resolved; | |
| 73 RejectedType m_rejected; | |
| 74 }; | |
| 75 | |
| 76 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 77 template<typename PassHolderType> | |
| 78 PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedT ype> > ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::create(Exe cutionContext* executionContext, PassHolderType holder, const char* name) | |
| 79 { | |
| 80 RefPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedType> > prope rty = adoptRef(new ScriptPromiseProperty<HolderType, ResolvedType, RejectedType> (executionContext, holder, name)); | |
| 81 return property.release(); | |
|
haraken
2014/07/02 09:04:31
return adoptRef(new ScriptPromiseProperty(...));
| |
| 82 } | |
| 83 | |
| 84 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 85 template<typename PassHolderType> | |
| 86 ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::ScriptPromiseProp erty(ExecutionContext* executionContext, PassHolderType holder, const char* name ) | |
| 87 : ScriptPromisePropertyBase(executionContext, name) | |
| 88 , m_holder(holder) | |
| 89 { | |
| 90 } | |
| 91 | |
| 92 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 93 template<typename PassResolvedType> | |
| 94 void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::resolve(Pass ResolvedType value) | |
| 95 { | |
| 96 if (!(executionContext() && state() == Pending)) | |
|
haraken
2014/07/02 09:04:31
Is it valid that resolve() is called while state()
dominicc (has gone to gerrit)
2014/07/03 02:13:39
I originally did it this way, but yhirano says thi
| |
| 97 return; | |
| 98 m_resolved = value; | |
| 99 settle(Resolved); | |
|
haraken
2014/07/02 09:04:31
settle => resolveOrReject (to align with the funct
| |
| 100 } | |
| 101 | |
| 102 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 103 template<typename PassRejectedType> | |
| 104 void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::reject(PassR ejectedType value) | |
| 105 { | |
| 106 if (!(executionContext() && state() == Pending)) | |
| 107 return; | |
| 108 m_rejected = value; | |
| 109 settle(Rejected); | |
| 110 } | |
| 111 | |
| 112 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 113 v8::Handle<v8::Object> ScriptPromiseProperty<HolderType, ResolvedType, RejectedT ype>::holder(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) | |
| 114 { | |
| 115 v8::Handle<v8::Value> value = V8ValueTraits<HolderType>::toV8Value(m_holder, creationContext, isolate); | |
|
haraken
2014/07/02 09:04:31
Is it guaranteed that we're in a correct context w
dominicc (has gone to gerrit)
2014/07/03 02:13:39
Yes, the ScriptPromisePropertyBase::promise and ::
| |
| 116 return value.As<v8::Object>(); | |
| 117 } | |
| 118 | |
| 119 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 120 v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedTy pe>::resolvedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) | |
| 121 { | |
| 122 ASSERT(state() == Resolved); | |
| 123 return V8ValueTraits<ResolvedType>::toV8Value(m_resolved, creationContext, i solate); | |
| 124 } | |
| 125 | |
| 126 template<typename HolderType, typename ResolvedType, typename RejectedType> | |
| 127 v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedTy pe>::rejectedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) | |
| 128 { | |
| 129 ASSERT(state() == Rejected); | |
| 130 return V8ValueTraits<RejectedType>::toV8Value(m_rejected, creationContext, i solate); | |
| 131 } | |
| 132 | |
| 133 } // namespace WebCore | |
| 134 | |
| 135 #endif // ScriptPromiseProperty_h | |
| OLD | NEW |