2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef DocumentThreadableLoader_h
33 #define DocumentThreadableLoader_h
35 #include "core/CoreExport.h"
36 #include "core/fetch/RawResource.h"
37 #include "core/fetch/ResourceOwner.h"
38 #include "core/frame/csp/ContentSecurityPolicy.h"
39 #include "core/loader/ThreadableLoader.h"
40 #include "platform/Timer.h"
41 #include "platform/network/HTTPHeaderMap.h"
42 #include "platform/network/ResourceError.h"
43 #include "wtf/Forward.h"
44 #include "wtf/OwnPtr.h"
45 #include "wtf/PassRefPtr.h"
46 #include "wtf/text/WTFString.h"
52 class ResourceRequest
;
54 class ThreadableLoaderClient
;
56 class CORE_EXPORT DocumentThreadableLoader final
: public ThreadableLoader
, private ResourceOwner
<RawResource
> {
57 WTF_MAKE_FAST_ALLOCATED(DocumentThreadableLoader
);
59 static void loadResourceSynchronously(Document
&, const ResourceRequest
&, ThreadableLoaderClient
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&);
60 static PassRefPtr
<DocumentThreadableLoader
> create(Document
&, ThreadableLoaderClient
*, const ResourceRequest
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&);
61 ~DocumentThreadableLoader() override
;
63 void overrideTimeout(unsigned long timeout
) override
;
65 // |this| may be dead after calling this method in async mode.
66 void cancel() override
;
67 void setDefersLoading(bool);
70 enum BlockingBehavior
{
75 DocumentThreadableLoader(Document
&, ThreadableLoaderClient
*, BlockingBehavior
, const ResourceRequest
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&);
81 // |this| may be dead after calling this method.
82 void notifyFinished(Resource
*) override
;
86 // |this| may be dead after calling these methods.
87 void dataSent(Resource
*, unsigned long long bytesSent
, unsigned long long totalBytesToBeSent
) override
;
88 void responseReceived(Resource
*, const ResourceResponse
&, PassOwnPtr
<WebDataConsumerHandle
>) override
;
89 void setSerializedCachedMetadata(Resource
*, const char*, size_t) override
;
90 void dataReceived(Resource
*, const char* data
, unsigned dataLength
) override
;
91 void redirectReceived(Resource
*, ResourceRequest
&, const ResourceResponse
&) override
;
92 void dataDownloaded(Resource
*, int) override
;
93 void didReceiveResourceTiming(Resource
*, const ResourceTimingInfo
&) override
;
95 // |this| may be dead after calling this method in async mode.
96 void cancelWithError(const ResourceError
&);
98 // Notify Inspector and log to console about resource response. Use
99 // this method if response is not going to be finished normally.
100 void reportResponseReceived(unsigned long identifier
, const ResourceResponse
&);
102 // Methods containing code to handle resource fetch results which are
103 // common to both sync and async mode.
105 // |this| may be dead after calling these method in async mode.
106 void handleResponse(unsigned long identifier
, const ResourceResponse
&, PassOwnPtr
<WebDataConsumerHandle
>);
107 void handleReceivedData(const char* data
, unsigned dataLength
);
108 void handleSuccessfulFinish(unsigned long identifier
, double finishTime
);
110 // |this| may be dead after calling this method.
111 void didTimeout(Timer
<DocumentThreadableLoader
>*);
112 // Calls the appropriate loading method according to policy and data
113 // about origin. Only for handling the initial load (including fallback
114 // after consulting ServiceWorker).
116 // |this| may be dead after calling this method in async mode.
117 void dispatchInitialRequest(const ResourceRequest
&);
118 // |this| may be dead after calling this method in async mode.
119 void makeCrossOriginAccessRequest(const ResourceRequest
&);
120 // Loads m_fallbackRequestForServiceWorker.
122 // |this| may be dead after calling this method in async mode.
123 void loadFallbackRequestForServiceWorker();
124 // Loads m_actualRequest.
125 void loadActualRequest();
126 // Clears m_actualRequest and reports access control check failure to
129 // |this| may be dead after calling this method in async mode.
130 void handlePreflightFailure(const String
& url
, const String
& errorDescription
);
131 // Investigates the response for the preflight request. If successful,
132 // the actual request will be made later in handleSuccessfulFinish().
134 // |this| may be dead after calling this method in async mode.
135 void handlePreflightResponse(const ResourceResponse
&);
136 // |this| may be dead after calling this method.
137 void handleError(const ResourceError
&);
139 void loadRequest(const ResourceRequest
&, ResourceLoaderOptions
);
140 bool isAllowedRedirect(const KURL
&) const;
141 bool isAllowedByContentSecurityPolicy(const KURL
&, ContentSecurityPolicy::RedirectStatus
) const;
142 // Returns DoNotAllowStoredCredentials
143 // if m_forceDoNotAllowStoredCredentials is set. Otherwise, just
144 // returns allowCredentials value of m_resourceLoaderOptions.
145 StoredCredentials
effectiveAllowCredentials() const;
147 SecurityOrigin
* securityOrigin() const;
149 ThreadableLoaderClient
* m_client
;
150 Document
& m_document
;
152 const ThreadableLoaderOptions m_options
;
153 // Some items may be overridden by m_forceDoNotAllowStoredCredentials
154 // and m_securityOrigin. In such a case, build a ResourceLoaderOptions
155 // with up-to-date values from them and this variable, and use it.
156 const ResourceLoaderOptions m_resourceLoaderOptions
;
158 bool m_forceDoNotAllowStoredCredentials
;
159 RefPtr
<SecurityOrigin
> m_securityOrigin
;
161 // True while the initial URL and all the URLs of the redirects
162 // this object has followed, if any, are same-origin to
164 bool m_sameOriginRequest
;
165 // Set to true if the current request is cross-origin and not simple.
166 bool m_crossOriginNonSimpleRequest
;
168 // Set to true when the response data is given to a data consumer
170 bool m_isUsingDataConsumerHandle
;
174 // Holds the original request context (used for sanity checks).
175 const WebURLRequest::RequestContext m_requestContext
;
177 // Holds the original request for fallback in case the Service Worker
179 OwnPtr
<ResourceRequest
> m_fallbackRequestForServiceWorker
;
181 // Holds the original request and options for it during preflight
182 // request handling phase.
183 OwnPtr
<ResourceRequest
> m_actualRequest
;
184 OwnPtr
<ResourceLoaderOptions
> m_actualOptions
;
186 HTTPHeaderMap m_simpleRequestHeaders
; // stores simple request headers in case of a cross-origin redirect.
187 Timer
<DocumentThreadableLoader
> m_timeoutTimer
;
188 double m_requestStartedSeconds
; // Time an asynchronous fetch request is started
190 // Max number of times that this DocumentThreadableLoader can follow
191 // cross-origin redirects.
192 // This is used to limit the number of redirects.
193 // But this value is not the max number of total redirects allowed,
194 // because same-origin redirects are not counted here.
195 int m_corsRedirectLimit
;
197 const WebURLRequest::FetchRedirectMode m_redirectMode
;
202 #endif // DocumentThreadableLoader_h