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 "config.h" | 5 #include "config.h" |
6 #include "web/SuspendableScriptExecutor.h" | 6 #include "web/SuspendableScriptExecutor.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptController.h" | 8 #include "bindings/core/v8/ScriptController.h" |
9 #include "bindings/core/v8/ScriptSourceCode.h" | 9 #include "bindings/core/v8/ScriptSourceCode.h" |
10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
11 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
12 #include "platform/UserGestureIndicator.h" | 12 #include "platform/UserGestureIndicator.h" |
13 #include "public/platform/WebVector.h" | 13 #include "public/platform/WebVector.h" |
14 #include "public/web/WebScriptExecutionCallback.h" | 14 #include "public/web/WebScriptExecutionCallback.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con
st WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGes
ture, WebScriptExecutionCallback* callback) | 18 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con
st WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGes
ture, WebScriptExecutionCallback* callback) |
19 { | 19 { |
20 RefPtrWillBeRawPtr<SuspendableScriptExecutor> executor = adoptRefWillBeNoop(
new SuspendableScriptExecutor(frame, worldID, sources, extensionGroup, userGestu
re, callback)); | 20 RefPtrWillBeRawPtr<SuspendableScriptExecutor> executor = adoptRefWillBeNoop(
new SuspendableScriptExecutor(frame, worldID, sources, extensionGroup, userGestu
re, callback)); |
21 executor->ref(); | 21 executor->ref(); |
22 executor->run(); | 22 executor->run(); |
23 } | 23 } |
24 | 24 |
25 void SuspendableScriptExecutor::resume() | |
26 { | |
27 executeAndDestroySelf(); | |
28 } | |
29 | |
30 void SuspendableScriptExecutor::contextDestroyed() | 25 void SuspendableScriptExecutor::contextDestroyed() |
31 { | 26 { |
32 // this method can only be called if the script was not called in run() | 27 // this method can only be called if the script was not called in run() |
33 // and context remained suspend (method resume has never called) | 28 // and context remained suspend (method resume has never called) |
34 ActiveDOMObject::contextDestroyed(); | 29 SuspendableTimer::contextDestroyed(); |
35 m_callback->completed(Vector<v8::Local<v8::Value>>()); | 30 m_callback->completed(Vector<v8::Local<v8::Value>>()); |
36 deref(); | 31 deref(); |
37 } | 32 } |
38 | 33 |
39 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl
dID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool
userGesture, WebScriptExecutionCallback* callback) | 34 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl
dID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool
userGesture, WebScriptExecutionCallback* callback) |
40 : ActiveDOMObject(frame->document()) | 35 : SuspendableTimer(frame->document()) |
41 , m_frame(frame) | 36 , m_frame(frame) |
42 , m_worldID(worldID) | 37 , m_worldID(worldID) |
43 , m_sources(sources) | 38 , m_sources(sources) |
44 , m_extensionGroup(extensionGroup) | 39 , m_extensionGroup(extensionGroup) |
45 , m_userGesture(userGesture) | 40 , m_userGesture(userGesture) |
46 , m_callback(callback) | 41 , m_callback(callback) |
47 { | 42 { |
48 } | 43 } |
49 | 44 |
50 SuspendableScriptExecutor::~SuspendableScriptExecutor() | 45 SuspendableScriptExecutor::~SuspendableScriptExecutor() |
51 { | 46 { |
52 } | 47 } |
53 | 48 |
| 49 void SuspendableScriptExecutor::fired() |
| 50 { |
| 51 executeAndDestroySelf(); |
| 52 } |
| 53 |
54 void SuspendableScriptExecutor::run() | 54 void SuspendableScriptExecutor::run() |
55 { | 55 { |
56 suspendIfNeeded(); | |
57 ExecutionContext* context = executionContext(); | 56 ExecutionContext* context = executionContext(); |
58 ASSERT(context); | 57 ASSERT(context); |
59 if (context && !context->activeDOMObjectsAreSuspended()) | 58 if (!context->activeDOMObjectsAreSuspended()) { |
60 executeAndDestroySelf(); | 59 executeAndDestroySelf(); |
| 60 return; |
| 61 } |
| 62 startOneShot(0, FROM_HERE); |
| 63 suspendIfNeeded(); |
61 } | 64 } |
62 | 65 |
63 void SuspendableScriptExecutor::executeAndDestroySelf() | 66 void SuspendableScriptExecutor::executeAndDestroySelf() |
64 { | 67 { |
65 // after calling the destructor of object - object will be unsubscribed from | 68 // after calling the destructor of object - object will be unsubscribed from |
66 // resumed and contextDestroyed LifecycleObserver methods | 69 // resumed and contextDestroyed LifecycleObserver methods |
67 OwnPtr<UserGestureIndicator> indicator; | 70 OwnPtr<UserGestureIndicator> indicator; |
68 if (m_userGesture) | 71 if (m_userGesture) |
69 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse
rGesture)); | 72 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse
rGesture)); |
70 | 73 |
71 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 74 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
72 Vector<v8::Local<v8::Value>> results; | 75 Vector<v8::Local<v8::Value>> results; |
73 if (m_worldID) { | 76 if (m_worldID) { |
74 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e
xtensionGroup, &results); | 77 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e
xtensionGroup, &results); |
75 } else { | 78 } else { |
76 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain
WorldAndReturnValue(m_sources.first()); | 79 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain
WorldAndReturnValue(m_sources.first()); |
77 results.append(scriptValue); | 80 results.append(scriptValue); |
78 } | 81 } |
79 m_callback->completed(results); | 82 m_callback->completed(results); |
80 deref(); | 83 deref(); |
81 } | 84 } |
82 | 85 |
83 void SuspendableScriptExecutor::trace(Visitor* visitor) | 86 void SuspendableScriptExecutor::trace(Visitor* visitor) |
84 { | 87 { |
85 #if ENABLE(OILPAN) | 88 #if ENABLE(OILPAN) |
86 visitor->trace(m_frame); | 89 visitor->trace(m_frame); |
87 visitor->trace(m_sources); | 90 visitor->trace(m_sources); |
88 #endif | 91 #endif |
89 ActiveDOMObject::trace(visitor); | 92 SuspendableTimer::trace(visitor); |
90 } | 93 } |
91 | 94 |
92 } // namespace blink | 95 } // namespace blink |
OLD | NEW |