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 "web/SuspendableScriptExecutor.h" | 5 #include "web/SuspendableScriptExecutor.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptController.h" | 7 #include "bindings/core/v8/ScriptController.h" |
| 8 #include "bindings/core/v8/ScriptSourceCode.h" | 8 #include "bindings/core/v8/ScriptSourceCode.h" |
| 9 #include "bindings/core/v8/V8PersistentValueVector.h" | 9 #include "bindings/core/v8/V8PersistentValueVector.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 class WebScriptExecutor : public SuspendableScriptExecutor::Executor { | 24 class WebScriptExecutor : public SuspendableScriptExecutor::Executor { |
| 25 public: | 25 public: |
| 26 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, | 26 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, |
| 27 int worldID, | 27 int worldID, |
| 28 int extensionGroup, | 28 int extensionGroup, |
| 29 bool userGesture); | 29 bool userGesture); |
| 30 | 30 |
| 31 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | 31 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; |
| 32 ScriptState* getScriptState() override { return nullptr; } | |
| 32 | 33 |
| 33 DEFINE_INLINE_VIRTUAL_TRACE() { | 34 DEFINE_INLINE_VIRTUAL_TRACE() { |
| 34 visitor->trace(m_sources); | 35 visitor->trace(m_sources); |
| 35 SuspendableScriptExecutor::Executor::trace(visitor); | 36 SuspendableScriptExecutor::Executor::trace(visitor); |
| 36 } | 37 } |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 HeapVector<ScriptSourceCode> m_sources; | 40 HeapVector<ScriptSourceCode> m_sources; |
| 40 int m_worldID; | 41 int m_worldID; |
| 41 int m_extensionGroup; | 42 int m_extensionGroup; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { | 78 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { |
| 78 public: | 79 public: |
| 79 V8FunctionExecutor(v8::Isolate*, | 80 V8FunctionExecutor(v8::Isolate*, |
| 80 ScriptState*, | 81 ScriptState*, |
| 81 v8::Local<v8::Function>, | 82 v8::Local<v8::Function>, |
| 82 v8::Local<v8::Value> receiver, | 83 v8::Local<v8::Value> receiver, |
| 83 int argc, | 84 int argc, |
| 84 v8::Local<v8::Value> argv[]); | 85 v8::Local<v8::Value> argv[]); |
| 85 | 86 |
| 86 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | 87 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; |
| 88 ScriptState* getScriptState() override { return m_scriptState.get(); } | |
| 87 | 89 |
| 88 private: | 90 private: |
| 89 ScopedPersistent<v8::Function> m_function; | 91 ScopedPersistent<v8::Function> m_function; |
| 90 ScopedPersistent<v8::Value> m_receiver; | 92 ScopedPersistent<v8::Value> m_receiver; |
| 91 V8PersistentValueVector<v8::Value> m_args; | 93 V8PersistentValueVector<v8::Value> m_args; |
| 92 RefPtr<ScriptState> m_scriptState; | 94 RefPtr<ScriptState> m_scriptState; |
| 95 RefPtr<UserGestureToken> m_gestureToken; | |
| 93 }; | 96 }; |
| 94 | 97 |
| 95 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, | 98 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, |
| 96 ScriptState* scriptState, | 99 ScriptState* scriptState, |
| 97 v8::Local<v8::Function> function, | 100 v8::Local<v8::Function> function, |
| 98 v8::Local<v8::Value> receiver, | 101 v8::Local<v8::Value> receiver, |
| 99 int argc, | 102 int argc, |
| 100 v8::Local<v8::Value> argv[]) | 103 v8::Local<v8::Value> argv[]) |
| 101 : m_function(isolate, function), | 104 : m_function(isolate, function), |
| 102 m_receiver(isolate, receiver), | 105 m_receiver(isolate, receiver), |
| 103 m_args(isolate), | 106 m_args(isolate), |
| 104 m_scriptState(scriptState) { | 107 m_scriptState(scriptState), |
| 108 m_gestureToken(UserGestureIndicator::currentToken()) { | |
| 105 m_args.ReserveCapacity(argc); | 109 m_args.ReserveCapacity(argc); |
| 106 for (int i = 0; i < argc; ++i) | 110 for (int i = 0; i < argc; ++i) |
| 107 m_args.Append(argv[i]); | 111 m_args.Append(argv[i]); |
| 108 } | 112 } |
| 109 | 113 |
| 110 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) { | 114 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) { |
| 111 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 115 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 112 Vector<v8::Local<v8::Value>> results; | 116 Vector<v8::Local<v8::Value>> results; |
| 113 if (!m_scriptState->contextIsValid()) | 117 DCHECK(m_scriptState->contextIsValid()); |
| 114 return results; | |
| 115 ScriptState::Scope scope(m_scriptState.get()); | |
| 116 v8::Local<v8::Value> singleResult; | 118 v8::Local<v8::Value> singleResult; |
| 117 Vector<v8::Local<v8::Value>> args; | 119 Vector<v8::Local<v8::Value>> args; |
| 118 args.reserveCapacity(m_args.Size()); | 120 args.reserveCapacity(m_args.Size()); |
| 119 for (size_t i = 0; i < m_args.Size(); ++i) | 121 for (size_t i = 0; i < m_args.Size(); ++i) |
| 120 args.append(m_args.Get(i)); | 122 args.append(m_args.Get(i)); |
| 121 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), | 123 { |
| 122 frame->document(), | 124 UserGestureIndicator gestureIndicator(m_gestureToken.release()); |
| 123 m_receiver.newLocal(isolate), args.size(), | 125 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), |
| 124 args.data(), toIsolate(frame)) | 126 frame->document(), |
| 125 .ToLocal(&singleResult)) | 127 m_receiver.newLocal(isolate), args.size(), |
| 126 results.append(singleResult); | 128 args.data(), toIsolate(frame)) |
| 129 .ToLocal(&singleResult)) | |
| 130 results.append(singleResult); | |
| 131 } | |
| 127 return results; | 132 return results; |
| 128 } | 133 } |
| 129 | 134 |
| 130 } // namespace | 135 } // namespace |
| 131 | 136 |
| 132 void SuspendableScriptExecutor::createAndRun( | 137 void SuspendableScriptExecutor::createAndRun( |
| 133 LocalFrame* frame, | 138 LocalFrame* frame, |
| 134 int worldID, | 139 int worldID, |
| 135 const HeapVector<ScriptSourceCode>& sources, | 140 const HeapVector<ScriptSourceCode>& sources, |
| 136 int extensionGroup, | 141 int extensionGroup, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 if (!context->activeDOMObjectsAreSuspended()) { | 197 if (!context->activeDOMObjectsAreSuspended()) { |
| 193 suspendIfNeeded(); | 198 suspendIfNeeded(); |
| 194 executeAndDestroySelf(); | 199 executeAndDestroySelf(); |
| 195 return; | 200 return; |
| 196 } | 201 } |
| 197 startOneShot(0, BLINK_FROM_HERE); | 202 startOneShot(0, BLINK_FROM_HERE); |
| 198 suspendIfNeeded(); | 203 suspendIfNeeded(); |
| 199 } | 204 } |
| 200 | 205 |
| 201 void SuspendableScriptExecutor::executeAndDestroySelf() { | 206 void SuspendableScriptExecutor::executeAndDestroySelf() { |
| 202 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 207 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
|
dcheng
2016/10/28 17:24:17
Btw, assuming we combine the two separate paths, t
Devlin
2016/10/28 20:31:37
Done.
| |
| 203 Vector<v8::Local<v8::Value>> results = m_executor->execute(m_frame); | |
| 204 | 208 |
| 205 // The script may have removed the frame, in which case contextDestroyed() | 209 Vector<v8::Local<v8::Value>> results; |
| 206 // will have handled the disposal/callback. | 210 ScriptState* scriptState = m_executor->getScriptState(); |
| 207 if (!m_frame->client()) | 211 if (scriptState && scriptState->contextIsValid()) { |
| 208 return; | 212 ScriptState::Scope scriptScope(scriptState); |
|
Devlin
2016/10/27 23:33:28
This change is necessary because when the scriptSc
haraken
2016/10/28 08:45:14
What specifically do you need to keep alive?
Devlin
2016/10/28 15:00:05
The results from the script execution. They're po
haraken
2016/10/28 15:47:51
What ScriptState::Scope are you referring to?
Not
Devlin
2016/10/28 16:00:00
In this patch set, as its written, this works, bec
haraken
2016/10/28 16:18:57
Ah, now I understand what you want to do.
BTW, ca
dcheng
2016/10/28 17:24:17
Hmm. Why does WebScriptExecutor not need the Scrip
Devlin
2016/10/28 20:31:37
WebScriptExecutor uses ScriptController::executeSc
| |
| 213 results = m_executor->execute(m_frame); | |
| 209 | 214 |
| 210 if (m_callback) | 215 // The script may have removed the frame, in which case contextDestroyed() |
| 211 m_callback->completed(results); | 216 // will have handled the disposal/callback. |
| 217 if (!m_frame->client()) | |
| 218 return; | |
| 219 if (m_callback) | |
| 220 m_callback->completed(results); | |
| 221 } else { | |
| 222 results = m_executor->execute(m_frame); | |
| 223 // The script may have removed the frame, in which case contextDestroyed() | |
| 224 // will have handled the disposal/callback. | |
| 225 if (!m_frame->client()) | |
| 226 return; | |
| 227 if (m_callback) | |
| 228 m_callback->completed(results); | |
| 229 } | |
| 212 dispose(); | 230 dispose(); |
| 213 } | 231 } |
| 214 | 232 |
| 215 void SuspendableScriptExecutor::dispose() { | 233 void SuspendableScriptExecutor::dispose() { |
| 216 // Remove object as a ContextLifecycleObserver. | 234 // Remove object as a ContextLifecycleObserver. |
| 217 ActiveDOMObject::clearContext(); | 235 ActiveDOMObject::clearContext(); |
| 218 m_keepAlive.clear(); | 236 m_keepAlive.clear(); |
| 219 stop(); | 237 stop(); |
| 220 } | 238 } |
| 221 | 239 |
| 222 DEFINE_TRACE(SuspendableScriptExecutor) { | 240 DEFINE_TRACE(SuspendableScriptExecutor) { |
| 223 visitor->trace(m_frame); | 241 visitor->trace(m_frame); |
| 224 visitor->trace(m_executor); | 242 visitor->trace(m_executor); |
| 225 SuspendableTimer::trace(visitor); | 243 SuspendableTimer::trace(visitor); |
| 226 } | 244 } |
| 227 | 245 |
| 228 } // namespace blink | 246 } // namespace blink |
| OLD | NEW |