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