| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2010, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2010, 2011 Apple Inc. All rights reserved. |
| 3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com) | 3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com) |
| 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 | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. 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 19 matching lines...) Expand all Loading... |
| 30 #include "core/fetch/ResourceLoader.h" | 30 #include "core/fetch/ResourceLoader.h" |
| 31 | 31 |
| 32 #include "core/fetch/CSSStyleSheetResource.h" | 32 #include "core/fetch/CSSStyleSheetResource.h" |
| 33 #include "core/fetch/Resource.h" | 33 #include "core/fetch/Resource.h" |
| 34 #include "core/fetch/ResourceFetcher.h" | 34 #include "core/fetch/ResourceFetcher.h" |
| 35 #include "platform/SharedBuffer.h" | 35 #include "platform/SharedBuffer.h" |
| 36 #include "platform/exported/WrappedResourceRequest.h" | 36 #include "platform/exported/WrappedResourceRequest.h" |
| 37 #include "platform/exported/WrappedResourceResponse.h" | 37 #include "platform/exported/WrappedResourceResponse.h" |
| 38 #include "platform/network/ResourceError.h" | 38 #include "platform/network/ResourceError.h" |
| 39 #include "public/platform/Platform.h" | 39 #include "public/platform/Platform.h" |
| 40 #include "public/platform/WebCachePolicy.h" |
| 40 #include "public/platform/WebData.h" | 41 #include "public/platform/WebData.h" |
| 41 #include "public/platform/WebURLError.h" | 42 #include "public/platform/WebURLError.h" |
| 42 #include "public/platform/WebURLRequest.h" | 43 #include "public/platform/WebURLRequest.h" |
| 43 #include "public/platform/WebURLResponse.h" | 44 #include "public/platform/WebURLResponse.h" |
| 44 #include "wtf/Assertions.h" | 45 #include "wtf/Assertions.h" |
| 45 #include "wtf/CurrentTime.h" | 46 #include "wtf/CurrentTime.h" |
| 46 #include "wtf/PtrUtil.h" | 47 #include "wtf/PtrUtil.h" |
| 47 #include <memory> | 48 #include <memory> |
| 48 | 49 |
| 49 namespace blink { | 50 namespace blink { |
| 50 | 51 |
| 51 ResourceLoader* ResourceLoader::create(ResourceFetcher* fetcher, | 52 ResourceLoader* ResourceLoader::create(ResourceFetcher* fetcher, |
| 52 Resource* resource) { | 53 Resource* resource) { |
| 53 return new ResourceLoader(fetcher, resource); | 54 return new ResourceLoader(fetcher, resource); |
| 54 } | 55 } |
| 55 | 56 |
| 56 ResourceLoader::ResourceLoader(ResourceFetcher* fetcher, Resource* resource) | 57 ResourceLoader::ResourceLoader(ResourceFetcher* fetcher, Resource* resource) |
| 57 : m_fetcher(fetcher), m_resource(resource) { | 58 : m_fetcher(fetcher), |
| 59 m_resource(resource), |
| 60 m_isCacheAwareLoadingActivated(false) { |
| 58 DCHECK(m_resource); | 61 DCHECK(m_resource); |
| 59 DCHECK(m_fetcher); | 62 DCHECK(m_fetcher); |
| 60 m_resource->setLoader(this); | 63 m_resource->setLoader(this); |
| 61 } | 64 } |
| 62 | 65 |
| 63 ResourceLoader::~ResourceLoader() { | 66 ResourceLoader::~ResourceLoader() { |
| 64 DCHECK(!m_loader); | 67 DCHECK(!m_loader); |
| 65 } | 68 } |
| 66 | 69 |
| 67 DEFINE_TRACE(ResourceLoader) { | 70 DEFINE_TRACE(ResourceLoader) { |
| 68 visitor->trace(m_fetcher); | 71 visitor->trace(m_fetcher); |
| 69 visitor->trace(m_resource); | 72 visitor->trace(m_resource); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void ResourceLoader::start(const ResourceRequest& request, | 75 void ResourceLoader::start(const ResourceRequest& request, |
| 73 WebTaskRunner* loadingTaskRunner, | 76 WebTaskRunner* loadingTaskRunner, |
| 74 bool defersLoading) { | 77 bool defersLoading) { |
| 75 DCHECK(!m_loader); | 78 DCHECK(!m_loader); |
| 76 if (m_resource->options().synchronousPolicy == RequestSynchronously && | 79 if (m_resource->options().synchronousPolicy == RequestSynchronously && |
| 77 defersLoading) { | 80 defersLoading) { |
| 78 cancel(); | 81 cancel(); |
| 79 return; | 82 return; |
| 80 } | 83 } |
| 81 | 84 |
| 82 m_loader = wrapUnique(Platform::current()->createURLLoader()); | 85 m_loader = wrapUnique(Platform::current()->createURLLoader()); |
| 83 DCHECK(m_loader); | 86 DCHECK(m_loader); |
| 84 m_loader->setDefersLoading(defersLoading); | 87 m_loader->setDefersLoading(defersLoading); |
| 85 m_loader->setLoadingTaskRunner(loadingTaskRunner); | 88 m_loader->setLoadingTaskRunner(loadingTaskRunner); |
| 86 | 89 |
| 90 if (m_isCacheAwareLoadingActivated) { |
| 91 // Override cache policy for cache-aware loading. If this request fails, a |
| 92 // reload with original request will be triggered in didFail(). |
| 93 ResourceRequest cacheAwareRequest(request); |
| 94 cacheAwareRequest.setCachePolicy(WebCachePolicy::ReturnCacheDataIfValid); |
| 95 m_loader->loadAsynchronously(WrappedResourceRequest(cacheAwareRequest), |
| 96 this); |
| 97 return; |
| 98 } |
| 99 |
| 87 if (m_resource->options().synchronousPolicy == RequestSynchronously) | 100 if (m_resource->options().synchronousPolicy == RequestSynchronously) |
| 88 requestSynchronously(request); | 101 requestSynchronously(request); |
| 89 else | 102 else |
| 90 m_loader->loadAsynchronously(WrappedResourceRequest(request), this); | 103 m_loader->loadAsynchronously(WrappedResourceRequest(request), this); |
| 91 } | 104 } |
| 92 | 105 |
| 93 void ResourceLoader::restart(const ResourceRequest& request, | 106 void ResourceLoader::restart(const ResourceRequest& request, |
| 94 WebTaskRunner* loadingTaskRunner, | 107 WebTaskRunner* loadingTaskRunner, |
| 95 bool defersLoading) { | 108 bool defersLoading) { |
| 96 CHECK_EQ(m_resource->options().synchronousPolicy, RequestAsynchronously); | 109 CHECK_EQ(m_resource->options().synchronousPolicy, RequestAsynchronously); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 didFail(ResourceError::cancelledDueToAccessCheckError(newURL)); | 143 didFail(ResourceError::cancelledDueToAccessCheckError(newURL)); |
| 131 } | 144 } |
| 132 | 145 |
| 133 bool ResourceLoader::willFollowRedirect( | 146 bool ResourceLoader::willFollowRedirect( |
| 134 WebURLLoader*, | 147 WebURLLoader*, |
| 135 WebURLRequest& passedNewRequest, | 148 WebURLRequest& passedNewRequest, |
| 136 const WebURLResponse& passedRedirectResponse) { | 149 const WebURLResponse& passedRedirectResponse) { |
| 137 DCHECK(!passedNewRequest.isNull()); | 150 DCHECK(!passedNewRequest.isNull()); |
| 138 DCHECK(!passedRedirectResponse.isNull()); | 151 DCHECK(!passedRedirectResponse.isNull()); |
| 139 | 152 |
| 153 if (m_isCacheAwareLoadingActivated) { |
| 154 // Fail as cache miss if cached response is a redirect. |
| 155 didFail( |
| 156 ResourceError::cacheMissError(m_resource->lastResourceRequest().url())); |
| 157 return false; |
| 158 } |
| 159 |
| 140 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest()); | 160 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest()); |
| 141 const ResourceResponse& redirectResponse( | 161 const ResourceResponse& redirectResponse( |
| 142 passedRedirectResponse.toResourceResponse()); | 162 passedRedirectResponse.toResourceResponse()); |
| 143 newRequest.setRedirectStatus( | 163 newRequest.setRedirectStatus( |
| 144 ResourceRequest::RedirectStatus::FollowedRedirect); | 164 ResourceRequest::RedirectStatus::FollowedRedirect); |
| 145 | 165 |
| 146 const KURL originalURL = newRequest.url(); | 166 const KURL originalURL = newRequest.url(); |
| 147 | 167 |
| 148 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest, | 168 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest, |
| 149 redirectResponse)) { | 169 redirectResponse)) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 m_loader.reset(); | 239 m_loader.reset(); |
| 220 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength, | 240 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength, |
| 221 ResourceFetcher::DidFinishLoading); | 241 ResourceFetcher::DidFinishLoading); |
| 222 } | 242 } |
| 223 | 243 |
| 224 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) { | 244 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) { |
| 225 didFail(error); | 245 didFail(error); |
| 226 } | 246 } |
| 227 | 247 |
| 228 void ResourceLoader::didFail(const ResourceError& error) { | 248 void ResourceLoader::didFail(const ResourceError& error) { |
| 249 if (m_isCacheAwareLoadingActivated && error.isCacheMiss()) { |
| 250 m_resource->willReloadAfterDiskCacheMiss(); |
| 251 m_isCacheAwareLoadingActivated = false; |
| 252 restart(m_resource->resourceRequest(), |
| 253 m_fetcher->context().loadingTaskRunner(), |
| 254 m_fetcher->context().defersLoading()); |
| 255 return; |
| 256 } |
| 257 |
| 229 m_loader.reset(); | 258 m_loader.reset(); |
| 230 m_fetcher->didFailLoading(m_resource.get(), error); | 259 m_fetcher->didFailLoading(m_resource.get(), error); |
| 231 } | 260 } |
| 232 | 261 |
| 233 void ResourceLoader::requestSynchronously(const ResourceRequest& request) { | 262 void ResourceLoader::requestSynchronously(const ResourceRequest& request) { |
| 234 // downloadToFile is not supported for synchronous requests. | 263 // downloadToFile is not supported for synchronous requests. |
| 235 DCHECK(!request.downloadToFile()); | 264 DCHECK(!request.downloadToFile()); |
| 236 DCHECK(m_loader); | 265 DCHECK(m_loader); |
| 237 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest); | 266 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest); |
| 238 | 267 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 262 // empty buffer is a noop in most cases, but is destructive in the case of | 291 // empty buffer is a noop in most cases, but is destructive in the case of |
| 263 // a 304, where it will overwrite the cached data we should be reusing. | 292 // a 304, where it will overwrite the cached data we should be reusing. |
| 264 if (dataOut.size()) { | 293 if (dataOut.size()) { |
| 265 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(), | 294 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(), |
| 266 encodedDataLength); | 295 encodedDataLength); |
| 267 m_resource->setResourceBuffer(dataOut); | 296 m_resource->setResourceBuffer(dataOut); |
| 268 } | 297 } |
| 269 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength); | 298 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength); |
| 270 } | 299 } |
| 271 | 300 |
| 301 void ResourceLoader::activateCacheAwareLoadingIfNeeded( |
| 302 const ResourceRequest& request) { |
| 303 DCHECK(!m_isCacheAwareLoadingActivated); |
| 304 |
| 305 if (m_resource->options().cacheAwareLoadingEnabled != |
| 306 IsCacheAwareLoadingEnabled) |
| 307 return; |
| 308 |
| 309 // Synchronous requests are not supported. |
| 310 if (m_resource->options().synchronousPolicy == RequestSynchronously) |
| 311 return; |
| 312 |
| 313 // Don't activate on Resource revalidation. |
| 314 if (m_resource->isCacheValidator()) |
| 315 return; |
| 316 |
| 317 // Don't activate if cache policy is explicitly set. |
| 318 if (request.getCachePolicy() != WebCachePolicy::UseProtocolCachePolicy) |
| 319 return; |
| 320 |
| 321 m_isCacheAwareLoadingActivated = true; |
| 322 } |
| 323 |
| 272 } // namespace blink | 324 } // namespace blink |
| OLD | NEW |