| 1 | /* |
| 2 | * Copyright (C) 2012-2019 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 | #pragma once |
| 27 | |
| 28 | #include "CacheStorageEngineConnection.h" |
| 29 | #include "Connection.h" |
| 30 | #include "DownloadID.h" |
| 31 | #include "NetworkActivityTracker.h" |
| 32 | #include "NetworkConnectionToWebProcessMessages.h" |
| 33 | #include "NetworkMDNSRegister.h" |
| 34 | #include "NetworkRTCProvider.h" |
| 35 | #include "NetworkResourceLoadMap.h" |
| 36 | #include "WebPaymentCoordinatorProxy.h" |
| 37 | #include <WebCore/NetworkLoadInformation.h> |
| 38 | #include <WebCore/ProcessIdentifier.h> |
| 39 | #include <WebCore/RegistrableDomain.h> |
| 40 | #include <wtf/RefCounted.h> |
| 41 | |
| 42 | namespace PAL { |
| 43 | class SessionID; |
| 44 | } |
| 45 | |
| 46 | namespace WebCore { |
| 47 | class BlobDataFileReference; |
| 48 | class BlobRegistryImpl; |
| 49 | class ResourceError; |
| 50 | class ResourceRequest; |
| 51 | enum class StorageAccessPromptWasShown : bool; |
| 52 | enum class StorageAccessWasGranted : bool; |
| 53 | struct SameSiteInfo; |
| 54 | |
| 55 | enum class IncludeSecureCookies : bool; |
| 56 | } |
| 57 | |
| 58 | namespace WebKit { |
| 59 | |
| 60 | class NetworkProcess; |
| 61 | class NetworkResourceLoader; |
| 62 | class NetworkSocketStream; |
| 63 | class WebIDBConnectionToClient; |
| 64 | class WebSWServerConnection; |
| 65 | typedef uint64_t ResourceLoadIdentifier; |
| 66 | |
| 67 | namespace NetworkCache { |
| 68 | struct DataKey; |
| 69 | } |
| 70 | |
| 71 | class NetworkConnectionToWebProcess |
| 72 | : public RefCounted<NetworkConnectionToWebProcess> |
| 73 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
| 74 | , public WebPaymentCoordinatorProxy::Client |
| 75 | #endif |
| 76 | , IPC::Connection::Client { |
| 77 | public: |
| 78 | using RegistrableDomain = WebCore::RegistrableDomain; |
| 79 | |
| 80 | static Ref<NetworkConnectionToWebProcess> create(NetworkProcess&, IPC::Connection::Identifier); |
| 81 | virtual ~NetworkConnectionToWebProcess(); |
| 82 | |
| 83 | IPC::Connection& connection() { return m_connection.get(); } |
| 84 | NetworkProcess& networkProcess() { return m_networkProcess.get(); } |
| 85 | |
| 86 | void didCleanupResourceLoader(NetworkResourceLoader&); |
| 87 | void transferKeptAliveLoad(NetworkResourceLoader&); |
| 88 | void setOnLineState(bool); |
| 89 | |
| 90 | bool () const { return m_captureExtraNetworkLoadMetricsEnabled; } |
| 91 | |
| 92 | RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path); |
| 93 | |
| 94 | void cleanupForSuspension(Function<void()>&&); |
| 95 | void endSuspension(); |
| 96 | |
| 97 | void getNetworkLoadInformationRequest(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceRequest&)>&& completionHandler) |
| 98 | { |
| 99 | completionHandler(m_networkLoadInformationByID.get(identifier).request); |
| 100 | } |
| 101 | |
| 102 | void getNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceResponse&)>&& completionHandler) |
| 103 | { |
| 104 | completionHandler(m_networkLoadInformationByID.get(identifier).response); |
| 105 | } |
| 106 | |
| 107 | void getNetworkLoadIntermediateInformation(ResourceLoadIdentifier identifier, CompletionHandler<void(const Vector<WebCore::NetworkTransactionInformation>&)>&& completionHandler) |
| 108 | { |
| 109 | completionHandler(m_networkLoadInformationByID.get(identifier).transactions); |
| 110 | } |
| 111 | |
| 112 | void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::NetworkLoadMetrics&)>&& completionHandler) |
| 113 | { |
| 114 | completionHandler(m_networkLoadInformationByID.take(identifier).metrics); |
| 115 | } |
| 116 | |
| 117 | void addNetworkLoadInformation(ResourceLoadIdentifier identifier, WebCore::NetworkLoadInformation&& information) |
| 118 | { |
| 119 | ASSERT(!m_networkLoadInformationByID.contains(identifier)); |
| 120 | m_networkLoadInformationByID.add(identifier, WTFMove(information)); |
| 121 | } |
| 122 | |
| 123 | void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics) |
| 124 | { |
| 125 | ASSERT(m_networkLoadInformationByID.contains(identifier)); |
| 126 | m_networkLoadInformationByID.ensure(identifier, [] { |
| 127 | return WebCore::NetworkLoadInformation { }; |
| 128 | }).iterator->value.metrics = metrics; |
| 129 | } |
| 130 | |
| 131 | void removeNetworkLoadInformation(ResourceLoadIdentifier identifier) |
| 132 | { |
| 133 | m_networkLoadInformationByID.remove(identifier); |
| 134 | } |
| 135 | |
| 136 | Optional<NetworkActivityTracker> startTrackingResourceLoad(uint64_t pageID, ResourceLoadIdentifier resourceID, bool isMainResource, const PAL::SessionID&); |
| 137 | void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode); |
| 138 | |
| 139 | WebCore::BlobRegistryImpl& blobRegistry(); |
| 140 | Vector<RefPtr<WebCore::BlobDataFileReference>> filesInBlob(const URL&); |
| 141 | Vector<RefPtr<WebCore::BlobDataFileReference>> resolveBlobReferences(const NetworkResourceLoadParameters&); |
| 142 | |
| 143 | void setWebProcessIdentifier(WebCore::ProcessIdentifier); |
| 144 | void setConnectionHasUploads(); |
| 145 | void clearConnectionHasUploads(); |
| 146 | |
| 147 | private: |
| 148 | NetworkConnectionToWebProcess(NetworkProcess&, IPC::Connection::Identifier); |
| 149 | |
| 150 | void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&); |
| 151 | |
| 152 | // IPC::Connection::Client |
| 153 | void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override; |
| 154 | void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override; |
| 155 | void didClose(IPC::Connection&) override; |
| 156 | void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override; |
| 157 | |
| 158 | // Message handlers. |
| 159 | void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&); |
| 160 | void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&); |
| 161 | |
| 162 | void scheduleResourceLoad(NetworkResourceLoadParameters&&); |
| 163 | void performSynchronousLoad(NetworkResourceLoadParameters&&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&&); |
| 164 | void loadPing(NetworkResourceLoadParameters&&); |
| 165 | void prefetchDNS(const String&); |
| 166 | void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&); |
| 167 | |
| 168 | void removeLoadIdentifier(ResourceLoadIdentifier); |
| 169 | void pageLoadCompleted(uint64_t webPageID); |
| 170 | void crossOriginRedirectReceived(ResourceLoadIdentifier, const URL& redirectURL); |
| 171 | void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { }); |
| 172 | void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&); |
| 173 | |
| 174 | void cookiesForDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, WebCore::IncludeSecureCookies, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&); |
| 175 | void setCookiesFromDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, const String&); |
| 176 | void cookiesEnabled(PAL::SessionID, CompletionHandler<void(bool)>&&); |
| 177 | void (PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, WebCore::IncludeSecureCookies, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&); |
| 178 | void getRawCookies(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, CompletionHandler<void(Vector<WebCore::Cookie>&&)>&&); |
| 179 | void deleteCookie(PAL::SessionID, const URL&, const String& cookieName); |
| 180 | |
| 181 | void registerFileBlobURL(const URL&, const String& path, SandboxExtension::Handle&&, const String& contentType); |
| 182 | void registerBlobURL(const URL&, Vector<WebCore::BlobPart>&&, const String& contentType); |
| 183 | void registerBlobURLFromURL(const URL&, const URL& srcURL, bool shouldBypassConnectionCheck); |
| 184 | void registerBlobURLOptionallyFileBacked(const URL&, const URL& srcURL, const String& fileBackedPath, const String& contentType); |
| 185 | void registerBlobURLForSlice(const URL&, const URL& srcURL, int64_t start, int64_t end); |
| 186 | void blobSize(const URL&, CompletionHandler<void(uint64_t)>&&); |
| 187 | void unregisterBlobURL(const URL&); |
| 188 | void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, CompletionHandler<void(Vector<String>&&)>&&); |
| 189 | |
| 190 | void (bool); |
| 191 | |
| 192 | void createSocketStream(URL&&, PAL::SessionID, String cachePartition, uint64_t); |
| 193 | void destroySocketStream(uint64_t); |
| 194 | |
| 195 | void ensureLegacyPrivateBrowsingSession(); |
| 196 | |
| 197 | #if ENABLE(INDEXED_DATABASE) |
| 198 | // Messages handlers (Modern IDB). |
| 199 | void establishIDBConnectionToServer(PAL::SessionID, CompletionHandler<void(uint64_t serverConnectionIdentifier)>&&); |
| 200 | #endif |
| 201 | |
| 202 | #if ENABLE(SERVICE_WORKER) |
| 203 | void establishSWServerConnection(PAL::SessionID, CompletionHandler<void(WebCore::SWServerConnectionIdentifier&&)>&&); |
| 204 | void unregisterSWConnections(); |
| 205 | #endif |
| 206 | |
| 207 | #if USE(LIBWEBRTC) |
| 208 | NetworkRTCProvider& rtcProvider(); |
| 209 | #endif |
| 210 | #if ENABLE(WEB_RTC) |
| 211 | NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; } |
| 212 | #endif |
| 213 | |
| 214 | CacheStorageEngineConnection& cacheStorageConnection(); |
| 215 | |
| 216 | #if ENABLE(RESOURCE_LOAD_STATISTICS) |
| 217 | void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID); |
| 218 | void clearPageSpecificDataForResourceLoadStatistics(PAL::SessionID, uint64_t pageID); |
| 219 | |
| 220 | void logUserInteraction(PAL::SessionID, const RegistrableDomain&); |
| 221 | void logWebSocketLoading(PAL::SessionID, const RegistrableDomain& targetDomain, const RegistrableDomain& topFrameDomain, WallTime lastSeen); |
| 222 | void logSubresourceLoading(PAL::SessionID, const RegistrableDomain& targetDomain, const RegistrableDomain& topFrameDomain, WallTime lastSeen); |
| 223 | void logSubresourceRedirect(PAL::SessionID, const RegistrableDomain& sourceDomain, const RegistrableDomain& targetDomain); |
| 224 | void resourceLoadStatisticsUpdated(Vector<WebCore::ResourceLoadStatistics>&&); |
| 225 | void hasStorageAccess(PAL::SessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&); |
| 226 | void requestStorageAccess(PAL::SessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(WebCore::StorageAccessWasGranted, WebCore::StorageAccessPromptWasShown)>&&); |
| 227 | void requestStorageAccessUnderOpener(PAL::SessionID, WebCore::RegistrableDomain&& domainInNeedOfStorageAccess, uint64_t openerPageID, WebCore::RegistrableDomain&& openerDomain); |
| 228 | #endif |
| 229 | |
| 230 | void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains); |
| 231 | void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains); |
| 232 | void resetOriginAccessWhitelists(); |
| 233 | |
| 234 | struct ResourceNetworkActivityTracker { |
| 235 | ResourceNetworkActivityTracker() = default; |
| 236 | ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default; |
| 237 | ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default; |
| 238 | ResourceNetworkActivityTracker(uint64_t pageID) |
| 239 | : pageID { pageID } |
| 240 | , isRootActivity { true } |
| 241 | , networkActivity { NetworkActivityTracker::Label::LoadPage } |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | ResourceNetworkActivityTracker(uint64_t pageID, ResourceLoadIdentifier resourceID) |
| 246 | : pageID { pageID } |
| 247 | , resourceID { resourceID } |
| 248 | , networkActivity { NetworkActivityTracker::Label::LoadResource } |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | uint64_t pageID { 0 }; |
| 253 | ResourceLoadIdentifier resourceID { 0 }; |
| 254 | bool isRootActivity { false }; |
| 255 | NetworkActivityTracker networkActivity; |
| 256 | }; |
| 257 | |
| 258 | void stopAllNetworkActivityTracking(); |
| 259 | void stopAllNetworkActivityTrackingForPage(uint64_t pageID); |
| 260 | size_t findRootNetworkActivity(uint64_t pageID); |
| 261 | size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID); |
| 262 | |
| 263 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
| 264 | WebPaymentCoordinatorProxy& paymentCoordinator(); |
| 265 | |
| 266 | // WebPaymentCoordinatorProxy::Client |
| 267 | IPC::Connection* paymentCoordinatorConnection(const WebPaymentCoordinatorProxy&) final; |
| 268 | UIViewController *paymentCoordinatorPresentingViewController(const WebPaymentCoordinatorProxy&) final; |
| 269 | const String& paymentCoordinatorCTDataConnectionServiceType(const WebPaymentCoordinatorProxy&, PAL::SessionID) final; |
| 270 | const String& paymentCoordinatorSourceApplicationBundleIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) final; |
| 271 | const String& paymentCoordinatorSourceApplicationSecondaryIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) final; |
| 272 | std::unique_ptr<PaymentAuthorizationPresenter> paymentCoordinatorAuthorizationPresenter(WebPaymentCoordinatorProxy&, PKPaymentRequest *) final; |
| 273 | void paymentCoordinatorAddMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&, IPC::MessageReceiver&) final; |
| 274 | void paymentCoordinatorRemoveMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&) final; |
| 275 | #endif |
| 276 | |
| 277 | Ref<IPC::Connection> m_connection; |
| 278 | Ref<NetworkProcess> m_networkProcess; |
| 279 | |
| 280 | HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams; |
| 281 | NetworkResourceLoadMap m_networkResourceLoaders; |
| 282 | HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences; |
| 283 | Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers; |
| 284 | |
| 285 | HashMap<ResourceLoadIdentifier, WebCore::NetworkLoadInformation> m_networkLoadInformationByID; |
| 286 | |
| 287 | |
| 288 | #if USE(LIBWEBRTC) |
| 289 | RefPtr<NetworkRTCProvider> m_rtcProvider; |
| 290 | #endif |
| 291 | #if ENABLE(WEB_RTC) |
| 292 | NetworkMDNSRegister m_mdnsRegister; |
| 293 | #endif |
| 294 | |
| 295 | bool { false }; |
| 296 | |
| 297 | RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection; |
| 298 | |
| 299 | #if ENABLE(INDEXED_DATABASE) |
| 300 | HashMap<uint64_t, RefPtr<WebIDBConnectionToClient>> m_webIDBConnections; |
| 301 | #endif |
| 302 | |
| 303 | #if ENABLE(SERVICE_WORKER) |
| 304 | HashMap<WebCore::SWServerConnectionIdentifier, WeakPtr<WebSWServerConnection>> m_swConnections; |
| 305 | #endif |
| 306 | |
| 307 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
| 308 | std::unique_ptr<WebPaymentCoordinatorProxy> m_paymentCoordinator; |
| 309 | #endif |
| 310 | |
| 311 | WebCore::ProcessIdentifier m_webProcessIdentifier; |
| 312 | bool m_connectionHasUploads { false }; |
| 313 | }; |
| 314 | |
| 315 | } // namespace WebKit |
| 316 | |