Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 // To prevent memory leaks, you should release the reference manually | 61 // To prevent memory leaks, you should release the reference manually |
| 62 // by calling resolve or reject. | 62 // by calling resolve or reject. |
| 63 // Destroying the object will also release the reference. | 63 // Destroying the object will also release the reference. |
| 64 // Note that ScriptPromiseResolver::promise returns an empty value when the | 64 // Note that ScriptPromiseResolver::promise returns an empty value when the |
| 65 // resolver is already resolved or rejected. If you want to resolve a resolver | 65 // resolver is already resolved or rejected. If you want to resolve a resolver |
| 66 // immediately and return the associated promise, you should get the promise | 66 // immediately and return the associated promise, you should get the promise |
| 67 // before resolving. | 67 // before resolving. |
| 68 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 68 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { |
| 69 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 69 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 70 public: | 70 public: |
| 71 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); | 71 static PassRefPtr<ScriptPromiseResolver> create(ScriptState*); |
| 72 static PassRefPtr<ScriptPromiseResolver> create(v8::Isolate*); | |
| 73 | 72 |
| 74 // A ScriptPromiseResolver should be resolved / rejected before | 73 // A ScriptPromiseResolver should be resolved / rejected before |
| 75 // its destruction. | 74 // its destruction. |
| 76 // A ScriptPromiseResolver can be destructed safely without | 75 // A ScriptPromiseResolver can be destructed safely without |
| 77 // entering a v8 context. | 76 // entering a v8 context. |
| 78 ~ScriptPromiseResolver(); | 77 ~ScriptPromiseResolver(); |
| 79 | 78 |
| 80 // Returns the underlying Promise. | 79 // Returns the underlying Promise. |
| 81 // Note that the underlying Promise is cleared when |resolve| or |reject| | 80 // Note that the underlying Promise is cleared when |resolve| or |reject| |
| 82 // is called. | 81 // is called. |
| 83 ScriptPromise promise(); | 82 ScriptPromise promise(); |
| 84 | 83 |
| 85 template<typename T> | 84 template<typename T> |
| 86 void resolve(const T& value, ExecutionContext* executionContext) | 85 void resolve(const T& value, ExecutionContext* executionContext) |
| 87 { | 86 { |
| 88 ASSERT(m_isolate->InContext()); | 87 ASSERT(m_scriptState->isolate()->InContext()); |
| 89 // You should use ScriptPromiseResolverWithContext when you want | 88 // You should use ScriptPromiseResolverWithContext when you want |
| 90 // to resolve a Promise in a non-original V8 context. | 89 // to resolve a Promise in a non-original V8 context. |
| 91 return resolve(value); | 90 return resolve(value); |
| 92 } | 91 } |
| 93 template<typename T> | 92 template<typename T> |
| 94 void reject(const T& value, ExecutionContext* executionContext) | 93 void reject(const T& value, ExecutionContext* executionContext) |
| 95 { | 94 { |
| 96 ASSERT(m_isolate->InContext()); | 95 ASSERT(m_scriptState->isolate()->InContext()); |
| 97 // You should use ScriptPromiseResolverWithContext when you want | 96 // You should use ScriptPromiseResolverWithContext when you want |
| 98 // to reject a Promise in a non-original V8 context. | 97 // to reject a Promise in a non-original V8 context. |
| 99 return reject(value); | 98 return reject(value); |
| 100 } | 99 } |
| 101 template<typename T> | 100 template<typename T> |
| 102 void resolve(const T& value) | 101 void resolve(const T& value) |
| 103 { | 102 { |
| 104 ASSERT(m_isolate->InContext()); | 103 ASSERT(m_scriptState->isolate()->InContext()); |
| 105 resolve(toV8Value(value)); | 104 resolve(toV8Value(value)); |
| 106 } | 105 } |
| 107 template<typename T> | 106 template<typename T> |
| 108 void reject(const T& value) | 107 void reject(const T& value) |
| 109 { | 108 { |
| 110 ASSERT(m_isolate->InContext()); | 109 ASSERT(m_scriptState->isolate()->InContext()); |
| 111 reject(toV8Value(value)); | 110 reject(toV8Value(value)); |
| 112 } | 111 } |
| 113 template<typename T> void resolve(const T& value, v8::Handle<v8::Object> cre ationContext) | 112 template<typename T> void resolve(const T& value, v8::Handle<v8::Object> cre ationContext) |
| 114 { | 113 { |
| 115 ASSERT(m_isolate->InContext()); | 114 ASSERT(m_scriptState->isolate()->InContext()); |
| 116 resolve(toV8Value(value, creationContext)); | 115 resolve(toV8Value(value, creationContext)); |
| 117 } | 116 } |
| 118 template<typename T> void reject(const T& value, v8::Handle<v8::Object> crea tionContext) | 117 template<typename T> void reject(const T& value, v8::Handle<v8::Object> crea tionContext) |
| 119 { | 118 { |
| 120 ASSERT(m_isolate->InContext()); | 119 ASSERT(m_scriptState->isolate()->InContext()); |
| 121 reject(toV8Value(value, creationContext)); | 120 reject(toV8Value(value, creationContext)); |
| 122 } | 121 } |
| 123 | 122 |
| 124 v8::Isolate* isolate() const { return m_isolate; } | 123 v8::Isolate* isolate() const { return m_scriptState->isolate(); } |
| 125 | 124 |
| 126 void resolve(v8::Handle<v8::Value>); | 125 void resolve(v8::Handle<v8::Value>); |
| 127 void reject(v8::Handle<v8::Value>); | 126 void reject(v8::Handle<v8::Value>); |
| 128 | 127 |
| 129 // Used by ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >. | 128 // Used by ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >. |
| 130 static v8::Handle<v8::Object> getCreationContext(v8::Handle<v8::Object> crea tionContext) | 129 static v8::Handle<v8::Object> getCreationContext(v8::Handle<v8::Object> crea tionContext) |
| 131 { | 130 { |
| 132 return creationContext; | 131 return creationContext; |
| 133 } | 132 } |
| 134 // Used by ToV8Value<ScriptPromiseResolver, v8::Isolate*>. | 133 // Used by ToV8Value<ScriptPromiseResolver, v8::Isolate*>. |
| 135 static v8::Handle<v8::Object> getCreationContext(v8::Isolate* isolate) | 134 static v8::Handle<v8::Object> getCreationContext(v8::Isolate* isolate) |
| 136 { | 135 { |
| 137 return ScriptState::current(isolate)->context()->Global(); | 136 return ScriptState::current(isolate)->context()->Global(); |
| 138 } | 137 } |
| 139 | 138 |
| 140 private: | 139 private: |
| 141 template<typename T> | 140 template<typename T> |
| 142 v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creat ionContext) | 141 v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creat ionContext) |
| 143 { | 142 { |
| 144 return ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >::toV8Va lue(value, creationContext, m_isolate); | 143 return ToV8Value<ScriptPromiseResolver, v8::Handle<v8::Object> >::toV8Va lue(value, creationContext, m_scriptState->isolate()); |
| 145 } | 144 } |
| 146 | 145 |
| 147 template<typename T> | 146 template<typename T> |
| 148 v8::Handle<v8::Value> toV8Value(const T& value) | 147 v8::Handle<v8::Value> toV8Value(const T& value) |
| 149 { | 148 { |
| 150 return ToV8Value<ScriptPromiseResolver, v8::Isolate*>::toV8Value(value, m_isolate, m_isolate); | 149 return ToV8Value<ScriptPromiseResolver, v8::Isolate*>::toV8Value(value, m_scriptState->isolate(), m_scriptState->isolate()); |
|
yhirano
2014/05/09 06:24:57
This can be ToV8Value<ScriptPromiseResolver, Scrip
haraken
2014/05/09 07:37:25
Actually I don't fully understand this template. I
yhirano
2014/05/09 07:58:37
The second parameter is used for getting the creat
haraken
2014/05/09 08:42:00
Thanks for the details, done.
| |
| 151 } | 150 } |
| 152 | 151 |
| 153 ScriptPromiseResolver(ExecutionContext*); | 152 explicit ScriptPromiseResolver(ScriptState*); |
| 154 ScriptPromiseResolver(v8::Isolate*); | |
| 155 | 153 |
| 156 v8::Isolate* m_isolate; | 154 RefPtr<ScriptState> m_scriptState; |
| 157 // Used when scriptPromiseOnV8Promise is disabled. | 155 // Used when scriptPromiseOnV8Promise is disabled. |
| 158 ScriptPromise m_promise; | 156 ScriptPromise m_promise; |
| 159 // Used when scriptPromiseOnV8Promise is enabled. | 157 // Used when scriptPromiseOnV8Promise is enabled. |
| 160 ScriptValue m_resolver; | 158 ScriptValue m_resolver; |
| 161 }; | 159 }; |
| 162 | 160 |
| 163 } // namespace WebCore | 161 } // namespace WebCore |
| 164 | 162 |
| 165 | 163 |
| 166 #endif // ScriptPromiseResolver_h | 164 #endif // ScriptPromiseResolver_h |
| OLD | NEW |