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 "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 11 #include "platform/UserGestureIndicator.h" | 11 #include "platform/UserGestureIndicator.h" |
| 12 #include "public/platform/WebVector.h" | 12 #include "public/platform/WebVector.h" |
| 13 #include "public/web/WebScriptExecutionCallback.h" | 13 #include "public/web/WebScriptExecutionCallback.h" |
| 14 #include "wtf/PtrUtil.h" | 14 #include "wtf/PtrUtil.h" |
| 15 #include <memory> | 15 #include <memory> |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 namespace { | |
| 20 | |
| 21 class WebScriptExecutor : public SuspendableScriptExecutor::Executor { | |
| 22 public: | |
| 23 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, int worldID, int extensionGroup, bool userGesture); | |
| 24 | |
| 25 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | |
| 26 | |
| 27 void trace(Visitor*) override; | |
|
haraken
2016/09/28 00:41:22
Use DEFINE_INLINE_VIRTUAL_TRACE.
Devlin
2016/09/30 21:22:22
Done.
| |
| 28 | |
| 29 private: | |
| 30 HeapVector<ScriptSourceCode> m_sources; | |
|
dcheng
2016/09/27 08:30:57
I'm a little confused by the ownership model of Sc
haraken
2016/09/27 08:39:45
Yes, because ScriptSourceCode is used only as a pa
dcheng
2016/09/27 09:02:59
I see. How come Oilpan doesn't warn that the holde
haraken
2016/09/27 13:25:41
ScriptSourceCode is in HeapVector (i.e., a GCed ob
dcheng
2016/09/27 22:24:25
So just to confirm, we have the following ownershi
| |
| 31 int m_worldID; | |
| 32 int m_extensionGroup; | |
| 33 bool m_userGesture; | |
| 34 }; | |
| 35 | |
| 36 WebScriptExecutor::WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources , int worldID, int extensionGroup, bool userGesture) | |
| 37 : m_sources(sources) | |
| 38 , m_worldID(worldID) | |
| 39 , m_extensionGroup(extensionGroup) | |
| 40 , m_userGesture(userGesture) | |
| 41 { | |
| 42 } | |
| 43 | |
| 44 Vector<v8::Local<v8::Value>> WebScriptExecutor::execute(LocalFrame* frame) | |
| 45 { | |
| 46 // after calling the destructor of object - object will be unsubscribed from | |
| 47 // resumed and contextDestroyed LifecycleObserver methods | |
|
dcheng
2016/09/27 08:30:57
Hmmm. I'm not sure /where/ this comment belongs, b
Devlin
2016/09/27 22:14:20
I'm happy to remove it. Done.
| |
| 48 std::unique_ptr<UserGestureIndicator> indicator; | |
| 49 if (m_userGesture) | |
| 50 indicator = wrapUnique(new UserGestureIndicator(DefinitelyProcessingNewU serGesture)); | |
| 51 | |
| 52 Vector<v8::Local<v8::Value>> results; | |
| 53 if (m_worldID) { | |
| 54 frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_ext ensionGroup, &results); | |
| 55 } else { | |
| 56 v8::Local<v8::Value> scriptValue = frame->script().executeScriptInMainWo rldAndReturnValue(m_sources.first()); | |
| 57 results.append(scriptValue); | |
| 58 } | |
| 59 | |
| 60 return results; | |
| 61 } | |
| 62 | |
| 63 void WebScriptExecutor::trace(Visitor* visitor) | |
| 64 { | |
| 65 visitor->trace(m_sources); | |
| 66 SuspendableScriptExecutor::Executor::trace(visitor); | |
| 67 } | |
| 68 | |
| 69 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor { | |
| 70 public: | |
| 71 V8FunctionExecutor(v8::Isolate*, v8::Local<v8::Function>, v8::Local<v8::Valu e> receiver, int argc, v8::Local<v8::Value> argv[]); | |
| 72 | |
| 73 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override; | |
| 74 | |
| 75 private: | |
| 76 ScopedPersistent<v8::Function> m_function; | |
| 77 ScopedPersistent<v8::Value> m_receiver; | |
| 78 Vector<std::unique_ptr<ScopedPersistent<v8::Value>>> m_args; | |
|
dcheng
2016/09/27 08:30:57
Maybe V8PersistentValueVector from https://cs.chro
Devlin
2016/09/27 22:14:20
Handy! Done.
| |
| 79 }; | |
| 80 | |
| 81 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, v8::Local<v8::Funct ion> function, v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> arg v[]) | |
| 82 : m_function(isolate, function) | |
| 83 , m_receiver(isolate, receiver) | |
| 84 { | |
| 85 for (int i = 0; i < argc; ++i) | |
| 86 m_args.append(std::unique_ptr<ScopedPersistent<v8::Value>>(new ScopedPer sistent<v8::Value>(isolate, argv[i]))); | |
|
dcheng
2016/09/27 08:30:57
wrapUnique from wtf/PtrUtil.h will make this a lit
Devlin
2016/09/27 22:14:20
Unnecessary with V8PersistentValueVector.
| |
| 87 } | |
| 88 | |
| 89 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) | |
| 90 { | |
| 91 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 92 Vector<v8::Local<v8::Value>> results; | |
| 93 v8::Local<v8::Value> singleResult; | |
| 94 v8::Local<v8::Value> args[m_args.size()]; | |
| 95 for (size_t i = 0; i < m_args.size(); ++i) | |
| 96 args[i] = m_args[i]->newLocal(isolate); | |
| 97 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), frame->docume nt(), m_receiver.newLocal(isolate), m_args.size(), static_cast<v8::Local<v8::Val ue>*>(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, std::unique_ptr<WebScriptExecutor>(new WebScriptExecutor(sources, world ID, 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, std::unique_ptr<V8FunctionExecutor>(new V8FunctionExecutor(isolate, fun ction, receiver, argc, argv))); | |
| 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, std::unique_ptr<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(std::move(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 |