Chromium Code Reviews| Index: third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| diff --git a/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp b/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| index c08ee5f31a0503f3eaff2cf213ad4023c961e87e..a1dff2a3b909478411e4faa936f6eddc4d6ca9a2 100644 |
| --- a/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| +++ b/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| @@ -76,19 +76,24 @@ |
| namespace blink { |
| -namespace { |
| +void FrameFetchContext::emitWarningForDocWriteScripts(const String& url) const |
|
Nate Chapin
2016/06/09 23:44:44
Maybe take the a const Document& here and keep thi
|
| +{ |
| + String message = "A Parser-blocking, cross-origin script " + url + " is invoked via document.write. This may get blocked by the browser in slow connections. It is recommended to invoke this script asynchronously."; |
| + addConsoleMessage(message, WarningMessageLevel); |
|
Nate Chapin
2016/06/09 23:44:44
Call frame()->document()->addConsoleMessage() dire
shivanisha
2016/06/10 14:41:01
Sounds good. Made these changes.
|
| + WTFLogAlways("%s", message.utf8().data()); |
| +} |
| -bool shouldDisallowFetchForMainFrameScript(const ResourceRequest& request, FetchRequest::DeferOption defer, const Document& document) |
| +bool FrameFetchContext::shouldDisallowFetchForMainFrameScript(const ResourceRequest& request, FetchRequest::DeferOption defer) const |
| { |
| // Only scripts inserted via document.write are candidates for having their |
| // fetch disallowed. |
| - if (!document.isInDocumentWrite()) |
| + if (!m_document->isInDocumentWrite()) |
| return false; |
| - if (!document.settings()) |
| + if (!m_document->settings()) |
| return false; |
| - if (!document.frame()) |
| + if (!m_document->frame()) |
| return false; |
| // Only block synchronously loaded (parser blocking) scripts. |
| @@ -101,34 +106,34 @@ bool shouldDisallowFetchForMainFrameScript(const ResourceRequest& request, Fetch |
| // Avoid blocking same origin scripts, as they may be used to render main |
| // page content, whereas cross-origin scripts inserted via document.write |
| // are likely to be third party content. |
| - if (request.url().host() == document.getSecurityOrigin()->domain()) |
| + if (request.url().host() == m_document->getSecurityOrigin()->domain()) |
| return false; |
| // Do not block scripts if it is a page reload. This is to enable pages to |
| // recover if blocking of a script is leading to a page break and the user |
| // reloads the page. |
| - const FrameLoadType loadType = document.frame()->loader().loadType(); |
| + const FrameLoadType loadType = m_document->frame()->loader().loadType(); |
| const bool isReload = loadType == FrameLoadTypeReload || loadType == FrameLoadTypeReloadBypassingCache || loadType == FrameLoadTypeReloadMainResource; |
| if (isReload) { |
| // Recording this metric since an increase in number of reloads for pages |
| // where a script was blocked could be indicative of a page break. |
| - document.loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoadingBehaviorDocumentWriteBlockReload); |
| + m_document->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoadingBehaviorDocumentWriteBlockReload); |
| return false; |
| } |
| // Add the metadata that this page has scripts inserted via document.write |
| // that are eligible for blocking. Note that if there are multiple scripts |
| // the flag will be conveyed to the browser process only once. |
| - document.loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoadingBehaviorDocumentWriteBlock); |
| + m_document->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoadingBehaviorDocumentWriteBlock); |
| + |
| + emitWarningForDocWriteScripts(request.url().getString()); |
| const bool isSlowConnection = networkStateNotifier().connectionType() == WebConnectionTypeCellular2G; |
| - const bool disallowFetch = document.settings()->disallowFetchForDocWrittenScriptsInMainFrame() || (document.settings()->disallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections() && isSlowConnection); |
| + const bool disallowFetch = m_document->settings()->disallowFetchForDocWrittenScriptsInMainFrame() || (m_document->settings()->disallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections() && isSlowConnection); |
| return disallowFetch; |
| } |
| -} // namespace |
| - |
| FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document) |
| : m_document(document) |
| , m_documentLoader(loader) |
| @@ -258,7 +263,7 @@ WebCachePolicy FrameFetchContext::resourceRequestCachePolicy(const ResourceReque |
| // For users on slow connections, we want to avoid blocking the parser in |
| // the main frame on script loads inserted via document.write, since it can |
| // add significant delays before page content is displayed on the screen. |
| - if (type == Resource::Script && isMainFrame() && m_document && shouldDisallowFetchForMainFrameScript(request, defer, *m_document)) |
| + if (type == Resource::Script && isMainFrame() && m_document && shouldDisallowFetchForMainFrameScript(request, defer)) |
| return WebCachePolicy::ReturnCacheDataDontLoad; |
| if (request.isConditional()) |
| @@ -632,6 +637,12 @@ void FrameFetchContext::sendImagePing(const KURL& url) |
| PingLoader::loadImage(frame(), url); |
| } |
| +void FrameFetchContext::addConsoleMessage(const String& message, MessageLevel level) const |
| +{ |
| + if (frame()->document()) |
| + frame()->document()->addConsoleMessage(ConsoleMessage::create(JSMessageSource, level, message)); |
| +} |
| + |
| void FrameFetchContext::addConsoleMessage(const String& message) const |
| { |
| if (frame()->document()) |