Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: third_party/WebKit/Source/core/fetch/ResourceLoader.cpp

Issue 2454983002: Cache-aware Resource loading (Closed)
Patch Set: copy ResourceRequest only on CAL activation Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
87 if (m_resource->options().synchronousPolicy == RequestSynchronously) 90 if (m_resource->options().synchronousPolicy == RequestSynchronously) {
kinuko 2016/11/09 04:34:24 nit: maybe should we assert m_isCacheAwareLoadingA
Shao-Chuan Lee 2016/11/09 06:54:32 Now the CAL check is moved before this and returns
88 requestSynchronously(request); 91 requestSynchronously(request);
89 else 92 } else if (m_isCacheAwareLoadingActivated) {
93 // Override cache policy for cache-aware loading. If this request fails, a
94 // reload with original request will be triggered in didFail().
95 ResourceRequest cacheAwareRequest(request);
96 cacheAwareRequest.setCachePolicy(WebCachePolicy::ReturnCacheDataIfValid);
97 m_loader->loadAsynchronously(WrappedResourceRequest(cacheAwareRequest),
98 this);
kinuko 2016/11/09 04:34:24 nit: we can remove else's from this if-elseif-else
99 } else {
90 m_loader->loadAsynchronously(WrappedResourceRequest(request), this); 100 m_loader->loadAsynchronously(WrappedResourceRequest(request), this);
101 }
91 } 102 }
92 103
93 void ResourceLoader::restart(const ResourceRequest& request, 104 void ResourceLoader::restart(const ResourceRequest& request,
94 WebTaskRunner* loadingTaskRunner, 105 WebTaskRunner* loadingTaskRunner,
95 bool defersLoading) { 106 bool defersLoading) {
96 CHECK_EQ(m_resource->options().synchronousPolicy, RequestAsynchronously); 107 CHECK_EQ(m_resource->options().synchronousPolicy, RequestAsynchronously);
97 m_loader.reset(); 108 m_loader.reset();
98 start(request, loadingTaskRunner, defersLoading); 109 start(request, loadingTaskRunner, defersLoading);
99 } 110 }
100 111
(...skipping 29 matching lines...) Expand all
130 didFail(ResourceError::cancelledDueToAccessCheckError(newURL)); 141 didFail(ResourceError::cancelledDueToAccessCheckError(newURL));
131 } 142 }
132 143
133 bool ResourceLoader::willFollowRedirect( 144 bool ResourceLoader::willFollowRedirect(
134 WebURLLoader*, 145 WebURLLoader*,
135 WebURLRequest& passedNewRequest, 146 WebURLRequest& passedNewRequest,
136 const WebURLResponse& passedRedirectResponse) { 147 const WebURLResponse& passedRedirectResponse) {
137 DCHECK(!passedNewRequest.isNull()); 148 DCHECK(!passedNewRequest.isNull());
138 DCHECK(!passedRedirectResponse.isNull()); 149 DCHECK(!passedRedirectResponse.isNull());
139 150
151 if (m_isCacheAwareLoadingActivated) {
152 // Fail as cache miss if cached response is a redirect.
153 didFail(
154 ResourceError::cacheMissError(m_resource->lastResourceRequest().url()));
155 return false;
156 }
157
140 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest()); 158 ResourceRequest& newRequest(passedNewRequest.toMutableResourceRequest());
141 const ResourceResponse& redirectResponse( 159 const ResourceResponse& redirectResponse(
142 passedRedirectResponse.toResourceResponse()); 160 passedRedirectResponse.toResourceResponse());
143 newRequest.setRedirectStatus( 161 newRequest.setRedirectStatus(
144 ResourceRequest::RedirectStatus::FollowedRedirect); 162 ResourceRequest::RedirectStatus::FollowedRedirect);
145 163
146 const KURL originalURL = newRequest.url(); 164 const KURL originalURL = newRequest.url();
147 165
148 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest, 166 if (!m_fetcher->willFollowRedirect(m_resource.get(), newRequest,
149 redirectResponse)) { 167 redirectResponse)) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 m_loader.reset(); 237 m_loader.reset();
220 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength, 238 m_fetcher->didFinishLoading(m_resource.get(), finishTime, encodedDataLength,
221 ResourceFetcher::DidFinishLoading); 239 ResourceFetcher::DidFinishLoading);
222 } 240 }
223 241
224 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) { 242 void ResourceLoader::didFail(WebURLLoader*, const WebURLError& error) {
225 didFail(error); 243 didFail(error);
226 } 244 }
227 245
228 void ResourceLoader::didFail(const ResourceError& error) { 246 void ResourceLoader::didFail(const ResourceError& error) {
247 if (m_isCacheAwareLoadingActivated && error.isCacheMiss()) {
248 m_resource->willReloadAfterDiskCacheMiss();
249 m_isCacheAwareLoadingActivated = false;
250 restart(m_resource->resourceRequest(),
251 m_fetcher->context().loadingTaskRunner(),
252 m_fetcher->context().defersLoading());
253 return;
254 }
255
229 m_loader.reset(); 256 m_loader.reset();
230 m_fetcher->didFailLoading(m_resource.get(), error); 257 m_fetcher->didFailLoading(m_resource.get(), error);
231 } 258 }
232 259
233 void ResourceLoader::requestSynchronously(const ResourceRequest& request) { 260 void ResourceLoader::requestSynchronously(const ResourceRequest& request) {
234 // downloadToFile is not supported for synchronous requests. 261 // downloadToFile is not supported for synchronous requests.
235 DCHECK(!request.downloadToFile()); 262 DCHECK(!request.downloadToFile());
236 DCHECK(m_loader); 263 DCHECK(m_loader);
237 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest); 264 DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest);
238 265
(...skipping 23 matching lines...) Expand all
262 // empty buffer is a noop in most cases, but is destructive in the case of 289 // 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. 290 // a 304, where it will overwrite the cached data we should be reusing.
264 if (dataOut.size()) { 291 if (dataOut.size()) {
265 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(), 292 m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size(),
266 encodedDataLength); 293 encodedDataLength);
267 m_resource->setResourceBuffer(dataOut); 294 m_resource->setResourceBuffer(dataOut);
268 } 295 }
269 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength); 296 didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength);
270 } 297 }
271 298
299 void ResourceLoader::activateCacheAwareLoadingIfNeeded(
300 const ResourceRequest& request) {
301 DCHECK(!m_isCacheAwareLoadingActivated);
302
303 if (m_resource->options().cacheAwareLoadingEnabled !=
304 IsCacheAwareLoadingEnabled)
305 return;
306
307 // Synchronous requests are not supported.
308 if (m_resource->options().synchronousPolicy == RequestSynchronously)
309 return;
310
311 // Don't activate on Resource revalidation.
312 if (m_resource->isCacheValidator())
313 return;
314
315 // Don't activate if cache policy is explicitly set.
316 if (request.getCachePolicy() != WebCachePolicy::UseProtocolCachePolicy)
317 return;
318
319 m_isCacheAwareLoadingActivated = true;
320 }
321
272 } // namespace blink 322 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/ResourceLoader.h ('k') | third_party/WebKit/Source/core/fetch/ResourceLoaderOptions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698