Only allow non-mixed content protected subresources to ask for credentials
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkResourceLoader.h
1 /*
2  * Copyright (C) 2012-2015 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 #ifndef NetworkResourceLoader_h
27 #define NetworkResourceLoader_h
28
29 #include "DownloadID.h"
30 #include "MessageSender.h"
31 #include "NetworkConnectionToWebProcessMessages.h"
32 #include "NetworkLoadClient.h"
33 #include "NetworkResourceLoadParameters.h"
34 #include "ShareableResource.h"
35 #include <WebCore/Timer.h>
36
37 namespace WebCore {
38 class BlobDataFileReference;
39 class ResourceRequest;
40 }
41
42 namespace WebKit {
43
44 class NetworkConnectionToWebProcess;
45 class NetworkLoad;
46 class SandboxExtension;
47
48 namespace NetworkCache {
49 class Entry;
50 }
51
52 class NetworkResourceLoader final : public RefCounted<NetworkResourceLoader>, public NetworkLoadClient, public IPC::MessageSender {
53 public:
54     static Ref<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess& connection, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply = nullptr)
55     {
56         return adoptRef(*new NetworkResourceLoader(parameters, connection, WTFMove(reply)));
57     }
58     virtual ~NetworkResourceLoader();
59
60     const WebCore::ResourceRequest& originalRequest() const { return m_parameters.request; }
61
62     NetworkLoad* networkLoad() const { return m_networkLoad.get(); }
63
64     void start();
65     void abort();
66
67     void setDefersLoading(bool);
68
69     // Message handlers.
70     void didReceiveNetworkResourceLoaderMessage(IPC::Connection&, IPC::Decoder&);
71
72 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
73     void continueCanAuthenticateAgainstProtectionSpace(bool);
74 #endif
75     void continueWillSendRequest(WebCore::ResourceRequest&& newRequest, bool isAllowedToAskUserForCredentials);
76
77     const WebCore::ResourceResponse& response() const { return m_response; }
78
79     NetworkConnectionToWebProcess& connectionToWebProcess() { return m_connection; }
80     PAL::SessionID sessionID() const { return m_parameters.sessionID; }
81     ResourceLoadIdentifier identifier() const { return m_parameters.identifier; }
82     uint64_t frameID() const { return m_parameters.webFrameID; }
83     uint64_t pageID() const { return m_parameters.webPageID; }
84
85     struct SynchronousLoadData;
86
87     // NetworkLoadClient.
88     void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
89 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
90     void canAuthenticateAgainstProtectionSpaceAsync(const WebCore::ProtectionSpace&) override;
91 #endif
92     bool isSynchronous() const override;
93     bool isAllowedToAskUserForCredentials() const override { return m_isAllowedToAskUserForCredentials; }
94     void willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&) override;
95     ShouldContinueDidReceiveResponse didReceiveResponse(WebCore::ResourceResponse&&) override;
96     void didReceiveBuffer(Ref<WebCore::SharedBuffer>&&, int reportedEncodedDataLength) override;
97     void didFinishLoading(const WebCore::NetworkLoadMetrics&) override;
98     void didFailLoading(const WebCore::ResourceError&) override;
99     bool shouldCaptureExtraNetworkLoadMetrics() const override;
100
101     void convertToDownload(DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
102
103     bool isMainResource() const { return m_parameters.request.requester() == WebCore::ResourceRequest::Requester::Main; }
104     bool isAlwaysOnLoggingAllowed() const;
105
106 private:
107     NetworkResourceLoader(const NetworkResourceLoadParameters&, NetworkConnectionToWebProcess&, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
108
109     // IPC::MessageSender
110     IPC::Connection* messageSenderConnection() override;
111     uint64_t messageSenderDestinationID() override { return m_parameters.identifier; }
112
113     bool canUseCache(const WebCore::ResourceRequest&) const;
114     bool canUseCachedRedirect(const WebCore::ResourceRequest&) const;
115
116     void tryStoreAsCacheEntry();
117     void retrieveCacheEntry(const WebCore::ResourceRequest&);
118     void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
119     void sendResultForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
120     void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
121     void dispatchWillSendRequestForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
122
123     void startNetworkLoad(const WebCore::ResourceRequest&);
124     void continueDidReceiveResponse();
125
126     void cleanup();
127     
128     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
129
130     void startBufferingTimerIfNeeded();
131     void bufferingTimerFired();
132     void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
133
134     void consumeSandboxExtensions();
135     void invalidateSandboxExtensions();
136
137     const NetworkResourceLoadParameters m_parameters;
138
139     Ref<NetworkConnectionToWebProcess> m_connection;
140
141     std::unique_ptr<NetworkLoad> m_networkLoad;
142
143     WebCore::ResourceResponse m_response;
144
145     size_t m_bytesReceived { 0 };
146     size_t m_bufferedDataEncodedDataLength { 0 };
147     RefPtr<WebCore::SharedBuffer> m_bufferedData;
148     unsigned m_redirectCount { 0 };
149
150     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
151     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
152
153     bool m_wasStarted { false };
154     bool m_didConsumeSandboxExtensions { false };
155     bool m_defersLoading { false };
156     bool m_isAllowedToAskUserForCredentials { false };
157     size_t m_numBytesReceived { 0 };
158
159     unsigned m_retrievedDerivedDataCount { 0 };
160
161     WebCore::Timer m_bufferingTimer;
162     RefPtr<NetworkCache::Cache> m_cache;
163     RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
164     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
165     bool m_isWaitingContinueWillSendRequestForCachedRedirect { false };
166 };
167
168 } // namespace WebKit
169
170 #endif // NetworkResourceLoader_h