| 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 "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 10 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
| 11 #include "platform/UserGestureIndicator.h" | 12 #include "platform/UserGestureIndicator.h" |
| 12 #include "public/platform/WebVector.h" | 13 #include "public/platform/WebVector.h" |
| 13 #include "public/web/WebScriptExecutionCallback.h" | 14 #include "public/web/WebScriptExecutionCallback.h" |
| 14 #include "wtf/PtrUtil.h" | 15 #include "wtf/PtrUtil.h" |
| 16 #include "wtf/Vector.h" |
| 15 #include <memory> | 17 #include <memory> |
| 16 | 18 |
| 17 namespace blink { | 19 namespace blink { |
| 18 | 20 |
| 21 namespace { |
| 22 |
| 23 class WebScriptExecutor : public SuspendableScriptExecutor::Executor { |
| 24 public: |
| 25 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, |
| 26 int worldID, |
| 27 int extensionGroup, |
| 28 bool userGesture); |
| 29 |
| 30 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; |
| 31 |
| 32 DEFINE_INLINE_VIRTUAL_TRACE() { |
| 33 visitor->trace(m_sources); |
| 34 SuspendableScriptExecutor::Executor::trace(visitor); |
| 35 } |
| 36 |
| 37 private: |
| 38 HeapVector<ScriptSourceCode> m_sources; |
| 39 int m_worldID; |
| 40 int m_extensionGroup; |
| 41 bool m_userGesture; |
| 42 }; |
| 43 |
| 44 WebScriptExecutor::WebScriptExecutor( |
| 45 const HeapVector<ScriptSourceCode>& sources, |
| 46 int worldID, |
| 47 int extensionGroup, |
| 48 bool userGesture) |
| 49 : m_sources(sources), |
| 50 m_worldID(worldID), |
| 51 m_extensionGroup(extensionGroup), |
| 52 m_userGesture(userGesture) {} |
| 53 |
| 54 Vector<v8::Local<v8::Value>> WebScriptExecutor::execute(LocalFrame* frame) { |
| 55 std::unique_ptr<UserGestureIndicator> indicator; |
| 56 if (m_userGesture) { |
| 57 indicator = wrapUnique( |
| 58 new UserGestureIndicator(DefinitelyProcessingNewUserGesture)); |
| 59 } |
| 60 |
| 61 Vector<v8::Local<v8::Value>> results; |
| 62 if (m_worldID) { |
| 63 frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, |
| 64 m_extensionGroup, &results); |
| 65 } else { |
| 66 v8::Local<v8::Value> scriptValue = |
| 67 frame->script().executeScriptInMainWorldAndReturnValue( |
| 68 m_sources.first()); |
| 69 results.append(scriptValue); |
| 70 } |
| 71 |
| 72 return results; |
| 73 } |
| 74 |
| 75 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { |
| 76 public: |
| 77 V8FunctionExecutor(v8::Isolate*, |
| 78 v8::Local<v8::Function>, |
| 79 v8::Local<v8::Value> receiver, |
| 80 int argc, |
| 81 v8::Local<v8::Value> argv[]); |
| 82 |
| 83 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; |
| 84 |
| 85 private: |
| 86 ScopedPersistent<v8::Function> m_function; |
| 87 ScopedPersistent<v8::Value> m_receiver; |
| 88 V8PersistentValueVector<v8::Value> m_args; |
| 89 }; |
| 90 |
| 91 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, |
| 92 v8::Local<v8::Function> function, |
| 93 v8::Local<v8::Value> receiver, |
| 94 int argc, |
| 95 v8::Local<v8::Value> argv[]) |
| 96 : m_function(isolate, function), |
| 97 m_receiver(isolate, receiver), |
| 98 m_args(isolate) { |
| 99 m_args.ReserveCapacity(argc); |
| 100 for (int i = 0; i < argc; ++i) |
| 101 m_args.Append(argv[i]); |
| 102 } |
| 103 |
| 104 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) { |
| 105 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 106 Vector<v8::Local<v8::Value>> results; |
| 107 v8::Local<v8::Value> singleResult; |
| 108 Vector<v8::Local<v8::Value>> args; |
| 109 args.reserveCapacity(m_args.Size()); |
| 110 for (size_t i = 0; i < m_args.Size(); ++i) |
| 111 args.append(m_args.Get(i)); |
| 112 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), |
| 113 frame->document(), |
| 114 m_receiver.newLocal(isolate), args.size(), |
| 115 args.data(), toIsolate(frame)) |
| 116 .ToLocal(&singleResult)) |
| 117 results.append(singleResult); |
| 118 return results; |
| 119 } |
| 120 |
| 121 } // namespace |
| 122 |
| 19 void SuspendableScriptExecutor::createAndRun( | 123 void SuspendableScriptExecutor::createAndRun( |
| 20 LocalFrame* frame, | 124 LocalFrame* frame, |
| 21 int worldID, | 125 int worldID, |
| 22 const HeapVector<ScriptSourceCode>& sources, | 126 const HeapVector<ScriptSourceCode>& sources, |
| 23 int extensionGroup, | 127 int extensionGroup, |
| 24 bool userGesture, | 128 bool userGesture, |
| 25 WebScriptExecutionCallback* callback) { | 129 WebScriptExecutionCallback* callback) { |
| 26 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( | 130 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( |
| 27 frame, worldID, sources, extensionGroup, userGesture, callback); | 131 frame, callback, |
| 132 new WebScriptExecutor(sources, worldID, extensionGroup, userGesture)); |
| 133 executor->run(); |
| 134 } |
| 135 |
| 136 void SuspendableScriptExecutor::createAndRun( |
| 137 LocalFrame* frame, |
| 138 v8::Isolate* isolate, |
| 139 v8::Local<v8::Function> function, |
| 140 v8::Local<v8::Value> receiver, |
| 141 int argc, |
| 142 v8::Local<v8::Value> argv[], |
| 143 WebScriptExecutionCallback* callback) { |
| 144 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( |
| 145 frame, callback, |
| 146 new V8FunctionExecutor(isolate, function, receiver, argc, argv)); |
| 28 executor->run(); | 147 executor->run(); |
| 29 } | 148 } |
| 30 | 149 |
| 31 void SuspendableScriptExecutor::contextDestroyed() { | 150 void SuspendableScriptExecutor::contextDestroyed() { |
| 32 SuspendableTimer::contextDestroyed(); | 151 SuspendableTimer::contextDestroyed(); |
| 33 m_callback->completed(Vector<v8::Local<v8::Value>>()); | 152 if (m_callback) |
| 153 m_callback->completed(Vector<v8::Local<v8::Value>>()); |
| 34 dispose(); | 154 dispose(); |
| 35 } | 155 } |
| 36 | 156 |
| 37 SuspendableScriptExecutor::SuspendableScriptExecutor( | 157 SuspendableScriptExecutor::SuspendableScriptExecutor( |
| 38 LocalFrame* frame, | 158 LocalFrame* frame, |
| 39 int worldID, | 159 WebScriptExecutionCallback* callback, |
| 40 const HeapVector<ScriptSourceCode>& sources, | 160 Executor* executor) |
| 41 int extensionGroup, | |
| 42 bool userGesture, | |
| 43 WebScriptExecutionCallback* callback) | |
| 44 : SuspendableTimer(frame->document()), | 161 : SuspendableTimer(frame->document()), |
| 45 m_frame(frame), | 162 m_frame(frame), |
| 46 m_sources(sources), | |
| 47 m_callback(callback), | 163 m_callback(callback), |
| 48 m_keepAlive(this), | 164 m_keepAlive(this), |
| 49 m_worldID(worldID), | 165 m_executor(executor) {} |
| 50 m_extensionGroup(extensionGroup), | |
| 51 m_userGesture(userGesture) {} | |
| 52 | 166 |
| 53 SuspendableScriptExecutor::~SuspendableScriptExecutor() {} | 167 SuspendableScriptExecutor::~SuspendableScriptExecutor() {} |
| 54 | 168 |
| 55 void SuspendableScriptExecutor::fired() { | 169 void SuspendableScriptExecutor::fired() { |
| 56 executeAndDestroySelf(); | 170 executeAndDestroySelf(); |
| 57 } | 171 } |
| 58 | 172 |
| 59 void SuspendableScriptExecutor::run() { | 173 void SuspendableScriptExecutor::run() { |
| 60 ExecutionContext* context = getExecutionContext(); | 174 ExecutionContext* context = getExecutionContext(); |
| 61 DCHECK(context); | 175 DCHECK(context); |
| 62 if (!context->activeDOMObjectsAreSuspended()) { | 176 if (!context->activeDOMObjectsAreSuspended()) { |
| 63 suspendIfNeeded(); | 177 suspendIfNeeded(); |
| 64 executeAndDestroySelf(); | 178 executeAndDestroySelf(); |
| 65 return; | 179 return; |
| 66 } | 180 } |
| 67 startOneShot(0, BLINK_FROM_HERE); | 181 startOneShot(0, BLINK_FROM_HERE); |
| 68 suspendIfNeeded(); | 182 suspendIfNeeded(); |
| 69 } | 183 } |
| 70 | 184 |
| 71 void SuspendableScriptExecutor::executeAndDestroySelf() { | 185 void SuspendableScriptExecutor::executeAndDestroySelf() { |
| 72 // after calling the destructor of object - object will be unsubscribed from | |
| 73 // resumed and contextDestroyed LifecycleObserver methods | |
| 74 std::unique_ptr<UserGestureIndicator> indicator; | |
| 75 if (m_userGesture) | |
| 76 indicator = wrapUnique( | |
| 77 new UserGestureIndicator(DefinitelyProcessingNewUserGesture)); | |
| 78 | |
| 79 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 186 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 80 Vector<v8::Local<v8::Value>> results; | 187 Vector<v8::Local<v8::Value>> results = m_executor->execute(m_frame); |
| 81 if (m_worldID) { | |
| 82 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, | |
| 83 m_extensionGroup, &results); | |
| 84 } else { | |
| 85 v8::Local<v8::Value> scriptValue = | |
| 86 m_frame->script().executeScriptInMainWorldAndReturnValue( | |
| 87 m_sources.first()); | |
| 88 results.append(scriptValue); | |
| 89 } | |
| 90 | 188 |
| 91 // The script may have removed the frame, in which case contextDestroyed() | 189 // The script may have removed the frame, in which case contextDestroyed() |
| 92 // will have handled the disposal/callback. | 190 // will have handled the disposal/callback. |
| 93 if (!m_frame->client()) | 191 if (!m_frame->client()) |
| 94 return; | 192 return; |
| 95 | 193 |
| 96 m_callback->completed(results); | 194 if (m_callback) |
| 195 m_callback->completed(results); |
| 97 dispose(); | 196 dispose(); |
| 98 } | 197 } |
| 99 | 198 |
| 100 void SuspendableScriptExecutor::dispose() { | 199 void SuspendableScriptExecutor::dispose() { |
| 101 // Remove object as a ContextLifecycleObserver. | 200 // Remove object as a ContextLifecycleObserver. |
| 102 ActiveDOMObject::clearContext(); | 201 ActiveDOMObject::clearContext(); |
| 103 m_keepAlive.clear(); | 202 m_keepAlive.clear(); |
| 104 stop(); | 203 stop(); |
| 105 } | 204 } |
| 106 | 205 |
| 107 DEFINE_TRACE(SuspendableScriptExecutor) { | 206 DEFINE_TRACE(SuspendableScriptExecutor) { |
| 108 visitor->trace(m_frame); | 207 visitor->trace(m_frame); |
| 109 visitor->trace(m_sources); | 208 visitor->trace(m_executor); |
| 110 SuspendableTimer::trace(visitor); | 209 SuspendableTimer::trace(visitor); |
| 111 } | 210 } |
| 112 | 211 |
| 113 } // namespace blink | 212 } // namespace blink |
| OLD | NEW |