Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "config.h" | 5 #include "config.h" |
| 6 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 6 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| 7 | 7 |
| 8 #include "bindings/v8/V8RecursionScope.h" | 8 #include "bindings/v8/V8RecursionScope.h" |
| 9 | 9 |
| 10 namespace WebCore { | 10 namespace WebCore { |
| 11 | 11 |
| 12 namespace { | |
| 13 const char hiddenPropertyName[] = "blink::keepObjectWhilePending"; | |
| 14 } // namespace | |
| 15 | |
| 12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) | 16 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) |
| 13 : ActiveDOMObject(scriptState->executionContext()) | 17 : ActiveDOMObject(scriptState->executionContext()) |
| 14 , m_state(Pending) | 18 , m_state(Pending) |
| 15 , m_scriptState(scriptState) | 19 , m_scriptState(scriptState) |
| 16 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) | 20 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
| 17 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) | 21 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) |
| 18 { | 22 { |
| 19 } | 23 } |
| 20 | 24 |
| 25 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext() | |
| 26 { | |
| 27 if (m_state != ResolvedOrRejected) { | |
| 28 unregisterKeptObject(); | |
| 29 ScriptState::Scope scope(m_scriptState.get()); | |
| 30 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(), | |
| 31 "ScriptPromiseResolverWithContext is destructed without resolve / re ject"))); | |
| 32 } | |
| 33 } | |
| 34 | |
| 21 void ScriptPromiseResolverWithContext::suspend() | 35 void ScriptPromiseResolverWithContext::suspend() |
| 22 { | 36 { |
| 23 m_timer.stop(); | 37 m_timer.stop(); |
| 24 } | 38 } |
| 25 | 39 |
| 26 void ScriptPromiseResolverWithContext::resume() | 40 void ScriptPromiseResolverWithContext::resume() |
| 27 { | 41 { |
| 28 if (m_state == Resolving || m_state == Rejecting) | 42 if (m_state == Resolving || m_state == Rejecting) |
| 29 m_timer.startOneShot(0, FROM_HERE); | 43 m_timer.startOneShot(0, FROM_HERE); |
| 30 } | 44 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 56 } else { | 70 } else { |
| 57 ASSERT(m_state == Rejecting); | 71 ASSERT(m_state == Rejecting); |
| 58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 72 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
| 59 } | 73 } |
| 60 } | 74 } |
| 61 clear(); | 75 clear(); |
| 62 } | 76 } |
| 63 | 77 |
| 64 void ScriptPromiseResolverWithContext::clear() | 78 void ScriptPromiseResolverWithContext::clear() |
| 65 { | 79 { |
| 80 unregisterKeptObject(); | |
| 66 ResolutionState state = m_state; | 81 ResolutionState state = m_state; |
| 67 m_state = ResolvedOrRejected; | 82 m_state = ResolvedOrRejected; |
| 68 m_resolver.clear(); | 83 m_resolver.clear(); |
| 69 m_value.clear(); | 84 m_value.clear(); |
| 70 if (state == Resolving || state == Rejecting) { | 85 if (state == Resolving || state == Rejecting) { |
| 71 // |ref| was called in |resolveOrReject|. | 86 // |ref| was called in |resolveOrReject|. |
| 72 deref(); | 87 deref(); |
| 73 } | 88 } |
| 74 // |this| may be deleted here. | 89 // |this| may be deleted here. |
| 75 } | 90 } |
| 76 | 91 |
| 92 void ScriptPromiseResolverWithContext::keepObjectWhilePendingInternal(v8::Handle <v8::Value> value) | |
| 93 { | |
| 94 v8::HandleScope handleScope(m_scriptState->isolate()); | |
| 95 if (!m_resolver) | |
| 96 return; | |
| 97 ScriptPromise promise = this->promise(); | |
| 98 if (promise.isEmpty()) | |
| 99 return; | |
| 100 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
| 101 v8::Local<v8::String> name = v8::String::NewFromUtf8(m_scriptState->isolate( ), hiddenPropertyName); | |
| 102 ASSERT(v8Promise->GetHiddenValue(name).IsEmpty()); | |
| 103 v8Promise->SetHiddenValue(name, value); | |
|
haraken
2014/06/13 05:03:51
Consider using helper methods in V8HiddenValue.h i
yhirano
2014/06/13 11:15:22
Done.
| |
| 104 } | |
| 105 | |
| 106 void ScriptPromiseResolverWithContext::unregisterKeptObject() | |
| 107 { | |
| 108 v8::HandleScope handleScope(m_scriptState->isolate()); | |
| 109 if (!m_resolver) | |
| 110 return; | |
| 111 ScriptPromise promise = this->promise(); | |
| 112 if (promise.isEmpty()) | |
| 113 return; | |
| 114 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
| 115 v8::Local<v8::String> name = v8::String::NewFromUtf8(m_scriptState->isolate( ), hiddenPropertyName); | |
| 116 v8Promise->DeleteHiddenValue(name); | |
|
yhirano
2014/06/12 11:02:51
Rafael, I heard that deleting a property is genera
rafaelw
2014/06/13 02:36:17
Haraken is probably more authoritative on this tha
haraken
2014/06/13 03:36:53
Agreed, I don't think deleting a hidden property a
yhirano
2014/06/13 04:19:30
Then is it better to have another boolean member v
yhirano
2014/06/13 04:19:30
Thanks!
haraken
2014/06/13 05:03:51
Only if this code is really performance-sensitive
| |
| 117 } | |
| 118 | |
| 77 } // namespace WebCore | 119 } // namespace WebCore |
| OLD | NEW |