Eliminate ResourceBuffer and use SharedBuffer directly instead
[WebKit-https.git] / Source / WebKit2 / WebProcess / Network / WebResourceLoadScheduler.cpp
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 "WebResourceLoadScheduler.h"
28
29 #include "Logging.h"
30 #include "NetworkConnectionToWebProcessMessages.h"
31 #include "NetworkProcessConnection.h"
32 #include "NetworkResourceLoadParameters.h"
33 #include "SessionTracker.h"
34 #include "WebCoreArgumentCoders.h"
35 #include "WebErrors.h"
36 #include "WebFrame.h"
37 #include "WebFrameLoaderClient.h"
38 #include "WebPage.h"
39 #include "WebProcess.h"
40 #include "WebResourceLoader.h"
41 #include <WebCore/ApplicationCacheHost.h>
42 #include <WebCore/CachedResource.h>
43 #include <WebCore/Document.h>
44 #include <WebCore/DocumentLoader.h>
45 #include <WebCore/Frame.h>
46 #include <WebCore/FrameLoader.h>
47 #include <WebCore/NetscapePlugInStreamLoader.h>
48 #include <WebCore/ReferrerPolicy.h>
49 #include <WebCore/ResourceLoader.h>
50 #include <WebCore/SessionID.h>
51 #include <WebCore/Settings.h>
52 #include <WebCore/SubresourceLoader.h>
53 #include <wtf/text/CString.h>
54
55 #if ENABLE(NETWORK_PROCESS)
56
57 using namespace WebCore;
58
59 namespace WebKit {
60
61 WebResourceLoadScheduler::WebResourceLoadScheduler()
62     : m_internallyFailedLoadTimer(RunLoop::main(), this, &WebResourceLoadScheduler::internallyFailedLoadTimerFired)
63     , m_suspendPendingRequestsCount(0)
64 {
65 }
66
67 WebResourceLoadScheduler::~WebResourceLoadScheduler()
68 {
69 }
70
71 PassRefPtr<SubresourceLoader> WebResourceLoadScheduler::scheduleSubresourceLoad(Frame* frame, CachedResource* resource, const ResourceRequest& request, const ResourceLoaderOptions& options)
72 {
73     RefPtr<SubresourceLoader> loader = SubresourceLoader::create(frame, resource, request, options);
74     if (loader)
75         scheduleLoad(loader.get(), resource, frame->document()->referrerPolicy() == ReferrerPolicyDefault);
76     return loader.release();
77 }
78
79 PassRefPtr<NetscapePlugInStreamLoader> WebResourceLoadScheduler::schedulePluginStreamLoad(Frame* frame, NetscapePlugInStreamLoaderClient* client, const ResourceRequest& request)
80 {
81     RefPtr<NetscapePlugInStreamLoader> loader = NetscapePlugInStreamLoader::create(frame, client, request);
82     if (loader)
83         scheduleLoad(loader.get(), 0, frame->document()->referrerPolicy() == ReferrerPolicyDefault);
84     return loader.release();
85 }
86
87 static std::chrono::milliseconds maximumBufferingTime(CachedResource* resource)
88 {
89     if (!resource)
90         return 0_ms;
91
92     switch (resource->type()) {
93     case CachedResource::CSSStyleSheet:
94     case CachedResource::Script:
95     case CachedResource::FontResource:
96         return std::chrono::milliseconds::max();
97     case CachedResource::ImageResource:
98         return 500_ms;
99     case CachedResource::MainResource:
100     case CachedResource::RawResource:
101     case CachedResource::SVGDocumentResource:
102 #if ENABLE(LINK_PREFETCH)
103     case CachedResource::LinkPrefetch:
104     case CachedResource::LinkSubresource:
105 #endif
106 #if ENABLE(VIDEO_TRACK)
107     case CachedResource::TextTrackResource:
108 #endif
109 #if ENABLE(XSLT)
110     case CachedResource::XSLStyleSheet:
111 #endif
112         return 0_ms;
113     }
114
115     ASSERT_NOT_REACHED();
116     return 0_ms;
117 }
118
119 void WebResourceLoadScheduler::scheduleLoad(ResourceLoader* resourceLoader, CachedResource* resource, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
120 {
121     ASSERT(resourceLoader);
122
123     ResourceLoadIdentifier identifier = resourceLoader->identifier();
124     ASSERT(identifier);
125
126 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
127     // If the DocumentLoader schedules this as an archive resource load,
128     // then we should remember the ResourceLoader in our records but not schedule it in the NetworkProcess.
129     if (resourceLoader->documentLoader()->scheduleArchiveLoad(resourceLoader, resourceLoader->request())) {
130         LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be handled as an archive resource.", resourceLoader->url().string().utf8().data());
131         m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
132         return;
133     }
134 #endif
135
136     if (resourceLoader->documentLoader()->applicationCacheHost()->maybeLoadResource(resourceLoader, resourceLoader->request(), resourceLoader->request().url())) {
137         LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be loaded from application cache.", resourceLoader->url().string().utf8().data());
138         m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
139         return;
140     }
141
142 #if USE(QUICK_LOOK)
143     if (maybeLoadQuickLookResource(*resourceLoader)) {
144         LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be handled as a QuickLook resource.", resourceLoader->url().string().utf8().data());
145         m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
146         return;
147     }
148 #endif
149
150     LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be scheduled with the NetworkProcess with priority %i", resourceLoader->url().string().utf8().data(), resourceLoader->request().priority());
151
152     ContentSniffingPolicy contentSniffingPolicy = resourceLoader->shouldSniffContent() ? SniffContent : DoNotSniffContent;
153     StoredCredentials allowStoredCredentials = resourceLoader->shouldUseCredentialStorage() ? AllowStoredCredentials : DoNotAllowStoredCredentials;
154
155     // FIXME: Some entities in WebCore use WebCore's "EmptyFrameLoaderClient" instead of having a proper WebFrameLoaderClient.
156     // EmptyFrameLoaderClient shouldn't exist and everything should be using a WebFrameLoaderClient,
157     // but in the meantime we have to make sure not to mis-cast.
158     WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(resourceLoader->frameLoader()->client());
159     WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
160     WebPage* webPage = webFrame ? webFrame->page() : 0;
161
162     NetworkResourceLoadParameters loadParameters;
163     loadParameters.identifier = identifier;
164     loadParameters.webPageID = webPage ? webPage->pageID() : 0;
165     loadParameters.webFrameID = webFrame ? webFrame->frameID() : 0;
166     loadParameters.sessionID = webPage ? webPage->sessionID() : SessionID::defaultSessionID();
167     loadParameters.request = resourceLoader->request();
168     loadParameters.contentSniffingPolicy = contentSniffingPolicy;
169     loadParameters.allowStoredCredentials = allowStoredCredentials;
170     // If there is no WebFrame then this resource cannot be authenticated with the client.
171     loadParameters.clientCredentialPolicy = (webFrame && webPage && resourceLoader->isAllowedToAskUserForCredentials()) ? AskClientForAllCredentials : DoNotAskClientForAnyCredentials;
172     loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = shouldClearReferrerOnHTTPSToHTTPRedirect;
173     loadParameters.isMainResource = resource && resource->type() == CachedResource::MainResource;
174     loadParameters.defersLoading = resourceLoader->defersLoading();
175     loadParameters.needsCertificateInfo = resourceLoader->shouldIncludeCertificateInfo();
176     loadParameters.maximumBufferingTime = maximumBufferingTime(resource);
177
178     ASSERT((loadParameters.webPageID && loadParameters.webFrameID) || loadParameters.clientCredentialPolicy == DoNotAskClientForAnyCredentials);
179
180     if (!WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::ScheduleResourceLoad(loadParameters), 0)) {
181         // We probably failed to schedule this load with the NetworkProcess because it had crashed.
182         // This load will never succeed so we will schedule it to fail asynchronously.
183         scheduleInternallyFailedLoad(resourceLoader);
184         return;
185     }
186     
187     m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
188     
189     notifyDidScheduleResourceRequest(resourceLoader);
190 }
191
192 void WebResourceLoadScheduler::scheduleInternallyFailedLoad(WebCore::ResourceLoader* resourceLoader)
193 {
194     m_internallyFailedResourceLoaders.add(resourceLoader);
195     m_internallyFailedLoadTimer.startOneShot(0);
196 }
197
198 void WebResourceLoadScheduler::internallyFailedLoadTimerFired()
199 {
200     Vector<RefPtr<ResourceLoader>> internallyFailedResourceLoaders;
201     copyToVector(m_internallyFailedResourceLoaders, internallyFailedResourceLoaders);
202     
203     for (size_t i = 0; i < internallyFailedResourceLoaders.size(); ++i)
204         internallyFailedResourceLoaders[i]->didFail(internalError(internallyFailedResourceLoaders[i]->url()));
205 }
206
207 void WebResourceLoadScheduler::remove(ResourceLoader* resourceLoader)
208 {
209     ASSERT(resourceLoader);
210     LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::remove, url '%s'", resourceLoader->url().string().utf8().data());
211
212     if (m_internallyFailedResourceLoaders.contains(resourceLoader)) {
213         m_internallyFailedResourceLoaders.remove(resourceLoader);
214         return;
215     }
216     
217     ResourceLoadIdentifier identifier = resourceLoader->identifier();
218     if (!identifier) {
219         LOG_ERROR("WebResourceLoadScheduler removing a ResourceLoader that has no identifier.");
220         return;
221     }
222     
223     RefPtr<WebResourceLoader> loader = m_webResourceLoaders.take(identifier);
224     // Loader may not be registered if we created it, but haven't scheduled yet (a bundle client can decide to cancel such request via willSendRequest).
225     if (!loader)
226         return;
227
228     WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::RemoveLoadIdentifier(identifier), 0);
229
230     // It's possible that this WebResourceLoader might be just about to message back to the NetworkProcess (e.g. ContinueWillSendRequest)
231     // but there's no point in doing so anymore.
232     loader->detachFromCoreLoader();
233 }
234
235 void WebResourceLoadScheduler::setDefersLoading(ResourceLoader* resourceLoader, bool defers)
236 {
237     ResourceLoadIdentifier identifier = resourceLoader->identifier();
238     WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::SetDefersLoading(identifier, defers), 0);
239 }
240
241 void WebResourceLoadScheduler::crossOriginRedirectReceived(ResourceLoader*, const URL&)
242 {
243     // We handle cross origin redirects entirely within the NetworkProcess.
244     // We override this call in the WebProcess to make it a no-op.
245 }
246
247 void WebResourceLoadScheduler::servePendingRequests(ResourceLoadPriority minimumPriority)
248 {
249     LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::servePendingRequests");
250     
251     // The NetworkProcess scheduler is good at making sure loads are serviced until there are no more pending requests.
252     // If this WebProcess isn't expecting requests to be served then we can ignore messaging the NetworkProcess right now.
253     if (m_suspendPendingRequestsCount)
254         return;
255
256     WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::ServePendingRequests(minimumPriority), 0);
257 }
258
259 void WebResourceLoadScheduler::suspendPendingRequests()
260 {
261     ++m_suspendPendingRequestsCount;
262 }
263
264 void WebResourceLoadScheduler::resumePendingRequests()
265 {
266     ASSERT(m_suspendPendingRequestsCount);
267     --m_suspendPendingRequestsCount;
268 }
269
270 void WebResourceLoadScheduler::setSerialLoadingEnabled(bool enabled)
271 {
272     WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::SetSerialLoadingEnabled(enabled), Messages::NetworkConnectionToWebProcess::SetSerialLoadingEnabled::Reply(), 0);
273 }
274
275 void WebResourceLoadScheduler::networkProcessCrashed()
276 {
277     HashMap<unsigned long, RefPtr<WebResourceLoader>>::iterator end = m_webResourceLoaders.end();
278     for (HashMap<unsigned long, RefPtr<WebResourceLoader>>::iterator i = m_webResourceLoaders.begin(); i != end; ++i)
279         scheduleInternallyFailedLoad(i->value.get()->resourceLoader());
280
281     m_webResourceLoaders.clear();
282 }
283
284 } // namespace WebKit
285
286 #endif // ENABLE(NETWORK_PROCESS)