Chromium Code Reviews| 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& originalRequest, |
| 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 |
| 85 // This is only for overriding cache policy if cache-aware loading is | |
| 86 // activated. | |
| 87 ResourceRequest request(originalRequest); | |
|
kinuko
2016/11/08 12:21:55
Always making an extra copy here seems a bit unfor
Shao-Chuan Lee
2016/11/09 03:48:01
I thought of using pointers but ResourceRequest ha
| |
| 88 | |
| 89 if (m_isCacheAwareLoadingActivated) | |
| 90 request.setCachePolicy(WebCachePolicy::ReturnCacheDataIfValid); | |
| 91 | |
| 82 m_loader = wrapUnique(Platform::current()->createURLLoader()); | 92 m_loader = wrapUnique(Platform::current()->createURLLoader()); |
| 83 DCHECK(m_loader); | 93 DCHECK(m_loader); |
| 84 m_loader->setDefersLoading(defersLoading); | 94 m_loader->setDefersLoading(defersLoading); |
| 85 m_loader->setLoadingTaskRunner(loadingTaskRunner); | 95 m_loader->setLoadingTaskRunner(loadingTaskRunner); |
| 86 | 96 |
| 87 if (m_resource->options().synchronousPolicy == RequestSynchronously) | 97 if (m_resource->options().synchronousPolicy == RequestSynchronously) |
| 88 requestSynchronously(request); | 98 requestSynchronously(request); |
| 89 else | 99 else |
| 90 m_loader->loadAsynchronously(WrappedResourceRequest(request), this); | 100 m_loader->loadAsynchronously(WrappedResourceRequest(request), this); |
| 91 } | 101 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 didFail(ResourceError::cancelledDueToAccessCheckError(newURL)); | 140 didFail(ResourceError::cancelledDueToAccessCheckError(newURL)); |
| 131 } | 141 } |
| 132 | 142 |
| 133 bool ResourceLoader::willFollowRedirect( | 143 bool ResourceLoader::willFollowRedirect( |
| 134 WebURLLoader*, | 144 WebURLLoader*, |
| 135 WebURLRequest& passedNewRequest, | 145 WebURLRequest& passedNewRequest, |
| 136 const WebURLResponse& passedRedirectResponse) { | 146 const WebURLResponse& passedRedirectResponse) { |
| 137 DCHECK(!passedNewRequest.isNull()); | 147 DCHECK(!passedNewRequest.isNull()); |
| 138 DCHECK(!passedRedirectResponse.isNull()); | 148 DCHECK(!passedRedirectResponse.isNull()); |
| 139 | 149 |
| 150 if (m_isCacheAwareLoadingActivated) { | |
| 151 // Fail as cache miss if cached response is a redirect. | |
| 152 didFail( | |
| 153 ResourceError::cacheMissError(m_resource->lastResourceRequest().url())); | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 140 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest()); | 157 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest()); |
| 141 const ResourceResponse& redirectResponse( | 158 const ResourceResponse& redirectResponse( |
| 142 passedRedirectResponse.toResourceResponse()); | 159 passedRedirectResponse.toResourceResponse()); |
| 143 newRequest.setRedirectStatus( | 160 newRequest.setRedirectStatus( |
| 144 ResourceRequest::RedirectStatus::FollowedRedirect); | 161 ResourceRequest::RedirectStatus::FollowedRedirect); |
| 145 | 162 |
| 146 const KURL originalURL = newRequest.url(); | 163 const KURL originalURL = newRequest.url(); |
| 147 | 164 |
| 148 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest, | 165 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest, |
| 149 redirectResponse)) { | 166 redirectResponse)) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 m_loader.reset(); | 236 m_loader.reset(); |
| 220 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength, | 237 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength, |
| 221 ResourceFetcher::DidFinishLoading); | 238 ResourceFetcher::DidFinishLoading); |
| 222 } | 239 } |
| 223 | 240 |
| 224 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) { | 241 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) { |
| 225 didFail(error); | 242 didFail(error); |
| 226 } | 243 } |
| 227 | 244 |
| 228 void ResourceLoader::didFail(const ResourceError& error) { | 245 void ResourceLoader::didFail(const ResourceError& error) { |
| 246 if (m_isCacheAwareLoadingActivated && error.isCacheMiss()) { | |
| 247 m_resource->willReloadAfterDiskCacheMiss(); | |
| 248 m_isCacheAwareLoadingActivated = false; | |
| 249 restart(m_resource->resourceRequest(), | |
| 250 m_fetcher->context().loadingTaskRunner(), | |
| 251 m_fetcher->context().defersLoading()); | |
| 252 return; | |
| 253 } | |
| 254 | |
| 229 m_loader.reset(); | 255 m_loader.reset(); |
| 230 m_fetcher->didFailLoading(m_resource.get(), error); | 256 m_fetcher->didFailLoading(m_resource.get(), error); |
| 231 } | 257 } |
| 232 | 258 |
| 233 void ResourceLoader::requestSynchronously(const ResourceRequest& request) { | 259 void ResourceLoader::requestSynchronously(const ResourceRequest& request) { |
| 234 // downloadToFile is not supported for synchronous requests. | 260 // downloadToFile is not supported for synchronous requests. |
| 235 DCHECK(!request.downloadToFile()); | 261 DCHECK(!request.downloadToFile()); |
| 236 DCHECK(m_loader); | 262 DCHECK(m_loader); |
| 237 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest); | 263 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest); |
| 238 | 264 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 262 // empty buffer is a noop in most cases, but is destructive in the case of | 288 // 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. | 289 // a 304, where it will overwrite the cached data we should be reusing. |
| 264 if (dataOut.size()) { | 290 if (dataOut.size()) { |
| 265 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(), | 291 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(), |
| 266 encodedDataLength); | 292 encodedDataLength); |
| 267 m_resource->setResourceBuffer(dataOut); | 293 m_resource->setResourceBuffer(dataOut); |
| 268 } | 294 } |
| 269 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength); | 295 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength); |
| 270 } | 296 } |
| 271 | 297 |
| 298 void ResourceLoader::activateCacheAwareLoadingIfNeeded( | |
| 299 const ResourceRequest& request) { | |
| 300 DCHECK(!m_isCacheAwareLoadingActivated); | |
| 301 | |
| 302 if (m_resource->options().cacheAwareLoadingEnabled != | |
| 303 IsCacheAwareLoadingEnabled) | |
| 304 return; | |
| 305 | |
| 306 // Synchronous requests are not supported. | |
| 307 if (m_resource->options().synchronousPolicy == RequestSynchronously) | |
| 308 return; | |
| 309 | |
| 310 // Don't activate on Resource revalidation. | |
| 311 if (m_resource->isCacheValidator()) | |
| 312 return; | |
| 313 | |
| 314 // Don't activate if cache policy is explicitly set. | |
| 315 if (request.getCachePolicy() != WebCachePolicy::UseProtocolCachePolicy) | |
| 316 return; | |
| 317 | |
| 318 m_isCacheAwareLoadingActivated = true; | |
| 319 } | |
| 320 | |
| 272 } // namespace blink | 321 } // namespace blink |
| OLD | NEW |