Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / fetch / ResourceLoader.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2010, 2011 Apple Inc. All rights reserved.
3  *           (C) 2007 Graham Dennis (graham.dennis@gmail.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
17  *
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.
28  */
29
30 #include "config.h"
31 #include "core/fetch/ResourceLoader.h"
32
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"
49
50 namespace blink {
51
52 ResourceLoader::RequestCountTracker::RequestCountTracker(ResourceLoaderHost* host, Resource* resource)
53     : m_host(host)
54     , m_resource(resource)
55 {
56     m_host->incrementRequestCount(m_resource);
57 }
58
59 ResourceLoader::RequestCountTracker::~RequestCountTracker()
60 {
61     m_host->decrementRequestCount(m_resource);
62 }
63
64 ResourceLoader::RequestCountTracker::RequestCountTracker(const RequestCountTracker& other)
65 {
66     m_host = other.m_host;
67     m_resource = other.m_resource;
68     m_host->incrementRequestCount(m_resource);
69 }
70
71 PassRefPtrWillBeRawPtr<ResourceLoader> ResourceLoader::create(ResourceLoaderHost* host, Resource* resource, const ResourceRequest& request, const ResourceLoaderOptions& options)
72 {
73     RefPtrWillBeRawPtr<ResourceLoader> loader(adoptRefWillBeNoop(new ResourceLoader(host, resource, options)));
74     loader->init(request);
75     return loader.release();
76 }
77
78 ResourceLoader::ResourceLoader(ResourceLoaderHost* host, Resource* resource, const ResourceLoaderOptions& options)
79     : m_host(host)
80     , m_notifiedLoadComplete(false)
81     , m_defersLoading(host->defersLoading())
82     , m_options(options)
83     , m_resource(resource)
84     , m_state(Initialized)
85     , m_connectionState(ConnectionStateNew)
86     , m_requestCountTracker(adoptPtr(new RequestCountTracker(host, resource)))
87 {
88 }
89
90 ResourceLoader::~ResourceLoader()
91 {
92     ASSERT(m_state == Terminated);
93 }
94
95 void ResourceLoader::trace(Visitor* visitor)
96 {
97     visitor->trace(m_host);
98     visitor->trace(m_resource);
99 }
100
101 void ResourceLoader::releaseResources()
102 {
103     ASSERT(m_state != Terminated);
104     ASSERT(m_notifiedLoadComplete);
105     m_requestCountTracker.clear();
106     m_host->didLoadResource(m_resource);
107     if (m_state == Terminated)
108         return;
109     m_resource->clearLoader();
110     m_resource->deleteIfPossible();
111     m_resource = nullptr;
112     m_host->willTerminateResourceLoader(this);
113
114     ASSERT(m_state != Terminated);
115
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);
121
122     m_host.clear();
123     m_state = Terminated;
124
125     if (m_loader) {
126         m_loader->cancel();
127         m_loader.clear();
128     }
129
130     m_deferredRequest = ResourceRequest();
131 }
132
133 void ResourceLoader::init(const ResourceRequest& passedRequest)
134 {
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);
144 }
145
146 void ResourceLoader::start()
147 {
148     ASSERT(!m_loader);
149     ASSERT(!m_request.isNull());
150     ASSERT(m_deferredRequest.isNull());
151
152     m_host->willStartLoadingResource(m_resource, m_request);
153
154     if (m_options.synchronousPolicy == RequestSynchronously) {
155         requestSynchronously();
156         return;
157     }
158
159     if (m_defersLoading) {
160         m_deferredRequest = m_request;
161         return;
162     }
163
164     if (m_state == Terminated)
165         return;
166
167     RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
168     m_connectionState = ConnectionStateStarted;
169
170     m_loader = adoptPtr(blink::Platform::current()->createURLLoader());
171     ASSERT(m_loader);
172     blink::WrappedResourceRequest wrappedRequest(m_request);
173     m_loader->loadAsynchronously(wrappedRequest, this);
174 }
175
176 void ResourceLoader::changeToSynchronous()
177 {
178     ASSERT(m_options.synchronousPolicy == RequestAsynchronously);
179     ASSERT(m_loader);
180     m_loader->cancel();
181     m_loader.clear();
182     m_request.setPriority(ResourceLoadPriorityHighest);
183     m_connectionState = ConnectionStateNew;
184     requestSynchronously();
185 }
186
187 void ResourceLoader::setDefersLoading(bool defers)
188 {
189     m_defersLoading = defers;
190     if (m_loader)
191         m_loader->setDefersLoading(defers);
192     if (!defers && !m_deferredRequest.isNull()) {
193         m_request = applyOptions(m_deferredRequest);
194         m_deferredRequest = ResourceRequest();
195         start();
196     }
197 }
198
199 void ResourceLoader::attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver> threadedDataReceiver)
200 {
201     if (m_loader) {
202         // The implementor of the WebURLLoader assumes ownership of the
203         // threaded data receiver if it signals that it got successfully
204         // attached.
205         blink::WebThreadedDataReceiver* rawThreadedDataReceiver = threadedDataReceiver.leakPtr();
206         if (!m_loader->attachThreadedDataReceiver(rawThreadedDataReceiver))
207             delete rawThreadedDataReceiver;
208     }
209 }
210
211 void ResourceLoader::didDownloadData(blink::WebURLLoader*, int length, int encodedDataLength)
212 {
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)
218         return;
219     m_resource->didDownloadData(length);
220 }
221
222 void ResourceLoader::didFinishLoadingOnePart(double finishTime, int64 encodedDataLength)
223 {
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)
227         return;
228
229     if (m_notifiedLoadComplete)
230         return;
231     m_notifiedLoadComplete = true;
232     m_host->didFinishLoading(m_resource, finishTime, encodedDataLength);
233 }
234
235 void ResourceLoader::didChangePriority(ResourceLoadPriority loadPriority, int intraPriorityValue)
236 {
237     if (m_loader) {
238         m_host->didChangeLoadingPriority(m_resource, loadPriority, intraPriorityValue);
239         ASSERT(m_state != Terminated);
240         m_loader->didChangePriority(static_cast<blink::WebURLRequest::Priority>(loadPriority), intraPriorityValue);
241     }
242 }
243
244 void ResourceLoader::cancelIfNotFinishing()
245 {
246     if (m_state != Initialized)
247         return;
248     cancel();
249 }
250
251 void ResourceLoader::cancel()
252 {
253     cancel(ResourceError());
254 }
255
256 void ResourceLoader::cancel(const ResourceError& error)
257 {
258     // If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
259     if (m_state == Terminated)
260         return;
261     if (m_state == Finishing) {
262         releaseResources();
263         return;
264     }
265
266     ResourceError nonNullError = error.isNull() ? ResourceError::cancelledError(m_request.url()) : error;
267
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);
271
272     WTF_LOG(ResourceLoading, "Cancelled load of '%s'.\n", m_resource->url().string().latin1().data());
273     if (m_state == Initialized)
274         m_state = Finishing;
275     m_resource->setResourceError(nonNullError);
276
277     if (m_loader) {
278         m_connectionState = ConnectionStateCanceled;
279         m_loader->cancel();
280         m_loader.clear();
281     }
282
283     if (!m_notifiedLoadComplete) {
284         m_notifiedLoadComplete = true;
285         m_host->didFailLoading(m_resource, nonNullError);
286     }
287
288     if (m_state == Finishing)
289         m_resource->error(Resource::LoadError);
290     if (m_state != Terminated)
291         releaseResources();
292 }
293
294 void ResourceLoader::willSendRequest(blink::WebURLLoader*, blink::WebURLRequest& passedRequest, const blink::WebURLResponse& passedRedirectResponse)
295 {
296     ASSERT(m_state != Terminated);
297     RefPtrWillBeRawPtr<ResourceLoader> protect(this);
298
299     ResourceRequest& request(applyOptions(passedRequest.toMutableResourceRequest()));
300
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);
303
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)) {
308         cancel();
309         return;
310     }
311     ASSERT(m_state != Terminated);
312
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)
318         return;
319
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);
324     m_request = request;
325 }
326
327 void ResourceLoader::didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length)
328 {
329     RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
330     ASSERT(m_state == Initialized);
331     m_resource->setSerializedCachedMetadata(data, length);
332 }
333
334 void ResourceLoader::didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
335 {
336     ASSERT(m_state == Initialized);
337     RefPtrWillBeRawPtr<ResourceLoader> protect(this);
338     m_resource->didSendData(bytesSent, totalBytesToBeSent);
339 }
340
341 bool ResourceLoader::responseNeedsAccessControlCheck() const
342 {
343     // If the fetch was (potentially) CORS enabled, an access control check of the response is required.
344     return m_options.corsEnabled == IsCORSEnabled;
345 }
346
347 void ResourceLoader::didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse& response)
348 {
349     ASSERT(!response.isNull());
350     ASSERT(m_state == Initialized);
351
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;
357
358     const ResourceResponse& resourceResponse = response.toResourceResponse();
359
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();
367         else
368             m_resource->setResponse(resourceResponse);
369         if (!m_host->canAccessResource(resource, m_options.securityOrigin.get(), response.url())) {
370             m_host->didReceiveResponse(m_resource, resourceResponse);
371             cancel();
372             return;
373         }
374     }
375
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)
381         return;
382
383     m_host->didReceiveResponse(m_resource, resourceResponse);
384     if (m_state == Terminated)
385         return;
386
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()) {
391             cancel();
392             return;
393         }
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);
400     }
401     if (m_state == Terminated)
402         return;
403
404     if (m_resource->response().httpStatusCode() < 400 || m_resource->shouldIgnoreHTTPStatusCodeErrors())
405         return;
406     m_state = Finishing;
407
408     if (!m_notifiedLoadComplete) {
409         m_notifiedLoadComplete = true;
410         m_host->didFailLoading(m_resource, ResourceError::cancelledError(m_request.url()));
411     }
412
413     ASSERT(m_state != Terminated);
414     m_resource->error(Resource::LoadError);
415     cancel();
416 }
417
418 void ResourceLoader::didReceiveData(blink::WebURLLoader*, const char* data, int length, int encodedDataLength)
419 {
420     ASSERT(m_state != Terminated);
421     RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
422     m_connectionState = ConnectionStateReceivingData;
423
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())
427         return;
428     ASSERT(m_state == Initialized);
429
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);
433
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)
439         return;
440     m_resource->appendData(data, length);
441 }
442
443 void ResourceLoader::didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength)
444 {
445     RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
446     m_connectionState = ConnectionStateFinishedLoading;
447     if (m_state != Initialized)
448         return;
449     ASSERT(m_state != Terminated);
450     WTF_LOG(ResourceLoading, "Received '%s'.", m_resource->url().string().latin1().data());
451
452     RefPtrWillBeRawPtr<ResourceLoader> protect(this);
453     ResourcePtr<Resource> protectResource(m_resource);
454     m_state = Finishing;
455     didFinishLoadingOnePart(finishTime, encodedDataLength);
456     if (m_state == Terminated)
457         return;
458     m_resource->finish(finishTime);
459
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)
463         return;
464     releaseResources();
465 }
466
467 void ResourceLoader::didFail(blink::WebURLLoader*, const blink::WebURLError& error)
468 {
469     m_connectionState = ConnectionStateFailed;
470     ASSERT(m_state != Terminated);
471     WTF_LOG(ResourceLoading, "Failed to load '%s'.\n", m_resource->url().string().latin1().data());
472
473     RefPtrWillBeRawPtr<ResourceLoader> protect(this);
474     RefPtrWillBeRawPtr<ResourceLoaderHost> protectHost(m_host.get());
475     ResourcePtr<Resource> protectResource(m_resource);
476     m_state = Finishing;
477     m_resource->setResourceError(error);
478
479     if (!m_notifiedLoadComplete) {
480         m_notifiedLoadComplete = true;
481         m_host->didFailLoading(m_resource, error);
482     }
483     if (m_state == Terminated)
484         return;
485
486     m_resource->error(Resource::LoadError);
487
488     if (m_state == Terminated)
489         return;
490
491     releaseResources();
492 }
493
494 bool ResourceLoader::isLoadedBy(ResourceLoaderHost* loader) const
495 {
496     return m_host->isLoadedBy(loader);
497 }
498
499 void ResourceLoader::requestSynchronously()
500 {
501     OwnPtr<blink::WebURLLoader> loader = adoptPtr(blink::Platform::current()->createURLLoader());
502     ASSERT(loader);
503
504     // downloadToFile is not supported for synchronous requests.
505     ASSERT(!m_request.downloadToFile());
506
507     RefPtrWillBeRawPtr<ResourceLoader> protect(this);
508     RefPtrWillBeRawPtr<ResourceLoaderHost> protectHost(m_host.get());
509     ResourcePtr<Resource> protectResource(m_resource);
510
511     RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
512     m_connectionState = ConnectionStateStarted;
513
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);
522         return;
523     }
524     didReceiveResponse(0, responseOut);
525     if (m_state == Terminated)
526         return;
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);
532 }
533
534 ResourceRequest& ResourceLoader::applyOptions(ResourceRequest& request) const
535 {
536     request.setAllowStoredCredentials(m_options.allowCredentials == AllowStoredCredentials);
537     return request;
538 }
539
540 }