OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved. |
3 * Copyright (C) 2013, Intel Corporation | 3 * Copyright (C) 2013, Intel Corporation |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 24 matching lines...) Expand all Loading... |
35 #include "core/CoreExport.h" | 35 #include "core/CoreExport.h" |
36 #include "core/fetch/RawResource.h" | 36 #include "core/fetch/RawResource.h" |
37 #include "core/fetch/ResourceOwner.h" | 37 #include "core/fetch/ResourceOwner.h" |
38 #include "core/loader/ThreadableLoader.h" | 38 #include "core/loader/ThreadableLoader.h" |
39 #include "platform/Timer.h" | 39 #include "platform/Timer.h" |
40 #include "platform/heap/Handle.h" | 40 #include "platform/heap/Handle.h" |
41 #include "platform/network/HTTPHeaderMap.h" | 41 #include "platform/network/HTTPHeaderMap.h" |
42 #include "platform/network/ResourceError.h" | 42 #include "platform/network/ResourceError.h" |
43 #include "platform/weborigin/Referrer.h" | 43 #include "platform/weborigin/Referrer.h" |
44 #include "wtf/Forward.h" | 44 #include "wtf/Forward.h" |
45 #include "wtf/WeakPtr.h" | |
46 #include "wtf/text/WTFString.h" | 45 #include "wtf/text/WTFString.h" |
47 #include <memory> | 46 #include <memory> |
48 | 47 |
49 namespace blink { | 48 namespace blink { |
50 | 49 |
51 class Document; | 50 class Document; |
52 class KURL; | 51 class KURL; |
53 class ResourceRequest; | 52 class ResourceRequest; |
54 class SecurityOrigin; | 53 class SecurityOrigin; |
55 class ThreadableLoaderClient; | 54 class ThreadableLoaderClient; |
56 | 55 |
57 class CORE_EXPORT DocumentThreadableLoader final : public ThreadableLoader, priv
ate RawResourceClient { | 56 class CORE_EXPORT DocumentThreadableLoader final : public ThreadableLoader, priv
ate RawResourceClient { |
58 USING_GARBAGE_COLLECTED_MIXIN(DocumentThreadableLoader); | 57 USING_GARBAGE_COLLECTED_MIXIN(DocumentThreadableLoader); |
59 public: | |
60 static void loadResourceSynchronously(Document&, const ResourceRequest&,
ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoaderOp
tions&); | |
61 static DocumentThreadableLoader* create(Document&, ThreadableLoaderClien
t*, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); | |
62 ~DocumentThreadableLoader() override; | |
63 | 58 |
64 void start(const ResourceRequest&) override; | 59 public: |
| 60 static void loadResourceSynchronously(Document&, const ResourceRequest&, Thr
eadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoaderOption
s&); |
| 61 static DocumentThreadableLoader* create(Document&, ThreadableLoaderClient*,
const ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
| 62 ~DocumentThreadableLoader() override; |
65 | 63 |
66 void overrideTimeout(unsigned long timeout) override; | 64 void start(const ResourceRequest&) override; |
67 | 65 |
68 // |this| may be dead after calling this method in async mode. | 66 void overrideTimeout(unsigned long timeout) override; |
69 void cancel() override; | |
70 void setDefersLoading(bool); | |
71 | 67 |
72 DECLARE_TRACE(); | 68 // |this| may be dead after calling this method in async mode. |
| 69 void cancel() override; |
| 70 void setDefersLoading(bool); |
73 | 71 |
74 private: | 72 DECLARE_TRACE(); |
75 enum BlockingBehavior { | |
76 LoadSynchronously, | |
77 LoadAsynchronously | |
78 }; | |
79 | 73 |
80 DocumentThreadableLoader(Document&, ThreadableLoaderClient*, BlockingBeh
avior, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); | 74 private: |
| 75 enum BlockingBehavior { |
| 76 LoadSynchronously, |
| 77 LoadAsynchronously |
| 78 }; |
81 | 79 |
82 void clear(); | 80 DocumentThreadableLoader(Document&, ThreadableLoaderClient*, BlockingBehavio
r, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
83 | 81 |
84 // ResourceClient | 82 void clear(); |
85 // | |
86 // |this| may be dead after calling this method. | |
87 void notifyFinished(Resource*) override; | |
88 | 83 |
89 String debugName() const override { return "DocumentThreadableLoader"; } | 84 // ResourceClient |
| 85 // |
| 86 // |this| may be dead after calling this method. |
| 87 void notifyFinished(Resource*) override; |
90 | 88 |
91 // RawResourceClient | 89 String debugName() const override { return "DocumentThreadableLoader"; } |
92 // | |
93 // |this| may be dead after calling these methods. | |
94 void dataSent(Resource*, unsigned long long bytesSent, unsigned long lon
g totalBytesToBeSent) override; | |
95 void responseReceived(Resource*, const ResourceResponse&, std::unique_pt
r<WebDataConsumerHandle>) override; | |
96 void setSerializedCachedMetadata(Resource*, const char*, size_t) overrid
e; | |
97 void dataReceived(Resource*, const char* data, size_t dataLength) overri
de; | |
98 void redirectReceived(Resource*, ResourceRequest&, const ResourceRespons
e&) override; | |
99 void redirectBlocked() override; | |
100 void dataDownloaded(Resource*, int) override; | |
101 void didReceiveResourceTiming(Resource*, const ResourceTimingInfo&) over
ride; | |
102 | 90 |
103 // |this| may be dead after calling this method in async mode. | 91 // RawResourceClient |
104 void cancelWithError(const ResourceError&); | 92 // |
| 93 // |this| may be dead after calling these methods. |
| 94 void dataSent(Resource*, unsigned long long bytesSent, unsigned long long to
talBytesToBeSent) override; |
| 95 void responseReceived(Resource*, const ResourceResponse&, std::unique_ptr<We
bDataConsumerHandle>) override; |
| 96 void setSerializedCachedMetadata(Resource*, const char*, size_t) override; |
| 97 void dataReceived(Resource*, const char* data, size_t dataLength) override; |
| 98 void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&)
override; |
| 99 void redirectBlocked() override; |
| 100 void dataDownloaded(Resource*, int) override; |
| 101 void didReceiveResourceTiming(Resource*, const ResourceTimingInfo&) override
; |
105 | 102 |
106 // Notify Inspector and log to console about resource response. Use | 103 // |this| may be dead after calling this method in async mode. |
107 // this method if response is not going to be finished normally. | 104 void cancelWithError(const ResourceError&); |
108 void reportResponseReceived(unsigned long identifier, const ResourceResp
onse&); | |
109 | 105 |
110 // Methods containing code to handle resource fetch results which are | 106 // Notify Inspector and log to console about resource response. Use |
111 // common to both sync and async mode. | 107 // this method if response is not going to be finished normally. |
112 // | 108 void reportResponseReceived(unsigned long identifier, const ResourceResponse
&); |
113 // |this| may be dead after calling these method in async mode. | |
114 void handleResponse(unsigned long identifier, const ResourceResponse&, s
td::unique_ptr<WebDataConsumerHandle>); | |
115 void handleReceivedData(const char* data, size_t dataLength); | |
116 void handleSuccessfulFinish(unsigned long identifier, double finishTime)
; | |
117 | 109 |
118 // |this| may be dead after calling this method. | 110 // Methods containing code to handle resource fetch results which are |
119 void didTimeout(TimerBase*); | 111 // common to both sync and async mode. |
120 // Calls the appropriate loading method according to policy and data | 112 // |
121 // about origin. Only for handling the initial load (including fallback | 113 // |this| may be dead after calling these method in async mode. |
122 // after consulting ServiceWorker). | 114 void handleResponse(unsigned long identifier, const ResourceResponse&, std::
unique_ptr<WebDataConsumerHandle>); |
123 // | 115 void handleReceivedData(const char* data, size_t dataLength); |
124 // |this| may be dead after calling this method in async mode. | 116 void handleSuccessfulFinish(unsigned long identifier, double finishTime); |
125 void dispatchInitialRequest(const ResourceRequest&); | |
126 // |this| may be dead after calling this method in async mode. | |
127 void makeCrossOriginAccessRequest(const ResourceRequest&); | |
128 // Loads m_fallbackRequestForServiceWorker. | |
129 // | |
130 // |this| may be dead after calling this method in async mode. | |
131 void loadFallbackRequestForServiceWorker(); | |
132 // Loads m_actualRequest. | |
133 void loadActualRequest(); | |
134 // Clears m_actualRequest and reports access control check failure to | |
135 // m_client. | |
136 // | |
137 // |this| may be dead after calling this method in async mode. | |
138 void handlePreflightFailure(const String& url, const String& errorDescri
ption); | |
139 // Investigates the response for the preflight request. If successful, | |
140 // the actual request will be made later in handleSuccessfulFinish(). | |
141 // | |
142 // |this| may be dead after calling this method in async mode. | |
143 void handlePreflightResponse(const ResourceResponse&); | |
144 // |this| may be dead after calling this method. | |
145 void handleError(const ResourceError&); | |
146 | 117 |
147 void loadRequest(const ResourceRequest&, ResourceLoaderOptions); | 118 // |this| may be dead after calling this method. |
148 bool isAllowedRedirect(const KURL&) const; | 119 void didTimeout(TimerBase*); |
149 // Returns DoNotAllowStoredCredentials | 120 // Calls the appropriate loading method according to policy and data |
150 // if m_forceDoNotAllowStoredCredentials is set. Otherwise, just | 121 // about origin. Only for handling the initial load (including fallback |
151 // returns allowCredentials value of m_resourceLoaderOptions. | 122 // after consulting ServiceWorker). |
152 StoredCredentials effectiveAllowCredentials() const; | 123 // |
| 124 // |this| may be dead after calling this method in async mode. |
| 125 void dispatchInitialRequest(const ResourceRequest&); |
| 126 // |this| may be dead after calling this method in async mode. |
| 127 void makeCrossOriginAccessRequest(const ResourceRequest&); |
| 128 // Loads m_fallbackRequestForServiceWorker. |
| 129 // |
| 130 // |this| may be dead after calling this method in async mode. |
| 131 void loadFallbackRequestForServiceWorker(); |
| 132 // Loads m_actualRequest. |
| 133 void loadActualRequest(); |
| 134 // Clears m_actualRequest and reports access control check failure to |
| 135 // m_client. |
| 136 // |
| 137 // |this| may be dead after calling this method in async mode. |
| 138 void handlePreflightFailure(const String& url, const String& errorDescriptio
n); |
| 139 // Investigates the response for the preflight request. If successful, |
| 140 // the actual request will be made later in handleSuccessfulFinish(). |
| 141 // |
| 142 // |this| may be dead after calling this method in async mode. |
| 143 void handlePreflightResponse(const ResourceResponse&); |
| 144 // |this| may be dead after calling this method. |
| 145 void handleError(const ResourceError&); |
153 | 146 |
154 // TODO(hiroshige): After crbug.com/633696 is fixed, | 147 void loadRequest(const ResourceRequest&, ResourceLoaderOptions); |
155 // - Remove RawResourceClientStateChecker logic, | 148 bool isAllowedRedirect(const KURL&) const; |
156 // - Make DocumentThreadableLoader to be a ResourceOwner and remove | 149 // Returns DoNotAllowStoredCredentials |
157 // this re-implementation of ResourceOwner, and | 150 // if m_forceDoNotAllowStoredCredentials is set. Otherwise, just |
158 // - Consider re-applying RawResourceClientStateChecker in a more | 151 // returns allowCredentials value of m_resourceLoaderOptions. |
159 // general fashion (crbug.com/640291). | 152 StoredCredentials effectiveAllowCredentials() const; |
160 RawResource* resource() const { return m_resource.get(); } | |
161 void clearResource() { setResource(nullptr); } | |
162 void setResource(RawResource* newResource) | |
163 { | |
164 if (newResource == m_resource) | |
165 return; | |
166 | 153 |
167 if (RawResource* oldResource = m_resource.release()) { | 154 // TODO(hiroshige): After crbug.com/633696 is fixed, |
168 m_checker.willRemoveClient(); | 155 // - Remove RawResourceClientStateChecker logic, |
169 oldResource->removeClient(this); | 156 // - Make DocumentThreadableLoader to be a ResourceOwner and remove |
170 } | 157 // this re-implementation of ResourceOwner, and |
| 158 // - Consider re-applying RawResourceClientStateChecker in a more |
| 159 // general fashion (crbug.com/640291). |
| 160 RawResource* resource() const { return m_resource.get(); } |
| 161 void clearResource() { setResource(nullptr); } |
| 162 void setResource(RawResource* newResource) |
| 163 { |
| 164 if (newResource == m_resource) |
| 165 return; |
171 | 166 |
172 if (newResource) { | 167 if (RawResource* oldResource = m_resource.release()) { |
173 m_resource = newResource; | 168 m_checker.willRemoveClient(); |
174 m_checker.willAddClient(); | 169 oldResource->removeClient(this); |
175 m_resource->addClient(this); | |
176 } | |
177 } | 170 } |
178 Member<RawResource> m_resource; | |
179 // End of ResourceOwner re-implementation, see above. | |
180 | 171 |
181 const SecurityOrigin* getSecurityOrigin() const; | 172 if (newResource) { |
182 Document& document() const; | 173 m_resource = newResource; |
| 174 m_checker.willAddClient(); |
| 175 m_resource->addClient(this); |
| 176 } |
| 177 } |
| 178 Member<RawResource> m_resource; |
| 179 // End of ResourceOwner re-implementation, see above. |
183 | 180 |
184 ThreadableLoaderClient* m_client; | 181 const SecurityOrigin* getSecurityOrigin() const; |
185 Member<Document> m_document; | 182 Document& document() const; |
186 | 183 |
187 const ThreadableLoaderOptions m_options; | 184 ThreadableLoaderClient* m_client; |
188 // Some items may be overridden by m_forceDoNotAllowStoredCredentials | 185 Member<Document> m_document; |
189 // and m_securityOrigin. In such a case, build a ResourceLoaderOptions | |
190 // with up-to-date values from them and this variable, and use it. | |
191 const ResourceLoaderOptions m_resourceLoaderOptions; | |
192 | 186 |
193 bool m_forceDoNotAllowStoredCredentials; | 187 const ThreadableLoaderOptions m_options; |
194 RefPtr<SecurityOrigin> m_securityOrigin; | 188 // Some items may be overridden by m_forceDoNotAllowStoredCredentials |
| 189 // and m_securityOrigin. In such a case, build a ResourceLoaderOptions |
| 190 // with up-to-date values from them and this variable, and use it. |
| 191 const ResourceLoaderOptions m_resourceLoaderOptions; |
195 | 192 |
196 // True while the initial URL and all the URLs of the redirects | 193 bool m_forceDoNotAllowStoredCredentials; |
197 // this object has followed, if any, are same-origin to | 194 RefPtr<SecurityOrigin> m_securityOrigin; |
198 // getSecurityOrigin(). | |
199 bool m_sameOriginRequest; | |
200 // Set to true if the current request is cross-origin and not simple. | |
201 bool m_crossOriginNonSimpleRequest; | |
202 | 195 |
203 // Set to true when the response data is given to a data consumer | 196 // True while the initial URL and all the URLs of the redirects |
204 // handle. | 197 // this object has followed, if any, are same-origin to |
205 bool m_isUsingDataConsumerHandle; | 198 // getSecurityOrigin(). |
| 199 bool m_sameOriginRequest; |
| 200 // Set to true if the current request is cross-origin and not simple. |
| 201 bool m_crossOriginNonSimpleRequest; |
206 | 202 |
207 const bool m_async; | 203 // Set to true when the response data is given to a data consumer |
| 204 // handle. |
| 205 bool m_isUsingDataConsumerHandle; |
208 | 206 |
209 // Holds the original request context (used for sanity checks). | 207 const bool m_async; |
210 WebURLRequest::RequestContext m_requestContext; | |
211 | 208 |
212 // Holds the original request for fallback in case the Service Worker | 209 // Holds the original request context (used for sanity checks). |
213 // does not respond. | 210 WebURLRequest::RequestContext m_requestContext; |
214 ResourceRequest m_fallbackRequestForServiceWorker; | |
215 | 211 |
216 // Holds the original request and options for it during preflight | 212 // Holds the original request for fallback in case the Service Worker |
217 // request handling phase. | 213 // does not respond. |
218 ResourceRequest m_actualRequest; | 214 ResourceRequest m_fallbackRequestForServiceWorker; |
219 ResourceLoaderOptions m_actualOptions; | |
220 | 215 |
221 HTTPHeaderMap m_simpleRequestHeaders; // stores simple request headers i
n case of a cross-origin redirect. | 216 // Holds the original request and options for it during preflight |
222 Timer<DocumentThreadableLoader> m_timeoutTimer; | 217 // request handling phase. |
223 double m_requestStartedSeconds; // Time an asynchronous fetch request is
started | 218 ResourceRequest m_actualRequest; |
| 219 ResourceLoaderOptions m_actualOptions; |
224 | 220 |
225 // Max number of times that this DocumentThreadableLoader can follow | 221 HTTPHeaderMap m_simpleRequestHeaders; // stores simple request headers in ca
se of a cross-origin redirect. |
226 // cross-origin redirects. | 222 Timer<DocumentThreadableLoader> m_timeoutTimer; |
227 // This is used to limit the number of redirects. | 223 double m_requestStartedSeconds; // Time an asynchronous fetch request is sta
rted |
228 // But this value is not the max number of total redirects allowed, | |
229 // because same-origin redirects are not counted here. | |
230 int m_corsRedirectLimit; | |
231 | 224 |
232 WebURLRequest::FetchRedirectMode m_redirectMode; | 225 // Max number of times that this DocumentThreadableLoader can follow |
| 226 // cross-origin redirects. |
| 227 // This is used to limit the number of redirects. |
| 228 // But this value is not the max number of total redirects allowed, |
| 229 // because same-origin redirects are not counted here. |
| 230 int m_corsRedirectLimit; |
233 | 231 |
234 // Holds the referrer after a redirect response was | 232 WebURLRequest::FetchRedirectMode m_redirectMode; |
235 // received. This referrer is used to populate the HTTP Referer | |
236 // header when following the redirect. | |
237 bool m_didRedirect; | |
238 Referrer m_referrerAfterRedirect; | |
239 | 233 |
240 RawResourceClientStateChecker m_checker; | 234 // Holds the referrer after a redirect response was |
241 WeakPtrFactory<DocumentThreadableLoader> m_weakFactory; | 235 // received. This referrer is used to populate the HTTP Referer |
242 }; | 236 // header when following the redirect. |
| 237 bool m_didRedirect; |
| 238 Referrer m_referrerAfterRedirect; |
| 239 |
| 240 RawResourceClientStateChecker m_checker; |
| 241 }; |
243 | 242 |
244 } // namespace blink | 243 } // namespace blink |
245 | 244 |
246 #endif // DocumentThreadableLoader_h | 245 #endif // DocumentThreadableLoader_h |
OLD | NEW |