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