Add facility for tracking times and results of page and resource loading
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkConnectionToWebProcess.h
1 /*
2  * Copyright (C) 2012-2017 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 "BlockingResponseMap.h"
29 #include "CacheStorageEngineConnection.h"
30 #include "Connection.h"
31 #include "DownloadID.h"
32 #include "NetworkActivityTracker.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkMDNSRegister.h"
35 #include "NetworkRTCProvider.h"
36 #include <WebCore/ResourceLoadPriority.h>
37 #include <wtf/RefCounted.h>
38
39 namespace WebCore {
40 class BlobDataFileReference;
41 class HTTPHeaderMap;
42 class ResourceError;
43 class ResourceRequest;
44 struct SameSiteInfo;
45
46 enum class IncludeSecureCookies;
47 }
48
49 namespace WebKit {
50
51 class NetworkConnectionToWebProcess;
52 class NetworkLoadParameters;
53 class NetworkResourceLoader;
54 class NetworkSocketStream;
55 class SyncNetworkResourceLoader;
56 typedef uint64_t ResourceLoadIdentifier;
57
58 namespace NetworkCache {
59 struct DataKey;
60 }
61
62 class NetworkConnectionToWebProcess : public RefCounted<NetworkConnectionToWebProcess>, IPC::Connection::Client {
63 public:
64     static Ref<NetworkConnectionToWebProcess> create(IPC::Connection::Identifier);
65     virtual ~NetworkConnectionToWebProcess();
66
67     IPC::Connection& connection() { return m_connection.get(); }
68
69     void didCleanupResourceLoader(NetworkResourceLoader&);
70     void didFinishPingLoad(uint64_t pingLoadIdentifier, const WebCore::ResourceError&, const WebCore::ResourceResponse&);
71     void setOnLineState(bool);
72
73     bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
74
75     RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
76
77     void cleanupForSuspension(Function<void()>&&);
78     void endSuspension();
79
80     // FIXME: We should store all redirected request/responses.
81     struct NetworkLoadInformation {
82         WebCore::ResourceResponse response;
83         WebCore::NetworkLoadMetrics metrics;
84     };
85
86     void takeNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, WebCore::ResourceResponse& response)
87     {
88         response = m_networkLoadInformationByID.get(identifier).response;
89     }
90
91     void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, WebCore::NetworkLoadMetrics& metrics)
92     {
93         metrics = m_networkLoadInformationByID.take(identifier).metrics;
94     }
95
96     void addNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, const WebCore::ResourceResponse& response)
97     {
98         ASSERT(!m_networkLoadInformationByID.contains(identifier));
99         m_networkLoadInformationByID.add(identifier, NetworkLoadInformation { response, { } });
100     }
101
102     void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
103     {
104         ASSERT(m_networkLoadInformationByID.contains(identifier));
105         m_networkLoadInformationByID.ensure(identifier, [] {
106             return NetworkLoadInformation { };
107         }).iterator->value.metrics = metrics;
108     }
109
110     void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
111     {
112         m_networkLoadInformationByID.remove(identifier);
113     }
114
115     std::optional<NetworkActivityTracker> startTrackingResourceLoad(uint64_t pageID, ResourceLoadIdentifier resourceID, bool isMainResource);
116     void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode);
117
118 private:
119     NetworkConnectionToWebProcess(IPC::Connection::Identifier);
120
121     void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
122
123     // IPC::Connection::Client
124     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
125     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
126     void didClose(IPC::Connection&) override;
127     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
128
129     // Message handlers.
130     void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
131     void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
132
133     void scheduleResourceLoad(NetworkResourceLoadParameters&&);
134     void performSynchronousLoad(NetworkResourceLoadParameters&&, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
135     void loadPing(NetworkResourceLoadParameters&&);
136     void prefetchDNS(const String&);
137     void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&);
138
139     void removeLoadIdentifier(ResourceLoadIdentifier);
140     void pageLoadCompleted(uint64_t webPageID);
141     void setDefersLoading(ResourceLoadIdentifier, bool);
142     void crossOriginRedirectReceived(ResourceLoadIdentifier, const WebCore::URL& redirectURL);
143     void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
144     void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
145
146     void cookiesForDOM(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
147     void setCookiesFromDOM(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, const String&);
148     void cookiesEnabled(PAL::SessionID, bool& result);
149     void cookieRequestHeaderFieldValue(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
150     void getRawCookies(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, Vector<WebCore::Cookie>&);
151     void deleteCookie(PAL::SessionID, const WebCore::URL&, const String& cookieName);
152
153     void registerFileBlobURL(const WebCore::URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
154     void registerBlobURL(const WebCore::URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
155     void registerBlobURLFromURL(const WebCore::URL&, const WebCore::URL& srcURL, bool shouldBypassConnectionCheck);
156     void preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& fileBackedPath, SandboxExtension::HandleArray&&);
157     void registerBlobURLOptionallyFileBacked(const WebCore::URL&, const WebCore::URL& srcURL, const String& fileBackedPath, const String& contentType);
158     void registerBlobURLForSlice(const WebCore::URL&, const WebCore::URL& srcURL, int64_t start, int64_t end);
159     void blobSize(const WebCore::URL&, uint64_t& resultSize);
160     void unregisterBlobURL(const WebCore::URL&);
161     void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier);
162
163     void storeDerivedDataToCache(const WebKit::NetworkCache::DataKey&, const IPC::DataReference&);
164
165     void setCaptureExtraNetworkLoadMetricsEnabled(bool);
166
167     void createSocketStream(WebCore::URL&&, PAL::SessionID, String cachePartition, uint64_t);
168     void destroySocketStream(uint64_t);
169     
170     void ensureLegacyPrivateBrowsingSession();
171
172 #if USE(LIBWEBRTC)
173     NetworkRTCProvider& rtcProvider();
174 #endif
175 #if ENABLE(WEB_RTC)
176     NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
177 #endif
178
179     CacheStorageEngineConnection& cacheStorageConnection();
180
181     void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
182     void removeStorageAccessForAllFramesOnPage(PAL::SessionID, uint64_t pageID);
183
184     void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
185     void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
186     void resetOriginAccessWhitelists();
187
188     struct ResourceNetworkActivityTracker {
189         ResourceNetworkActivityTracker() = default;
190         ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default;
191         ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default;
192         ResourceNetworkActivityTracker(uint64_t pageID)
193             : pageID { pageID }
194             , isRootActivity { true }
195             , networkActivity { NetworkActivityTracker::Label::LoadPage }
196         {
197         }
198
199         ResourceNetworkActivityTracker(uint64_t pageID, ResourceLoadIdentifier resourceID)
200             : pageID { pageID }
201             , resourceID { resourceID }
202             , networkActivity { NetworkActivityTracker::Label::LoadResource }
203         {
204         }
205
206         uint64_t pageID { 0 };
207         ResourceLoadIdentifier resourceID { 0 };
208         bool isRootActivity { false };
209         NetworkActivityTracker networkActivity;
210     };
211
212     void stopAllNetworkActivityTracking();
213     void stopAllNetworkActivityTrackingForPage(uint64_t pageID);
214     size_t findRootNetworkActivity(uint64_t pageID);
215     size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID);
216
217     Ref<IPC::Connection> m_connection;
218
219     HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
220     HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>> m_networkResourceLoaders;
221     HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
222     Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers;
223
224     HashMap<ResourceLoadIdentifier, NetworkLoadInformation> m_networkLoadInformationByID;
225
226
227 #if USE(LIBWEBRTC)
228     RefPtr<NetworkRTCProvider> m_rtcProvider;
229 #endif
230 #if ENABLE(WEB_RTC)
231     NetworkMDNSRegister m_mdnsRegister;
232 #endif
233
234     bool m_captureExtraNetworkLoadMetricsEnabled { false };
235
236     RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
237 };
238
239 } // namespace WebKit