2 * Copyright (C) 2006, 2007, 2010, 2011 Apple Inc. All rights reserved.
3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/fetch/ResourceLoader.h"
33 #include "core/fetch/Resource.h"
34 #include "core/fetch/ResourceLoaderHost.h"
35 #include "core/fetch/ResourcePtr.h"
36 #include "platform/Logging.h"
37 #include "platform/SharedBuffer.h"
38 #include "platform/exported/WrappedResourceRequest.h"
39 #include "platform/exported/WrappedResourceResponse.h"
40 #include "platform/network/ResourceError.h"
41 #include "public/platform/Platform.h"
42 #include "public/platform/WebData.h"
43 #include "public/platform/WebThreadedDataReceiver.h"
44 #include "public/platform/WebURLError.h"
45 #include "public/platform/WebURLRequest.h"
46 #include "public/platform/WebURLResponse.h"
47 #include "wtf/Assertions.h"
48 #include "wtf/CurrentTime.h"
52 ResourceLoader::RequestCountTracker::RequestCountTracker(ResourceLoaderHost* host, Resource* resource)
54 , m_resource(resource)
56 m_host->incrementRequestCount(m_resource);
59 ResourceLoader::RequestCountTracker::~RequestCountTracker()
61 m_host->decrementRequestCount(m_resource);
64 ResourceLoader::RequestCountTracker::RequestCountTracker(const RequestCountTracker& other)
66 m_host = other.m_host;
67 m_resource = other.m_resource;
68 m_host->incrementRequestCount(m_resource);
71 PassRefPtrWillBeRawPtr<ResourceLoader> ResourceLoader::create(ResourceLoaderHost* host, Resource* resource, const ResourceRequest& request, const ResourceLoaderOptions& options)
73 RefPtrWillBeRawPtr<ResourceLoader> loader(adoptRefWillBeNoop(new ResourceLoader(host, resource, options)));
74 loader->init(request);
75 return loader.release();
78 ResourceLoader::ResourceLoader(ResourceLoaderHost* host, Resource* resource, const ResourceLoaderOptions& options)
80 , m_notifiedLoadComplete(false)
81 , m_defersLoading(host->defersLoading())
83 , m_resource(resource)
84 , m_state(Initialized)
85 , m_connectionState(ConnectionStateNew)
86 , m_requestCountTracker(adoptPtr(new RequestCountTracker(host, resource)))
90 ResourceLoader::~ResourceLoader()
92 ASSERT(m_state == Terminated);
95 void ResourceLoader::trace(Visitor* visitor)
97 visitor->trace(m_host);
98 visitor->trace(m_resource);
101 void ResourceLoader::releaseResources()
103 ASSERT(m_state != Terminated);
104 ASSERT(m_notifiedLoadComplete);
105 m_requestCountTracker.clear();
106 m_host->didLoadResource(m_resource);
107 if (m_state == Terminated)
109 m_resource->clearLoader();
110 m_resource->deleteIfPossible();
111 m_resource = nullptr;
112 m_host->willTerminateResourceLoader(this);
114 ASSERT(m_state != Terminated);
116 // It's possible that when we release the loader, it will be
117 // deallocated and release the last reference to this object.
118 // We need to retain to avoid accessing the object after it
119 // has been deallocated and also to avoid reentering this method.
120 RefPtrWillBeRawPtr<ResourceLoader> protector(this);
123 m_state = Terminated;
130 m_deferredRequest = ResourceRequest();
133 void ResourceLoader::init(const ResourceRequest& passedRequest)
135 ASSERT(m_state != Terminated);
136 ResourceRequest request(passedRequest);
137 m_host->willSendRequest(m_resource->identifier(), request, ResourceResponse(), m_options.initiatorInfo);
138 ASSERT(m_state != Terminated);
139 ASSERT(!request.isNull());
140 m_originalRequest = m_request = applyOptions(request);
141 m_resource->updateRequest(request);
142 ASSERT(m_state != Terminated);
143 m_host->didInitializeResourceLoader(this);
146 void ResourceLoader::start()
149 ASSERT(!m_request.isNull());
150 ASSERT(m_deferredRequest.isNull());
152 m_host->willStartLoadingResource(m_resource, m_request);
154 if (m_options.synchronousPolicy == RequestSynchronously) {
155 requestSynchronously();
159 if (m_defersLoading) {
160 m_deferredRequest = m_request;
164 if (m_state == Terminated)
167 RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
168 m_connectionState = ConnectionStateStarted;
170 m_loader = adoptPtr(blink::Platform::current()->createURLLoader());
172 blink::WrappedResourceRequest wrappedRequest(m_request);
173 m_loader->loadAsynchronously(wrappedRequest, this);
176 void ResourceLoader::changeToSynchronous()
178 ASSERT(m_options.synchronousPolicy == RequestAsynchronously);
182 m_request.setPriority(ResourceLoadPriorityHighest);
183 m_connectionState = ConnectionStateNew;
184 requestSynchronously();
187 void ResourceLoader::setDefersLoading(bool defers)
189 m_defersLoading = defers;
191 m_loader->setDefersLoading(defers);
192 if (!defers && !m_deferredRequest.isNull()) {
193 m_request = applyOptions(m_deferredRequest);
194 m_deferredRequest = ResourceRequest();
199 void ResourceLoader::attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver> threadedDataReceiver)
202 // The implementor of the WebURLLoader assumes ownership of the
203 // threaded data receiver if it signals that it got successfully
205 blink::WebThreadedDataReceiver* rawThreadedDataReceiver = threadedDataReceiver.leakPtr();
206 if (!m_loader->attachThreadedDataReceiver(rawThreadedDataReceiver))
207 delete rawThreadedDataReceiver;
211 void ResourceLoader::didDownloadData(blink::WebURLLoader*, int length, int encodedDataLength)
213 ASSERT(m_state != Terminated);
214 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
215 RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse);
216 m_host->didDownloadData(m_resource, length, encodedDataLength);
217 if (m_state == Terminated)
219 m_resource->didDownloadData(length);
222 void ResourceLoader::didFinishLoadingOnePart(double finishTime, int64 encodedDataLength)
224 // If load has been cancelled after finishing (which could happen with a
225 // JavaScript that changes the window location), do nothing.
226 if (m_state == Terminated)
229 if (m_notifiedLoadComplete)
231 m_notifiedLoadComplete = true;
232 m_host->didFinishLoading(m_resource, finishTime, encodedDataLength);
235 void ResourceLoader::didChangePriority(ResourceLoadPriority loadPriority, int intraPriorityValue)
238 m_host->didChangeLoadingPriority(m_resource, loadPriority, intraPriorityValue);
239 ASSERT(m_state != Terminated);
240 m_loader->didChangePriority(static_cast<blink::WebURLRequest::Priority>(loadPriority), intraPriorityValue);
244 void ResourceLoader::cancelIfNotFinishing()
246 if (m_state != Initialized)
251 void ResourceLoader::cancel()
253 cancel(ResourceError());
256 void ResourceLoader::cancel(const ResourceError& error)
258 // If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
259 if (m_state == Terminated)
261 if (m_state == Finishing) {
266 ResourceError nonNullError = error.isNull() ? ResourceError::cancelledError(m_request.url()) : error;
268 // This function calls out to clients at several points that might do
269 // something that causes the last reference to this object to go away.
270 RefPtrWillBeRawPtr<ResourceLoader> protector(this);
272 WTF_LOG(ResourceLoading, "Cancelled load of '%s'.\n", m_resource->url().string().latin1().data());
273 if (m_state == Initialized)
275 m_resource->setResourceError(nonNullError);
278 m_connectionState = ConnectionStateCanceled;
283 if (!m_notifiedLoadComplete) {
284 m_notifiedLoadComplete = true;
285 m_host->didFailLoading(m_resource, nonNullError);
288 if (m_state == Finishing)
289 m_resource->error(Resource::LoadError);
290 if (m_state != Terminated)
294 void ResourceLoader::willSendRequest(blink::WebURLLoader*, blink::WebURLRequest& passedRequest, const blink::WebURLResponse& passedRedirectResponse)
296 ASSERT(m_state != Terminated);
297 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
299 ResourceRequest& request(applyOptions(passedRequest.toMutableResourceRequest()));
301 // FIXME: We should have a real context for redirect requests. Currently, we don't: see WebURLLoaderImpl::Context::OnReceivedRedirect in content/.
302 request.setRequestContext(blink::WebURLRequest::RequestContextInternal);
304 ASSERT(!request.isNull());
305 const ResourceResponse& redirectResponse(passedRedirectResponse.toResourceResponse());
306 ASSERT(!redirectResponse.isNull());
307 if (!m_host->canAccessRedirect(m_resource, request, redirectResponse, m_options)) {
311 ASSERT(m_state != Terminated);
313 applyOptions(request); // canAccessRedirect() can modify m_options so we should re-apply it.
314 m_host->redirectReceived(m_resource, redirectResponse);
315 ASSERT(m_state != Terminated);
316 m_resource->willSendRequest(request, redirectResponse);
317 if (request.isNull() || m_state == Terminated)
320 m_host->willSendRequest(m_resource->identifier(), request, redirectResponse, m_options.initiatorInfo);
321 ASSERT(m_state != Terminated);
322 ASSERT(!request.isNull());
323 m_resource->updateRequest(request);
327 void ResourceLoader::didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length)
329 RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
330 ASSERT(m_state == Initialized);
331 m_resource->setSerializedCachedMetadata(data, length);
334 void ResourceLoader::didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
336 ASSERT(m_state == Initialized);
337 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
338 m_resource->didSendData(bytesSent, totalBytesToBeSent);
341 bool ResourceLoader::responseNeedsAccessControlCheck() const
343 // If the fetch was (potentially) CORS enabled, an access control check of the response is required.
344 return m_options.corsEnabled == IsCORSEnabled;
347 void ResourceLoader::didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse& response)
349 ASSERT(!response.isNull());
350 ASSERT(m_state == Initialized);
352 bool isMultipartPayload = response.isMultipartPayload();
353 bool isValidStateTransition = (m_connectionState == ConnectionStateStarted || m_connectionState == ConnectionStateReceivedResponse);
354 // In the case of multipart loads, calls to didReceiveData & didReceiveResponse can be interleaved.
355 RELEASE_ASSERT(isMultipartPayload || isValidStateTransition);
356 m_connectionState = ConnectionStateReceivedResponse;
358 const ResourceResponse& resourceResponse = response.toResourceResponse();
360 if (responseNeedsAccessControlCheck()) {
361 // If the response successfully validated a cached resource, perform
362 // the access control with respect to it. Need to do this right here
363 // before the resource switches clients over to that validated resource.
364 Resource* resource = m_resource;
365 if (resource->isCacheValidator() && resourceResponse.httpStatusCode() == 304)
366 resource = m_resource->resourceToRevalidate();
368 m_resource->setResponse(resourceResponse);
369 if (!m_host->canAccessResource(resource, m_options.securityOrigin.get(), response.url())) {
370 m_host->didReceiveResponse(m_resource, resourceResponse);
376 // Reference the object in this method since the additional processing can do
377 // anything including removing the last reference to this object.
378 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
379 m_resource->responseReceived(resourceResponse);
380 if (m_state == Terminated)
383 m_host->didReceiveResponse(m_resource, resourceResponse);
384 if (m_state == Terminated)
387 if (response.toResourceResponse().isMultipart()) {
388 // We don't count multiParts in a ResourceFetcher's request count
389 m_requestCountTracker.clear();
390 if (!m_resource->isImage()) {
394 } else if (isMultipartPayload) {
395 // Since a subresource loader does not load multipart sections progressively, data was delivered to the loader all at once.
396 // After the first multipart section is complete, signal to delegates that this load is "finished"
397 m_host->subresourceLoaderFinishedLoadingOnePart(this);
398 ASSERT(m_state != Terminated);
399 didFinishLoadingOnePart(0, blink::WebURLLoaderClient::kUnknownEncodedDataLength);
401 if (m_state == Terminated)
404 if (m_resource->response().httpStatusCode() < 400 || m_resource->shouldIgnoreHTTPStatusCodeErrors())
408 if (!m_notifiedLoadComplete) {
409 m_notifiedLoadComplete = true;
410 m_host->didFailLoading(m_resource, ResourceError::cancelledError(m_request.url()));
413 ASSERT(m_state != Terminated);
414 m_resource->error(Resource::LoadError);
418 void ResourceLoader::didReceiveData(blink::WebURLLoader*, const char* data, int length, int encodedDataLength)
420 ASSERT(m_state != Terminated);
421 RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
422 m_connectionState = ConnectionStateReceivingData;
424 // It is possible to receive data on uninitialized resources if it had an error status code, and we are running a nested message
425 // loop. When this occurs, ignoring the data is the correct action.
426 if (m_resource->response().httpStatusCode() >= 400 && !m_resource->shouldIgnoreHTTPStatusCodeErrors())
428 ASSERT(m_state == Initialized);
430 // Reference the object in this method since the additional processing can do
431 // anything including removing the last reference to this object.
432 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
434 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
435 // However, with today's computers and networking speeds, this won't happen in practice.
436 // Could be an issue with a giant local file.
437 m_host->didReceiveData(m_resource, data, length, encodedDataLength);
438 if (m_state == Terminated)
440 m_resource->appendData(data, length);
443 void ResourceLoader::didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength)
445 RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
446 m_connectionState = ConnectionStateFinishedLoading;
447 if (m_state != Initialized)
449 ASSERT(m_state != Terminated);
450 WTF_LOG(ResourceLoading, "Received '%s'.", m_resource->url().string().latin1().data());
452 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
453 ResourcePtr<Resource> protectResource(m_resource);
455 didFinishLoadingOnePart(finishTime, encodedDataLength);
456 if (m_state == Terminated)
458 m_resource->finish(finishTime);
460 // If the load has been cancelled by a delegate in response to didFinishLoad(), do not release
461 // the resources a second time, they have been released by cancel.
462 if (m_state == Terminated)
467 void ResourceLoader::didFail(blink::WebURLLoader*, const blink::WebURLError& error)
469 m_connectionState = ConnectionStateFailed;
470 ASSERT(m_state != Terminated);
471 WTF_LOG(ResourceLoading, "Failed to load '%s'.\n", m_resource->url().string().latin1().data());
473 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
474 RefPtrWillBeRawPtr<ResourceLoaderHost> protectHost(m_host.get());
475 ResourcePtr<Resource> protectResource(m_resource);
477 m_resource->setResourceError(error);
479 if (!m_notifiedLoadComplete) {
480 m_notifiedLoadComplete = true;
481 m_host->didFailLoading(m_resource, error);
483 if (m_state == Terminated)
486 m_resource->error(Resource::LoadError);
488 if (m_state == Terminated)
494 bool ResourceLoader::isLoadedBy(ResourceLoaderHost* loader) const
496 return m_host->isLoadedBy(loader);
499 void ResourceLoader::requestSynchronously()
501 OwnPtr<blink::WebURLLoader> loader = adoptPtr(blink::Platform::current()->createURLLoader());
504 // downloadToFile is not supported for synchronous requests.
505 ASSERT(!m_request.downloadToFile());
507 RefPtrWillBeRawPtr<ResourceLoader> protect(this);
508 RefPtrWillBeRawPtr<ResourceLoaderHost> protectHost(m_host.get());
509 ResourcePtr<Resource> protectResource(m_resource);
511 RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
512 m_connectionState = ConnectionStateStarted;
514 blink::WrappedResourceRequest requestIn(m_request);
515 blink::WebURLResponse responseOut;
516 responseOut.initialize();
517 blink::WebURLError errorOut;
518 blink::WebData dataOut;
519 loader->loadSynchronously(requestIn, responseOut, errorOut, dataOut);
520 if (errorOut.reason) {
521 didFail(0, errorOut);
524 didReceiveResponse(0, responseOut);
525 if (m_state == Terminated)
527 RefPtr<ResourceLoadInfo> resourceLoadInfo = responseOut.toResourceResponse().resourceLoadInfo();
528 int64 encodedDataLength = resourceLoadInfo ? resourceLoadInfo->encodedDataLength : blink::WebURLLoaderClient::kUnknownEncodedDataLength;
529 m_host->didReceiveData(m_resource, dataOut.data(), dataOut.size(), encodedDataLength);
530 m_resource->setResourceBuffer(dataOut);
531 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength);
534 ResourceRequest& ResourceLoader::applyOptions(ResourceRequest& request) const
536 request.setAllowStoredCredentials(m_options.allowCredentials == AllowStoredCredentials);