Add WTF::crossThreadCopy(T&&) to utilize String::isolatedCopy() &&
https://bugs.webkit.org/show_bug.cgi?id=198957

Reviewed by Alex Christensen.

Source/WTF:

&&-qualified String::isolatedCopy() has a optimization path which
does just WTFMove if it isSafeToSendToAnotherThread which means
the object hasOneRef.

However, WTF::crossThreadCopy was using only &-qualified
isolatedCopy. To use the optimization, added
WTF::crossThreadCopy(T&&) overloading.

* wtf/CrossThreadCopier.h:
(WTF::crossThreadCopy): Added a overload of (T&&).
* wtf/CrossThreadTask.h:
(WTF::createCrossThreadTask): Removed explicit template arguments of crossThreadCopy.

Tools:

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/CrossThreadCopier.cpp: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@246623 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog
index 17a1288..2b4e1f7 100644
--- a/Source/WTF/ChangeLog
+++ b/Source/WTF/ChangeLog
@@ -1,3 +1,23 @@
+2019-06-19  Fujii Hironori  <Hironori.Fujii@sony.com>
+
+        Add WTF::crossThreadCopy(T&&) to utilize String::isolatedCopy() &&
+        https://bugs.webkit.org/show_bug.cgi?id=198957
+
+        Reviewed by Alex Christensen.
+
+        &&-qualified String::isolatedCopy() has a optimization path which
+        does just WTFMove if it isSafeToSendToAnotherThread which means
+        the object hasOneRef.
+
+        However, WTF::crossThreadCopy was using only &-qualified
+        isolatedCopy. To use the optimization, added
+        WTF::crossThreadCopy(T&&) overloading.
+
+        * wtf/CrossThreadCopier.h:
+        (WTF::crossThreadCopy): Added a overload of (T&&).
+        * wtf/CrossThreadTask.h:
+        (WTF::createCrossThreadTask): Removed explicit template arguments of crossThreadCopy.
+
 2019-06-19  Devin Rousso  <drousso@apple.com>
 
         Web Inspector: Network: replace CFNetwork SPI with new API where able
diff --git a/Source/WTF/wtf/CrossThreadCopier.h b/Source/WTF/wtf/CrossThreadCopier.h
index 633878a..307ccaa 100644
--- a/Source/WTF/wtf/CrossThreadCopier.h
+++ b/Source/WTF/wtf/CrossThreadCopier.h
@@ -31,6 +31,7 @@
 
 #pragma once
 
+#include <type_traits>
 #include <wtf/Assertions.h>
 #include <wtf/Forward.h>
 #include <wtf/HashSet.h>
@@ -77,9 +78,9 @@
 
 // Classes that have an isolatedCopy() method get a default specialization.
 template<class T> struct CrossThreadCopierBase<false, false, T> {
-    static T copy(const T& value)
+    template<typename U> static auto copy(U&& value)
     {
-        return value.isolatedCopy();
+        return std::forward<U>(value).isolatedCopy();
     }
 };
 
@@ -146,18 +147,17 @@
 
 // Default specialization for Optional of CrossThreadCopyable class.
 template<typename T> struct CrossThreadCopierBase<false, false, Optional<T>> {
-    typedef Optional<T> Type;
-    static Type copy(const Type& source)
+    template<typename U> static Optional<T> copy(U&& source)
     {
         if (!source)
             return WTF::nullopt;
-        return CrossThreadCopier<T>::copy(*source);
+        return CrossThreadCopier<T>::copy(std::forward<U>(source).value());
     }
 };
 
-template<typename T> T crossThreadCopy(const T& source)
+template<typename T> auto crossThreadCopy(T&& source)
 {
-    return CrossThreadCopier<T>::copy(source);
+    return CrossThreadCopier<std::remove_cv_t<std::remove_reference_t<T>>>::copy(std::forward<T>(source));
 }
     
 } // namespace WTF
diff --git a/Source/WTF/wtf/CrossThreadTask.h b/Source/WTF/wtf/CrossThreadTask.h
index ec43774..2982e0d 100644
--- a/Source/WTF/wtf/CrossThreadTask.h
+++ b/Source/WTF/wtf/CrossThreadTask.h
@@ -67,7 +67,7 @@
 template<typename... Parameters, typename... Arguments>
 CrossThreadTask createCrossThreadTask(void (*method)(Parameters...), const Arguments&... arguments)
 {
-    return CrossThreadTask([method, arguments = std::make_tuple(crossThreadCopy<Arguments>(arguments)...)]() mutable {
+    return CrossThreadTask([method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
         callFunctionForCrossThreadTask(method, WTFMove(arguments));
     });
 }
@@ -87,7 +87,7 @@
 template<typename T, typename std::enable_if<std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments>
 CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments)
 {
-    return CrossThreadTask([callee = makeRefPtr(&callee), method, arguments = std::make_tuple(crossThreadCopy<Arguments>(arguments)...)]() mutable {
+    return CrossThreadTask([callee = makeRefPtr(&callee), method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
         callMemberFunctionForCrossThreadTask(callee.get(), method, WTFMove(arguments));
     });
 }
@@ -95,7 +95,7 @@
 template<typename T, typename std::enable_if<!std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments>
 CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments)
 {
-    return CrossThreadTask([callee = &callee, method, arguments = std::make_tuple(crossThreadCopy<Arguments>(arguments)...)]() mutable {
+    return CrossThreadTask([callee = &callee, method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
         callMemberFunctionForCrossThreadTask(callee, method, WTFMove(arguments));
     });
 }
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 944f1f6..18ae39d 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,14 @@
+2019-06-19  Fujii Hironori  <Hironori.Fujii@sony.com>
+
+        Add WTF::crossThreadCopy(T&&) to utilize String::isolatedCopy() &&
+        https://bugs.webkit.org/show_bug.cgi?id=198957
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/CMakeLists.txt:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/CrossThreadCopier.cpp: Added.
+
 2019-06-19  Keith Rollin  <krollin@apple.com>
 
         Relocate some test tools in non-mac builds
diff --git a/Tools/TestWebKitAPI/CMakeLists.txt b/Tools/TestWebKitAPI/CMakeLists.txt
index 7adb608..619e78d 100644
--- a/Tools/TestWebKitAPI/CMakeLists.txt
+++ b/Tools/TestWebKitAPI/CMakeLists.txt
@@ -30,6 +30,7 @@
     Tests/WTF/CheckedArithmeticOperations.cpp
     Tests/WTF/ConcurrentPtrHashSet.cpp
     Tests/WTF/Condition.cpp
+    Tests/WTF/CrossThreadCopier.cpp
     Tests/WTF/CrossThreadTask.cpp
     Tests/WTF/DateMath.cpp
     Tests/WTF/Deque.cpp
diff --git a/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj b/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
index 9161863..cbc0135 100644
--- a/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
+++ b/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
@@ -1470,6 +1470,7 @@
 		26F52EB018288F0F0023D412 /* geolocationWatchPosition.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = geolocationWatchPosition.html; sourceTree = "<group>"; };
 		26F52EB118288F0F0023D412 /* geolocationWatchPositionWithHighAccuracy.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = geolocationWatchPositionWithHighAccuracy.html; sourceTree = "<group>"; };
 		26F6E1EF1ADC749B00DE696B /* DFAMinimizer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFAMinimizer.cpp; sourceTree = "<group>"; };
+		278DE64B22B8D611004E0E7A /* CrossThreadCopier.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CrossThreadCopier.cpp; sourceTree = "<group>"; };
 		290A9BB51735DE8A00D71BBC /* CloseNewWindowInNavigationPolicyDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CloseNewWindowInNavigationPolicyDelegate.mm; sourceTree = "<group>"; };
 		290A9BB81735F42300D71BBC /* OpenNewWindow.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OpenNewWindow.html; sourceTree = "<group>"; };
 		290F4274172A1FDE00939FF0 /* custom-protocol-sync-xhr.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "custom-protocol-sync-xhr.html"; sourceTree = "<group>"; };
@@ -3399,6 +3400,7 @@
 				A7A966DA140ECCC8005EF9B4 /* CheckedArithmeticOperations.cpp */,
 				0F30CB5B1FCE1792004B5323 /* ConcurrentPtrHashSet.cpp */,
 				0FEAE3671B7D19CB00CE17F2 /* Condition.cpp */,
+				278DE64B22B8D611004E0E7A /* CrossThreadCopier.cpp */,
 				51714EB91D087416004723C4 /* CrossThreadTask.cpp */,
 				26A2C72E15E2E73C005B1A14 /* CString.cpp */,
 				7AA021BA1AB09EA70052953F /* DateMath.cpp */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/CrossThreadCopier.cpp b/Tools/TestWebKitAPI/Tests/WTF/CrossThreadCopier.cpp
new file mode 100644
index 0000000..78632ec
--- /dev/null
+++ b/Tools/TestWebKitAPI/Tests/WTF/CrossThreadCopier.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2019 Sony Interactive Entertainment Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/CrossThreadCopier.h>
+
+#include "Test.h"
+#include <wtf/Optional.h>
+#include <wtf/text/WTFString.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF_CrossThreadCopier, CopyLVString)
+{
+    String original { "1234" };
+    auto copy = crossThreadCopy(original);
+    EXPECT_TRUE(original.impl()->hasOneRef());
+    EXPECT_TRUE(copy.impl()->hasOneRef());
+    EXPECT_FALSE(original.impl() == copy.impl());
+}
+
+TEST(WTF_CrossThreadCopier, MoveRVString)
+{
+    String original { "1234" };
+    auto copy = crossThreadCopy(WTFMove(original));
+    EXPECT_TRUE(copy.impl()->hasOneRef());
+    EXPECT_NULL(original.impl());
+}
+
+TEST(WTF_CrossThreadCopier, CopyRVStringHavingTwoRef)
+{
+    String original { "1234" };
+    String original2 { original };
+    auto copy = crossThreadCopy(WTFMove(original));
+    EXPECT_EQ(original.impl()->refCount(), 2);
+    EXPECT_FALSE(original.impl() == copy.impl());
+    EXPECT_TRUE(copy.impl()->hasOneRef());
+}
+
+TEST(WTF_CrossThreadCopier, CopyLVOptionalString)
+{
+    Optional<String> original { "1234" };
+    auto copy = crossThreadCopy(original);
+    EXPECT_TRUE(original->impl()->hasOneRef());
+    EXPECT_TRUE(copy->impl()->hasOneRef());
+    EXPECT_FALSE(original->impl() == copy->impl());
+}
+
+TEST(WTF_CrossThreadCopier, MoveRVOptionalString)
+{
+    Optional<String> original { "1234" };
+    auto copy = crossThreadCopy(WTFMove(original));
+    EXPECT_TRUE(copy->impl()->hasOneRef());
+    EXPECT_NULL(original->impl());
+}
+
+TEST(WTF_CrossThreadCopier, CopyRVOptionalStringHavingTwoRef)
+{
+    String string { "1234" };
+    Optional<String> original { string };
+    auto copy = crossThreadCopy(original);
+    EXPECT_EQ(original->impl()->refCount(), 2);
+    EXPECT_FALSE(original->impl() == copy->impl());
+    EXPECT_TRUE(copy->impl()->hasOneRef());
+}
+
+} // namespace TestWebKitAPI