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