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 "ResourceLoader.h"
32
33 #include "ApplicationCacheHost.h"
34 #include "DocumentLoader.h"
35 #include "FileStreamProxy.h"
36 #include "Frame.h"
37 #include "FrameLoader.h"
38 #include "FrameLoaderClient.h"
39 #include "InspectorInstrumentation.h"
40 #include "Page.h"
41 #include "ProgressTracker.h"
42 #include "ResourceError.h"
43 #include "ResourceHandle.h"
44 #include "ResourceLoadScheduler.h"
45 #include "Settings.h"
46 #include "SharedBuffer.h"
47
48 namespace WebCore {
49
resourceData()50 PassRefPtr<SharedBuffer> ResourceLoader::resourceData()
51 {
52 if (m_resourceData)
53 return m_resourceData;
54
55 if (ResourceHandle::supportsBufferedData() && m_handle)
56 return m_handle->bufferedData();
57
58 return 0;
59 }
60
ResourceLoader(Frame * frame,bool sendResourceLoadCallbacks,bool shouldContentSniff)61 ResourceLoader::ResourceLoader(Frame* frame, bool sendResourceLoadCallbacks, bool shouldContentSniff)
62 : m_frame(frame)
63 , m_documentLoader(frame->loader()->activeDocumentLoader())
64 , m_identifier(0)
65 , m_reachedTerminalState(false)
66 , m_cancelled(false)
67 , m_calledDidFinishLoad(false)
68 , m_sendResourceLoadCallbacks(sendResourceLoadCallbacks)
69 , m_shouldContentSniff(shouldContentSniff)
70 , m_shouldBufferData(true)
71 , m_defersLoading(frame->page()->defersLoading())
72 {
73 }
74
~ResourceLoader()75 ResourceLoader::~ResourceLoader()
76 {
77 ASSERT(m_reachedTerminalState);
78 }
79
releaseResources()80 void ResourceLoader::releaseResources()
81 {
82 ASSERT(!m_reachedTerminalState);
83
84 // It's possible that when we release the handle, it will be
85 // deallocated and release the last reference to this object.
86 // We need to retain to avoid accessing the object after it
87 // has been deallocated and also to avoid reentering this method.
88 RefPtr<ResourceLoader> protector(this);
89
90 m_frame = 0;
91 m_documentLoader = 0;
92
93 // We need to set reachedTerminalState to true before we release
94 // the resources to prevent a double dealloc of WebView <rdar://problem/4372628>
95 m_reachedTerminalState = true;
96
97 m_identifier = 0;
98
99 resourceLoadScheduler()->remove(this);
100
101 if (m_handle) {
102 // Clear out the ResourceHandle's client so that it doesn't try to call
103 // us back after we release it, unless it has been replaced by someone else.
104 if (m_handle->client() == this)
105 m_handle->setClient(0);
106 m_handle = 0;
107 }
108
109 m_resourceData = 0;
110 m_deferredRequest = ResourceRequest();
111 }
112
init(const ResourceRequest & r)113 bool ResourceLoader::init(const ResourceRequest& r)
114 {
115 ASSERT(!m_handle);
116 ASSERT(m_request.isNull());
117 ASSERT(m_deferredRequest.isNull());
118 ASSERT(!m_documentLoader->isSubstituteLoadPending(this));
119
120 ResourceRequest clientRequest(r);
121
122 // https://bugs.webkit.org/show_bug.cgi?id=26391
123 // The various plug-in implementations call directly to ResourceLoader::load() instead of piping requests
124 // through FrameLoader. As a result, they miss the FrameLoader::addExtraFieldsToRequest() step which sets
125 // up the 1st party for cookies URL. Until plug-in implementations can be reigned in to pipe through that
126 // method, we need to make sure there is always a 1st party for cookies set.
127 if (clientRequest.firstPartyForCookies().isNull()) {
128 if (Document* document = m_frame->document())
129 clientRequest.setFirstPartyForCookies(document->firstPartyForCookies());
130 }
131
132 willSendRequest(clientRequest, ResourceResponse());
133 if (clientRequest.isNull()) {
134 didFail(frameLoader()->cancelledError(m_request));
135 return false;
136 }
137
138 m_request = clientRequest;
139 return true;
140 }
141
start()142 void ResourceLoader::start()
143 {
144 ASSERT(!m_handle);
145 ASSERT(!m_request.isNull());
146 ASSERT(m_deferredRequest.isNull());
147
148 #if ENABLE(WEB_ARCHIVE)
149 if (m_documentLoader->scheduleArchiveLoad(this, m_request, m_request.url()))
150 return;
151 #endif
152
153 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
154 if (m_documentLoader->applicationCacheHost()->maybeLoadResource(this, m_request, m_request.url()))
155 return;
156 #endif
157
158 if (m_defersLoading) {
159 m_deferredRequest = m_request;
160 return;
161 }
162
163 if (!m_reachedTerminalState)
164 m_handle = ResourceHandle::create(m_frame->loader()->networkingContext(), m_request, this, m_defersLoading, m_shouldContentSniff);
165 }
166
setDefersLoading(bool defers)167 void ResourceLoader::setDefersLoading(bool defers)
168 {
169 m_defersLoading = defers;
170 if (m_handle)
171 m_handle->setDefersLoading(defers);
172 if (!defers && !m_deferredRequest.isNull()) {
173 m_request = m_deferredRequest;
174 m_deferredRequest = ResourceRequest();
175 start();
176 }
177 }
178
frameLoader() const179 FrameLoader* ResourceLoader::frameLoader() const
180 {
181 if (!m_frame)
182 return 0;
183 return m_frame->loader();
184 }
185
setShouldBufferData(bool shouldBufferData)186 void ResourceLoader::setShouldBufferData(bool shouldBufferData)
187 {
188 m_shouldBufferData = shouldBufferData;
189
190 // Reset any already buffered data
191 if (!m_shouldBufferData)
192 m_resourceData = 0;
193 }
194
195
addData(const char * data,int length,bool allAtOnce)196 void ResourceLoader::addData(const char* data, int length, bool allAtOnce)
197 {
198 if (!m_shouldBufferData)
199 return;
200
201 if (allAtOnce) {
202 m_resourceData = SharedBuffer::create(data, length);
203 return;
204 }
205
206 if (ResourceHandle::supportsBufferedData()) {
207 // Buffer data only if the connection has handed us the data because is has stopped buffering it.
208 if (m_resourceData)
209 m_resourceData->append(data, length);
210 } else {
211 if (!m_resourceData)
212 m_resourceData = SharedBuffer::create(data, length);
213 else
214 m_resourceData->append(data, length);
215 }
216 }
217
clearResourceData()218 void ResourceLoader::clearResourceData()
219 {
220 if (m_resourceData)
221 m_resourceData->clear();
222 }
223
willSendRequest(ResourceRequest & request,const ResourceResponse & redirectResponse)224 void ResourceLoader::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
225 {
226 // Protect this in this delegate method since the additional processing can do
227 // anything including possibly derefing this; one example of this is Radar 3266216.
228 RefPtr<ResourceLoader> protector(this);
229
230 ASSERT(!m_reachedTerminalState);
231
232 if (m_sendResourceLoadCallbacks) {
233 if (!m_identifier) {
234 m_identifier = m_frame->page()->progress()->createUniqueIdentifier();
235 frameLoader()->notifier()->assignIdentifierToInitialRequest(m_identifier, documentLoader(), request);
236 }
237
238 frameLoader()->notifier()->willSendRequest(this, request, redirectResponse);
239 }
240
241 if (!redirectResponse.isNull())
242 resourceLoadScheduler()->crossOriginRedirectReceived(this, request.url());
243 m_request = request;
244 }
245
didSendData(unsigned long long,unsigned long long)246 void ResourceLoader::didSendData(unsigned long long, unsigned long long)
247 {
248 }
249
didReceiveResponse(const ResourceResponse & r)250 void ResourceLoader::didReceiveResponse(const ResourceResponse& r)
251 {
252 ASSERT(!m_reachedTerminalState);
253
254 // Protect this in this delegate method since the additional processing can do
255 // anything including possibly derefing this; one example of this is Radar 3266216.
256 RefPtr<ResourceLoader> protector(this);
257
258 m_response = r;
259
260 if (FormData* data = m_request.httpBody())
261 data->removeGeneratedFilesIfNeeded();
262
263 if (m_sendResourceLoadCallbacks)
264 frameLoader()->notifier()->didReceiveResponse(this, m_response);
265 }
266
didReceiveData(const char * data,int length,long long encodedDataLength,bool allAtOnce)267 void ResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
268 {
269 // The following assertions are not quite valid here, since a subclass
270 // might override didReceiveData in a way that invalidates them. This
271 // happens with the steps listed in 3266216
272 // ASSERT(con == connection);
273 // ASSERT(!m_reachedTerminalState);
274
275 // Protect this in this delegate method since the additional processing can do
276 // anything including possibly derefing this; one example of this is Radar 3266216.
277 RefPtr<ResourceLoader> protector(this);
278
279 addData(data, length, allAtOnce);
280 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
281 // However, with today's computers and networking speeds, this won't happen in practice.
282 // Could be an issue with a giant local file.
283 if (m_sendResourceLoadCallbacks && m_frame)
284 frameLoader()->notifier()->didReceiveData(this, data, length, static_cast<int>(encodedDataLength));
285 }
286
willStopBufferingData(const char * data,int length)287 void ResourceLoader::willStopBufferingData(const char* data, int length)
288 {
289 if (!m_shouldBufferData)
290 return;
291
292 ASSERT(!m_resourceData);
293 m_resourceData = SharedBuffer::create(data, length);
294 }
295
didFinishLoading(double finishTime)296 void ResourceLoader::didFinishLoading(double finishTime)
297 {
298 // If load has been cancelled after finishing (which could happen with a
299 // JavaScript that changes the window location), do nothing.
300 if (m_cancelled)
301 return;
302 ASSERT(!m_reachedTerminalState);
303
304 didFinishLoadingOnePart(finishTime);
305 releaseResources();
306 }
307
didFinishLoadingOnePart(double finishTime)308 void ResourceLoader::didFinishLoadingOnePart(double finishTime)
309 {
310 if (m_cancelled)
311 return;
312 ASSERT(!m_reachedTerminalState);
313
314 if (m_calledDidFinishLoad)
315 return;
316 m_calledDidFinishLoad = true;
317 if (m_sendResourceLoadCallbacks)
318 frameLoader()->notifier()->didFinishLoad(this, finishTime);
319 }
320
didFail(const ResourceError & error)321 void ResourceLoader::didFail(const ResourceError& error)
322 {
323 if (m_cancelled)
324 return;
325 ASSERT(!m_reachedTerminalState);
326
327 // Protect this in this delegate method since the additional processing can do
328 // anything including possibly derefing this; one example of this is Radar 3266216.
329 RefPtr<ResourceLoader> protector(this);
330
331 if (FormData* data = m_request.httpBody())
332 data->removeGeneratedFilesIfNeeded();
333
334 if (m_sendResourceLoadCallbacks && !m_calledDidFinishLoad)
335 frameLoader()->notifier()->didFailToLoad(this, error);
336
337 releaseResources();
338 }
339
didCancel(const ResourceError & error)340 void ResourceLoader::didCancel(const ResourceError& error)
341 {
342 // If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
343 if (m_reachedTerminalState)
344 return;
345
346 ASSERT(!m_cancelled);
347
348 if (FormData* data = m_request.httpBody())
349 data->removeGeneratedFilesIfNeeded();
350
351 // This flag prevents bad behavior when loads that finish cause the
352 // load itself to be cancelled (which could happen with a javascript that
353 // changes the window location). This is used to prevent both the body
354 // of this method and the body of connectionDidFinishLoading: running
355 // for a single delegate. Canceling wins.
356 m_cancelled = true;
357
358 if (m_handle)
359 m_handle->clearAuthentication();
360
361 m_documentLoader->cancelPendingSubstituteLoad(this);
362 if (m_handle) {
363 m_handle->cancel();
364 m_handle = 0;
365 }
366 if (m_sendResourceLoadCallbacks && m_identifier && !m_calledDidFinishLoad)
367 frameLoader()->notifier()->didFailToLoad(this, error);
368
369 releaseResources();
370 }
371
cancel()372 void ResourceLoader::cancel()
373 {
374 cancel(ResourceError());
375 }
376
cancel(const ResourceError & error)377 void ResourceLoader::cancel(const ResourceError& error)
378 {
379 if (m_reachedTerminalState)
380 return;
381 if (!error.isNull())
382 didCancel(error);
383 else
384 didCancel(cancelledError());
385 }
386
response() const387 const ResourceResponse& ResourceLoader::response() const
388 {
389 return m_response;
390 }
391
cancelledError()392 ResourceError ResourceLoader::cancelledError()
393 {
394 return frameLoader()->cancelledError(m_request);
395 }
396
blockedError()397 ResourceError ResourceLoader::blockedError()
398 {
399 return frameLoader()->blockedError(m_request);
400 }
401
cannotShowURLError()402 ResourceError ResourceLoader::cannotShowURLError()
403 {
404 return frameLoader()->cannotShowURLError(m_request);
405 }
406
willSendRequest(ResourceHandle *,ResourceRequest & request,const ResourceResponse & redirectResponse)407 void ResourceLoader::willSendRequest(ResourceHandle*, ResourceRequest& request, const ResourceResponse& redirectResponse)
408 {
409 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
410 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(this, request, redirectResponse))
411 return;
412 #endif
413 willSendRequest(request, redirectResponse);
414 }
415
didSendData(ResourceHandle *,unsigned long long bytesSent,unsigned long long totalBytesToBeSent)416 void ResourceLoader::didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
417 {
418 didSendData(bytesSent, totalBytesToBeSent);
419 }
420
didReceiveResponse(ResourceHandle *,const ResourceResponse & response)421 void ResourceLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
422 {
423 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
424 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(this, response))
425 return;
426 #endif
427 didReceiveResponse(response);
428 }
429
didReceiveData(ResourceHandle *,const char * data,int length,int encodedDataLength)430 void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int encodedDataLength)
431 {
432 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceData(m_frame.get(), identifier());
433 didReceiveData(data, length, encodedDataLength, false);
434 InspectorInstrumentation::didReceiveResourceData(cookie);
435 }
436
didFinishLoading(ResourceHandle *,double finishTime)437 void ResourceLoader::didFinishLoading(ResourceHandle*, double finishTime)
438 {
439 didFinishLoading(finishTime);
440 }
441
didFail(ResourceHandle *,const ResourceError & error)442 void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error)
443 {
444 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
445 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(this, error))
446 return;
447 #endif
448 didFail(error);
449 }
450
wasBlocked(ResourceHandle *)451 void ResourceLoader::wasBlocked(ResourceHandle*)
452 {
453 didFail(blockedError());
454 }
455
cannotShowURL(ResourceHandle *)456 void ResourceLoader::cannotShowURL(ResourceHandle*)
457 {
458 didFail(cannotShowURLError());
459 }
460
shouldUseCredentialStorage()461 bool ResourceLoader::shouldUseCredentialStorage()
462 {
463 RefPtr<ResourceLoader> protector(this);
464 return frameLoader()->shouldUseCredentialStorage(this);
465 }
466
didReceiveAuthenticationChallenge(const AuthenticationChallenge & challenge)467 void ResourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
468 {
469 // Protect this in this delegate method since the additional processing can do
470 // anything including possibly derefing this; one example of this is Radar 3266216.
471 RefPtr<ResourceLoader> protector(this);
472 frameLoader()->notifier()->didReceiveAuthenticationChallenge(this, challenge);
473 }
474
didCancelAuthenticationChallenge(const AuthenticationChallenge & challenge)475 void ResourceLoader::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge)
476 {
477 // Protect this in this delegate method since the additional processing can do
478 // anything including possibly derefing this; one example of this is Radar 3266216.
479 RefPtr<ResourceLoader> protector(this);
480 frameLoader()->notifier()->didCancelAuthenticationChallenge(this, challenge);
481 }
482
483 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
canAuthenticateAgainstProtectionSpace(const ProtectionSpace & protectionSpace)484 bool ResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
485 {
486 RefPtr<ResourceLoader> protector(this);
487 return frameLoader()->canAuthenticateAgainstProtectionSpace(this, protectionSpace);
488 }
489 #endif
490
receivedCancellation(const AuthenticationChallenge &)491 void ResourceLoader::receivedCancellation(const AuthenticationChallenge&)
492 {
493 cancel();
494 }
495
willCacheResponse(ResourceHandle *,CacheStoragePolicy & policy)496 void ResourceLoader::willCacheResponse(ResourceHandle*, CacheStoragePolicy& policy)
497 {
498 // <rdar://problem/7249553> - There are reports of crashes with this method being called
499 // with a null m_frame->settings(), which can only happen if the frame doesn't have a page.
500 // Sadly we have no reproducible cases of this.
501 // We think that any frame without a page shouldn't have any loads happening in it, yet
502 // there is at least one code path where that is not true.
503 ASSERT(m_frame->settings());
504
505 // When in private browsing mode, prevent caching to disk
506 if (policy == StorageAllowed && m_frame->settings() && m_frame->settings()->privateBrowsingEnabled())
507 policy = StorageAllowedInMemoryOnly;
508 }
509
510 #if ENABLE(BLOB)
createAsyncFileStream(FileStreamClient * client)511 AsyncFileStream* ResourceLoader::createAsyncFileStream(FileStreamClient* client)
512 {
513 // It is OK to simply return a pointer since FileStreamProxy::create adds an extra ref.
514 return FileStreamProxy::create(m_frame->document()->scriptExecutionContext(), client).get();
515 }
516 #endif
517
518 }
519