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 "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" |
| 15 #include <memory> | 16 #include <memory> |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 20 namespace { | |
| 21 | |
| 22 class WebScriptExecutor : public GarbageCollectedFinalized<WebScriptExecutor>, p ublic SuspendableScriptExecutor::Executor { | |
|
haraken
2016/10/03 02:30:09
WebScriptExecutor should not inherit from GarbageC
Devlin
2016/10/03 20:53:30
Done.
| |
| 23 public: | |
| 24 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, int worldID, int extensionGroup, bool userGesture); | |
| 25 | |
| 26 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | |
| 27 | |
| 28 DEFINE_INLINE_VIRTUAL_TRACE() | |
| 29 { | |
| 30 visitor->trace(m_sources); | |
| 31 SuspendableScriptExecutor::Executor::trace(visitor); | |
| 32 } | |
| 33 | |
| 34 private: | |
| 35 HeapVector<ScriptSourceCode> m_sources; | |
| 36 int m_worldID; | |
| 37 int m_extensionGroup; | |
| 38 bool m_userGesture; | |
| 39 }; | |
| 40 | |
| 41 WebScriptExecutor::WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources , int worldID, int extensionGroup, bool userGesture) | |
| 42 : m_sources(sources) | |
| 43 , m_worldID(worldID) | |
| 44 , m_extensionGroup(extensionGroup) | |
| 45 , m_userGesture(userGesture) | |
| 46 { | |
| 47 } | |
| 48 | |
| 49 Vector<v8::Local<v8::Value>> WebScriptExecutor::execute(LocalFrame* frame) | |
| 50 { | |
| 51 std::unique_ptr<UserGestureIndicator> indicator; | |
| 52 if (m_userGesture) | |
| 53 indicator = wrapUnique(new UserGestureIndicator(DefinitelyProcessingNewU serGesture)); | |
| 54 | |
| 55 Vector<v8::Local<v8::Value>> results; | |
| 56 if (m_worldID) { | |
| 57 frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_ext ensionGroup, &results); | |
| 58 } else { | |
| 59 v8::Local<v8::Value> scriptValue = frame->script().executeScriptInMainWo rldAndReturnValue(m_sources.first()); | |
| 60 results.append(scriptValue); | |
| 61 } | |
| 62 | |
| 63 return results; | |
| 64 } | |
| 65 | |
| 66 class V8FunctionExecutor : public GarbageCollectedFinalized<V8FunctionExecutor>, public SuspendableScriptExecutor::Executor { | |
|
haraken
2016/10/03 02:30:09
Ditto.
Devlin
2016/10/03 20:53:30
Done.
| |
| 67 public: | |
| 68 V8FunctionExecutor(v8::Isolate*, v8::Local<v8::Function>, v8::Local<v8::Valu e> receiver, int argc, v8::Local<v8::Value> argv[]); | |
| 69 | |
| 70 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | |
| 71 | |
| 72 private: | |
| 73 ScopedPersistent<v8::Function> m_function; | |
| 74 ScopedPersistent<v8::Value> m_receiver; | |
| 75 V8PersistentValueVector<v8::Value> m_args; | |
| 76 }; | |
| 77 | |
| 78 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, v8::Local<v8::Funct ion> function, v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> arg v[]) | |
| 79 : m_function(isolate, function) | |
| 80 , m_receiver(isolate, receiver) | |
| 81 , m_args(isolate) | |
| 82 { | |
| 83 m_args.ReserveCapacity(argc); | |
| 84 for (int i = 0; i < argc; ++i) | |
| 85 m_args.Append(argv[i]); | |
| 86 } | |
| 87 | |
| 88 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) | |
| 89 { | |
| 90 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 91 Vector<v8::Local<v8::Value>> results; | |
| 92 v8::Local<v8::Value> singleResult; | |
| 93 size_t numArgs = m_args.Size(); | |
| 94 v8::Local<v8::Value> args[numArgs]; | |
| 95 for (size_t i = 0; i < numArgs; ++i) | |
| 96 args[i] = m_args.Get(i); | |
| 97 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), frame->docume nt(), m_receiver.newLocal(isolate), numArgs, static_cast<v8::Local<v8::Value>*>( args), toIsolate(frame)).ToLocal(&singleResult)) | |
| 98 results.append(singleResult); | |
| 99 return results; | |
| 100 } | |
| 101 | |
| 102 } // namespace | |
| 103 | |
| 19 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con st HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback) | 104 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con st HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback) |
| 20 { | 105 { |
| 21 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, w orldID, sources, extensionGroup, userGesture, callback); | 106 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, c allback, new WebScriptExecutor(sources, worldID, extensionGroup, userGesture)); |
| 107 executor->run(); | |
| 108 } | |
| 109 | |
| 110 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, v8::Isolate* iso late, v8::Local<v8::Function> function, v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> argv[], WebScriptExecutionCallback* callback) | |
| 111 { | |
| 112 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, c allback, new V8FunctionExecutor(isolate, function, receiver, argc, argv)); | |
|
Devlin
2016/09/30 21:22:22
Compiler error here (and on line 106): "error: mem
haraken
2016/10/03 02:30:09
BTW, is SuspendableScriptExecutor used only on sta
haraken
2016/10/03 02:30:09
See my comment in the above.
Devlin
2016/10/03 20:53:30
SuspendableScriptExecutor is only used on the heap
haraken
2016/10/04 00:08:30
My question is if you can write the code like:
Devlin
2016/10/04 00:14:26
The SuspendableScriptExecutor needs to be able to
| |
| 22 executor->run(); | 113 executor->run(); |
| 23 } | 114 } |
| 24 | 115 |
| 25 void SuspendableScriptExecutor::contextDestroyed() | 116 void SuspendableScriptExecutor::contextDestroyed() |
| 26 { | 117 { |
| 27 SuspendableTimer::contextDestroyed(); | 118 SuspendableTimer::contextDestroyed(); |
| 28 m_callback->completed(Vector<v8::Local<v8::Value>>()); | 119 if (m_callback) |
| 120 m_callback->completed(Vector<v8::Local<v8::Value>>()); | |
| 29 dispose(); | 121 dispose(); |
| 30 } | 122 } |
| 31 | 123 |
| 32 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl dID, const HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userG esture, WebScriptExecutionCallback* callback) | 124 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, WebScrip tExecutionCallback* callback, Executor* executor) |
| 33 : SuspendableTimer(frame->document()) | 125 : SuspendableTimer(frame->document()) |
| 34 , m_frame(frame) | 126 , m_frame(frame) |
| 35 , m_sources(sources) | |
| 36 , m_callback(callback) | 127 , m_callback(callback) |
| 37 , m_keepAlive(this) | 128 , m_keepAlive(this) |
| 38 , m_worldID(worldID) | 129 , m_executor(executor) |
| 39 , m_extensionGroup(extensionGroup) | |
| 40 , m_userGesture(userGesture) | |
| 41 { | 130 { |
| 42 } | 131 } |
| 43 | 132 |
| 44 SuspendableScriptExecutor::~SuspendableScriptExecutor() | 133 SuspendableScriptExecutor::~SuspendableScriptExecutor() |
| 45 { | 134 { |
| 46 } | 135 } |
| 47 | 136 |
| 48 void SuspendableScriptExecutor::fired() | 137 void SuspendableScriptExecutor::fired() |
| 49 { | 138 { |
| 50 executeAndDestroySelf(); | 139 executeAndDestroySelf(); |
| 51 } | 140 } |
| 52 | 141 |
| 53 void SuspendableScriptExecutor::run() | 142 void SuspendableScriptExecutor::run() |
| 54 { | 143 { |
| 55 ExecutionContext* context = getExecutionContext(); | 144 ExecutionContext* context = getExecutionContext(); |
| 56 DCHECK(context); | 145 DCHECK(context); |
| 57 if (!context->activeDOMObjectsAreSuspended()) { | 146 if (!context->activeDOMObjectsAreSuspended()) { |
| 58 suspendIfNeeded(); | 147 suspendIfNeeded(); |
| 59 executeAndDestroySelf(); | 148 executeAndDestroySelf(); |
| 60 return; | 149 return; |
| 61 } | 150 } |
| 62 startOneShot(0, BLINK_FROM_HERE); | 151 startOneShot(0, BLINK_FROM_HERE); |
| 63 suspendIfNeeded(); | 152 suspendIfNeeded(); |
| 64 } | 153 } |
| 65 | 154 |
| 66 void SuspendableScriptExecutor::executeAndDestroySelf() | 155 void SuspendableScriptExecutor::executeAndDestroySelf() |
| 67 { | 156 { |
| 68 // after calling the destructor of object - object will be unsubscribed from | |
| 69 // resumed and contextDestroyed LifecycleObserver methods | |
| 70 std::unique_ptr<UserGestureIndicator> indicator; | |
| 71 if (m_userGesture) | |
| 72 indicator = wrapUnique(new UserGestureIndicator(DefinitelyProcessingNewU serGesture)); | |
| 73 | |
| 74 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 157 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 75 Vector<v8::Local<v8::Value>> results; | 158 Vector<v8::Local<v8::Value>> results = m_executor->execute(m_frame); |
| 76 if (m_worldID) { | |
| 77 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e xtensionGroup, &results); | |
| 78 } else { | |
| 79 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain WorldAndReturnValue(m_sources.first()); | |
| 80 results.append(scriptValue); | |
| 81 } | |
| 82 | 159 |
| 83 // The script may have removed the frame, in which case contextDestroyed() | 160 // The script may have removed the frame, in which case contextDestroyed() |
| 84 // will have handled the disposal/callback. | 161 // will have handled the disposal/callback. |
| 85 if (!m_frame->client()) | 162 if (!m_frame->client()) |
| 86 return; | 163 return; |
| 87 | 164 |
| 88 m_callback->completed(results); | 165 if (m_callback) |
| 166 m_callback->completed(results); | |
| 89 dispose(); | 167 dispose(); |
| 90 } | 168 } |
| 91 | 169 |
| 92 void SuspendableScriptExecutor::dispose() | 170 void SuspendableScriptExecutor::dispose() |
| 93 { | 171 { |
| 94 // Remove object as a ContextLifecycleObserver. | 172 // Remove object as a ContextLifecycleObserver. |
| 95 ActiveDOMObject::clearContext(); | 173 ActiveDOMObject::clearContext(); |
| 96 m_keepAlive.clear(); | 174 m_keepAlive.clear(); |
| 97 stop(); | 175 stop(); |
| 98 } | 176 } |
| 99 | 177 |
| 100 DEFINE_TRACE(SuspendableScriptExecutor) | 178 DEFINE_TRACE(SuspendableScriptExecutor) |
| 101 { | 179 { |
| 102 visitor->trace(m_frame); | 180 visitor->trace(m_frame); |
| 103 visitor->trace(m_sources); | |
| 104 SuspendableTimer::trace(visitor); | 181 SuspendableTimer::trace(visitor); |
| 105 } | 182 } |
| 106 | 183 |
| 107 } // namespace blink | 184 } // namespace blink |
| OLD | NEW |