Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(816)

Unified Diff: Source/web/SuspendableScriptExecutor.cpp

Issue 660863002: [DevTools] Added public method for async execution of scripts (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Added deprecated label for not suspendable execute methods Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/web/SuspendableScriptExecutor.cpp
diff --git a/Source/web/SuspendableScriptExecutor.cpp b/Source/web/SuspendableScriptExecutor.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d59c9aa630be5b66a2688a95e47cd8bde1a4e9a9
--- /dev/null
+++ b/Source/web/SuspendableScriptExecutor.cpp
@@ -0,0 +1,76 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "web/SuspendableScriptExecutor.h"
+
+#include "bindings/core/v8/ScriptController.h"
+#include "core/dom/Document.h"
+#include "core/frame/LocalFrame.h"
+#include "platform/UserGestureIndicator.h"
+#include "public/platform/WebVector.h"
+#include "public/web/WebScriptExecutionCallback.h"
+
+namespace blink {
+
+SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
+ : ActiveDOMObject(frame->document())
+ , m_frame(frame)
+ , m_worldID(worldID)
+ , m_sources(sources)
+ , m_extensionGroup(extensionGroup)
+ , m_userGesture(userGesture)
+ , m_callback(callback)
+{
+}
+
+SuspendableScriptExecutor::~SuspendableScriptExecutor()
+{
+}
+
+void SuspendableScriptExecutor::run()
+{
+ suspendIfNeeded();
+ ExecutionContext* context = executionContext();
+ ASSERT(context);
+ if (context && !context->activeDOMObjectsAreSuspended())
+ executeAndDestroySelf();
+}
+
+void SuspendableScriptExecutor::resume()
+{
+ executeAndDestroySelf();
+}
+
+void SuspendableScriptExecutor::contextDestroyed()
+{
+ // this method can only be called if the script was not called in run()
+ // and context remained suspend (method resume has never called)
+ ActiveDOMObject::contextDestroyed();
+ m_callback->completed(Vector<v8::Local<v8::Value> >());
+ delete this;
+}
+
+void SuspendableScriptExecutor::executeAndDestroySelf()
+{
+ // after calling the destructor of object - object will be unsubscribed from
+ // resumed and contextDestroyed LifecycleObserver methods
+ OwnPtr<UserGestureIndicator> indicator;
+ if (m_userGesture)
+ indicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
+
+ v8::HandleScope scope(v8::Isolate::GetCurrent());
+ Vector<v8::Local<v8::Value> > results;
+ if (m_worldID) {
+ m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_extensionGroup, &results);
+ } else {
+ v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMainWorldAndReturnValue(m_sources.first());
+ results.append(scriptValue);
+ }
+ m_callback->completed(results);
+ delete this;
+}
+
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698