1 | /* |
2 | * Copyright (C) 2012 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "DownloadProxyMap.h" |
28 | |
29 | #include "AuxiliaryProcessProxy.h" |
30 | #include "DownloadProxy.h" |
31 | #include "DownloadProxyMessages.h" |
32 | #include "MessageReceiverMap.h" |
33 | #include "NetworkProcessMessages.h" |
34 | #include "NetworkProcessProxy.h" |
35 | #include "ProcessAssertion.h" |
36 | #include "WebProcessPool.h" |
37 | #include <wtf/StdLibExtras.h> |
38 | |
39 | #if PLATFORM(COCOA) |
40 | #include <wtf/cocoa/Entitlements.h> |
41 | #endif |
42 | |
43 | namespace WebKit { |
44 | |
45 | DownloadProxyMap::DownloadProxyMap(NetworkProcessProxy& process) |
46 | : m_process(makeWeakPtr(process)) |
47 | #if PLATFORM(COCOA) |
48 | , m_shouldTakeAssertion(WTF::processHasEntitlement("com.apple.multitasking.systemappassertions" )) |
49 | #endif |
50 | { |
51 | platformCreate(); |
52 | } |
53 | |
54 | DownloadProxyMap::~DownloadProxyMap() |
55 | { |
56 | ASSERT(m_downloads.isEmpty()); |
57 | platformDestroy(); |
58 | } |
59 | |
60 | #if !PLATFORM(COCOA) |
61 | void DownloadProxyMap::platformCreate() |
62 | { |
63 | } |
64 | |
65 | void DownloadProxyMap::platformDestroy() |
66 | { |
67 | } |
68 | #endif |
69 | |
70 | void DownloadProxyMap::applicationDidEnterBackground() |
71 | { |
72 | if (m_process) |
73 | m_process->send(Messages::NetworkProcess::ApplicationDidEnterBackground(), 0); |
74 | } |
75 | |
76 | void DownloadProxyMap::applicationWillEnterForeground() |
77 | { |
78 | if (m_process) |
79 | m_process->send(Messages::NetworkProcess::ApplicationWillEnterForeground(), 0); |
80 | } |
81 | |
82 | DownloadProxy& DownloadProxyMap::createDownloadProxy(WebProcessPool& processPool, const WebCore::ResourceRequest& resourceRequest) |
83 | { |
84 | auto downloadProxy = DownloadProxy::create(*this, processPool, resourceRequest); |
85 | m_downloads.set(downloadProxy->downloadID(), downloadProxy.copyRef()); |
86 | |
87 | RELEASE_LOG(Loading, "Adding download %" PRIu64 " to UIProcess DownloadProxyMap" , downloadProxy->downloadID().downloadID()); |
88 | |
89 | if (m_downloads.size() == 1 && m_shouldTakeAssertion) { |
90 | ASSERT(!m_downloadUIAssertion); |
91 | m_downloadUIAssertion = std::make_unique<ProcessAssertion>(getCurrentProcessID(), "WebKit downloads"_s , AssertionState::UnboundedNetworking); |
92 | |
93 | ASSERT(!m_downloadNetworkingAssertion); |
94 | RELEASE_ASSERT(m_process); |
95 | m_downloadNetworkingAssertion = std::make_unique<ProcessAssertion>(m_process->processIdentifier(), "WebKit downloads"_s , AssertionState::UnboundedNetworking); |
96 | |
97 | RELEASE_LOG(ProcessSuspension, "UIProcess took 'WebKit downloads' assertions for UIProcess and NetworkProcess" ); |
98 | } |
99 | |
100 | m_process->addMessageReceiver(Messages::DownloadProxy::messageReceiverName(), downloadProxy->downloadID().downloadID(), downloadProxy.get()); |
101 | |
102 | return downloadProxy; |
103 | } |
104 | |
105 | void DownloadProxyMap::downloadFinished(DownloadProxy& downloadProxy) |
106 | { |
107 | auto downloadID = downloadProxy.downloadID(); |
108 | |
109 | RELEASE_LOG(Loading, "Removing download %" PRIu64 " from UIProcess DownloadProxyMap" , downloadID.downloadID()); |
110 | |
111 | // The DownloadProxy may be holding the last reference to the process pool. |
112 | auto protectedProcessPool = makeRefPtr(m_process->processPool()); |
113 | |
114 | ASSERT(m_downloads.contains(downloadID)); |
115 | |
116 | m_process->removeMessageReceiver(Messages::DownloadProxy::messageReceiverName(), downloadID.downloadID()); |
117 | downloadProxy.invalidate(); |
118 | m_downloads.remove(downloadID); |
119 | |
120 | if (m_downloads.isEmpty() && m_shouldTakeAssertion) { |
121 | ASSERT(m_downloadUIAssertion); |
122 | ASSERT(m_downloadNetworkingAssertion); |
123 | m_downloadUIAssertion = nullptr; |
124 | m_downloadNetworkingAssertion = nullptr; |
125 | RELEASE_LOG(ProcessSuspension, "UIProcess released 'WebKit downloads' assertions for UIProcess and NetworkProcess" ); |
126 | } |
127 | } |
128 | |
129 | void DownloadProxyMap::invalidate() |
130 | { |
131 | // Invalidate all outstanding downloads. |
132 | for (const auto& download : m_downloads.values()) { |
133 | download->processDidClose(); |
134 | download->invalidate(); |
135 | m_process->removeMessageReceiver(Messages::DownloadProxy::messageReceiverName(), download->downloadID().downloadID()); |
136 | } |
137 | |
138 | m_downloads.clear(); |
139 | m_downloadUIAssertion = nullptr; |
140 | m_downloadNetworkingAssertion = nullptr; |
141 | RELEASE_LOG(ProcessSuspension, "UIProcess DownloadProxyMap invalidated - Released 'WebKit downloads' assertions for UIProcess and NetworkProcess" ); |
142 | |
143 | m_process = nullptr; |
144 | } |
145 | |
146 | } // namespace WebKit |
147 | |