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

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

Issue 2045313003: Dev tools warning and console warning for document.write script blocking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
« no previous file with comments | « third_party/WebKit/Source/core/loader/FrameFetchContext.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 #include "platform/network/ResourceTimingInfo.h" 69 #include "platform/network/ResourceTimingInfo.h"
70 #include "platform/weborigin/SchemeRegistry.h" 70 #include "platform/weborigin/SchemeRegistry.h"
71 #include "platform/weborigin/SecurityPolicy.h" 71 #include "platform/weborigin/SecurityPolicy.h"
72 #include "public/platform/WebCachePolicy.h" 72 #include "public/platform/WebCachePolicy.h"
73 #include "public/platform/WebFrameScheduler.h" 73 #include "public/platform/WebFrameScheduler.h"
74 74
75 #include <algorithm> 75 #include <algorithm>
76 76
77 namespace blink { 77 namespace blink {
78 78
79 namespace { 79 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
80 {
81 String message = "A Parser-blocking, cross-origin script " + url + " is invo ked via document.write. This may get blocked by the browser in slow connections. It is recommended to invoke this script asynchronously.";
82 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.
83 WTFLogAlways("%s", message.utf8().data());
84 }
80 85
81 bool shouldDisallowFetchForMainFrameScript(const ResourceRequest& request, Fetch Request::DeferOption defer, const Document& document) 86 bool FrameFetchContext::shouldDisallowFetchForMainFrameScript(const ResourceRequ est& request, FetchRequest::DeferOption defer) const
82 { 87 {
83 // Only scripts inserted via document.write are candidates for having their 88 // Only scripts inserted via document.write are candidates for having their
84 // fetch disallowed. 89 // fetch disallowed.
85 if (!document.isInDocumentWrite()) 90 if (!m_document->isInDocumentWrite())
86 return false; 91 return false;
87 92
88 if (!document.settings()) 93 if (!m_document->settings())
89 return false; 94 return false;
90 95
91 if (!document.frame()) 96 if (!m_document->frame())
92 return false; 97 return false;
93 98
94 // Only block synchronously loaded (parser blocking) scripts. 99 // Only block synchronously loaded (parser blocking) scripts.
95 if (defer != FetchRequest::NoDefer) 100 if (defer != FetchRequest::NoDefer)
96 return false; 101 return false;
97 102
98 if (!request.url().protocolIsInHTTPFamily()) 103 if (!request.url().protocolIsInHTTPFamily())
99 return false; 104 return false;
100 105
101 // Avoid blocking same origin scripts, as they may be used to render main 106 // Avoid blocking same origin scripts, as they may be used to render main
102 // page content, whereas cross-origin scripts inserted via document.write 107 // page content, whereas cross-origin scripts inserted via document.write
103 // are likely to be third party content. 108 // are likely to be third party content.
104 if (request.url().host() == document.getSecurityOrigin()->domain()) 109 if (request.url().host() == m_document->getSecurityOrigin()->domain())
105 return false; 110 return false;
106 111
107 // Do not block scripts if it is a page reload. This is to enable pages to 112 // Do not block scripts if it is a page reload. This is to enable pages to
108 // recover if blocking of a script is leading to a page break and the user 113 // recover if blocking of a script is leading to a page break and the user
109 // reloads the page. 114 // reloads the page.
110 const FrameLoadType loadType = document.frame()->loader().loadType(); 115 const FrameLoadType loadType = m_document->frame()->loader().loadType();
111 const bool isReload = loadType == FrameLoadTypeReload || loadType == FrameLo adTypeReloadBypassingCache || loadType == FrameLoadTypeReloadMainResource; 116 const bool isReload = loadType == FrameLoadTypeReload || loadType == FrameLo adTypeReloadBypassingCache || loadType == FrameLoadTypeReloadMainResource;
112 if (isReload) { 117 if (isReload) {
113 // Recording this metric since an increase in number of reloads for page s 118 // Recording this metric since an increase in number of reloads for page s
114 // where a script was blocked could be indicative of a page break. 119 // where a script was blocked could be indicative of a page break.
115 document.loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::Web LoadingBehaviorDocumentWriteBlockReload); 120 m_document->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag:: WebLoadingBehaviorDocumentWriteBlockReload);
116 return false; 121 return false;
117 } 122 }
118 123
119 // Add the metadata that this page has scripts inserted via document.write 124 // Add the metadata that this page has scripts inserted via document.write
120 // that are eligible for blocking. Note that if there are multiple scripts 125 // that are eligible for blocking. Note that if there are multiple scripts
121 // the flag will be conveyed to the browser process only once. 126 // the flag will be conveyed to the browser process only once.
122 document.loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebLoad ingBehaviorDocumentWriteBlock); 127 m_document->loader()->didObserveLoadingBehavior(WebLoadingBehaviorFlag::WebL oadingBehaviorDocumentWriteBlock);
128
129 emitWarningForDocWriteScripts(request.url().getString());
123 130
124 const bool isSlowConnection = networkStateNotifier().connectionType() == Web ConnectionTypeCellular2G; 131 const bool isSlowConnection = networkStateNotifier().connectionType() == Web ConnectionTypeCellular2G;
125 const bool disallowFetch = document.settings()->disallowFetchForDocWrittenSc riptsInMainFrame() || (document.settings()->disallowFetchForDocWrittenScriptsInM ainFrameOnSlowConnections() && isSlowConnection); 132 const bool disallowFetch = m_document->settings()->disallowFetchForDocWritte nScriptsInMainFrame() || (m_document->settings()->disallowFetchForDocWrittenScri ptsInMainFrameOnSlowConnections() && isSlowConnection);
126 133
127 return disallowFetch; 134 return disallowFetch;
128 } 135 }
129 136
130 } // namespace
131
132 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document) 137 FrameFetchContext::FrameFetchContext(DocumentLoader* loader, Document* document)
133 : m_document(document) 138 : m_document(document)
134 , m_documentLoader(loader) 139 , m_documentLoader(loader)
135 { 140 {
136 ASSERT(frame()); 141 ASSERT(frame());
137 } 142 }
138 143
139 FrameFetchContext::~FrameFetchContext() 144 FrameFetchContext::~FrameFetchContext()
140 { 145 {
141 m_document = nullptr; 146 m_document = nullptr;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 return WebCachePolicy::BypassingCache; 256 return WebCachePolicy::BypassingCache;
252 if (frameLoadType == FrameLoadTypeReload) 257 if (frameLoadType == FrameLoadTypeReload)
253 return WebCachePolicy::ValidatingCacheData; 258 return WebCachePolicy::ValidatingCacheData;
254 } 259 }
255 return WebCachePolicy::UseProtocolCachePolicy; 260 return WebCachePolicy::UseProtocolCachePolicy;
256 } 261 }
257 262
258 // For users on slow connections, we want to avoid blocking the parser in 263 // For users on slow connections, we want to avoid blocking the parser in
259 // the main frame on script loads inserted via document.write, since it can 264 // the main frame on script loads inserted via document.write, since it can
260 // add significant delays before page content is displayed on the screen. 265 // add significant delays before page content is displayed on the screen.
261 if (type == Resource::Script && isMainFrame() && m_document && shouldDisallo wFetchForMainFrameScript(request, defer, *m_document)) 266 if (type == Resource::Script && isMainFrame() && m_document && shouldDisallo wFetchForMainFrameScript(request, defer))
262 return WebCachePolicy::ReturnCacheDataDontLoad; 267 return WebCachePolicy::ReturnCacheDataDontLoad;
263 268
264 if (request.isConditional()) 269 if (request.isConditional())
265 return WebCachePolicy::ValidatingCacheData; 270 return WebCachePolicy::ValidatingCacheData;
266 271
267 if (m_documentLoader && m_document && !m_document->loadEventFinished()) { 272 if (m_documentLoader && m_document && !m_document->loadEventFinished()) {
268 // For POST requests, we mutate the main resource's cache policy to avoi d form resubmission. 273 // For POST requests, we mutate the main resource's cache policy to avoi d form resubmission.
269 // This policy should not be inherited by subresources. 274 // This policy should not be inherited by subresources.
270 WebCachePolicy mainResourceCachePolicy = m_documentLoader->request().get CachePolicy(); 275 WebCachePolicy mainResourceCachePolicy = m_documentLoader->request().get CachePolicy();
271 if (m_documentLoader->request().httpMethod() == "POST") { 276 if (m_documentLoader->request().httpMethod() == "POST") {
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 return false; 630 return false;
626 info->setInitiatorType(frame()->deprecatedLocalOwner()->localName()); 631 info->setInitiatorType(frame()->deprecatedLocalOwner()->localName());
627 return true; 632 return true;
628 } 633 }
629 634
630 void FrameFetchContext::sendImagePing(const KURL& url) 635 void FrameFetchContext::sendImagePing(const KURL& url)
631 { 636 {
632 PingLoader::loadImage(frame(), url); 637 PingLoader::loadImage(frame(), url);
633 } 638 }
634 639
640 void FrameFetchContext::addConsoleMessage(const String& message, MessageLevel le vel) const
641 {
642 if (frame()->document())
643 frame()->document()->addConsoleMessage(ConsoleMessage::create(JSMessageS ource, level, message));
644 }
645
635 void FrameFetchContext::addConsoleMessage(const String& message) const 646 void FrameFetchContext::addConsoleMessage(const String& message) const
636 { 647 {
637 if (frame()->document()) 648 if (frame()->document())
638 frame()->document()->addConsoleMessage(ConsoleMessage::create(JSMessageS ource, ErrorMessageLevel, message)); 649 frame()->document()->addConsoleMessage(ConsoleMessage::create(JSMessageS ource, ErrorMessageLevel, message));
639 } 650 }
640 651
641 SecurityOrigin* FrameFetchContext::getSecurityOrigin() const 652 SecurityOrigin* FrameFetchContext::getSecurityOrigin() const
642 { 653 {
643 return m_document ? m_document->getSecurityOrigin() : nullptr; 654 return m_document ? m_document->getSecurityOrigin() : nullptr;
644 } 655 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 } 768 }
758 769
759 DEFINE_TRACE(FrameFetchContext) 770 DEFINE_TRACE(FrameFetchContext)
760 { 771 {
761 visitor->trace(m_document); 772 visitor->trace(m_document);
762 visitor->trace(m_documentLoader); 773 visitor->trace(m_documentLoader);
763 FetchContext::trace(visitor); 774 FetchContext::trace(visitor);
764 } 775 }
765 776
766 } // namespace blink 777 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/loader/FrameFetchContext.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698