Make ScriptExecutionContext::Task work in terms of wtf::NoncopyableFunction instead of std::function.
https://bugs.webkit.org/show_bug.cgi?id=158187

Reviewed by Chris Dumez.

No new tests (Refactor, no behavior change).

Also make postTask take an rvalue reference.

* bindings/js/JSDOMGlobalObjectTask.cpp:
(WebCore::JSGlobalObjectTask::JSGlobalObjectTask):

* dom/Document.cpp:
(WebCore::Document::postTask):
* dom/Document.h:

* dom/ScriptExecutionContext.h:
(WebCore::ScriptExecutionContext::Task::Task):

* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::postTask):
* workers/WorkerGlobalScope.h:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@201496 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 750f918..73cf781 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,28 @@
+2016-05-29  Brady Eidson  <beidson@apple.com>
+
+        Make ScriptExecutionContext::Task work in terms of wtf::NoncopyableFunction instead of std::function.
+        https://bugs.webkit.org/show_bug.cgi?id=158187
+
+        Reviewed by Chris Dumez.
+
+        No new tests (Refactor, no behavior change).
+
+        Also make postTask take an rvalue reference.
+
+        * bindings/js/JSDOMGlobalObjectTask.cpp:
+        (WebCore::JSGlobalObjectTask::JSGlobalObjectTask):
+        
+        * dom/Document.cpp:
+        (WebCore::Document::postTask):
+        * dom/Document.h:
+        
+        * dom/ScriptExecutionContext.h:
+        (WebCore::ScriptExecutionContext::Task::Task):
+        
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::postTask):
+        * workers/WorkerGlobalScope.h:
+
 2016-05-28  Chris Dumez  <cdumez@apple.com>
 
         Templatize NoncopyableFunction class similarly to std::function
diff --git a/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp b/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp
index 9f36ed3..5b959d2 100644
--- a/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp
+++ b/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp
@@ -80,7 +80,7 @@
 };
 
 JSGlobalObjectTask::JSGlobalObjectTask(JSDOMGlobalObject* globalObject, Ref<Microtask>&& task)
-    : ScriptExecutionContext::Task(nullptr)
+    : ScriptExecutionContext::Task({ })
 {
     RefPtr<JSGlobalObjectCallback> callback = JSGlobalObjectCallback::create(globalObject, WTFMove(task));
     m_task = [callback] (ScriptExecutionContext&) {
diff --git a/Source/WebCore/dom/Document.cpp b/Source/WebCore/dom/Document.cpp
index 6c6b31c..cfb47b5 100644
--- a/Source/WebCore/dom/Document.cpp
+++ b/Source/WebCore/dom/Document.cpp
@@ -5384,7 +5384,7 @@
     return topDocument().securityOrigin();
 }
 
-void Document::postTask(Task task)
+void Document::postTask(Task&& task)
 {
     callOnMainThread([documentReference = m_weakFactory.createWeakPtr(), task = WTFMove(task)]() mutable {
         ASSERT(isMainThread());
diff --git a/Source/WebCore/dom/Document.h b/Source/WebCore/dom/Document.h
index 65819df..12baa20 100644
--- a/Source/WebCore/dom/Document.h
+++ b/Source/WebCore/dom/Document.h
@@ -975,7 +975,7 @@
     bool isDNSPrefetchEnabled() const { return m_isDNSPrefetchEnabled; }
     void parseDNSPrefetchControlHeader(const String&);
 
-    void postTask(Task) final; // Executes the task on context's thread asynchronously.
+    void postTask(Task&&) final; // Executes the task on context's thread asynchronously.
 
 #if ENABLE(REQUEST_ANIMATION_FRAME)
     ScriptedAnimationController* scriptedAnimationController() { return m_scriptedAnimationController.get(); }
diff --git a/Source/WebCore/dom/ScriptExecutionContext.h b/Source/WebCore/dom/ScriptExecutionContext.h
index 4ad00b3..26ea5d9 100644
--- a/Source/WebCore/dom/ScriptExecutionContext.h
+++ b/Source/WebCore/dom/ScriptExecutionContext.h
@@ -35,6 +35,7 @@
 #include "Supplementable.h"
 #include <runtime/ConsoleTypes.h>
 #include <wtf/HashSet.h>
+#include <wtf/NoncopyableFunction.h>
 
 namespace JSC {
 class ExecState;
@@ -132,20 +133,20 @@
     public:
         enum CleanupTaskTag { CleanupTask };
 
-        template<typename T, typename = typename std::enable_if<!std::is_base_of<Task, T>::value && std::is_convertible<T, std::function<void (ScriptExecutionContext&)>>::value>::type>
+        template<typename T, typename = typename std::enable_if<!std::is_base_of<Task, T>::value && std::is_convertible<T, NoncopyableFunction<void (ScriptExecutionContext&)>>::value>::type>
         Task(T task)
             : m_task(WTFMove(task))
             , m_isCleanupTask(false)
         {
         }
 
-        Task(std::function<void()> task)
+        Task(std::function<void ()> task)
             : m_task([task](ScriptExecutionContext&) { task(); })
             , m_isCleanupTask(false)
         {
         }
 
-        template<typename T, typename = typename std::enable_if<std::is_convertible<T, std::function<void (ScriptExecutionContext&)>>::value>::type>
+        template<typename T, typename = typename std::enable_if<std::is_convertible<T, NoncopyableFunction<void (ScriptExecutionContext&)>>::value>::type>
         Task(CleanupTaskTag, T task)
             : m_task(WTFMove(task))
             , m_isCleanupTask(true)
@@ -162,11 +163,11 @@
         bool isCleanupTask() const { return m_isCleanupTask; }
 
     protected:
-        std::function<void (ScriptExecutionContext&)> m_task;
+        NoncopyableFunction<void (ScriptExecutionContext&)> m_task;
         bool m_isCleanupTask;
     };
 
-    virtual void postTask(Task) = 0; // Executes the task on context's thread asynchronously.
+    virtual void postTask(Task&&) = 0; // Executes the task on context's thread asynchronously.
 
     template<typename... Arguments>
     void postCrossThreadTask(Arguments&&... arguments)
diff --git a/Source/WebCore/workers/WorkerGlobalScope.cpp b/Source/WebCore/workers/WorkerGlobalScope.cpp
index 5474fa8..15456e4 100644
--- a/Source/WebCore/workers/WorkerGlobalScope.cpp
+++ b/Source/WebCore/workers/WorkerGlobalScope.cpp
@@ -173,7 +173,7 @@
     return *m_navigator;
 }
 
-void WorkerGlobalScope::postTask(Task task)
+void WorkerGlobalScope::postTask(Task&& task)
 {
     thread().runLoop().postTask(WTFMove(task));
 }
diff --git a/Source/WebCore/workers/WorkerGlobalScope.h b/Source/WebCore/workers/WorkerGlobalScope.h
index 7dfc5d4..2fccdab 100644
--- a/Source/WebCore/workers/WorkerGlobalScope.h
+++ b/Source/WebCore/workers/WorkerGlobalScope.h
@@ -88,7 +88,7 @@
 
     using ScriptExecutionContext::hasPendingActivity;
 
-    void postTask(Task) override; // Executes the task on context's thread asynchronously.
+    void postTask(Task&&) final; // Executes the task on context's thread asynchronously.
 
     // WorkerGlobalScope
     WorkerGlobalScope& self() { return *this; }