Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 #include "platform/network/ResourceTimingInfo.h" | 68 #include "platform/network/ResourceTimingInfo.h" |
| 69 #include "platform/weborigin/SchemeRegistry.h" | 69 #include "platform/weborigin/SchemeRegistry.h" |
| 70 #include "platform/weborigin/SecurityPolicy.h" | 70 #include "platform/weborigin/SecurityPolicy.h" |
| 71 #include "public/platform/WebCachePolicy.h" | 71 #include "public/platform/WebCachePolicy.h" |
| 72 #include "public/platform/WebFrameScheduler.h" | 72 #include "public/platform/WebFrameScheduler.h" |
| 73 | 73 |
| 74 #include <algorithm> | 74 #include <algorithm> |
| 75 | 75 |
| 76 namespace blink { | 76 namespace blink { |
| 77 | 77 |
| 78 namespace { | |
|
Nate Chapin
2016/04/12 16:53:15
Why this namespace?
Bryan McQuade
2016/04/12 17:28:38
Oh, I'm accustomed to using an anonymous namespace
| |
| 79 | |
| 80 bool ShouldDisallowFetchForMainFrameScript(const ResourceRequest& request, Fetch Request::DeferOption defer, const Document& document) | |
|
Nate Chapin
2016/04/12 16:53:15
Nit: shouldDisallowFetchForMainFrameScript
Bryan McQuade
2016/04/12 17:28:38
Done
| |
| 81 { | |
| 82 // Only scripts inserted via document.write are candidates for having their | |
| 83 // fetch disallowed. | |
| 84 if (!document.isInDocumentWrite()) { | |
|
Nate Chapin
2016/04/12 16:53:15
Here and below, no {} for ifs with single-line bod
Bryan McQuade
2016/04/12 17:28:38
Done
| |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 if (!document.settings()) { | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 const bool isSlowConnection = networkStateNotifier().connectionType() == Web ConnectionTypeCellular2G; | |
| 93 const bool disallowFetch = document.settings()->disallowFetchForDocWrittenSc riptsInMainFrame() || (document.settings()->disallowFetchForDocWrittenScriptsInM ainFrameOnSlowConnections() && isSlowConnection); | |
| 94 if (!disallowFetch) { | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 // Only block synchronously loaded (parser blocking) scripts. | |
| 99 if (defer != FetchRequest::NoDefer) { | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 // Avoid blocking same origin scripts, as they may be used to render main | |
| 104 // page content, whereas cross-origin scripts inserted via document.write | |
| 105 // are likely to be third party content. | |
| 106 if (request.url().host() == document.getSecurityOrigin()->domain()) { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 78 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document) | 115 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document) |
| 79 : m_document(document) | 116 : m_document(document) |
| 80 , m_documentLoader(loader) | 117 , m_documentLoader(loader) |
| 81 , m_imageFetched(false) | 118 , m_imageFetched(false) |
| 82 { | 119 { |
| 83 ASSERT(frame()); | 120 ASSERT(frame()); |
| 84 } | 121 } |
| 85 | 122 |
| 86 FrameFetchContext::~FrameFetchContext() | 123 FrameFetchContext::~FrameFetchContext() |
| 87 { | 124 { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 return WebCachePolicy::BypassingCache; | 235 return WebCachePolicy::BypassingCache; |
| 199 if (frameLoadType == FrameLoadTypeReload) | 236 if (frameLoadType == FrameLoadTypeReload) |
| 200 return WebCachePolicy::ValidatingCacheData; | 237 return WebCachePolicy::ValidatingCacheData; |
| 201 } | 238 } |
| 202 return WebCachePolicy::UseProtocolCachePolicy; | 239 return WebCachePolicy::UseProtocolCachePolicy; |
| 203 } | 240 } |
| 204 | 241 |
| 205 // For users on slow connections, we want to avoid blocking the parser in | 242 // For users on slow connections, we want to avoid blocking the parser in |
| 206 // the main frame on script loads inserted via document.write, since it can | 243 // the main frame on script loads inserted via document.write, since it can |
| 207 // add significant delays before page content is displayed on the screen. | 244 // add significant delays before page content is displayed on the screen. |
| 208 if (type == Resource::Script && isMainFrame()) { | 245 if (type == Resource::Script && isMainFrame() && m_document && ShouldDisallo wFetchForMainFrameScript(request, defer, *m_document)) { |
| 209 const bool isInDocumentWrite = m_document && m_document->isInDocumentWri te(); | 246 return WebCachePolicy::ReturnCacheDataDontLoad; |
| 210 const bool disallowFetchForDocWriteScripts = frame()->settings() && fram e()->settings()->disallowFetchForDocWrittenScriptsInMainFrame(); | |
| 211 | |
| 212 if (isInDocumentWrite && disallowFetchForDocWriteScripts) { | |
| 213 // only synchronously loaded scripts should be blocked | |
| 214 const bool isSync = defer == FetchRequest::NoDefer; | |
| 215 | |
| 216 // Not blocking same origin scripts as they may be used to render ma in page content | |
| 217 // whereas cross-origin scripts inserted via document.write are like ly | |
| 218 // for third party content. | |
| 219 const bool isThirdParty = request.url().host() != m_document->getSec urityOrigin()->domain(); | |
| 220 | |
| 221 // Only blocking in slow connections where the performance penalty i s worst case. | |
| 222 // For now we restrict slow connections to 2G, in future this might be expanded using the | |
| 223 // network quality estimator. | |
| 224 const bool isSlowConnection = networkStateNotifier().connectionType( ) == WebConnectionTypeCellular2G; | |
| 225 | |
| 226 if (isSync && isThirdParty && isSlowConnection) | |
| 227 return WebCachePolicy::ReturnCacheDataDontLoad; | |
| 228 } | |
| 229 } | 247 } |
| 230 | 248 |
| 231 if (request.isConditional()) | 249 if (request.isConditional()) |
| 232 return WebCachePolicy::ValidatingCacheData; | 250 return WebCachePolicy::ValidatingCacheData; |
| 233 | 251 |
| 234 if (m_documentLoader && m_document && !m_document->loadEventFinished()) { | 252 if (m_documentLoader && m_document && !m_document->loadEventFinished()) { |
| 235 // For POST requests, we mutate the main resource's cache policy to avoi d form resubmission. | 253 // For POST requests, we mutate the main resource's cache policy to avoi d form resubmission. |
| 236 // This policy should not be inherited by subresources. | 254 // This policy should not be inherited by subresources. |
| 237 WebCachePolicy mainResourceCachePolicy = m_documentLoader->request().get CachePolicy(); | 255 WebCachePolicy mainResourceCachePolicy = m_documentLoader->request().get CachePolicy(); |
| 238 if (m_documentLoader->request().httpMethod() == "POST") { | 256 if (m_documentLoader->request().httpMethod() == "POST") { |
| (...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 } | 799 } |
| 782 | 800 |
| 783 DEFINE_TRACE(FrameFetchContext) | 801 DEFINE_TRACE(FrameFetchContext) |
| 784 { | 802 { |
| 785 visitor->trace(m_document); | 803 visitor->trace(m_document); |
| 786 visitor->trace(m_documentLoader); | 804 visitor->trace(m_documentLoader); |
| 787 FetchContext::trace(visitor); | 805 FetchContext::trace(visitor); |
| 788 } | 806 } |
| 789 | 807 |
| 790 } // namespace blink | 808 } // namespace blink |
| OLD | NEW |