Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / Microtask.cpp
index 27cd217..b38af16 100644 (file)
 #include "config.h"
 #include "core/dom/Microtask.h"
 
-#include "core/dom/MutationObserver.h"
-#include "core/dom/custom/CustomElementCallbackDispatcher.h"
+#include "bindings/v8/V8PerIsolateData.h"
+#include "platform/Task.h"
+#include "public/platform/WebThread.h"
 #include "wtf/Vector.h"
+#include <v8.h>
 
 namespace WebCore {
 
 void Microtask::performCheckpoint()
 {
-    static bool performingCheckpoint = false;
-    if (performingCheckpoint)
+    v8::Isolate* isolate = v8::Isolate::GetCurrent();
+    V8PerIsolateData* isolateData = V8PerIsolateData::from(isolate);
+    ASSERT(isolateData);
+    if (isolateData->recursionLevel() || isolateData->performingMicrotaskCheckpoint())
         return;
-    performingCheckpoint = true;
+    isolateData->setPerformingMicrotaskCheckpoint(true);
 
-    bool anyWorkDone;
-    do {
-        MutationObserver::deliverAllMutations();
-        anyWorkDone = CustomElementCallbackDispatcher::instance().dispatch();
-    } while (anyWorkDone);
+    v8::HandleScope handleScope(isolate);
+    v8::Local<v8::Context> context = isolateData->ensureDomInJSContext();
+    v8::Context::Scope scope(context);
+    isolate->RunMicrotasks();
 
-    performingCheckpoint = false;
+    isolateData->setPerformingMicrotaskCheckpoint(false);
+}
+
+static void microtaskFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+    OwnPtr<blink::WebThread::Task> task = adoptPtr(static_cast<blink::WebThread::Task*>(info.Data().As<v8::External>()->Value()));
+    task->run();
+}
+
+void Microtask::enqueueMicrotask(PassOwnPtr<blink::WebThread::Task> callback)
+{
+    v8::Isolate* isolate = v8::Isolate::GetCurrent();
+    V8PerIsolateData* isolateData = V8PerIsolateData::from(isolate);
+    v8::HandleScope handleScope(isolate);
+    v8::Local<v8::Context> context = isolateData->ensureDomInJSContext();
+    v8::Context::Scope scope(context);
+    v8::Local<v8::External> handler = v8::External::New(isolate, callback.leakPtr());
+    isolate->EnqueueMicrotask(v8::Function::New(isolate, &microtaskFunctionCallback, handler));
+}
+
+void Microtask::enqueueMicrotask(const Closure& callback)
+{
+    enqueueMicrotask(adoptPtr(new Task(callback)));
 }
 
 } // namespace WebCore