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 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, w
orldID, sources, extensionGroup, userGesture, callback); |
21 executor->ref(); | |
22 executor->run(); | 21 executor->run(); |
23 } | 22 } |
24 | 23 |
25 void SuspendableScriptExecutor::contextDestroyed() | 24 void SuspendableScriptExecutor::contextDestroyed() |
26 { | 25 { |
27 SuspendableTimer::contextDestroyed(); | 26 SuspendableTimer::contextDestroyed(); |
28 m_callback->completed(Vector<v8::Local<v8::Value>>()); | 27 m_callback->completed(Vector<v8::Local<v8::Value>>()); |
29 dispose(); | 28 dispose(); |
30 } | 29 } |
31 | 30 |
32 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl
dID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool
userGesture, WebScriptExecutionCallback* callback) | 31 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl
dID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool
userGesture, WebScriptExecutionCallback* callback) |
33 : SuspendableTimer(frame->document()) | 32 : SuspendableTimer(frame->document()) |
34 , m_frame(frame) | 33 , m_frame(frame) |
| 34 , m_sources(sources) |
| 35 , m_callback(callback) |
| 36 , m_keepAlive(this) |
35 , m_worldID(worldID) | 37 , m_worldID(worldID) |
36 , m_sources(sources) | |
37 , m_extensionGroup(extensionGroup) | 38 , m_extensionGroup(extensionGroup) |
38 , m_userGesture(userGesture) | 39 , m_userGesture(userGesture) |
39 , m_callback(callback) | |
40 #if ENABLE(ASSERT) | |
41 , m_disposed(false) | |
42 #endif | |
43 { | 40 { |
44 } | 41 } |
45 | 42 |
46 SuspendableScriptExecutor::~SuspendableScriptExecutor() | 43 SuspendableScriptExecutor::~SuspendableScriptExecutor() |
47 { | 44 { |
48 #if ENABLE(ASSERT) | |
49 ASSERT(m_disposed); | |
50 #endif | |
51 } | 45 } |
52 | 46 |
53 void SuspendableScriptExecutor::fired() | 47 void SuspendableScriptExecutor::fired() |
54 { | 48 { |
55 executeAndDestroySelf(); | 49 executeAndDestroySelf(); |
56 } | 50 } |
57 | 51 |
58 void SuspendableScriptExecutor::run() | 52 void SuspendableScriptExecutor::run() |
59 { | 53 { |
60 ExecutionContext* context = executionContext(); | 54 ExecutionContext* context = executionContext(); |
61 ASSERT(context); | 55 ASSERT(context); |
62 if (!context->activeDOMObjectsAreSuspended()) { | 56 if (!context->activeDOMObjectsAreSuspended()) { |
63 suspendIfNeeded(); | 57 suspendIfNeeded(); |
64 executeAndDestroySelf(); | 58 executeAndDestroySelf(); |
65 return; | 59 return; |
66 } | 60 } |
67 startOneShot(0, FROM_HERE); | 61 startOneShot(0, FROM_HERE); |
68 suspendIfNeeded(); | 62 suspendIfNeeded(); |
69 } | 63 } |
70 | 64 |
71 void SuspendableScriptExecutor::executeAndDestroySelf() | 65 void SuspendableScriptExecutor::executeAndDestroySelf() |
72 { | 66 { |
73 // Ensure that this object is not deleted even if the context is destroyed. | |
74 RefPtrWillBeRawPtr<SuspendableScriptExecutor> protect(this); | |
75 | |
76 // after calling the destructor of object - object will be unsubscribed from | 67 // after calling the destructor of object - object will be unsubscribed from |
77 // resumed and contextDestroyed LifecycleObserver methods | 68 // resumed and contextDestroyed LifecycleObserver methods |
78 OwnPtr<UserGestureIndicator> indicator; | 69 OwnPtr<UserGestureIndicator> indicator; |
79 if (m_userGesture) | 70 if (m_userGesture) |
80 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse
rGesture)); | 71 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUse
rGesture)); |
81 | 72 |
82 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 73 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
83 Vector<v8::Local<v8::Value>> results; | 74 Vector<v8::Local<v8::Value>> results; |
84 if (m_worldID) { | 75 if (m_worldID) { |
85 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e
xtensionGroup, &results); | 76 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e
xtensionGroup, &results); |
86 } else { | 77 } else { |
87 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain
WorldAndReturnValue(m_sources.first()); | 78 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain
WorldAndReturnValue(m_sources.first()); |
88 results.append(scriptValue); | 79 results.append(scriptValue); |
89 } | 80 } |
90 | 81 |
91 // The script may have removed the frame, in which case contextDestroyed() | 82 // The script may have removed the frame, in which case contextDestroyed() |
92 // will have handled the disposal/callback. | 83 // will have handled the disposal/callback. |
93 if (!m_frame->client()) | 84 if (!m_frame->client()) |
94 return; | 85 return; |
95 | 86 |
96 m_callback->completed(results); | 87 m_callback->completed(results); |
97 dispose(); | 88 dispose(); |
98 } | 89 } |
99 | 90 |
100 void SuspendableScriptExecutor::dispose() | 91 void SuspendableScriptExecutor::dispose() |
101 { | 92 { |
102 #if ENABLE(ASSERT) | |
103 m_disposed = true; | |
104 #endif | |
105 | |
106 #if ENABLE(OILPAN) | |
107 // Remove object as a ContextLifecycleObserver. | 93 // Remove object as a ContextLifecycleObserver. |
108 ActiveDOMObject::clearContext(); | 94 ActiveDOMObject::clearContext(); |
109 #endif | 95 m_keepAlive.clear(); |
110 deref(); | 96 stop(); |
111 } | 97 } |
112 | 98 |
113 DEFINE_TRACE(SuspendableScriptExecutor) | 99 DEFINE_TRACE(SuspendableScriptExecutor) |
114 { | 100 { |
115 #if ENABLE(OILPAN) | |
116 visitor->trace(m_frame); | 101 visitor->trace(m_frame); |
117 visitor->trace(m_sources); | 102 visitor->trace(m_sources); |
118 #endif | |
119 SuspendableTimer::trace(visitor); | 103 SuspendableTimer::trace(visitor); |
120 } | 104 } |
121 | 105 |
122 } // namespace blink | 106 } // namespace blink |
OLD | NEW |