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

Side by Side Diff: Source/bindings/core/v8/ScriptPromisePropertyBase.cpp

Issue 361863003: Add a helper for implementing Promise-valued properties. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Remove a RefCountedWillBeGarbageCollected. 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 #include "config.h"
6 #include "bindings/core/v8/ScriptPromisePropertyBase.h"
7
8 #include "bindings/core/v8/V8Binding.h"
9 #include "bindings/core/v8/V8HiddenValue.h"
10 #include "core/dom/ExecutionContext.h"
11 #include "wtf/text/StringBuilder.h"
12
13 namespace WebCore {
14
15 static String makeName(const char* name, const char* suffix)
16 {
17 StringBuilder builder;
18 builder.append(name);
19 builder.append(suffix);
20 return builder.toString();
21 }
22
23 ScriptPromisePropertyBase::ScriptPromisePropertyBase(ExecutionContext* execution Context, const char* name)
24 : ContextLifecycleObserver(executionContext)
25 , m_isolate(toIsolate(executionContext))
26 , m_state(Pending)
27 {
28 v8::HandleScope handleScope(m_isolate);
haraken 2014/07/02 09:04:31 The HandleScope is not needed.
29 m_resolverName = makeName(name, "/Resolver");
30 m_promiseName = makeName(name, "/Promise");
haraken 2014/07/02 09:04:32 It's a bit fragile that there is no mechanism to p
31 }
32
33 ScriptPromisePropertyBase::~ScriptPromisePropertyBase()
34 {
35 v8::HandleScope handleScope(m_isolate);
36 v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate);
37 if (!wrapper.IsEmpty()) {
38 v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName );
39 wrapper->DeleteHiddenValue(resolverName);
40 v8::Handle<v8::String> promiseName = v8String(m_isolate, m_promiseName);
41 wrapper->DeleteHiddenValue(promiseName);
42 }
43 }
44
45 static void clearHandle(const v8::WeakCallbackData<v8::Object, ScopedPersistent< v8::Object> >& data)
46 {
47 data.GetParameter()->clear();
48 }
49
50 ScriptPromise ScriptPromisePropertyBase::promise(DOMWrapperWorld& world)
51 {
52 if (!executionContext())
53 return ScriptPromise();
54
55 if (!world.isMainWorld()) {
56 // FIXME: Support isolated worlds.
57 return ScriptPromise();
58 }
59
60 v8::HandleScope handleScope(m_isolate);
61 v8::Handle<v8::Context> context = toV8Context(executionContext(), world);
62 if (context.IsEmpty())
63 return ScriptPromise();
64 ScriptState* scriptState = ScriptState::from(context);
65 ScriptState::Scope scope(scriptState);
66
67 v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName);
68 v8::Handle<v8::String> promiseName = v8String(m_isolate, m_promiseName);
69
70 v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate);
71 if (wrapper.IsEmpty()) {
72 wrapper = holder(context->Global(), m_isolate);
73 ASSERT(!wrapper.IsEmpty());
74 ASSERT(V8HiddenValue::getHiddenValue(m_isolate, wrapper, resolverName).I sEmpty());
75 ASSERT(V8HiddenValue::getHiddenValue(m_isolate, wrapper, promiseName).Is Empty());
76 m_mainWorldWrapper.set(m_isolate, wrapper);
77 m_mainWorldWrapper.setWeak(&m_mainWorldWrapper, &clearHandle);
78 }
79 ASSERT(wrapper->CreationContext() == context);
80
81 v8::Handle<v8::Promise> promise = V8HiddenValue::getHiddenValue(m_isolate, w rapper, promiseName).As<v8::Promise>();
82 if (!promise.IsEmpty()) {
83 // Return cached Promise
84 return ScriptPromise(scriptState, promise);
85 }
86
87 // Create and cache the Promise
88 v8::Handle<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(m_is olate);
89 promise = resolver->GetPromise();
90 V8HiddenValue::setHiddenValue(m_isolate, wrapper, promiseName, promise);
91 V8HiddenValue::setHiddenValue(m_isolate, promise, promiseName, wrapper);
92
93 switch (m_state) {
94 case Pending:
95 // Cache the resolver too
96 V8HiddenValue::setHiddenValue(m_isolate, wrapper, resolverName, resolver );
97 break;
98 case Resolved:
99 case Rejected:
100 resolveOrReject(resolver);
101 break;
102 }
103
104 return ScriptPromise(scriptState, promise);
105 }
106
107 void ScriptPromisePropertyBase::settle(State targetState)
108 {
109 ASSERT(executionContext());
110 ASSERT(m_state == Pending);
111 ASSERT(targetState == Resolved || targetState == Rejected);
112
113 m_state = targetState;
114
115 v8::HandleScope handleScope(m_isolate);
116 v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate);
117 if (wrapper.IsEmpty())
118 return; // wrapper has died or was never populated
haraken 2014/07/02 09:04:32 Is this a valid state? In order to provide determi
dominicc (has gone to gerrit) 2014/07/03 02:13:39 We don't want to prevent garbage collection! So if
119 ScriptState::Scope scope(ScriptState::from(wrapper->CreationContext()));
120
121 v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName);
122 v8::Handle<v8::Promise::Resolver> resolver = V8HiddenValue::getHiddenValue(m _isolate, wrapper, resolverName).As<v8::Promise::Resolver>();
123
124 V8HiddenValue::deleteHiddenValue(m_isolate, wrapper, resolverName);
haraken 2014/07/02 09:04:31 Can we delete the hidden value for the Promise as
dominicc (has gone to gerrit) 2014/07/03 02:13:39 No. We need to keep the reference to the Promise b
125 resolveOrReject(resolver);
126 }
127
128 void ScriptPromisePropertyBase::resolveOrReject(v8::Handle<v8::Promise::Resolver > resolver)
haraken 2014/07/02 09:04:32 Shall we rename settle to resolveOrReject, and res
129 {
130 switch (m_state) {
131 case Pending:
132 ASSERT_NOT_REACHED();
133 break;
134 case Resolved:
135 resolver->Resolve(resolvedValue(resolver->CreationContext()->Global(), m _isolate));
136 break;
137 case Rejected:
138 resolver->Reject(rejectedValue(resolver->CreationContext()->Global(), m_ isolate));
139 break;
140 }
141 }
142
143 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698