Port PrerenderResourceThrottle to work with Network Service.

This throttle is used for Prerendering and NoStatePrefetch. A new URLLoaderThrottle,
PrerenderURLLoaderThrottle, is created as a replacement. It's used for subresources in the renderer
 when both the network service is enabled and disabled. In the browser, PrerenderURLLoaderThrottle
is used if the network service is enabled. When network service is disabled,
PrerenderResourceThrottle is still used in the browser until URLLoader is used
(http://crbug.com/740130).

Two behavior change with NoStatePrefetch that were needed:
1) The subresource data is sent to the renderer. This is needed
so that the URLLoaderThrottle gets notifications. The data isn't delivered to Blink but is just
consumed, which maintains the previous intent of not storing increasing renderer memory by having
this data in Blink's caches.
2) The prefetch renderer is kept alive until all fetched subresources complete. Previously it was
kept alive only until the main resource is parsed. This is needed to ensure all the subresources
get put into the cache and the URLLoaderThrottle get their notifications for histogramming
purposes.

Bug: 769401
Change-Id: I6fe30e6c48d4791b30530e4e45bf8061d4b04db6
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_mojo
Reviewed-on: https://chromium-review.googlesource.com/845114
Reviewed-by: Tom Sepez <[email protected]>
Reviewed-by: Nate Chapin <[email protected]>
Reviewed-by: Alexei Svitkine <[email protected]>
Reviewed-by: Matthew Cary <[email protected]>
Commit-Queue: John Abd-El-Malek <[email protected]>
Cr-Commit-Position: refs/heads/master@{#527760}

TBR=tsepez,japhet,asvitkine,mattcary
for reland after conflict with another cl

Change-Id: I6fe30e6c48d4791b30530e4e45bf8061d4b04db6
Reviewed-on: https://chromium-review.googlesource.com/855041
Commit-Queue: John Abd-El-Malek <[email protected]>
Reviewed-by: John Abd-El-Malek <[email protected]>
Cr-Commit-Position: refs/heads/master@{#527821}
diff --git a/chrome/browser/prerender/prerender_contents.cc b/chrome/browser/prerender/prerender_contents.cc
index c7c70cc..6c94f3f5 100644
--- a/chrome/browser/prerender/prerender_contents.cc
+++ b/chrome/browser/prerender/prerender_contents.cc
@@ -23,12 +23,14 @@
 #include "chrome/browser/prerender/prerender_manager.h"
 #include "chrome/browser/prerender/prerender_manager_factory.h"
 #include "chrome/browser/prerender/prerender_resource_throttle.h"
+#include "chrome/browser/prerender/prerender_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/task_manager/web_contents_tags.h"
 #include "chrome/browser/ui/tab_helpers.h"
 #include "chrome/browser/ui/web_contents_sizer.h"
 #include "chrome/common/prerender_messages.h"
 #include "chrome/common/prerender_types.h"
+#include "chrome/common/prerender_util.h"
 #include "components/history/core/browser/history_types.h"
 #include "content/public/browser/browser_child_process_host.h"
 #include "content/public/browser/browser_thread.h"
@@ -57,16 +59,6 @@
 
 namespace {
 
-// Valid HTTP methods for both prefetch and prerendering.
-const char* const kValidHttpMethods[] = {
-    "GET", "HEAD",
-};
-
-// Additional valid HTTP methods for prerendering.
-const char* const kValidHttpMethodsForPrerendering[] = {
-    "OPTIONS", "POST", "TRACE",
-};
-
 void ResumeThrottles(
     std::vector<base::WeakPtr<PrerenderResourceThrottle>> throttles,
     std::vector<base::WeakPtr<PrerenderResourceThrottle>> idle_resources) {
@@ -232,27 +224,6 @@
   prerender_mode_ = mode;
 }
 
-bool PrerenderContents::IsValidHttpMethod(const std::string& method) {
-  DCHECK_NE(prerender_mode(), NO_PRERENDER);
-  // |method| has been canonicalized to upper case at this point so we can just
-  // compare them.
-  DCHECK_EQ(method, base::ToUpperASCII(method));
-  for (auto* valid_method : kValidHttpMethods) {
-    if (method == valid_method)
-      return true;
-  }
-
-  if (prerender_mode() == PREFETCH_ONLY)
-    return false;
-
-  for (auto* valid_method : kValidHttpMethodsForPrerendering) {
-    if (method == valid_method)
-      return true;
-  }
-
-  return false;
-}
-
 // static
 PrerenderContents::Factory* PrerenderContents::CreateFactory() {
   return new PrerenderContentsFactoryImpl();
@@ -538,7 +509,8 @@
   // occur.  Note that this is always triggered before the first navigation, so
   // there's no need to send the message just after the WebContents is created.
   render_frame_host->Send(new PrerenderMsg_SetIsPrerendering(
-      render_frame_host->GetRoutingID(), prerender_mode_));
+      render_frame_host->GetRoutingID(), prerender_mode_,
+      PrerenderHistograms::GetHistogramPrefix(origin_)));
 }
 
 void PrerenderContents::DidStopLoading() {
@@ -738,8 +710,8 @@
   SetFinalStatus(FINAL_STATUS_USED);
 
   if (prerender_contents_.get()) {
-    prerender_contents_->SendToAllFrames(
-        new PrerenderMsg_SetIsPrerendering(MSG_ROUTING_NONE, NO_PRERENDER));
+    prerender_contents_->SendToAllFrames(new PrerenderMsg_SetIsPrerendering(
+        MSG_ROUTING_NONE, NO_PRERENDER, std::string()));
   }
 
   NotifyPrerenderStop();
@@ -755,6 +727,19 @@
   Destroy(FINAL_STATUS_WINDOW_PRINT);
 }
 
+void PrerenderContents::CancelPrerenderForUnsupportedMethod() {
+  Destroy(FINAL_STATUS_INVALID_HTTP_METHOD);
+}
+
+void PrerenderContents::CancelPrerenderForUnsupportedScheme(const GURL& url) {
+  Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME);
+  ReportUnsupportedPrerenderScheme(url);
+}
+
+void PrerenderContents::CancelPrerenderForSyncDeferredRedirect() {
+  Destroy(FINAL_STATUS_BAD_DEFERRED_REDIRECT);
+}
+
 void PrerenderContents::OnPrerenderCancelerRequest(
     chrome::mojom::PrerenderCancelerRequest request) {
   if (!prerender_canceler_binding_.is_bound())