Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2011 Google Inc. All rights reserved.
      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 #ifndef DocumentLoader_h
     31 #define DocumentLoader_h
     32 
     33 #include "core/loader/DocumentLoadTiming.h"
     34 #include "core/loader/DocumentWriter.h"
     35 #include "core/loader/NavigationAction.h"
     36 #include "core/loader/ResourceLoaderOptions.h"
     37 #include "core/loader/SubstituteData.h"
     38 #include "core/loader/cache/RawResource.h"
     39 #include "core/loader/cache/ResourcePtr.h"
     40 #include "core/platform/Timer.h"
     41 #include "core/platform/network/ResourceError.h"
     42 #include "core/platform/network/ResourceRequest.h"
     43 #include "core/platform/network/ResourceResponse.h"
     44 #include "core/platform/text/StringWithDirection.h"
     45 #include "wtf/HashSet.h"
     46 #include "wtf/RefPtr.h"
     47 
     48 namespace WTF {
     49 class SchedulePair;
     50 }
     51 
     52 namespace WebCore {
     53     class ApplicationCacheHost;
     54     class ArchiveResource;
     55     class ArchiveResourceCollection;
     56     class ResourceFetcher;
     57     class ContentFilter;
     58     class FormState;
     59     class Frame;
     60     class FrameLoader;
     61     class MHTMLArchive;
     62     class Page;
     63     class ResourceLoader;
     64     class SharedBuffer;
     65 
     66     typedef HashSet<RefPtr<ResourceLoader> > ResourceLoaderSet;
     67 
     68     class DocumentLoader : public RefCounted<DocumentLoader>, private RawResourceClient {
     69         WTF_MAKE_FAST_ALLOCATED;
     70     public:
     71         static PassRefPtr<DocumentLoader> create(const ResourceRequest& request, const SubstituteData& data)
     72         {
     73             return adoptRef(new DocumentLoader(request, data));
     74         }
     75         virtual ~DocumentLoader();
     76 
     77         void setFrame(Frame*);
     78         Frame* frame() const { return m_frame; }
     79 
     80         void detachFromFrame();
     81 
     82         FrameLoader* frameLoader() const;
     83 
     84         unsigned long mainResourceIdentifier() const;
     85 
     86         void replaceDocument(const String& source, Document*);
     87         DocumentWriter* beginWriting(const String& mimeType, const String& encoding, const KURL& = KURL());
     88         void endWriting(DocumentWriter*);
     89 
     90         String mimeType() const;
     91 
     92         const ResourceRequest& originalRequest() const;
     93         const ResourceRequest& originalRequestCopy() const;
     94 
     95         const ResourceRequest& request() const;
     96         ResourceRequest& request();
     97 
     98         ResourceFetcher* fetcher() const { return m_fetcher.get(); }
     99 
    100         const SubstituteData& substituteData() const { return m_substituteData; }
    101 
    102         // FIXME: This is the same as requestURL(). We should remove one of them.
    103         const KURL& url() const;
    104         const KURL& unreachableURL() const;
    105 
    106         const KURL& originalURL() const;
    107         const KURL& requestURL() const;
    108         const String& responseMIMEType() const;
    109 
    110         void replaceRequestURLForSameDocumentNavigation(const KURL&);
    111         bool isStopping() const { return m_isStopping; }
    112         void stopLoading();
    113         void setCommitted(bool committed) { m_committed = committed; }
    114         bool isCommitted() const { return m_committed; }
    115         bool isLoading() const;
    116         const ResourceResponse& response() const { return m_response; }
    117         const ResourceError& mainDocumentError() const { return m_mainDocumentError; }
    118         bool isClientRedirect() const { return m_isClientRedirect; }
    119         void setIsClientRedirect(bool isClientRedirect) { m_isClientRedirect = isClientRedirect; }
    120         bool replacesCurrentHistoryItem() const { return m_replacesCurrentHistoryItem; }
    121         void setReplacesCurrentHistoryItem(bool replacesCurrentHistoryItem) { m_replacesCurrentHistoryItem = replacesCurrentHistoryItem; }
    122         bool isLoadingInAPISense() const;
    123         void setTitle(const StringWithDirection&);
    124         const String& overrideEncoding() const { return m_overrideEncoding; }
    125 
    126         bool scheduleArchiveLoad(Resource*, const ResourceRequest&);
    127         void cancelPendingSubstituteLoad(ResourceLoader*);
    128 
    129         enum PolicyCheckLoadType {
    130             PolicyCheckStandard,
    131             PolicyCheckFragment
    132         };
    133         bool shouldContinueForNavigationPolicy(const ResourceRequest&, PolicyCheckLoadType);
    134         const NavigationAction& triggeringAction() const { return m_triggeringAction; }
    135         void setTriggeringAction(const NavigationAction& action) { m_triggeringAction = action; }
    136 
    137         void setOverrideEncoding(const String& encoding) { m_overrideEncoding = encoding; }
    138         const StringWithDirection& title() const { return m_pageTitle; }
    139 
    140         KURL urlForHistory() const;
    141 
    142         void setDefersLoading(bool);
    143 
    144         void startLoadingMainResource();
    145         void cancelMainResourceLoad(const ResourceError&);
    146 
    147         bool isLoadingMainResource() const { return m_loadingMainResource; }
    148 
    149         void stopLoadingSubresources();
    150         void addResourceLoader(ResourceLoader*);
    151         void removeResourceLoader(ResourceLoader*);
    152 
    153         void subresourceLoaderFinishedLoadingOnePart(ResourceLoader*);
    154 
    155         void setDeferMainResourceDataLoad(bool defer) { m_deferMainResourceDataLoad = defer; }
    156 
    157         DocumentLoadTiming* timing() { return &m_documentLoadTiming; }
    158         void resetTiming() { m_documentLoadTiming = DocumentLoadTiming(); }
    159 
    160         ApplicationCacheHost* applicationCacheHost() const { return m_applicationCacheHost.get(); }
    161 
    162         void checkLoadComplete();
    163 
    164         bool isRedirect() const { return m_redirectChain.size() > 1; }
    165         void clearRedirectChain();
    166         void appendRedirect(const KURL&);
    167 
    168     protected:
    169         DocumentLoader(const ResourceRequest&, const SubstituteData&);
    170 
    171         bool m_deferMainResourceDataLoad;
    172         Vector<KURL> m_redirectChain;
    173 
    174     private:
    175         static PassRefPtr<DocumentWriter> createWriterFor(Frame*, const Document* ownerDocument, const KURL&, const String& mimeType, const String& encoding, bool userChosen, bool dispatch);
    176 
    177         void ensureWriter();
    178         void ensureWriter(const String& mimeType, const KURL& overridingURL = KURL());
    179 
    180         Document* document() const;
    181 
    182         void setRequest(const ResourceRequest&);
    183 
    184         void commitIfReady();
    185         void commitData(const char* bytes, size_t length);
    186         void setMainDocumentError(const ResourceError&);
    187         void clearMainResourceLoader();
    188         ResourceLoader* mainResourceLoader() const;
    189         void clearMainResourceHandle();
    190         PassRefPtr<SharedBuffer> mainResourceData() const;
    191 
    192         void createArchive();
    193         void clearArchiveResources();
    194 
    195         void prepareSubframeArchiveLoadIfNeeded();
    196         void addAllArchiveResources(MHTMLArchive*);
    197 
    198         void willSendRequest(ResourceRequest&, const ResourceResponse&);
    199         void finishedLoading(double finishTime);
    200         void mainReceivedError(const ResourceError&);
    201         virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&) OVERRIDE;
    202         virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE;
    203         virtual void dataReceived(Resource*, const char* data, int length) OVERRIDE;
    204         virtual void notifyFinished(Resource*) OVERRIDE;
    205 
    206         bool maybeLoadEmpty();
    207 
    208         bool isRedirectAfterPost(const ResourceRequest&, const ResourceResponse&);
    209 
    210         bool shouldContinueForResponse() const;
    211         void stopLoadingForPolicyChange();
    212 
    213         typedef Timer<DocumentLoader> DocumentLoaderTimer;
    214 
    215         void handleSubstituteDataLoadSoon();
    216         void handleSubstituteDataLoadNow(DocumentLoaderTimer*);
    217         void startDataLoadTimer();
    218 
    219         Frame* m_frame;
    220         RefPtr<ResourceFetcher> m_fetcher;
    221 
    222         ResourcePtr<RawResource> m_mainResource;
    223         ResourceLoaderSet m_resourceLoaders;
    224         ResourceLoaderSet m_multipartResourceLoaders;
    225 
    226         RefPtr<DocumentWriter> m_writer;
    227 
    228         // A reference to actual request used to create the data source.
    229         // This should only be used by the resourceLoadDelegate's
    230         // identifierForInitialRequest:fromDatasource: method. It is
    231         // not guaranteed to remain unchanged, as requests are mutable.
    232         ResourceRequest m_originalRequest;
    233 
    234         SubstituteData m_substituteData;
    235 
    236         // A copy of the original request used to create the data source.
    237         // We have to copy the request because requests are mutable.
    238         ResourceRequest m_originalRequestCopy;
    239 
    240         // The 'working' request. It may be mutated
    241         // several times from the original request to include additional
    242         // headers, cookie information, canonicalization and redirects.
    243         ResourceRequest m_request;
    244 
    245         ResourceResponse m_response;
    246 
    247         ResourceError m_mainDocumentError;
    248 
    249         bool m_committed;
    250         bool m_isStopping;
    251         bool m_isClientRedirect;
    252         bool m_replacesCurrentHistoryItem;
    253 
    254         StringWithDirection m_pageTitle;
    255 
    256         String m_overrideEncoding;
    257 
    258         // The action that triggered loading - we keep this around for the
    259         // benefit of the various policy handlers.
    260         NavigationAction m_triggeringAction;
    261 
    262         OwnPtr<ArchiveResourceCollection> m_archiveResourceCollection;
    263         RefPtr<MHTMLArchive> m_archive;
    264 
    265         bool m_loadingMainResource;
    266         DocumentLoadTiming m_documentLoadTiming;
    267 
    268         double m_timeOfLastDataReceived;
    269         unsigned long m_identifierForLoadWithoutResourceLoader;
    270 
    271         DocumentLoaderTimer m_dataLoadTimer;
    272 
    273         friend class ApplicationCacheHost;  // for substitute resource delivery
    274         OwnPtr<ApplicationCacheHost> m_applicationCacheHost;
    275     };
    276 }
    277 
    278 #endif // DocumentLoader_h
    279