Check IDB quota usage through QuotaManager
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkDataTask.cpp
1 /*
2  * Copyright (C) 2016 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 "NetworkDataTask.h"
28
29 #include "NetworkDataTaskBlob.h"
30 #include "NetworkLoadParameters.h"
31 #include "NetworkSession.h"
32 #include <WebCore/ResourceResponse.h>
33 #include <wtf/RunLoop.h>
34
35 #if PLATFORM(COCOA)
36 #include "NetworkDataTaskCocoa.h"
37 #endif
38 #if USE(SOUP)
39 #include "NetworkDataTaskSoup.h"
40 #endif
41 #if USE(CURL)
42 #include "NetworkDataTaskCurl.h"
43 #endif
44
45 namespace WebKit {
46 using namespace WebCore;
47
48 Ref<NetworkDataTask> NetworkDataTask::create(NetworkSession& session, NetworkDataTaskClient& client, const NetworkLoadParameters& parameters)
49 {
50     ASSERT(!parameters.request.url().protocolIsBlob());
51 #if PLATFORM(COCOA)
52     return NetworkDataTaskCocoa::create(session, client, parameters.request, parameters.webFrameID, parameters.webPageID, parameters.storedCredentialsPolicy, parameters.contentSniffingPolicy, parameters.contentEncodingSniffingPolicy, parameters.shouldClearReferrerOnHTTPSToHTTPRedirect, parameters.shouldPreconnectOnly, parameters.isMainFrameNavigation, parameters.networkActivityTracker);
53 #endif
54 #if USE(SOUP)
55     return NetworkDataTaskSoup::create(session, client, parameters.request, parameters.storedCredentialsPolicy, parameters.contentSniffingPolicy, parameters.contentEncodingSniffingPolicy, parameters.shouldClearReferrerOnHTTPSToHTTPRedirect, parameters.isMainFrameNavigation);
56 #endif
57 #if USE(CURL)
58     return NetworkDataTaskCurl::create(session, client, parameters.request, parameters.storedCredentialsPolicy, parameters.contentSniffingPolicy, parameters.contentEncodingSniffingPolicy, parameters.shouldClearReferrerOnHTTPSToHTTPRedirect, parameters.isMainFrameNavigation);
59 #endif
60 }
61
62 NetworkDataTask::NetworkDataTask(NetworkSession& session, NetworkDataTaskClient& client, const ResourceRequest& requestWithCredentials, StoredCredentialsPolicy storedCredentialsPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation)
63     : m_failureTimer(*this, &NetworkDataTask::failureTimerFired)
64     , m_session(session)
65     , m_client(&client)
66     , m_partition(requestWithCredentials.cachePartition())
67     , m_storedCredentialsPolicy(storedCredentialsPolicy)
68     , m_lastHTTPMethod(requestWithCredentials.httpMethod())
69     , m_firstRequest(requestWithCredentials)
70     , m_shouldClearReferrerOnHTTPSToHTTPRedirect(shouldClearReferrerOnHTTPSToHTTPRedirect)
71     , m_dataTaskIsForMainFrameNavigation(dataTaskIsForMainFrameNavigation)
72 {
73     ASSERT(RunLoop::isMain());
74
75     if (!requestWithCredentials.url().isValid()) {
76         scheduleFailure(InvalidURLFailure);
77         return;
78     }
79
80     if (!portAllowed(requestWithCredentials.url())) {
81         scheduleFailure(BlockedFailure);
82         return;
83     }
84 }
85
86 NetworkDataTask::~NetworkDataTask()
87 {
88     ASSERT(RunLoop::isMain());
89     ASSERT(!m_client);
90 }
91
92 void NetworkDataTask::scheduleFailure(FailureType type)
93 {
94     ASSERT(type != NoFailure);
95     m_scheduledFailureType = type;
96     m_failureTimer.startOneShot(0_s);
97 }
98
99 void NetworkDataTask::didReceiveResponse(ResourceResponse&& response, ResponseCompletionHandler&& completionHandler)
100 {
101     ASSERT(m_client);
102     if (response.isHTTP09()) {
103         auto url = response.url();
104         Optional<uint16_t> port = url.port();
105         if (port && !WTF::isDefaultPortForProtocol(port.value(), url.protocol())) {
106             completionHandler(PolicyAction::Ignore);
107             cancel();
108             m_client->didCompleteWithError({ String(), 0, url, "Cancelled load from '" + url.stringCenterEllipsizedToLength() + "' because it is using HTTP/0.9." });
109             return;
110         }
111     }
112     m_client->didReceiveResponse(WTFMove(response), WTFMove(completionHandler));
113 }
114
115 bool NetworkDataTask::shouldCaptureExtraNetworkLoadMetrics() const
116 {
117     return m_client ? m_client->shouldCaptureExtraNetworkLoadMetrics() : false;
118 }
119
120 void NetworkDataTask::failureTimerFired()
121 {
122     RefPtr<NetworkDataTask> protectedThis(this);
123
124     switch (m_scheduledFailureType) {
125     case BlockedFailure:
126         m_scheduledFailureType = NoFailure;
127         if (m_client)
128             m_client->wasBlocked();
129         return;
130     case InvalidURLFailure:
131         m_scheduledFailureType = NoFailure;
132         if (m_client)
133             m_client->cannotShowURL();
134         return;
135     case NoFailure:
136         ASSERT_NOT_REACHED();
137         break;
138     }
139     ASSERT_NOT_REACHED();
140 }
141
142 String NetworkDataTask::description() const
143 {
144     return emptyString();
145 }
146
147 } // namespace WebKit