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

Side by Side Diff: third_party/WebKit/Source/core/loader/FrameFetchContext.cpp

Issue 1867493003: remove FetchContext::getCachePolicy() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git merge master Created 3 years, 9 months 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) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 return document.settings() 210 return document.settings()
211 ->getDisallowFetchForDocWrittenScriptsInMainFrame() || 211 ->getDisallowFetchForDocWrittenScriptsInMainFrame() ||
212 (document.settings() 212 (document.settings()
213 ->getDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections () && 213 ->getDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections () &&
214 is2G) || 214 is2G) ||
215 (document.settings() 215 (document.settings()
216 ->getDisallowFetchForDocWrittenScriptsInMainFrameIfEffectively2G() && 216 ->getDisallowFetchForDocWrittenScriptsInMainFrameIfEffectively2G() &&
217 isConnectionEffectively2G(effectiveConnection)); 217 isConnectionEffectively2G(effectiveConnection));
218 } 218 }
219 219
220 enum class RequestMethod { kIsPost, kIsNotPost };
221 enum class RequestType { kIsConditional, kIsNotConditional };
222 enum class ResourceType { kIsMainResource, kIsNotMainResource };
223
224 WebCachePolicy determineWebCachePolicy(RequestMethod method,
225 RequestType requestType,
226 ResourceType resourceType,
227 FrameLoadType loadType) {
228 switch (loadType) {
229 case FrameLoadTypeStandard:
230 return (requestType == RequestType::kIsConditional ||
231 method == RequestMethod::kIsPost)
232 ? WebCachePolicy::ValidatingCacheData
233 : WebCachePolicy::UseProtocolCachePolicy;
234 case FrameLoadTypeReplaceCurrentItem:
235 case FrameLoadTypeInitialInChildFrame:
yhirano 2017/03/17 12:55:48 (resourceType == kIsMainResource && (requestType =
Takashi Toyoshima 2017/03/22 07:15:20 oops. good eyes.
236 // TODO(toyoshim): Should be the same with FrameLoadTypeStandard, but
237 // keep returning UseProtocolCachePolicy to be compatible with previous
238 // behavior for a while.
239 return WebCachePolicy::UseProtocolCachePolicy;
240 case FrameLoadTypeInitialHistoryLoad:
yhirano 2017/03/17 12:55:48 Ditto
Takashi Toyoshima 2017/03/22 07:15:20 Done.
241 // TODO(toyoshim): Should be the same with FrameLoadTypeBackForward, but
242 // keep returning UseProtocolCachePolicy to be compatible with previous
243 // behavior for a while.
244 return WebCachePolicy::UseProtocolCachePolicy;
245 case FrameLoadTypeBackForward:
246 // Mutates the policy for POST requests to avoid form resubmission.
247 return method == RequestMethod::kIsPost
248 ? WebCachePolicy::ReturnCacheDataDontLoad
249 : WebCachePolicy::ReturnCacheDataElseLoad;
250 case FrameLoadTypeReload:
251 return WebCachePolicy::ValidatingCacheData;
252 case FrameLoadTypeReloadMainResource:
253 return resourceType == ResourceType::kIsMainResource
254 ? WebCachePolicy::ValidatingCacheData
255 : WebCachePolicy::UseProtocolCachePolicy;
256 case FrameLoadTypeReloadBypassingCache:
yhirano 2017/03/17 12:55:49 ditto
Takashi Toyoshima 2017/03/22 07:15:20 Done.
257 return WebCachePolicy::BypassingCache;
258 }
259 NOTREACHED();
260 return WebCachePolicy::UseProtocolCachePolicy;
261 }
262
263 // TODO(toyoshim): Remove |resourceType|. See comments in
264 // resourceRequestCachePolicy().
265 WebCachePolicy determineFrameWebCachePolicy(Frame* frame,
266 ResourceType resourceType) {
267 if (!frame)
268 return WebCachePolicy::UseProtocolCachePolicy;
269 if (!frame->isLocalFrame())
270 return determineFrameWebCachePolicy(frame->tree().parent(), resourceType);
271
272 // Does not propagate cache policy for subresources after the load event.
273 // TODO(toyoshim): We should be able to remove following parents' policy check
274 // if each frame has a relevant FrameLoadType for reload and history
275 // navigations.
276 if (resourceType == ResourceType::kIsNotMainResource &&
277 toLocalFrame(frame)->document()->loadEventFinished()) {
278 return WebCachePolicy::UseProtocolCachePolicy;
279 }
280
281 // Respects BypassingCache rather than parent's policy.
282 // TODO(toyoshim): Adopt BypassingCache even for MainResource.
283 FrameLoadType loadType =
284 toLocalFrame(frame)->loader().documentLoader()->loadType();
285 if (resourceType == ResourceType::kIsNotMainResource &&
286 loadType == FrameLoadTypeReloadBypassingCache) {
287 return WebCachePolicy::BypassingCache;
288 }
289
290 // Respects parent's policy if it has a special one.
291 WebCachePolicy parentPolicy =
292 determineFrameWebCachePolicy(frame->tree().parent(), resourceType);
293 if (parentPolicy != WebCachePolicy::UseProtocolCachePolicy)
294 return parentPolicy;
295
296 // Otherwise, follows FrameLoadType. Use kIsNotPost, kIsNotConditional, and
297 // kIsNotMainResource to obtain a representative policy for the frame.
298 return determineWebCachePolicy(RequestMethod::kIsNotPost,
299 RequestType::kIsNotConditional,
300 ResourceType::kIsNotMainResource, loadType);
301 }
302
220 } // namespace 303 } // namespace
221 304
222 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document) 305 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document)
223 : m_document(document), m_documentLoader(loader) { 306 : m_document(document), m_documentLoader(loader) {
224 DCHECK(frame()); 307 DCHECK(frame());
225 } 308 }
226 309
227 FrameFetchContext::~FrameFetchContext() { 310 FrameFetchContext::~FrameFetchContext() {
228 m_document = nullptr; 311 m_document = nullptr;
229 m_documentLoader = nullptr; 312 m_documentLoader = nullptr;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily()) 363 if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
281 return; 364 return;
282 365
283 if (masterDocumentLoader()->loadType() == FrameLoadTypeReload) 366 if (masterDocumentLoader()->loadType() == FrameLoadTypeReload)
284 request.clearHTTPHeaderField("Save-Data"); 367 request.clearHTTPHeaderField("Save-Data");
285 368
286 if (frame()->settings() && frame()->settings()->getDataSaverEnabled()) 369 if (frame()->settings() && frame()->settings()->getDataSaverEnabled())
287 request.setHTTPHeaderField("Save-Data", "on"); 370 request.setHTTPHeaderField("Save-Data", "on");
288 } 371 }
289 372
290 CachePolicy FrameFetchContext::getCachePolicy() const {
291 if (m_document && m_document->loadEventFinished())
292 return CachePolicyVerify;
293
294 FrameLoadType loadType = masterDocumentLoader()->loadType();
295 if (loadType == FrameLoadTypeReloadBypassingCache)
296 return CachePolicyReload;
297
298 Frame* parentFrame = frame()->tree().parent();
299 if (parentFrame && parentFrame->isLocalFrame()) {
300 CachePolicy parentCachePolicy = toLocalFrame(parentFrame)
301 ->document()
302 ->fetcher()
303 ->context()
304 .getCachePolicy();
305 if (parentCachePolicy != CachePolicyVerify)
306 return parentCachePolicy;
307 }
308
309 if (loadType == FrameLoadTypeReload)
310 return CachePolicyRevalidate;
311
312 if (m_documentLoader &&
313 m_documentLoader->getRequest().getCachePolicy() ==
314 WebCachePolicy::ReturnCacheDataElseLoad)
315 return CachePolicyHistoryBuffer;
316
317 // Returns CachePolicyVerify for other cases, mainly FrameLoadTypeStandard and
318 // FrameLoadTypeReloadMainResource. See public/web/WebFrameLoadType.h to know
319 // how these load types work.
320 return CachePolicyVerify;
321 }
322
323 static WebCachePolicy memoryCachePolicyToResourceRequestCachePolicy(
324 const CachePolicy policy) {
325 if (policy == CachePolicyVerify)
326 return WebCachePolicy::UseProtocolCachePolicy;
327 if (policy == CachePolicyRevalidate)
328 return WebCachePolicy::ValidatingCacheData;
329 if (policy == CachePolicyReload)
330 return WebCachePolicy::BypassingCache;
331 if (policy == CachePolicyHistoryBuffer)
332 return WebCachePolicy::ReturnCacheDataElseLoad;
333 return WebCachePolicy::UseProtocolCachePolicy;
334 }
335
336 static WebCachePolicy frameLoadTypeToWebCachePolicy(FrameLoadType type) {
337 if (type == FrameLoadTypeBackForward)
338 return WebCachePolicy::ReturnCacheDataElseLoad;
339 if (type == FrameLoadTypeReloadBypassingCache)
340 return WebCachePolicy::BypassingCache;
341 if (type == FrameLoadTypeReload)
342 return WebCachePolicy::ValidatingCacheData;
343 return WebCachePolicy::UseProtocolCachePolicy;
344 }
345
346 WebCachePolicy FrameFetchContext::resourceRequestCachePolicy( 373 WebCachePolicy FrameFetchContext::resourceRequestCachePolicy(
347 ResourceRequest& request, 374 ResourceRequest& request,
348 Resource::Type type, 375 Resource::Type type,
349 FetchRequest::DeferOption defer) const { 376 FetchRequest::DeferOption defer) const {
350 DCHECK(frame()); 377 DCHECK(frame());
351 if (type == Resource::MainResource) { 378 if (type == Resource::MainResource) {
352 FrameLoadType frameLoadType = masterDocumentLoader()->loadType(); 379 const WebCachePolicy cachePolicy = determineWebCachePolicy(
353 if (request.httpMethod() == "POST" && 380 request.httpMethod() == "POST" ? RequestMethod::kIsPost
354 frameLoadType == FrameLoadTypeBackForward) 381 : RequestMethod::kIsNotPost,
355 return WebCachePolicy::ReturnCacheDataDontLoad; 382 request.isConditional() ? RequestType::kIsConditional
356 if (frameLoadType == FrameLoadTypeReloadMainResource || 383 : RequestType::kIsNotConditional,
357 request.isConditional() || request.httpMethod() == "POST") 384 ResourceType::kIsMainResource, masterDocumentLoader()->loadType());
358 return WebCachePolicy::ValidatingCacheData; 385 // Follows the parent frame's policy.
359 386 // TODO(toyoshim): Probably, FrameLoadType for each frame should have a
360 WebCachePolicy policy = frameLoadTypeToWebCachePolicy(frameLoadType); 387 // right type for reload or history navigations, and should not need to
361 if (policy != WebCachePolicy::UseProtocolCachePolicy) 388 // check parent's frame policy here. Once it has a right FrameLoadType,
362 return policy; 389 // we can remove Resource::Type argument from determineFrameWebCachePolicy.
363 390 // See also crbug.com/332602.
364 for (Frame* f = frame()->tree().parent(); f; f = f->tree().parent()) { 391 if (cachePolicy != WebCachePolicy::UseProtocolCachePolicy)
365 if (!f->isLocalFrame()) 392 return cachePolicy;
366 continue; 393 return determineFrameWebCachePolicy(frame()->tree().parent(),
367 policy = frameLoadTypeToWebCachePolicy( 394 ResourceType::kIsMainResource);
368 toLocalFrame(f)->loader().documentLoader()->loadType());
369 if (policy != WebCachePolicy::UseProtocolCachePolicy)
370 return policy;
371 }
372 // Returns UseProtocolCachePolicy for other cases, parent frames not having
373 // special kinds of FrameLoadType as they are checked inside the for loop
374 // above, or |frameLoadType| being FrameLoadTypeStandard. See
375 // public/web/WebFrameLoadType.h to know how these load types work.
376 return WebCachePolicy::UseProtocolCachePolicy;
377 } 395 }
378 396
379 // For users on slow connections, we want to avoid blocking the parser in 397 // For users on slow connections, we want to avoid blocking the parser in
380 // the main frame on script loads inserted via document.write, since it can 398 // the main frame on script loads inserted via document.write, since it can
381 // add significant delays before page content is displayed on the screen. 399 // add significant delays before page content is displayed on the screen.
400 // TODO(toyoshim): Move following logic that rewrites ResourceRequest to
401 // somewhere that should be relevant to the script resource handling.
382 if (type == Resource::Script && isMainFrame() && m_document && 402 if (type == Resource::Script && isMainFrame() && m_document &&
383 shouldDisallowFetchForMainFrameScript(request, defer, *m_document)) 403 shouldDisallowFetchForMainFrameScript(request, defer, *m_document))
384 return WebCachePolicy::ReturnCacheDataDontLoad; 404 return WebCachePolicy::ReturnCacheDataDontLoad;
385 405
406 // TODO(toyoshim): We should check isConditional() and use ValidatingCacheData
407 // only when |cachePolicy| below is UseProtocolCachePolicy.
386 if (request.isConditional()) 408 if (request.isConditional())
387 return WebCachePolicy::ValidatingCacheData; 409 return WebCachePolicy::ValidatingCacheData;
388 410
389 if (m_documentLoader && m_document && !m_document->loadEventFinished()) { 411 return determineFrameWebCachePolicy(frame(),
390 // For POST requests, we mutate the main resource's cache policy to avoid 412 ResourceType::kIsNotMainResource);
391 // form resubmission. This policy should not be inherited by subresources.
392 WebCachePolicy mainResourceCachePolicy =
393 m_documentLoader->getRequest().getCachePolicy();
394 if (m_documentLoader->getRequest().httpMethod() == "POST") {
395 if (mainResourceCachePolicy == WebCachePolicy::ReturnCacheDataDontLoad)
396 return WebCachePolicy::ReturnCacheDataElseLoad;
397 return WebCachePolicy::UseProtocolCachePolicy;
398 }
399 return memoryCachePolicyToResourceRequestCachePolicy(getCachePolicy());
400 }
401 return WebCachePolicy::UseProtocolCachePolicy;
402 } 413 }
403 414
404 // The |m_documentLoader| is null in the FrameFetchContext of an imported 415 // The |m_documentLoader| is null in the FrameFetchContext of an imported
405 // document. 416 // document.
406 // FIXME(http://crbug.com/274173): This means Inspector, which uses 417 // FIXME(http://crbug.com/274173): This means Inspector, which uses
407 // DocumentLoader as a grouping entity, cannot see imported documents. 418 // DocumentLoader as a grouping entity, cannot see imported documents.
408 inline DocumentLoader* FrameFetchContext::masterDocumentLoader() const { 419 inline DocumentLoader* FrameFetchContext::masterDocumentLoader() const {
409 if (m_documentLoader) 420 if (m_documentLoader)
410 return m_documentLoader.get(); 421 return m_documentLoader.get();
411 422
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 response); 1068 response);
1058 } 1069 }
1059 1070
1060 DEFINE_TRACE(FrameFetchContext) { 1071 DEFINE_TRACE(FrameFetchContext) {
1061 visitor->trace(m_document); 1072 visitor->trace(m_document);
1062 visitor->trace(m_documentLoader); 1073 visitor->trace(m_documentLoader);
1063 FetchContext::trace(visitor); 1074 FetchContext::trace(visitor);
1064 } 1075 }
1065 1076
1066 } // namespace blink 1077 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698