Add a new DownloadMap type that manages taking an assertion automatically.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 21:27:21 +0000 (21:27 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 21:27:21 +0000 (21:27 +0000)
https://bugs.webkit.org/show_bug.cgi?id=194294

Reviewed by Alex Christensen.

If we don't need the download assertion, we use a vanilla HashMap like today.
If we need the download assertion, we use the new DownloadMap class instead.

The new DownloadMap is a wrapper around a HashMap that also creates/destroys the assertion as needed.

* NetworkProcess/Downloads/DownloadManager.cpp:
(WebKit::DownloadManager::dataTaskBecameDownloadTask):
(WebKit::DownloadManager::downloadFinished):
* NetworkProcess/Downloads/DownloadManager.h:

* NetworkProcess/Downloads/DownloadMap.cpp: Added.
(WebKit::DownloadMap::get const):
(WebKit::DownloadMap::isEmpty const):
(WebKit::DownloadMap::size const):
(WebKit::DownloadMap::contains const):
(WebKit::DownloadMap::add):
(WebKit::DownloadMap::remove):
* NetworkProcess/Downloads/DownloadMap.h: Added.

* Sources.txt:

* UIProcess/ios/ProcessAssertionIOS.mm:
(WebKit::ProcessAssertion::ProcessAssertion):

* WebKit.xcodeproj/project.pbxproj:

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

Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/Downloads/DownloadManager.cpp
Source/WebKit/NetworkProcess/Downloads/DownloadManager.h
Source/WebKit/NetworkProcess/Downloads/DownloadMap.cpp [new file with mode: 0644]
Source/WebKit/NetworkProcess/Downloads/DownloadMap.h [new file with mode: 0644]
Source/WebKit/Sources.txt
Source/WebKit/UIProcess/ios/ProcessAssertionIOS.mm
Source/WebKit/WebKit.xcodeproj/project.pbxproj

index eab39a1..27536c7 100644 (file)
@@ -1,5 +1,38 @@
 2019-02-05  Brady Eidson  <beidson@apple.com>
 
+        Add a new DownloadMap type that manages taking an assertion automatically.
+        https://bugs.webkit.org/show_bug.cgi?id=194294
+
+        Reviewed by Alex Christensen.
+
+        If we don't need the download assertion, we use a vanilla HashMap like today.
+        If we need the download assertion, we use the new DownloadMap class instead.
+
+        The new DownloadMap is a wrapper around a HashMap that also creates/destroys the assertion as needed.
+
+        * NetworkProcess/Downloads/DownloadManager.cpp:
+        (WebKit::DownloadManager::dataTaskBecameDownloadTask):
+        (WebKit::DownloadManager::downloadFinished):
+        * NetworkProcess/Downloads/DownloadManager.h:
+
+        * NetworkProcess/Downloads/DownloadMap.cpp: Added.
+        (WebKit::DownloadMap::get const):
+        (WebKit::DownloadMap::isEmpty const):
+        (WebKit::DownloadMap::size const):
+        (WebKit::DownloadMap::contains const):
+        (WebKit::DownloadMap::add):
+        (WebKit::DownloadMap::remove):
+        * NetworkProcess/Downloads/DownloadMap.h: Added.
+
+        * Sources.txt:
+
+        * UIProcess/ios/ProcessAssertionIOS.mm:
+        (WebKit::ProcessAssertion::ProcessAssertion):
+
+        * WebKit.xcodeproj/project.pbxproj:
+
+2019-02-05  Brady Eidson  <beidson@apple.com>
+
         Fix iOS simulator build after r240954
 
         Unreviewed.
index 28efe98..da403c4 100644 (file)
@@ -71,13 +71,6 @@ void DownloadManager::dataTaskBecameDownloadTask(DownloadID downloadID, std::uni
     ASSERT(!m_downloads.contains(downloadID));
     m_downloadsAfterDestinationDecided.remove(downloadID);
     m_downloads.add(downloadID, WTFMove(download));
-
-#if ENABLE(TAKE_DOWNLOAD_ASSERTION)
-    if (m_downloads.size() == 1) {
-        ASSERT(!m_downloadAssertion);
-        m_downloadAssertion = std::make_unique<ProcessAssertion>(getpid(), "WebKit downloads"_s, AssertionState::Download);
-    }
-#endif
 }
 
 void DownloadManager::continueWillSendRequest(DownloadID downloadID, WebCore::ResourceRequest&& request)
@@ -180,12 +173,6 @@ void DownloadManager::downloadFinished(Download* download)
     ASSERT(m_downloads.contains(download->downloadID()));
     m_downloads.remove(download->downloadID());
     
-#if ENABLE(TAKE_DOWNLOAD_ASSERTION)
-    if (m_downloads.isEmpty()) {
-        ASSERT(m_downloadAssertion);
-        m_downloadAssertion = nullptr;
-    }
-#endif
 }
 
 void DownloadManager::didCreateDownload()
index ffe1ad0..ff11dfb 100644 (file)
@@ -26,9 +26,9 @@
 #pragma once
 
 #include "DownloadID.h"
+#include "DownloadMap.h"
 #include "NetworkDataTask.h"
 #include "PendingDownload.h"
-#include "ProcessAssertion.h"
 #include "SandboxExtension.h"
 #include <WebCore/NotImplemented.h>
 #include <wtf/Forward.h>
@@ -113,11 +113,7 @@ private:
     HashMap<DownloadID, std::unique_ptr<PendingDownload>> m_pendingDownloads;
     HashMap<DownloadID, std::pair<RefPtr<NetworkDataTask>, ResponseCompletionHandler>> m_downloadsWaitingForDestination;
     HashMap<DownloadID, RefPtr<NetworkDataTask>> m_downloadsAfterDestinationDecided;
-    HashMap<DownloadID, std::unique_ptr<Download>> m_downloads;
-
-#if ENABLE(TAKE_DOWNLOAD_ASSERTION)
-    std::unique_ptr<ProcessAssertion> m_downloadAssertion;
-#endif
+    DownloadMap m_downloads;
 };
 
 } // namespace WebKit
diff --git a/Source/WebKit/NetworkProcess/Downloads/DownloadMap.cpp b/Source/WebKit/NetworkProcess/Downloads/DownloadMap.cpp
new file mode 100644 (file)
index 0000000..b2baa33
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 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 "DownloadMap.h"
+
+#if ENABLE(TAKE_DOWNLOAD_ASSERTION)
+
+#include "Download.h"
+
+namespace WebKit {
+
+
+Download* DownloadMap::get(DownloadID downloadID) const
+{
+    return m_downloads.get(downloadID);
+}
+
+bool DownloadMap::isEmpty() const
+{
+    return m_downloads.isEmpty();
+}
+
+uint64_t DownloadMap::size() const
+{
+    return m_downloads.size();
+}
+
+bool DownloadMap::contains(DownloadID downloadID) const
+{
+    return m_downloads.contains(downloadID);
+}
+
+DownloadMap::DownloadMapType::AddResult DownloadMap::add(DownloadID downloadID, std::unique_ptr<Download>&& download)
+{
+    auto result = m_downloads.add(downloadID, WTFMove(download));
+    if (m_downloads.size() == 1) {
+        ASSERT(!m_downloadAssertion);
+        m_downloadAssertion = std::make_unique<ProcessAssertion>(getpid(), "WebKit downloads"_s, AssertionState::Download);
+    }
+
+    return result;
+}
+
+bool DownloadMap::remove(DownloadID downloadID)
+{
+    auto result = m_downloads.remove(downloadID);
+    if (m_downloads.isEmpty()) {
+        ASSERT(m_downloadAssertion);
+        m_downloadAssertion = nullptr;
+    }
+    
+    return result;
+}
+
+} // namespace WebKit
+
+#endif // ENABLE(TAKE_DOWNLOAD_ASSERTION)
diff --git a/Source/WebKit/NetworkProcess/Downloads/DownloadMap.h b/Source/WebKit/NetworkProcess/Downloads/DownloadMap.h
new file mode 100644 (file)
index 0000000..9843a83
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 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 "DownloadID.h"
+#include "ProcessAssertion.h"
+#include <wtf/HashMap.h>
+
+namespace WebKit {
+
+class Download;
+
+#if !ENABLE(TAKE_DOWNLOAD_ASSERTION)
+typedef HashMap<DownloadID, std::unique_ptr<Download>> DownloadMap;
+#else
+
+class DownloadMap {
+public:
+    typedef HashMap<DownloadID, std::unique_ptr<Download>> DownloadMapType;
+
+    Download* get(DownloadID) const;
+    bool isEmpty() const;
+    uint64_t size() const;
+    bool contains(DownloadID) const;
+
+    DownloadMapType::AddResult add(DownloadID, std::unique_ptr<Download>&&);
+    bool remove(DownloadID);
+
+private:
+    DownloadMapType m_downloads;
+    std::unique_ptr<ProcessAssertion> m_downloadAssertion;
+};
+
+#endif // !ENABLE(TAKE_DOWNLOAD_ASSERTION)
+
+} // namespace WebKit
index d2595d7..b39d264 100644 (file)
@@ -48,6 +48,7 @@ NetworkProcess/Cookies/WebCookieManager.cpp
 
 NetworkProcess/Downloads/Download.cpp
 NetworkProcess/Downloads/DownloadManager.cpp
+NetworkProcess/Downloads/DownloadMap.cpp
 NetworkProcess/Downloads/PendingDownload.cpp
 
 NetworkProcess/FileAPI/NetworkBlobRegistry.cpp
index 2370eaa..dc86d42 100644 (file)
@@ -192,7 +192,7 @@ ProcessAssertion::ProcessAssertion(pid_t pid, const String& name, AssertionState
             });
         }
     };
-    RELEASE_LOG(ProcessSuspension, "%p - ProcessAssertion() PID %d acquiring assertion for process with PID %d", this, getpid(), pid);
+    RELEASE_LOG(ProcessSuspension, "%p - ProcessAssertion() PID %d acquiring assertion for process with PID %d, name '%s'", this, getpid(), pid, name.utf8().data());
     
     m_assertion = adoptNS([[BKSProcessAssertion alloc] initWithPID:pid flags:flagsForState(assertionState) reason:reasonForState(assertionState) name:(NSString *)name withHandler:handler]);
     m_assertion.get().invalidationHandler = ^() {
index f4577b0..34c016a 100644 (file)
                512127C41908239A00DAF35C /* WebPasteboardOverrides.h in Headers */ = {isa = PBXBuildFile; fileRef = 512127C21908239A00DAF35C /* WebPasteboardOverrides.h */; };
                51217461164C20E30037A5C1 /* ShareableResource.h in Headers */ = {isa = PBXBuildFile; fileRef = 5121745F164C20E30037A5C1 /* ShareableResource.h */; };
                5123CF1C133D260A0056F800 /* WKIconDatabaseCG.h in Headers */ = {isa = PBXBuildFile; fileRef = 5123CF1A133D260A0056F800 /* WKIconDatabaseCG.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               51240EBA220A08D2005CFC63 /* DownloadMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 51240EB8220A08CA005CFC63 /* DownloadMap.h */; };
                512935D81288D19400A4B695 /* WebContextMenuItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 512935D61288D19400A4B695 /* WebContextMenuItem.h */; };
                512935E41288D97800A4B695 /* InjectedBundlePageContextMenuClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 512935E21288D97800A4B695 /* InjectedBundlePageContextMenuClient.h */; };
                512E34E5130B4D0500ABD19A /* WKApplicationCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 517A33B4130B308C00F80CB5 /* WKApplicationCacheManager.h */; settings = {ATTRIBUTES = (Private, ); }; };
                5121745F164C20E30037A5C1 /* ShareableResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShareableResource.h; sourceTree = "<group>"; };
                5123CF19133D260A0056F800 /* WKIconDatabaseCG.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WKIconDatabaseCG.cpp; path = cg/WKIconDatabaseCG.cpp; sourceTree = "<group>"; };
                5123CF1A133D260A0056F800 /* WKIconDatabaseCG.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WKIconDatabaseCG.h; path = cg/WKIconDatabaseCG.h; sourceTree = "<group>"; };
+               51240EB8220A08CA005CFC63 /* DownloadMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DownloadMap.h; sourceTree = "<group>"; };
+               51240EB9220A08CA005CFC63 /* DownloadMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DownloadMap.cpp; sourceTree = "<group>"; };
                512935D51288D19400A4B695 /* WebContextMenuItem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebContextMenuItem.cpp; sourceTree = "<group>"; };
                512935D61288D19400A4B695 /* WebContextMenuItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebContextMenuItem.h; sourceTree = "<group>"; };
                512935E11288D97800A4B695 /* InjectedBundlePageContextMenuClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InjectedBundlePageContextMenuClient.cpp; sourceTree = "<group>"; };
                                5C1426F71C23F84300D41183 /* DownloadID.h */,
                                5C1426F81C23F84300D41183 /* DownloadManager.cpp */,
                                5C1426F91C23F84300D41183 /* DownloadManager.h */,
+                               51240EB9220A08CA005CFC63 /* DownloadMap.cpp */,
+                               51240EB8220A08CA005CFC63 /* DownloadMap.h */,
                                5C85C7861C3F23C50061A4FA /* PendingDownload.cpp */,
                                5C298D9E1C3DEF2900470AFE /* PendingDownload.h */,
                        );
                                A1DF631318E0B7C8003A3E2A /* DownloadClient.h in Headers */,
                                5C1427051C23F84C00D41183 /* DownloadID.h in Headers */,
                                5C1427071C23F84C00D41183 /* DownloadManager.h in Headers */,
+                               51240EBA220A08D2005CFC63 /* DownloadMap.h in Headers */,
                                1AB7D4CA1288AAA700CFD08C /* DownloadProxy.h in Headers */,
                                1AD25E96167AB08100EA9BCD /* DownloadProxyMap.h in Headers */,
                                1AB7D61A1288B9D900CFD08C /* DownloadProxyMessages.h in Headers */,