Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: Source/bindings/v8/ScriptPromiseProperty.h

Issue 361863003: Add a helper for implementing Promise-valued properties. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Feedback--more state assertions in test. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/v8/ScriptPromise.h"
9 #include "bindings/v8/ScriptPromisePropertyBase.h"
10 #include "bindings/v8/V8Binding.h"
11 #include "platform/heap/Visitor.h"
haraken 2014/07/01 06:29:12 We normally include platform/heap/Handle.h.
12 #include "wtf/Noncopyable.h"
13 #include "wtf/PassRefPtr.h"
14 #include "wtf/RefPtr.h"
15
16 namespace WebCore {
17
18 class ExecutionContext;
19
20 // ScriptPromiseProperty is a helper for implementing a DOM method or
21 // attribute whose value is a Promise, and the same Promise must be
22 // returned each time.
23 //
24 // ScriptPromiseProperty does not keep Promises or worlds alive to
25 // deliver Promise resolution/rejection to them; the Promise
26 // resolution/rejections are delivered if the wrapper for the object
27 // with the property is alive. To avoid exposing the action of the
28 // garbage collector to script, you should keep the wrapper alive as
29 // long as a promise may be settled.
30 //
31 // ScriptPromiseProperty is an ActiveDOMObject and defers resolving or
32 // rejecting Promises while active DOM objects are suspended. Do not
33 // call promise, resolve or reject when active DOM objects are
34 // stopped.
35 //
36 // ScriptPromiseProperty only supports the main world.
37 // FIXME: Implement support for isolated worlds.
38 template<typename HolderType, typename ResolvedType, typename RejectedType>
39 class ScriptPromiseProperty : public ScriptPromisePropertyBase {
40 WTF_MAKE_NONCOPYABLE(ScriptPromiseProperty);
41 public:
42 // Creates a ScriptPromiseProperty that will create Promises in
43 // the specified ExecutionContext for a property of 'holder'
44 // (typically ScriptPromiseProperty should be a member of the
45 // property holder). 'name' must be a static string that is a
46 // unique name for the hidden value backing the property.
47 template<typename PassHolderType>
48 static PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType , RejectedType> > create(ExecutionContext*, PassHolderType, const char* name);
49
50 virtual ~ScriptPromiseProperty() { }
51
52 virtual void trace(Visitor*);
53
54 template<typename PassResolvedType>
55 void resolve(PassResolvedType);
56
57 template<typename PassRejectedType>
58 void reject(PassRejectedType);
59
60 private:
61 template<typename PassHolderType>
62 ScriptPromiseProperty(ExecutionContext*, PassHolderType, const char* name);
63
64 virtual v8::Handle<v8::Object> holder(v8::Handle<v8::Object> creationContext , v8::Isolate*) OVERRIDE;
65 virtual v8::Handle<v8::Value> resolvedValue(v8::Handle<v8::Object> creationC ontext, v8::Isolate*) OVERRIDE;
66 virtual v8::Handle<v8::Value> rejectedValue(v8::Handle<v8::Object> creationC ontext, v8::Isolate*) OVERRIDE;
67
68 HolderType m_holder;
69 ResolvedType m_resolved;
70 RejectedType m_rejected;
71 };
72
73 template<typename HolderType, typename ResolvedType, typename RejectedType>
74 template<typename PassHolderType>
75 PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedT ype> > ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::create(Exe cutionContext* executionContext, PassHolderType holder, const char* name)
76 {
77 RefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedT ype> > property = adoptRef(new ScriptPromiseProperty<HolderType, ResolvedType, R ejectedType>(executionContext, holder, name));
78 property->suspendIfNeeded();
79 return property.release();
80 }
81
82 template<typename HolderType, typename ResolvedType, typename RejectedType>
83 template<typename PassHolderType>
84 ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::ScriptPromiseProp erty(ExecutionContext* executionContext, PassHolderType holder, const char* name )
85 : ScriptPromisePropertyBase(executionContext, name)
86 , m_holder(holder)
87 {
88 }
89
90 template<typename HolderType, typename ResolvedType, typename RejectedType>
91 void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::trace(Visito r* visitor)
92 {
93 visitor->trace(m_holder);
94 visitor->trace(m_resolved);
95 visitor->trace(m_rejected);
haraken 2014/07/01 06:29:12 What are types of m_holder, m_resolved and m_rejec
96 }
97
98 template<typename HolderType, typename ResolvedType, typename RejectedType>
99 template<typename PassResolvedType>
100 void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::resolve(Pass ResolvedType value)
101 {
102 if (state() != Pending)
103 return;
104 m_resolved = value;
105 settle(Resolved);
106 }
107
108 template<typename HolderType, typename ResolvedType, typename RejectedType>
109 template<typename PassRejectedType>
110 void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::reject(PassR ejectedType value)
111 {
112 if (state() != Pending)
113 return;
114 m_rejected = value;
115 settle(Rejected);
116 }
117
118 template<typename HolderType, typename ResolvedType, typename RejectedType>
119 v8::Handle<v8::Object> ScriptPromiseProperty<HolderType, ResolvedType, RejectedT ype>::holder(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
120 {
121 v8::Handle<v8::Value> value = V8ValueTraits<HolderType>::toV8Value(m_holder, creationContext, isolate);
122 return value.As<v8::Object>();
123 }
124
125 template<typename HolderType, typename ResolvedType, typename RejectedType>
126 v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedTy pe>::resolvedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
127 {
128 ASSERT(state() == Resolved);
129 return V8ValueTraits<ResolvedType>::toV8Value(m_resolved, creationContext, i solate);
130 }
131
132 template<typename HolderType, typename ResolvedType, typename RejectedType>
133 v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedTy pe>::rejectedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
134 {
135 ASSERT(state() == Rejected);
136 return V8ValueTraits<RejectedType>::toV8Value(m_rejected, creationContext, i solate);
137 }
138
139 } // namespace WebCore
140
141 #endif // ScriptPromiseProperty_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698