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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 return results; | 74 return results; |
| 75 } | 75 } |
| 76 | 76 |
| 77 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { | 77 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { |
| 78 public: | 78 public: |
| 79 V8FunctionExecutor(v8::Isolate*, | 79 V8FunctionExecutor(v8::Isolate*, |
| 80 ScriptState*, | 80 ScriptState*, |
| 81 v8::Local<v8::Function>, | 81 v8::Local<v8::Function>, |
| 82 v8::Local<v8::Value> receiver, | 82 v8::Local<v8::Value> receiver, |
| 83 int argc, | 83 int argc, |
| 84 v8::Local<v8::Value> argv[]); | 84 v8::Local<v8::Value> argv[], |
| 85 bool userGesture); | |
| 85 | 86 |
| 86 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | 87 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; |
| 87 | 88 |
| 88 private: | 89 private: |
| 89 ScopedPersistent<v8::Function> m_function; | 90 ScopedPersistent<v8::Function> m_function; |
| 90 ScopedPersistent<v8::Value> m_receiver; | 91 ScopedPersistent<v8::Value> m_receiver; |
| 91 V8PersistentValueVector<v8::Value> m_args; | 92 V8PersistentValueVector<v8::Value> m_args; |
| 92 RefPtr<ScriptState> m_scriptState; | 93 RefPtr<ScriptState> m_scriptState; |
| 94 bool m_userGesture; | |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, | 97 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, |
| 96 ScriptState* scriptState, | 98 ScriptState* scriptState, |
| 97 v8::Local<v8::Function> function, | 99 v8::Local<v8::Function> function, |
| 98 v8::Local<v8::Value> receiver, | 100 v8::Local<v8::Value> receiver, |
| 99 int argc, | 101 int argc, |
| 100 v8::Local<v8::Value> argv[]) | 102 v8::Local<v8::Value> argv[], |
| 103 bool userGesture) | |
| 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_userGesture(userGesture) { | |
| 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 if (!m_scriptState->contextIsValid()) |
| 114 return results; | 118 return results; |
| 115 ScriptState::Scope scope(m_scriptState.get()); | 119 ScriptState::Scope scope(m_scriptState.get()); |
| 116 v8::Local<v8::Value> singleResult; | 120 v8::Local<v8::Value> singleResult; |
| 117 Vector<v8::Local<v8::Value>> args; | 121 Vector<v8::Local<v8::Value>> args; |
| 118 args.reserveCapacity(m_args.Size()); | 122 args.reserveCapacity(m_args.Size()); |
| 119 for (size_t i = 0; i < m_args.Size(); ++i) | 123 for (size_t i = 0; i < m_args.Size(); ++i) |
| 120 args.append(m_args.Get(i)); | 124 args.append(m_args.Get(i)); |
| 121 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), | 125 |
| 122 frame->document(), | 126 { |
| 123 m_receiver.newLocal(isolate), args.size(), | 127 std::unique_ptr<UserGestureIndicator> indicator; |
| 124 args.data(), toIsolate(frame)) | 128 if (m_userGesture && !UserGestureIndicator::processingUserGesture()) { |
|
Devlin
2016/10/26 22:45:22
I'm very unsure about this.
I originally started
dcheng
2016/10/26 23:16:49
I believe we should be saving the original UserGes
Devlin
2016/10/27 23:33:28
Tried it out; ptal.
| |
| 125 .ToLocal(&singleResult)) | 129 indicator = |
| 126 results.append(singleResult); | 130 wrapUnique(new UserGestureIndicator(DocumentUserGestureToken::create( |
| 131 frame->document(), UserGestureToken::NewGesture))); | |
| 132 } | |
| 133 | |
| 134 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), | |
| 135 frame->document(), | |
| 136 m_receiver.newLocal(isolate), args.size(), | |
| 137 args.data(), toIsolate(frame)) | |
| 138 .ToLocal(&singleResult)) | |
| 139 results.append(singleResult); | |
| 140 } | |
| 127 return results; | 141 return results; |
| 128 } | 142 } |
| 129 | 143 |
| 130 } // namespace | 144 } // namespace |
| 131 | 145 |
| 132 void SuspendableScriptExecutor::createAndRun( | 146 void SuspendableScriptExecutor::createAndRun( |
| 133 LocalFrame* frame, | 147 LocalFrame* frame, |
| 134 int worldID, | 148 int worldID, |
| 135 const HeapVector<ScriptSourceCode>& sources, | 149 const HeapVector<ScriptSourceCode>& sources, |
| 136 int extensionGroup, | 150 int extensionGroup, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 151 int argc, | 165 int argc, |
| 152 v8::Local<v8::Value> argv[], | 166 v8::Local<v8::Value> argv[], |
| 153 WebScriptExecutionCallback* callback) { | 167 WebScriptExecutionCallback* callback) { |
| 154 ScriptState* scriptState = ScriptState::from(context); | 168 ScriptState* scriptState = ScriptState::from(context); |
| 155 if (!scriptState->contextIsValid()) { | 169 if (!scriptState->contextIsValid()) { |
| 156 if (callback) | 170 if (callback) |
| 157 callback->completed(Vector<v8::Local<v8::Value>>()); | 171 callback->completed(Vector<v8::Local<v8::Value>>()); |
| 158 return; | 172 return; |
| 159 } | 173 } |
| 160 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( | 174 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( |
| 161 frame, callback, new V8FunctionExecutor(isolate, scriptState, function, | 175 frame, callback, new V8FunctionExecutor( |
| 162 receiver, argc, argv)); | 176 isolate, scriptState, function, receiver, argc, argv, |
| 177 UserGestureIndicator::processingUserGesture())); | |
| 163 executor->run(); | 178 executor->run(); |
| 164 } | 179 } |
| 165 | 180 |
| 166 void SuspendableScriptExecutor::contextDestroyed() { | 181 void SuspendableScriptExecutor::contextDestroyed() { |
| 167 SuspendableTimer::contextDestroyed(); | 182 SuspendableTimer::contextDestroyed(); |
| 168 if (m_callback) | 183 if (m_callback) |
| 169 m_callback->completed(Vector<v8::Local<v8::Value>>()); | 184 m_callback->completed(Vector<v8::Local<v8::Value>>()); |
| 170 dispose(); | 185 dispose(); |
| 171 } | 186 } |
| 172 | 187 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 stop(); | 234 stop(); |
| 220 } | 235 } |
| 221 | 236 |
| 222 DEFINE_TRACE(SuspendableScriptExecutor) { | 237 DEFINE_TRACE(SuspendableScriptExecutor) { |
| 223 visitor->trace(m_frame); | 238 visitor->trace(m_frame); |
| 224 visitor->trace(m_executor); | 239 visitor->trace(m_executor); |
| 225 SuspendableTimer::trace(visitor); | 240 SuspendableTimer::trace(visitor); |
| 226 } | 241 } |
| 227 | 242 |
| 228 } // namespace blink | 243 } // namespace blink |
| OLD | NEW |