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.
6 #include "web/SuspendableScriptExecutor.h"
8 #include "bindings/core/v8/ScriptController.h"
9 #include "core/dom/Document.h"
10 #include "core/frame/LocalFrame.h"
11 #include "platform/UserGestureIndicator.h"
12 #include "public/platform/WebVector.h"
13 #include "public/web/WebScriptExecutionCallback.h"
17 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
19 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, worldID, sources, extensionGroup, userGesture, callback);
23 void SuspendableScriptExecutor::resume()
25 executeAndDestroySelf();
28 void SuspendableScriptExecutor::contextDestroyed()
30 // this method can only be called if the script was not called in run()
31 // and context remained suspend (method resume has never called)
32 ActiveDOMObject::contextDestroyed();
33 m_callback->completed(Vector<v8::Local<v8::Value> >());
37 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
38 : ActiveDOMObject(frame->document())
42 , m_extensionGroup(extensionGroup)
43 , m_userGesture(userGesture)
44 , m_callback(callback)
48 SuspendableScriptExecutor::~SuspendableScriptExecutor()
52 void SuspendableScriptExecutor::run()
55 ExecutionContext* context = executionContext();
57 if (context && !context->activeDOMObjectsAreSuspended())
58 executeAndDestroySelf();
61 void SuspendableScriptExecutor::executeAndDestroySelf()
63 // after calling the destructor of object - object will be unsubscribed from
64 // resumed and contextDestroyed LifecycleObserver methods
65 OwnPtr<UserGestureIndicator> indicator;
67 indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
69 v8::HandleScope scope(v8::Isolate::GetCurrent());
70 Vector<v8::Local<v8::Value> > results;
72 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_extensionGroup, &results);
74 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMainWorldAndReturnValue(m_sources.first());
75 results.append(scriptValue);
77 m_callback->completed(results);