Add URLSchemeHandler API tests that verify the lack of URLSchemeTask object leaks.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 30 Jul 2017 17:36:26 +0000 (17:36 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 30 Jul 2017 17:36:26 +0000 (17:36 +0000)
https://bugs.webkit.org/show_bug.cgi?id=174958

Reviewed by Darin Adler.

Source/WebKit:

* UIProcess/API/APIURLSchemeTask.h:

* UIProcess/API/C/WKTestingSupport.cpp:
(WKGetAPIURLSchemeTaskInstanceCount):
(WKGetWebURLSchemeTaskInstanceCount):
* UIProcess/API/C/WKTestingSupport.h:

* UIProcess/WebURLSchemeTask.h:

* WebKit.xcodeproj/project.pbxproj:

Source/WTF:

This patch adds a new template class "InstanceCounted<T>".

For each specialization, "InstanceCounted" will keep track of the total number of
instances in existence.

This makes explicate leak checking in API tests possible.

Since this adds some runtime and storage overhead the code that actually does anything
is only compiled in debug builds.

* WTF.xcodeproj/project.pbxproj:
* wtf/InstanceCounted.h: Added.
(WTF::InstanceCounted::InstanceCounted):
(WTF::InstanceCounted::instanceCount):
(WTF::InstanceCounted::~InstanceCounted):

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit2Cocoa/WKURLSchemeHandler-leaks.mm: Added.
(-[LeakSchemeHandler webView:startURLSchemeTask:]):
(-[LeakSchemeHandler webView:stopURLSchemeTask:]):
(runUntilTasksInFlight):
(TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@220049 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/InstanceCounted.h [new file with mode: 0644]
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/APIURLSchemeTask.h
Source/WebKit/UIProcess/API/C/WKTestingSupport.cpp [new file with mode: 0644]
Source/WebKit/UIProcess/API/C/WKTestingSupport.h [new file with mode: 0644]
Source/WebKit/UIProcess/WebURLSchemeTask.h
Source/WebKit/WebKit.xcodeproj/project.pbxproj
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKURLSchemeHandler-leaks.mm [new file with mode: 0644]

index 465d2de..8f7e352 100644 (file)
@@ -1,3 +1,26 @@
+2017-07-30  Brady Eidson  <beidson@apple.com>
+
+        Add URLSchemeHandler API tests that verify the lack of URLSchemeTask object leaks.
+        https://bugs.webkit.org/show_bug.cgi?id=174958
+
+        Reviewed by Darin Adler.
+
+        This patch adds a new template class "InstanceCounted<T>".
+        
+        For each specialization, "InstanceCounted" will keep track of the total number of
+        instances in existence.
+        
+        This makes explicate leak checking in API tests possible.
+        
+        Since this adds some runtime and storage overhead the code that actually does anything
+        is only compiled in debug builds.
+        
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/InstanceCounted.h: Added.
+        (WTF::InstanceCounted::InstanceCounted):
+        (WTF::InstanceCounted::instanceCount):
+        (WTF::InstanceCounted::~InstanceCounted):
+
 2017-07-27  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         [WTF] Drop Thread initialization wait in some platforms by introducing StackBounds::newThreadStackBounds(PlatformThreadHandle&)
index d861671..fb83fa5 100644 (file)
                515F794C1CFC9F4A00CCED93 /* CrossThreadCopier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadCopier.h; sourceTree = "<group>"; };
                515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTask.h; sourceTree = "<group>"; };
                515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadQueue.h; sourceTree = "<group>"; };
+               5182C22C1F2BC7E60059BA7C /* InstanceCounted.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InstanceCounted.h; sourceTree = "<group>"; };
                5311BD511EA71CAD00525281 /* Signals.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Signals.cpp; sourceTree = "<group>"; };
                5311BD551EA7E15A00525281 /* LocklessBag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocklessBag.h; sourceTree = "<group>"; };
                5311BD571EA7E1A100525281 /* Signals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Signals.h; sourceTree = "<group>"; };
                                2684D4351C000D400081D663 /* IndexSparseSet.h */,
                                A8A472BC151A825A004123FF /* InlineASM.h */,
                                A70DA0821799F04D00529A9B /* Insertion.h */,
+                               5182C22C1F2BC7E60059BA7C /* InstanceCounted.h */,
                                7CDD7FF7186D291E007433CD /* IteratorAdaptors.h */,
                                7CDD7FF9186D2A54007433CD /* IteratorRange.h */,
                                539EB0621D55284200C82EF7 /* LEBDecoder.h */,
diff --git a/Source/WTF/wtf/InstanceCounted.h b/Source/WTF/wtf/InstanceCounted.h
new file mode 100644 (file)
index 0000000..c84b4ab
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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. ``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
+ * 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.
+ */
+
+#pragma once
+
+// This class implements "instance count" management for regression test coverage.
+// Since it adds runtime overhead to manage the count variable, the actual
+// functionality of the class is limited to debug builds.
+
+namespace WTF {
+
+template<typename T> class InstanceCounted {
+public:
+    static size_t instanceCount()
+    {
+#ifndef NDEBUG
+        return s_instanceCount;
+#else
+        return 0;
+#endif
+    }
+
+#ifndef NDEBUG
+protected:
+    InstanceCounted()
+    {
+        ++s_instanceCount;
+    }
+
+    InstanceCounted(const InstanceCounted&)
+    {
+        ++s_instanceCount;
+    }
+
+    InstanceCounted(InstanceCounted&&)
+    {
+        ++s_instanceCount;
+    }
+
+    ~InstanceCounted()
+    {
+        ASSERT(s_instanceCount);
+        --s_instanceCount;
+    }
+
+private:
+    static std::atomic_size_t s_instanceCount;
+#endif // NDEBUG
+};
+
+#ifndef NDEBUG
+template<typename T> std::atomic_size_t InstanceCounted<T>::s_instanceCount;
+#endif
+
+} // namespace WTF
+
+using WTF::InstanceCounted;
+
index 7128ebc..e203283 100644 (file)
@@ -1,3 +1,21 @@
+2017-07-30  Brady Eidson  <beidson@apple.com>
+
+        Add URLSchemeHandler API tests that verify the lack of URLSchemeTask object leaks.
+        https://bugs.webkit.org/show_bug.cgi?id=174958
+
+        Reviewed by Darin Adler.
+
+        * UIProcess/API/APIURLSchemeTask.h:
+
+        * UIProcess/API/C/WKTestingSupport.cpp:
+        (WKGetAPIURLSchemeTaskInstanceCount):
+        (WKGetWebURLSchemeTaskInstanceCount):
+        * UIProcess/API/C/WKTestingSupport.h:
+
+        * UIProcess/WebURLSchemeTask.h:
+
+        * WebKit.xcodeproj/project.pbxproj:
+
 2017-07-29  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         Unreviewed, build fix for GTK and WPE about StorageProcess renaming
index 78560d9..00a5402 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "APIObject.h"
+#include <wtf/InstanceCounted.h>
 
 namespace WebKit {
 class WebURLSchemeTask;
@@ -33,7 +34,7 @@ class WebURLSchemeTask;
 
 namespace API {
 
-class URLSchemeTask final : public ObjectImpl<Object::Type::URLSchemeTask> {
+class URLSchemeTask final : public ObjectImpl<Object::Type::URLSchemeTask>, public InstanceCounted<URLSchemeTask> {
 public:
     static Ref<URLSchemeTask> create(WebKit::WebURLSchemeTask&);
 
diff --git a/Source/WebKit/UIProcess/API/C/WKTestingSupport.cpp b/Source/WebKit/UIProcess/API/C/WKTestingSupport.cpp
new file mode 100644 (file)
index 0000000..690e50a
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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 "WKTestingSupport.h"
+
+#include "APIURLSchemeTask.h"
+#include "WebURLSchemeTask.h"
+
+size_t WKGetAPIURLSchemeTaskInstanceCount()
+{
+    return API::URLSchemeTask::instanceCount();
+}
+
+size_t WKGetWebURLSchemeTaskInstanceCount()
+{
+    return WebKit::WebURLSchemeTask::instanceCount();
+}
diff --git a/Source/WebKit/UIProcess/API/C/WKTestingSupport.h b/Source/WebKit/UIProcess/API/C/WKTestingSupport.h
new file mode 100644 (file)
index 0000000..cff4701
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <WebKit/WKBase.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+WK_EXPORT size_t WKGetAPIURLSchemeTaskInstanceCount();
+WK_EXPORT size_t WKGetWebURLSchemeTaskInstanceCount();
+
+#ifdef __cplusplus
+}
+#endif
index 691b439..3d113a9 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include <WebCore/ResourceRequest.h>
+#include <wtf/InstanceCounted.h>
 #include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
 
@@ -40,7 +41,7 @@ namespace WebKit {
 class WebURLSchemeHandler;
 class WebPageProxy;
 
-class WebURLSchemeTask : public RefCounted<WebURLSchemeTask> {
+class WebURLSchemeTask : public RefCounted<WebURLSchemeTask>, public InstanceCounted<WebURLSchemeTask> {
     WTF_MAKE_NONCOPYABLE(WebURLSchemeTask);
 public:
     static Ref<WebURLSchemeTask> create(WebURLSchemeHandler&, WebPageProxy&, uint64_t identifier, const WebCore::ResourceRequest&);
index ebca6e9..801d1c2 100644 (file)
                51F7DC41180CC93600212CA3 /* XPCServiceMain.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC82839616B47EC400A278FE /* XPCServiceMain.mm */; };
                51F7DC43180CC93600212CA3 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC3DE46815A91763008D26FC /* Foundation.framework */; };
                51F7DC44180CC93600212CA3 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* WebKit.framework */; };
+               51F886A51F2C227E00C193EF /* WKTestingSupport.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F886A31F2C214A00C193EF /* WKTestingSupport.cpp */; };
+               51F886A61F2C228100C193EF /* WKTestingSupport.h in Headers */ = {isa = PBXBuildFile; fileRef = 51F886A41F2C214A00C193EF /* WKTestingSupport.h */; settings = {ATTRIBUTES = (Private, ); }; };
                51FA2D7415212DF100C1BA0B /* InjectedBundleDOMWindowExtension.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51FA2D5C15211A5000C1BA0B /* InjectedBundleDOMWindowExtension.cpp */; };
                51FA2D7715212E2600C1BA0B /* WKBundleDOMWindowExtension.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51FA2D7515212E1E00C1BA0B /* WKBundleDOMWindowExtension.cpp */; };
                51FAEC3A1B0657630009C4E7 /* ChildProcessMessages.h in Headers */ = {isa = PBXBuildFile; fileRef = 51FAEC371B0657310009C4E7 /* ChildProcessMessages.h */; };
                51F060DD1654317500F3282E /* NetworkRTCProviderMessageReceiver.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkRTCProviderMessageReceiver.cpp; sourceTree = "<group>"; };
                51F060DE1654317500F3281B /* WebResourceLoaderMessages.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebResourceLoaderMessages.h; sourceTree = "<group>"; };
                51F7DC4A180CC93600212CA3 /* com.apple.WebKit.Databases.xpc */ = {isa = PBXFileReference; explicitFileType = "wrapper.xpc-service"; includeInIndex = 0; path = com.apple.WebKit.Databases.xpc; sourceTree = BUILT_PRODUCTS_DIR; };
+               51F886A31F2C214A00C193EF /* WKTestingSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKTestingSupport.cpp; sourceTree = "<group>"; };
+               51F886A41F2C214A00C193EF /* WKTestingSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKTestingSupport.h; sourceTree = "<group>"; };
                51FA2D541521118600C1BA0B /* WKBundleDOMWindowExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKBundleDOMWindowExtension.h; sourceTree = "<group>"; };
                51FA2D5A15211A1E00C1BA0B /* InjectedBundleDOMWindowExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InjectedBundleDOMWindowExtension.h; sourceTree = "<group>"; };
                51FA2D5C15211A5000C1BA0B /* InjectedBundleDOMWindowExtension.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InjectedBundleDOMWindowExtension.cpp; sourceTree = "<group>"; };
                                33367639130C99DC006C9DE2 /* WKResourceCacheManager.h */,
                                1ADE46B01954EC61000F7985 /* WKSessionStateRef.cpp */,
                                1ADE46B11954EC61000F7985 /* WKSessionStateRef.h */,
+                               51F886A31F2C214A00C193EF /* WKTestingSupport.cpp */,
+                               51F886A41F2C214A00C193EF /* WKTestingSupport.h */,
                                314888FE1D91B11D00377042 /* WKTextChecker.cpp */,
                                314888FF1D91B11D00377042 /* WKTextChecker.h */,
                                7C89D29D1A678554003A5FDE /* WKUserContentControllerRef.cpp */,
                                F409BA181E6E64BC009DA28E /* WKDragDestinationAction.h in Headers */,
                                1AF4592F19464B2000F9D4A2 /* WKError.h in Headers */,
                                BCFD548C132D82680055D816 /* WKErrorCF.h in Headers */,
+                               51F886A61F2C228100C193EF /* WKTestingSupport.h in Headers */,
                                37B5045219EEF31300CE2CF8 /* WKErrorPrivate.h in Headers */,
                                BC4075FC124FF0270068F20A /* WKErrorRef.h in Headers */,
                                BC40783D1250FADD0068F20A /* WKEvent.h in Headers */,
                                2DA944BD188511E700ED86DB /* NetworkProcessIOS.mm in Sources */,
                                51795567162876CB00FA43B6 /* NetworkProcessMac.mm in Sources */,
                                5163199416289A6000E22F00 /* NetworkProcessMessageReceiver.cpp in Sources */,
+                               51F886A51F2C227E00C193EF /* WKTestingSupport.cpp in Sources */,
                                E14A954916E016A40068DE82 /* NetworkProcessPlatformStrategies.cpp in Sources */,
                                5179556D162877B100FA43B6 /* NetworkProcessProxy.cpp in Sources */,
                                516319921628980A00E22F00 /* NetworkProcessProxyMac.mm in Sources */,
index 0129d34..1bec40c 100644 (file)
@@ -1,3 +1,17 @@
+2017-07-30  Brady Eidson  <beidson@apple.com>
+
+        Add URLSchemeHandler API tests that verify the lack of URLSchemeTask object leaks.
+        https://bugs.webkit.org/show_bug.cgi?id=174958
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit2Cocoa/WKURLSchemeHandler-leaks.mm: Added.
+        (-[LeakSchemeHandler webView:startURLSchemeTask:]):
+        (-[LeakSchemeHandler webView:stopURLSchemeTask:]):
+        (runUntilTasksInFlight):
+        (TEST):
+
 2017-07-28  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r219990.
index c52e547..1c2eba1 100644 (file)
                51714EB51CF8C78C004723C4 /* WebProcessKillIDBCleanup-2.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 51714EB31CF8C761004723C4 /* WebProcessKillIDBCleanup-2.html */; };
                51714EB81CF8CA17004723C4 /* WebProcessKillIDBCleanup.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51714EB61CF8C7A4004723C4 /* WebProcessKillIDBCleanup.mm */; };
                517E7E04151119C100D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 517E7E031511187500D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.html */; };
+               5182C22E1F2BCE540059BA7C /* WKURLSchemeHandler-leaks.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5182C22D1F2BCB410059BA7C /* WKURLSchemeHandler-leaks.mm */; };
                5198A2401EA7E59F008910B7 /* InitialWarmedProcessUsed.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5198A23F1EA7E595008910B7 /* InitialWarmedProcessUsed.mm */; };
                51A5877D1D1B49CD004BA9AF /* IndexedDBMultiProcess-3.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 51A5877C1D1B3D8D004BA9AF /* IndexedDBMultiProcess-3.html */; };
                51A587851D2739E3004BA9AF /* IndexedDBDatabaseProcessKill-1.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 51A587821D272EB5004BA9AF /* IndexedDBDatabaseProcessKill-1.html */; };
                51714EB91D087416004723C4 /* CrossThreadTask.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CrossThreadTask.cpp; sourceTree = "<group>"; };
                517E7DFB15110EA600D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MemoryCachePruneWithinResourceLoadDelegate.mm; sourceTree = "<group>"; };
                517E7E031511187500D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = MemoryCachePruneWithinResourceLoadDelegate.html; sourceTree = "<group>"; };
+               5182C22D1F2BCB410059BA7C /* WKURLSchemeHandler-leaks.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "WKURLSchemeHandler-leaks.mm"; sourceTree = "<group>"; };
                5198A23F1EA7E595008910B7 /* InitialWarmedProcessUsed.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InitialWarmedProcessUsed.mm; sourceTree = "<group>"; };
                51A5877C1D1B3D8D004BA9AF /* IndexedDBMultiProcess-3.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "IndexedDBMultiProcess-3.html"; sourceTree = "<group>"; };
                51A587821D272EB5004BA9AF /* IndexedDBDatabaseProcessKill-1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "IndexedDBDatabaseProcessKill-1.html"; sourceTree = "<group>"; };
                                44817A2E1F0486BF00003810 /* WKRequestActivatedElementInfo.mm */,
                                5E4B1D2C1D404C6100053621 /* WKScrollViewDelegateCrash.mm */,
                                51C683DD1EA134DB00650183 /* WKURLSchemeHandler-1.mm */,
+                               5182C22D1F2BCB410059BA7C /* WKURLSchemeHandler-leaks.mm */,
                                5CE354D81E70D9C300BEFE3B /* WKContentExtensionStore.mm */,
                                2EFF06D61D8AF34A0004BB30 /* WKWebViewCandidateTests.mm */,
                                7C417F311D19E14800B8EF53 /* WKWebViewDefaultNavigationDelegate.mm */,
                                7CCE7F1A1A411AE600447C4C /* WebCoreStatisticsWithNoWebProcess.cpp in Sources */,
                                7CCE7EAB1A411A2400447C4C /* WebKitAgnosticTest.mm in Sources */,
                                51714EB81CF8CA17004723C4 /* WebProcessKillIDBCleanup.mm in Sources */,
+                               5182C22E1F2BCE540059BA7C /* WKURLSchemeHandler-leaks.mm in Sources */,
                                536770341CC8022800D425B1 /* WebScriptObjectDescription.mm in Sources */,
                                5120C83D1E6751290025B250 /* WebsiteDataStoreCustomPaths.mm in Sources */,
                                7A7B0E7F1EAFE4C3006AB8AE /* LimitTitleSize.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKURLSchemeHandler-leaks.mm b/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKURLSchemeHandler-leaks.mm
new file mode 100644 (file)
index 0000000..0b4ddd6
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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.
+ */
+
+#import "config.h"
+
+#ifndef NDEBUG
+
+#import "PlatformUtilities.h"
+#import "Test.h"
+#import <WebKit/WKProcessPoolPrivate.h>
+#import <WebKit/WKTestingSupport.h>
+#import <WebKit/WKURLSchemeHandler.h>
+#import <WebKit/WKURLSchemeTaskPrivate.h>
+#import <WebKit/WKWebViewConfigurationPrivate.h>
+#import <WebKit/WKWebViewPrivate.h>
+#import <WebKit/WebKit.h>
+#import <wtf/RetainPtr.h>
+#import <wtf/Vector.h>
+
+#if WK_API_ENABLED
+
+@interface WKWebView (TestingSecrets)
++ (size_t)_webURLSchemeTaskInstanceCount;
++ (size_t)_apiURLSchemeTaskInstanceCount;
+@end
+
+@interface LeakSchemeHandler : NSObject <WKURLSchemeHandler>
+@end
+
+@implementation LeakSchemeHandler
+
+- (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
+{
+    RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:1 textEncodingName:nil]);
+    [task didReceiveResponse:response.get()];
+    [task didReceiveData:[NSData dataWithBytes:"1" length:1]];
+
+    // Don't finish these tasks, as we're exploring their leaks
+}
+
+- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
+{
+}
+
+@end
+
+static void runUntilTasksInFlight(size_t count)
+{
+    while (true) {
+        EXPECT_EQ(WKGetAPIURLSchemeTaskInstanceCount(), WKGetWebURLSchemeTaskInstanceCount());
+
+        if (WKGetAPIURLSchemeTaskInstanceCount() == count)
+            return;
+
+        TestWebKitAPI::Util::spinRunLoop(10);
+    }
+}
+
+auto e = EPERM;
+
+TEST(URLSchemeHandler, Leaks1)
+{
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
+
+    @autoreleasepool {
+    RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+        @autoreleasepool {
+        RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+            @autoreleasepool {
+                RetainPtr<WKWebView> webView3 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+                handler = nil;
+                configuration = nil;
+
+                [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
+                [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
+                [webView3 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main3"]]];
+
+                runUntilTasksInFlight(3);
+            }
+            runUntilTasksInFlight(2);
+        }
+        runUntilTasksInFlight(1);
+    }
+    runUntilTasksInFlight(0);
+}
+
+TEST(URLSchemeHandler, Leaks2)
+{
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
+
+    RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    handler = nil;
+    configuration = nil;
+
+    [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
+    [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
+
+    runUntilTasksInFlight(2);
+
+    kill([webView1 _webProcessIdentifier], SIGKILL);
+    kill([webView2 _webProcessIdentifier], SIGKILL);
+
+    runUntilTasksInFlight(0);
+}
+
+TEST(URLSchemeHandler, Leaks3)
+{
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
+
+    RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    handler = nil;
+    configuration = nil;
+
+    [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
+    [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
+
+    runUntilTasksInFlight(2);
+
+    [webView1.get().configuration.processPool _terminateNetworkProcess];
+
+    runUntilTasksInFlight(0);
+}
+
+#endif // WK_API_ENABLED
+#endif // NDEBUG