Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / SuspendableScriptExecutor.cpp
blob5a1b5ebe6dc8e4776791e07ca595afc7886b58e5
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
3 // found in the LICENSE file.
5 #include "config.h"
6 #include "web/SuspendableScriptExecutor.h"
8 #include "bindings/core/v8/ScriptController.h"
9 #include "bindings/core/v8/ScriptSourceCode.h"
10 #include "core/dom/Document.h"
11 #include "core/frame/LocalFrame.h"
12 #include "platform/UserGestureIndicator.h"
13 #include "public/platform/WebVector.h"
14 #include "public/web/WebScriptExecutionCallback.h"
16 namespace blink {
18 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
20 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, worldID, sources, extensionGroup, userGesture, callback);
21 executor->run();
24 void SuspendableScriptExecutor::contextDestroyed()
26 SuspendableTimer::contextDestroyed();
27 m_callback->completed(Vector<v8::Local<v8::Value>>());
28 dispose();
31 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worldID, const WillBeHeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
32 : SuspendableTimer(frame->document())
33 , m_frame(frame)
34 , m_sources(sources)
35 , m_callback(callback)
36 , m_keepAlive(this)
37 , m_worldID(worldID)
38 , m_extensionGroup(extensionGroup)
39 , m_userGesture(userGesture)
43 SuspendableScriptExecutor::~SuspendableScriptExecutor()
47 void SuspendableScriptExecutor::fired()
49 executeAndDestroySelf();
52 void SuspendableScriptExecutor::run()
54 ExecutionContext* context = executionContext();
55 ASSERT(context);
56 if (!context->activeDOMObjectsAreSuspended()) {
57 suspendIfNeeded();
58 executeAndDestroySelf();
59 return;
61 startOneShot(0, FROM_HERE);
62 suspendIfNeeded();
65 void SuspendableScriptExecutor::executeAndDestroySelf()
67 // after calling the destructor of object - object will be unsubscribed from
68 // resumed and contextDestroyed LifecycleObserver methods
69 OwnPtr<UserGestureIndicator> indicator;
70 if (m_userGesture)
71 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
73 v8::HandleScope scope(v8::Isolate::GetCurrent());
74 Vector<v8::Local<v8::Value>> results;
75 if (m_worldID) {
76 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_extensionGroup, &results);
77 } else {
78 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMainWorldAndReturnValue(m_sources.first());
79 results.append(scriptValue);
82 // The script may have removed the frame, in which case contextDestroyed()
83 // will have handled the disposal/callback.
84 if (!m_frame->client())
85 return;
87 m_callback->completed(results);
88 dispose();
91 void SuspendableScriptExecutor::dispose()
93 // Remove object as a ContextLifecycleObserver.
94 ActiveDOMObject::clearContext();
95 m_keepAlive.clear();
96 stop();
99 DEFINE_TRACE(SuspendableScriptExecutor)
101 visitor->trace(m_frame);
102 visitor->trace(m_sources);
103 SuspendableTimer::trace(visitor);
106 } // namespace blink