Stop creating ThrottlingURLLoaders for redirect

RenderFrameImpl::WillSendRequest creates ThrottlingURLLoaders for
redirects, which is weird because ThrottleURLLoader's lifetime spans
across redirects.

Bug: 1112310
Change-Id: Ib40f45cb0f044fea23cf5499ea94f421ad482c56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2335293
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Dominic Farolino <[email protected]>
Commit-Queue: Yutaka Hirano <[email protected]>
Cr-Commit-Position: refs/heads/master@{#795384}
diff --git a/content/renderer/render_frame_impl.cc b/content/renderer/render_frame_impl.cc
index 5b85a0c8..9ab84b4d 100644
--- a/content/renderer/render_frame_impl.cc
+++ b/content/renderer/render_frame_impl.cc
@@ -4582,17 +4582,19 @@
   }
 }
 
-void RenderFrameImpl::WillSendRequest(blink::WebURLRequest& request) {
+void RenderFrameImpl::WillSendRequest(blink::WebURLRequest& request,
+                                      ForRedirect for_redirect) {
   // This method is called for subresources, while transition type is
   // a navigation concept. We pass ui::PAGE_TRANSITION_LINK as default one.
   WillSendRequestInternal(request, /*for_main_frame=*/false,
-                          ui::PAGE_TRANSITION_LINK);
+                          ui::PAGE_TRANSITION_LINK, for_redirect);
 }
 
 void RenderFrameImpl::WillSendRequestInternal(
     blink::WebURLRequest& request,
     bool for_main_frame,
-    ui::PageTransition transition_type) {
+    ui::PageTransition transition_type,
+    ForRedirect for_redirect) {
   if (render_view_->renderer_preferences_.enable_do_not_track) {
     request.SetHttpHeaderField(blink::WebString::FromUTF8(kDoNotTrackHeader),
                                "1");
@@ -4652,7 +4654,8 @@
   // The RenderThreadImpl or its URLLoaderThrottleProvider member may not be
   // valid in some tests.
   RenderThreadImpl* render_thread = RenderThreadImpl::current();
-  if (render_thread && render_thread->url_loader_throttle_provider()) {
+  if (!for_redirect && render_thread &&
+      render_thread->url_loader_throttle_provider()) {
     extra_data->set_url_loader_throttles(
         render_thread->url_loader_throttle_provider()->CreateThrottles(
             routing_id_, request));
@@ -6006,7 +6009,8 @@
   // TODO(clamy): Make sure that navigation requests are not modified somewhere
   // else in blink.
   bool for_main_frame = !frame_->Parent();
-  WillSendRequestInternal(request, for_main_frame, transition_type);
+  WillSendRequestInternal(request, for_main_frame, transition_type,
+                          ForRedirect(false));
 
   if (!info->url_request.GetExtraData())
     info->url_request.SetExtraData(base::MakeRefCounted<RequestExtraData>());
diff --git a/content/renderer/render_frame_impl.h b/content/renderer/render_frame_impl.h
index e5e67da..ede4600 100644
--- a/content/renderer/render_frame_impl.h
+++ b/content/renderer/render_frame_impl.h
@@ -707,7 +707,8 @@
   void FocusedElementChanged(const blink::WebElement& element) override;
   void OnMainFrameIntersectionChanged(
       const blink::WebRect& intersect_rect) override;
-  void WillSendRequest(blink::WebURLRequest& request) override;
+  void WillSendRequest(blink::WebURLRequest& request,
+                       ForRedirect for_redirect) override;
   void DidLoadResourceFromMemoryCache(
       const blink::WebURLRequest& request,
       const blink::WebURLResponse& response) override;
@@ -1108,7 +1109,8 @@
   // |transition_type| corresponds to the document which triggered this request.
   void WillSendRequestInternal(blink::WebURLRequest& request,
                                bool for_main_frame,
-                               ui::PageTransition transition_type);
+                               ui::PageTransition transition_type,
+                               ForRedirect for_redirect);
 
   // Returns the URL being loaded by the |frame_|'s request.
   GURL GetLoadingUrl() const;
diff --git a/content/renderer/render_frame_impl_browsertest.cc b/content/renderer/render_frame_impl_browsertest.cc
index 7f230d8..f043795f 100644
--- a/content/renderer/render_frame_impl_browsertest.cc
+++ b/content/renderer/render_frame_impl_browsertest.cc
@@ -452,7 +452,8 @@
   for (const auto& test_case : kTestCases) {
     WebURLRequest request;
     request.SetUrl(GURL(test_case.original));
-    GetMainRenderFrame()->WillSendRequest(request);
+    GetMainRenderFrame()->WillSendRequest(
+        request, blink::WebLocalFrameClient::ForRedirect(false));
     EXPECT_EQ(test_case.transformed, request.Url().GetString().Utf8());
   }
 }
diff --git a/content/shell/renderer/web_test/web_frame_test_proxy.cc b/content/shell/renderer/web_test/web_frame_test_proxy.cc
index 9562666..51e32e2 100644
--- a/content/shell/renderer/web_test/web_frame_test_proxy.cc
+++ b/content/shell/renderer/web_test/web_frame_test_proxy.cc
@@ -428,8 +428,9 @@
   RenderFrameImpl::DidDispatchPingLoader(url);
 }
 
-void WebFrameTestProxy::WillSendRequest(blink::WebURLRequest& request) {
-  RenderFrameImpl::WillSendRequest(request);
+void WebFrameTestProxy::WillSendRequest(blink::WebURLRequest& request,
+                                        ForRedirect for_redirect) {
+  RenderFrameImpl::WillSendRequest(request, for_redirect);
 
   // Need to use GURL for host() and SchemeIs()
   GURL url = request.Url();
diff --git a/content/shell/renderer/web_test/web_frame_test_proxy.h b/content/shell/renderer/web_test/web_frame_test_proxy.h
index 92293c5..330f4b56 100644
--- a/content/shell/renderer/web_test/web_frame_test_proxy.h
+++ b/content/shell/renderer/web_test/web_frame_test_proxy.h
@@ -72,7 +72,8 @@
   void ShowContextMenu(const blink::WebContextMenuData& context_menu_data,
                        const base::Optional<gfx::Point>&) override;
   void DidDispatchPingLoader(const blink::WebURL& url) override;
-  void WillSendRequest(blink::WebURLRequest& request) override;
+  void WillSendRequest(blink::WebURLRequest& request,
+                       ForRedirect for_redirect) override;
   void BeginNavigation(std::unique_ptr<blink::WebNavigationInfo> info) override;
   void PostAccessibilityEvent(const ui::AXEvent& event) override;
   void MarkWebAXObjectDirty(const blink::WebAXObject& object,
diff --git a/third_party/blink/public/web/web_local_frame_client.h b/third_party/blink/public/web/web_local_frame_client.h
index cc7c11d6..6d2a073 100644
--- a/third_party/blink/public/web/web_local_frame_client.h
+++ b/third_party/blink/public/web/web_local_frame_client.h
@@ -444,10 +444,11 @@
 
   // Low-level resource notifications ------------------------------------
 
+  using ForRedirect = util::StrongAlias<class ForRedirectTag, bool>;
   // A request is about to be sent out, and the client may modify it.  Request
   // is writable, and changes to the URL, for example, will change the request
   // made.
-  virtual void WillSendRequest(WebURLRequest&) {}
+  virtual void WillSendRequest(WebURLRequest&, ForRedirect) {}
 
   // The specified request was satified from WebCore's memory cache.
   virtual void DidLoadResourceFromMemoryCache(const WebURLRequest&,
diff --git a/third_party/blink/renderer/core/exported/local_frame_client_impl.cc b/third_party/blink/renderer/core/exported/local_frame_client_impl.cc
index 887c0c0..2e00b61 100644
--- a/third_party/blink/renderer/core/exported/local_frame_client_impl.cc
+++ b/third_party/blink/renderer/core/exported/local_frame_client_impl.cc
@@ -489,7 +489,9 @@
   // Give the WebLocalFrameClient a crack at the request.
   if (web_frame_->Client()) {
     WrappedResourceRequest webreq(request);
-    web_frame_->Client()->WillSendRequest(webreq);
+    web_frame_->Client()->WillSendRequest(
+        webreq, WebLocalFrameClient::ForRedirect(
+                    request.GetRedirectInfo().has_value()));
   }
 }
 
diff --git a/third_party/blink/renderer/core/exported/web_frame_test.cc b/third_party/blink/renderer/core/exported/web_frame_test.cc
index aea2d2d..234a5c7 100644
--- a/third_party/blink/renderer/core/exported/web_frame_test.cc
+++ b/third_party/blink/renderer/core/exported/web_frame_test.cc
@@ -7305,7 +7305,8 @@
   ~TestSameDocumentWithImageWebFrameClient() override = default;
 
   // frame_test_helpers::TestWebFrameClient:
-  void WillSendRequest(WebURLRequest& request) override {
+  void WillSendRequest(WebURLRequest& request,
+                       ForRedirect for_redirect) override {
     if (request.GetRequestContext() == mojom::RequestContextType::IMAGE) {
       num_of_image_requests_++;
       EXPECT_EQ(mojom::FetchCacheMode::kDefault, request.GetCacheMode());
@@ -11320,7 +11321,8 @@
   ~TestResourcePriorityWebFrameClient() override = default;
 
   // frame_test_helpers::TestWebFrameClient:
-  void WillSendRequest(WebURLRequest& request) override {
+  void WillSendRequest(WebURLRequest& request,
+                       ForRedirect for_redirect) override {
     ExpectedRequest* expected_request = expected_requests_.at(request.Url());
     DCHECK(expected_request);
     EXPECT_EQ(expected_request->priority, request.GetPriority());