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/frame/csp/ContentSecurityPolicy.h" 37 #include "core/loader/DocumentLoadTiming.h" 38 #include "core/loader/DocumentWriter.h" 39 #include "core/loader/NavigationAction.h" 40 #include "core/loader/SubstituteData.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 blink { 48 class WebThreadedDataReceiver; 49 } 50 51 namespace blink { 52 class ApplicationCacheHost; 53 class ArchiveResourceCollection; 54 class ResourceFetcher; 55 class DocumentInit; 56 class LocalFrame; 57 class FrameLoader; 58 class MHTMLArchive; 59 class ResourceLoader; 60 61 class DocumentLoader : public RefCounted<DocumentLoader>, private RawResourceClient { 62 WTF_MAKE_FAST_ALLOCATED; 63 public: create(LocalFrame * frame,const ResourceRequest & request,const SubstituteData & data)64 static PassRefPtr<DocumentLoader> create(LocalFrame* frame, const ResourceRequest& request, const SubstituteData& data) 65 { 66 return adoptRef(new DocumentLoader(frame, request, data)); 67 } 68 virtual ~DocumentLoader(); 69 frame()70 LocalFrame* frame() const { return m_frame; } 71 72 void detachFromFrame(); 73 74 unsigned long mainResourceIdentifier() const; 75 76 void replaceDocumentWhileExecutingJavaScriptURL(const DocumentInit&, const String& source, Document*); 77 78 const AtomicString& mimeType() const; 79 80 void setUserChosenEncoding(const String& charset); 81 82 const ResourceRequest& originalRequest() const; 83 84 const ResourceRequest& request() const; 85 fetcher()86 ResourceFetcher* fetcher() const { return m_fetcher.get(); } 87 substituteData()88 const SubstituteData& substituteData() const { return m_substituteData; } 89 90 const KURL& url() const; 91 const KURL& unreachableURL() const; 92 const KURL& urlForHistory() const; 93 94 const AtomicString& responseMIMEType() const; 95 96 void updateForSameDocumentNavigation(const KURL&, SameDocumentNavigationSource); 97 void stopLoading(); isCommitted()98 bool isCommitted() const { return m_committed; } 99 bool isLoading() const; isLoadingMainResource()100 bool isLoadingMainResource() const { return m_loadingMainResource; } response()101 const ResourceResponse& response() const { return m_response; } mainDocumentError()102 const ResourceError& mainDocumentError() const { return m_mainDocumentError; } isClientRedirect()103 bool isClientRedirect() const { return m_isClientRedirect; } setIsClientRedirect(bool isClientRedirect)104 void setIsClientRedirect(bool isClientRedirect) { m_isClientRedirect = isClientRedirect; } replacesCurrentHistoryItem()105 bool replacesCurrentHistoryItem() const { return m_replacesCurrentHistoryItem; } setReplacesCurrentHistoryItem(bool replacesCurrentHistoryItem)106 void setReplacesCurrentHistoryItem(bool replacesCurrentHistoryItem) { m_replacesCurrentHistoryItem = replacesCurrentHistoryItem; } overrideEncoding()107 const AtomicString& overrideEncoding() const { return m_overrideEncoding; } 108 109 bool scheduleArchiveLoad(Resource*, const ResourceRequest&); 110 111 bool shouldContinueForNavigationPolicy(const ResourceRequest&, ContentSecurityPolicyCheck shouldCheckMainWorldContentSecurityPolicy, bool isTransitionNavigation = false); triggeringAction()112 const NavigationAction& triggeringAction() const { return m_triggeringAction; } setTriggeringAction(const NavigationAction & action)113 void setTriggeringAction(const NavigationAction& action) { m_triggeringAction = action; } 114 setOverrideEncoding(const AtomicString & encoding)115 void setOverrideEncoding(const AtomicString& encoding) { m_overrideEncoding = encoding; } 116 117 void setDefersLoading(bool); 118 119 void startLoadingMainResource(); 120 void cancelMainResourceLoad(const ResourceError&); 121 122 void attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver>); timing()123 DocumentLoadTiming* timing() { return &m_documentLoadTiming; } 124 applicationCacheHost()125 ApplicationCacheHost* applicationCacheHost() const { return m_applicationCacheHost.get(); } 126 isRedirect()127 bool isRedirect() const { return m_redirectChain.size() > 1; } 128 void clearRedirectChain(); 129 void appendRedirect(const KURL&); 130 releaseContentSecurityPolicy()131 PassRefPtr<ContentSecurityPolicy> releaseContentSecurityPolicy() { return m_contentSecurityPolicy.release(); } 132 133 protected: 134 DocumentLoader(LocalFrame*, const ResourceRequest&, const SubstituteData&); 135 136 Vector<KURL> m_redirectChain; 137 138 private: 139 static PassRefPtrWillBeRawPtr<DocumentWriter> createWriterFor(const Document* ownerDocument, const DocumentInit&, const AtomicString& mimeType, const AtomicString& encoding, bool dispatch); 140 141 void ensureWriter(const AtomicString& mimeType, const KURL& overridingURL = KURL()); 142 void endWriting(DocumentWriter*); 143 144 Document* document() const; 145 FrameLoader* frameLoader() const; 146 147 void commitIfReady(); 148 void commitData(const char* bytes, size_t length); 149 void setMainDocumentError(const ResourceError&); 150 void clearMainResourceLoader(); 151 ResourceLoader* mainResourceLoader() const; 152 void clearMainResourceHandle(); 153 154 bool maybeCreateArchive(); 155 156 void prepareSubframeArchiveLoadIfNeeded(); 157 void addAllArchiveResources(MHTMLArchive*); 158 159 void willSendRequest(ResourceRequest&, const ResourceResponse&); 160 void finishedLoading(double finishTime); 161 void mainReceivedError(const ResourceError&); 162 void cancelLoadAfterXFrameOptionsOrCSPDenied(const ResourceResponse&); 163 virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&) OVERRIDE FINAL; 164 virtual void updateRequest(Resource*, const ResourceRequest&) OVERRIDE FINAL; 165 virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE FINAL; 166 virtual void dataReceived(Resource*, const char* data, int length) OVERRIDE FINAL; 167 virtual void notifyFinished(Resource*) OVERRIDE FINAL; 168 169 bool maybeLoadEmpty(); 170 171 bool isRedirectAfterPost(const ResourceRequest&, const ResourceResponse&); 172 173 bool shouldContinueForResponse() const; 174 175 LocalFrame* m_frame; 176 RefPtrWillBePersistent<ResourceFetcher> m_fetcher; 177 178 ResourcePtr<RawResource> m_mainResource; 179 180 RefPtrWillBePersistent<DocumentWriter> m_writer; 181 182 // A reference to actual request used to create the data source. 183 // The only part of this request that should change is the url, and 184 // that only in the case of a same-document navigation. 185 ResourceRequest m_originalRequest; 186 187 SubstituteData m_substituteData; 188 189 // The 'working' request. It may be mutated 190 // several times from the original request to include additional 191 // headers, cookie information, canonicalization and redirects. 192 ResourceRequest m_request; 193 194 ResourceResponse m_response; 195 196 ResourceError m_mainDocumentError; 197 198 bool m_committed; 199 bool m_isClientRedirect; 200 bool m_replacesCurrentHistoryItem; 201 202 AtomicString m_overrideEncoding; 203 204 // The action that triggered loading - we keep this around for the 205 // benefit of the various policy handlers. 206 NavigationAction m_triggeringAction; 207 208 OwnPtrWillBePersistent<ArchiveResourceCollection> m_archiveResourceCollection; 209 RefPtrWillBePersistent<MHTMLArchive> m_archive; 210 211 bool m_loadingMainResource; 212 DocumentLoadTiming m_documentLoadTiming; 213 214 double m_timeOfLastDataReceived; 215 216 friend class ApplicationCacheHost; // for substitute resource delivery 217 OwnPtrWillBePersistent<ApplicationCacheHost> m_applicationCacheHost; 218 219 RefPtr<ContentSecurityPolicy> m_contentSecurityPolicy; 220 }; 221 } 222 223 #endif // DocumentLoader_h 224