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

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

Issue 209853010: [ABANDONED] Enable V8 Promises (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef ScriptPromiseResolverWithContext_h 5 #ifndef ScriptPromiseResolverWithContext_h
6 #define ScriptPromiseResolverWithContext_h 6 #define ScriptPromiseResolverWithContext_h
7 7
8 #include "bindings/v8/NewScriptState.h" 8 #include "bindings/v8/NewScriptState.h"
9 #include "bindings/v8/ScopedPersistent.h" 9 #include "bindings/v8/ScopedPersistent.h"
10 #include "bindings/v8/ScriptPromise.h" 10 #include "bindings/v8/ScriptPromise.h"
(...skipping 23 matching lines...) Expand all
34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s criptState) 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s criptState)
35 { 35 {
36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState)); 36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState));
37 resolver->suspendIfNeeded(); 37 resolver->suspendIfNeeded();
38 return resolver.release(); 38 return resolver.release();
39 } 39 }
40 40
41 template <typename T> 41 template <typename T>
42 void resolve(T value) 42 void resolve(T value)
43 { 43 {
44 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) 44 resolveOrReject(value, Resolving);
45 return;
46 m_state = Resolving;
47 NewScriptState::Scope scope(m_scriptState.get());
48 m_value.set(m_scriptState->isolate(), toV8(value));
49 if (!executionContext()->activeDOMObjectsAreSuspended())
50 resolveOrRejectImmediately(&m_timer);
51 } 45 }
52 46
53 template <typename T> 47 template <typename T>
54 void reject(T value) 48 void reject(T value)
55 { 49 {
56 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) 50 resolveOrReject(value, Rejecting);
57 return;
58 m_state = Rejecting;
59 NewScriptState::Scope scope(m_scriptState.get());
60 m_value.set(m_scriptState->isolate(), toV8(value));
61 if (!executionContext()->activeDOMObjectsAreSuspended())
62 resolveOrRejectImmediately(&m_timer);
63 } 51 }
64 52
65 // Note that an empty ScriptPromise will be returned after resolve or 53 // Note that an empty ScriptPromise will be returned after resolve or
66 // reject is called. 54 // reject is called.
67 ScriptPromise promise() 55 ScriptPromise promise()
68 { 56 {
69 return m_resolver ? m_resolver->promise() : ScriptPromise(); 57 return m_resolver ? m_resolver->promise() : ScriptPromise();
70 } 58 }
71 59
72 // ActiveDOMObject implementation. 60 // ActiveDOMObject implementation.
(...skipping 19 matching lines...) Expand all
92 return toV8NoInline(value, m_scriptState->context()->Global(), m_scriptS tate->isolate()); 80 return toV8NoInline(value, m_scriptState->context()->Global(), m_scriptS tate->isolate());
93 } 81 }
94 template <typename T> v8::Handle<v8::Value> toV8(PassRefPtr<T> value) { retu rn toV8(value.get()); } 82 template <typename T> v8::Handle<v8::Value> toV8(PassRefPtr<T> value) { retu rn toV8(value.get()); }
95 template <typename T> v8::Handle<v8::Value> toV8(RawPtr<T> value) { return t oV8(value.get()); } 83 template <typename T> v8::Handle<v8::Value> toV8(RawPtr<T> value) { return t oV8(value.get()); }
96 template <typename T, size_t inlineCapacity> 84 template <typename T, size_t inlineCapacity>
97 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value) 85 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value)
98 { 86 {
99 ASSERT(m_scriptState); 87 ASSERT(m_scriptState);
100 return v8ArrayNoInline(value, m_scriptState->isolate()); 88 return v8ArrayNoInline(value, m_scriptState->isolate());
101 } 89 }
102 v8::Handle<v8::Value> toV8(ScriptValue value) 90 v8::Handle<v8::Value> toV8(ScriptValue value) { return value.v8Value(); }
91 v8::Handle<v8::Value> toV8(v8::Handle<v8::Value> value) { return value; }
92
93 template <typename T>
94 void resolveOrReject(T value, ResolutionState newState)
103 { 95 {
104 return value.v8Value(); 96 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped())
97 return;
98 m_state = newState;
99 // Retain this object until it is actually resolved or rejected.
100 // deref will be called in resolveOrRejectImmediately.
101 ref();
yhirano 2014/04/15 08:45:06 I don't like the manual ref counting, but I couldn
102
103 bool isInContext = m_scriptState->isolate()->InContext();
104 NewScriptState::Scope scope(m_scriptState.get());
105 m_value.set(m_scriptState->isolate(), toV8(value));
106 if (!executionContext()->activeDOMObjectsAreSuspended()) {
107 if (isInContext) {
108 resolveOrRejectImmediately();
109 } else {
110 m_timer.startOneShot(0, FROM_HERE);
111 }
112 }
105 } 113 }
106 114
107 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); 115 void resolveOrRejectImmediately();
116 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*);
108 void clear(); 117 void clear();
109 118
110 ResolutionState m_state; 119 ResolutionState m_state;
111 RefPtr<NewScriptState> m_scriptState; 120 RefPtr<NewScriptState> m_scriptState;
112 Timer<ScriptPromiseResolverWithContext> m_timer; 121 Timer<ScriptPromiseResolverWithContext> m_timer;
113 RefPtr<ScriptPromiseResolver> m_resolver; 122 RefPtr<ScriptPromiseResolver> m_resolver;
114 ScopedPersistent<v8::Value> m_value; 123 ScopedPersistent<v8::Value> m_value;
115 }; 124 };
116 125
117 } // namespace WebCore 126 } // namespace WebCore
118 127
119 #endif // #ifndef ScriptPromiseResolverWithContext_h 128 #endif // #ifndef ScriptPromiseResolverWithContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698