Moving HTTP POST body from StartNavigationParams to CommonNavigationParams.

StartNavigationParams used to hold
    std::vector<unsigned char> browser_initiated_post_data
field.  This CL moves the field to CommonNavigationParams and makes it
more generic, so that it can be used to carry any POST data (nost just
"browser initiated" post data):
    scoped_refptr<ResourceRequestBody> post_data;

Making the type more generic (i.e. replacing a vector of bytes with
ResourceRequestBody) is needed to prepare for forwarding POST data back to the
renderer during process transfers.  This fix will come in crrev.com/1956383003.

Moving the type from StartNavigationParams to CommonNavigationParams helps
unify many scenarios across default and PlzNavigate modes and consequently
allows avoiding passing of the body as a separate field and/or param in
quite a few places.

BUG=582211
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_site_isolation

Review-Url: https://codereview.chromium.org/1999943002
Cr-Commit-Position: refs/heads/master@{#395915}
diff --git a/content/browser/frame_host/navigation_entry_impl.cc b/content/browser/frame_host/navigation_entry_impl.cc
index 81931e4b..cd0d68a1c 100644
--- a/content/browser/frame_host/navigation_entry_impl.cc
+++ b/content/browser/frame_host/navigation_entry_impl.cc
@@ -18,6 +18,7 @@
 #include "content/common/content_constants_internal.h"
 #include "content/common/navigation_params.h"
 #include "content/common/page_state_serialization.h"
+#include "content/common/resource_request_body.h"
 #include "content/common/site_isolation_policy.h"
 #include "content/public/common/browser_side_navigation_policy.h"
 #include "content/public/common/content_constants.h"
@@ -569,8 +570,22 @@
   return copy;
 }
 
+scoped_refptr<ResourceRequestBody>
+NavigationEntryImpl::ConstructBodyFromBrowserInitiatedPostData() const {
+  scoped_refptr<ResourceRequestBody> browser_initiated_post_body;
+  if (GetHasPostData()) {
+    if (const base::RefCountedMemory* memory = GetBrowserInitiatedPostData()) {
+      browser_initiated_post_body = new ResourceRequestBody();
+      browser_initiated_post_body->AppendBytes(memory->front_as<char>(),
+                                               memory->size());
+    }
+  }
+  return browser_initiated_post_body;
+}
+
 CommonNavigationParams NavigationEntryImpl::ConstructCommonNavigationParams(
     const FrameNavigationEntry& frame_entry,
+    const scoped_refptr<ResourceRequestBody>& post_body,
     const GURL& dest_url,
     const Referrer& dest_referrer,
     FrameMsg_Navigate_Type::Value navigation_type,
@@ -592,26 +607,19 @@
   if (IsBrowserSideNavigationEnabled())
     method = frame_entry.method();
   else
-    method = GetHasPostData() ? "POST" : "GET";
+    method = (post_body.get() || GetHasPostData()) ? "POST" : "GET";
 
   return CommonNavigationParams(
       dest_url, dest_referrer, GetTransitionType(), navigation_type,
       !IsViewSourceMode(), should_replace_entry(), ui_timestamp, report_type,
       GetBaseURLForDataURL(), GetHistoryURLForDataURL(), lofi_state,
-      navigation_start, method);
+      navigation_start, method,
+      post_body ? post_body : ConstructBodyFromBrowserInitiatedPostData());
 }
 
 StartNavigationParams NavigationEntryImpl::ConstructStartNavigationParams()
     const {
-  std::vector<unsigned char> browser_initiated_post_data;
-  if (GetBrowserInitiatedPostData()) {
-    browser_initiated_post_data.assign(
-        GetBrowserInitiatedPostData()->front(),
-        GetBrowserInitiatedPostData()->front() +
-            GetBrowserInitiatedPostData()->size());
-  }
-
-  return StartNavigationParams(extra_headers(), browser_initiated_post_data,
+  return StartNavigationParams(extra_headers(),
 #if defined(OS_ANDROID)
                                has_user_gesture(),
 #endif
diff --git a/content/browser/frame_host/navigation_entry_impl.h b/content/browser/frame_host/navigation_entry_impl.h
index d2b34d3..313f03b 100644
--- a/content/browser/frame_host/navigation_entry_impl.h
+++ b/content/browser/frame_host/navigation_entry_impl.h
@@ -23,6 +23,7 @@
 #include "content/public/common/ssl_status.h"
 
 namespace content {
+class ResourceRequestBody;
 struct CommonNavigationParams;
 struct RequestNavigationParams;
 struct StartNavigationParams;
@@ -159,8 +160,11 @@
 
   // Helper functions to construct NavigationParameters for a navigation to this
   // NavigationEntry.
+  scoped_refptr<ResourceRequestBody> ConstructBodyFromBrowserInitiatedPostData()
+      const;
   CommonNavigationParams ConstructCommonNavigationParams(
       const FrameNavigationEntry& frame_entry,
+      const scoped_refptr<ResourceRequestBody>& post_body,
       const GURL& dest_url,
       const Referrer& dest_referrer,
       FrameMsg_Navigate_Type::Value navigation_type,
diff --git a/content/browser/frame_host/navigation_request.cc b/content/browser/frame_host/navigation_request.cc
index b8ef2b1..0095578 100644
--- a/content/browser/frame_host/navigation_request.cc
+++ b/content/browser/frame_host/navigation_request.cc
@@ -83,20 +83,12 @@
 
   // Fill POST data in the request body.
   scoped_refptr<ResourceRequestBody> request_body;
-  if (frame_entry.method() == "POST") {
+  if (frame_entry.method() == "POST")
     request_body = frame_entry.GetPostData();
-    if (!request_body && entry.GetBrowserInitiatedPostData()) {
-      request_body = new ResourceRequestBody();
-      request_body->AppendBytes(
-          reinterpret_cast<const char*>(
-              entry.GetBrowserInitiatedPostData()->front()),
-          entry.GetBrowserInitiatedPostData()->size());
-    }
-  }
 
   std::unique_ptr<NavigationRequest> navigation_request(new NavigationRequest(
       frame_tree_node, entry.ConstructCommonNavigationParams(
-                           frame_entry, dest_url, dest_referrer,
+                           frame_entry, request_body, dest_url, dest_referrer,
                            navigation_type, lofi_state, navigation_start),
       BeginNavigationParams(headers.ToString(),
                             LoadFlagFromNavigationType(navigation_type),
@@ -110,7 +102,7 @@
           controller->GetIndexOfEntry(&entry),
           controller->GetLastCommittedEntryIndex(),
           controller->GetEntryCount()),
-      request_body, true, &frame_entry, &entry));
+      true, &frame_entry, &entry));
   return navigation_request;
 }
 
@@ -119,7 +111,6 @@
     FrameTreeNode* frame_tree_node,
     const CommonNavigationParams& common_params,
     const BeginNavigationParams& begin_params,
-    scoped_refptr<ResourceRequestBody> body,
     int current_history_list_offset,
     int current_history_list_length) {
   // TODO(clamy): Check if some PageState should be provided here.
@@ -145,7 +136,7 @@
       false);                  // should_clear_history_list
   std::unique_ptr<NavigationRequest> navigation_request(
       new NavigationRequest(frame_tree_node, common_params, begin_params,
-                            request_params, body, false, nullptr, nullptr));
+                            request_params, false, nullptr, nullptr));
   return navigation_request;
 }
 
@@ -154,7 +145,6 @@
     const CommonNavigationParams& common_params,
     const BeginNavigationParams& begin_params,
     const RequestNavigationParams& request_params,
-    scoped_refptr<ResourceRequestBody> body,
     bool browser_initiated,
     const FrameNavigationEntry* frame_entry,
     const NavigationEntryImpl* entry)
@@ -167,7 +157,6 @@
       restore_type_(NavigationEntryImpl::RESTORE_NONE),
       is_view_source_(false),
       bindings_(NavigationEntryImpl::kInvalidBindings),
-      post_data_(body),
       associated_site_instance_type_(AssociatedSiteInstanceType::NONE) {
   DCHECK(!browser_initiated || (entry != nullptr && frame_entry != nullptr));
   if (browser_initiated) {
@@ -200,7 +189,7 @@
   info_.reset(new NavigationRequestInfo(
       common_params, begin_params, first_party_for_cookies,
       frame_tree_node->current_origin(), frame_tree_node->IsMainFrame(),
-      parent_is_main_frame, frame_tree_node->frame_tree_node_id(), body));
+      parent_is_main_frame, frame_tree_node->frame_tree_node_id()));
 }
 
 NavigationRequest::~NavigationRequest() {
@@ -264,7 +253,7 @@
     const scoped_refptr<ResourceResponse>& response) {
   // If the navigation is no longer a POST, the POST data should be reset.
   if (redirect_info.new_method != "POST")
-    post_data_ = nullptr;
+    common_params_.post_data = nullptr;
 
   common_params_.url = redirect_info.new_url;
   common_params_.method = redirect_info.new_method;
@@ -435,7 +424,7 @@
   TransferNavigationHandleOwnership(render_frame_host);
   render_frame_host->CommitNavigation(response_.get(), std::move(body_),
                                       common_params_, request_params_,
-                                      is_view_source_, post_data_);
+                                      is_view_source_);
 
   // When navigating to a Javascript url, the NavigationRequest is not stored
   // in the FrameTreeNode. Therefore do not reset it, as this could cancel an
diff --git a/content/browser/frame_host/navigation_request.h b/content/browser/frame_host/navigation_request.h
index 1f4994a..30e9b2f 100644
--- a/content/browser/frame_host/navigation_request.h
+++ b/content/browser/frame_host/navigation_request.h
@@ -92,7 +92,6 @@
       FrameTreeNode* frame_tree_node,
       const CommonNavigationParams& common_params,
       const BeginNavigationParams& begin_params,
-      scoped_refptr<ResourceRequestBody> body,
       int current_history_list_offset,
       int current_history_list_length);
 
@@ -166,7 +165,6 @@
                     const CommonNavigationParams& common_params,
                     const BeginNavigationParams& begin_params,
                     const RequestNavigationParams& request_params,
-                    scoped_refptr<ResourceRequestBody> body,
                     bool browser_initiated,
                     const FrameNavigationEntry* frame_navigation_entry,
                     const NavigationEntryImpl* navitation_entry);
@@ -230,9 +228,6 @@
   bool is_view_source_;
   int bindings_;
 
-  // This is kept to be sent to the renderer on commit.
-  scoped_refptr<ResourceRequestBody> post_data_;
-
   // The type of SiteInstance associated with this navigation.
   AssociatedSiteInstanceType associated_site_instance_type_;
 
diff --git a/content/browser/frame_host/navigation_request_info.cc b/content/browser/frame_host/navigation_request_info.cc
index 5bfd979a..6b01863 100644
--- a/content/browser/frame_host/navigation_request_info.cc
+++ b/content/browser/frame_host/navigation_request_info.cc
@@ -14,16 +14,14 @@
     const url::Origin& request_initiator,
     bool is_main_frame,
     bool parent_is_main_frame,
-    int frame_tree_node_id,
-    scoped_refptr<ResourceRequestBody> request_body)
+    int frame_tree_node_id)
     : common_params(common_params),
       begin_params(begin_params),
       first_party_for_cookies(first_party_for_cookies),
       request_initiator(request_initiator),
       is_main_frame(is_main_frame),
       parent_is_main_frame(parent_is_main_frame),
-      frame_tree_node_id(frame_tree_node_id),
-      request_body(request_body) {}
+      frame_tree_node_id(frame_tree_node_id) {}
 
 NavigationRequestInfo::~NavigationRequestInfo() {}
 
diff --git a/content/browser/frame_host/navigation_request_info.h b/content/browser/frame_host/navigation_request_info.h
index ef677db4..9244818 100644
--- a/content/browser/frame_host/navigation_request_info.h
+++ b/content/browser/frame_host/navigation_request_info.h
@@ -28,8 +28,7 @@
                         const url::Origin& request_initiator,
                         bool is_main_frame,
                         bool parent_is_main_frame,
-                        int frame_tree_node_id,
-                        scoped_refptr<ResourceRequestBody> request_body);
+                        int frame_tree_node_id);
   ~NavigationRequestInfo();
 
   const CommonNavigationParams common_params;
@@ -46,8 +45,6 @@
   const bool parent_is_main_frame;
 
   const int frame_tree_node_id;
-
-  scoped_refptr<ResourceRequestBody> request_body;
 };
 
 }  // namespace content
diff --git a/content/browser/frame_host/navigator.cc b/content/browser/frame_host/navigator.cc
index ac6c248..c2eec4b 100644
--- a/content/browser/frame_host/navigator.cc
+++ b/content/browser/frame_host/navigator.cc
@@ -35,11 +35,8 @@
   return base::TimeTicks::Now();
 }
 
-void Navigator::OnBeginNavigation(
-    FrameTreeNode* frame_tree_node,
-    const CommonNavigationParams& common_params,
-    const BeginNavigationParams& begin_params,
-    scoped_refptr<ResourceRequestBody> body) {
-}
+void Navigator::OnBeginNavigation(FrameTreeNode* frame_tree_node,
+                                  const CommonNavigationParams& common_params,
+                                  const BeginNavigationParams& begin_params) {}
 
 }  // namespace content
diff --git a/content/browser/frame_host/navigator.h b/content/browser/frame_host/navigator.h
index ce1c97d..9dfe024 100644
--- a/content/browser/frame_host/navigator.h
+++ b/content/browser/frame_host/navigator.h
@@ -142,11 +142,9 @@
   // PlzNavigate
   // Used to start a new renderer-initiated navigation, following a
   // BeginNavigation IPC from the renderer.
-  virtual void OnBeginNavigation(
-      FrameTreeNode* frame_tree_node,
-      const CommonNavigationParams& common_params,
-      const BeginNavigationParams& begin_params,
-      scoped_refptr<ResourceRequestBody> body);
+  virtual void OnBeginNavigation(FrameTreeNode* frame_tree_node,
+                                 const CommonNavigationParams& common_params,
+                                 const BeginNavigationParams& begin_params);
 
   // PlzNavigate
   // Called when a NavigationRequest for |frame_tree_node| failed. An
diff --git a/content/browser/frame_host/navigator_impl.cc b/content/browser/frame_host/navigator_impl.cc
index 71453f0..961b37d 100644
--- a/content/browser/frame_host/navigator_impl.cc
+++ b/content/browser/frame_host/navigator_impl.cc
@@ -387,7 +387,7 @@
       FrameMsg_Navigate_Type::Value navigation_type = GetNavigationType(
           controller_->GetBrowserContext(), entry, reload_type);
       dest_render_frame_host->Navigate(
-          entry.ConstructCommonNavigationParams(frame_entry, dest_url,
+          entry.ConstructCommonNavigationParams(frame_entry, nullptr, dest_url,
                                                 dest_referrer, navigation_type,
                                                 lofi_state, navigation_start),
           entry.ConstructStartNavigationParams(),
@@ -860,8 +860,7 @@
 void NavigatorImpl::OnBeginNavigation(
     FrameTreeNode* frame_tree_node,
     const CommonNavigationParams& common_params,
-    const BeginNavigationParams& begin_params,
-    scoped_refptr<ResourceRequestBody> body) {
+    const BeginNavigationParams& begin_params) {
   // TODO(clamy): the url sent by the renderer should be validated with
   // FilterURL.
   // This is a renderer-initiated navigation.
@@ -889,7 +888,7 @@
   // NavigationRequest is created for the node.
   frame_tree_node->CreatedNavigationRequest(
       NavigationRequest::CreateRendererInitiated(
-          frame_tree_node, common_params, begin_params, body,
+          frame_tree_node, common_params, begin_params,
           controller_->GetLastCommittedEntryIndex(),
           controller_->GetEntryCount()));
   NavigationRequest* navigation_request = frame_tree_node->navigation_request();
diff --git a/content/browser/frame_host/navigator_impl.h b/content/browser/frame_host/navigator_impl.h
index 14958713..d760f7c 100644
--- a/content/browser/frame_host/navigator_impl.h
+++ b/content/browser/frame_host/navigator_impl.h
@@ -80,8 +80,7 @@
   void OnBeforeUnloadACK(FrameTreeNode* frame_tree_node, bool proceed) override;
   void OnBeginNavigation(FrameTreeNode* frame_tree_node,
                          const CommonNavigationParams& common_params,
-                         const BeginNavigationParams& begin_params,
-                         scoped_refptr<ResourceRequestBody> body) override;
+                         const BeginNavigationParams& begin_params) override;
   void FailedNavigation(FrameTreeNode* frame_tree_node,
                         bool has_stale_copy_in_cache,
                         int error_code) override;
diff --git a/content/browser/frame_host/render_frame_host_impl.cc b/content/browser/frame_host/render_frame_host_impl.cc
index a22c5e0..f7d3b40 100644
--- a/content/browser/frame_host/render_frame_host_impl.cc
+++ b/content/browser/frame_host/render_frame_host_impl.cc
@@ -1644,13 +1644,12 @@
 
 void RenderFrameHostImpl::OnBeginNavigation(
     const CommonNavigationParams& common_params,
-    const BeginNavigationParams& begin_params,
-    scoped_refptr<ResourceRequestBody> body) {
+    const BeginNavigationParams& begin_params) {
   CHECK(IsBrowserSideNavigationEnabled());
   CommonNavigationParams validated_params = common_params;
   GetProcess()->FilterURL(false, &validated_params.url);
   frame_tree_node()->navigator()->OnBeginNavigation(
-      frame_tree_node(), validated_params, begin_params, body);
+      frame_tree_node(), validated_params, begin_params);
 }
 
 void RenderFrameHostImpl::OnDispatchLoad() {
@@ -2100,10 +2099,10 @@
       data_url, Referrer(), ui::PAGE_TRANSITION_LINK,
       FrameMsg_Navigate_Type::NORMAL, false, false, base::TimeTicks::Now(),
       FrameMsg_UILoadMetricsReportType::NO_REPORT, GURL(), GURL(), LOFI_OFF,
-      base::TimeTicks::Now(), "GET");
+      base::TimeTicks::Now(), "GET", nullptr);
   if (IsBrowserSideNavigationEnabled()) {
     CommitNavigation(nullptr, nullptr, common_params, RequestNavigationParams(),
-                     false, nullptr);
+                     false);
   } else {
     Navigate(common_params, StartNavigationParams(), RequestNavigationParams());
   }
@@ -2240,8 +2239,7 @@
     std::unique_ptr<StreamHandle> body,
     const CommonNavigationParams& common_params,
     const RequestNavigationParams& request_params,
-    bool is_view_source,
-    scoped_refptr<ResourceRequestBody> post_data) {
+    bool is_view_source) {
   DCHECK((response && body.get()) ||
           !ShouldMakeNetworkRequestForURL(common_params.url));
   UpdatePermissionsForNavigation(common_params, request_params);
@@ -2262,7 +2260,7 @@
   const ResourceResponseHead head = response ?
       response->head : ResourceResponseHead();
   Send(new FrameMsg_CommitNavigation(routing_id_, head, body_url, common_params,
-                                     request_params, post_data));
+                                     request_params));
 
   // If a network request was made, update the LoFi state.
   if (ShouldMakeNetworkRequestForURL(common_params.url))
diff --git a/content/browser/frame_host/render_frame_host_impl.h b/content/browser/frame_host/render_frame_host_impl.h
index f3cb4189..420a2c27 100644
--- a/content/browser/frame_host/render_frame_host_impl.h
+++ b/content/browser/frame_host/render_frame_host_impl.h
@@ -496,8 +496,7 @@
                         std::unique_ptr<StreamHandle> body,
                         const CommonNavigationParams& common_params,
                         const RequestNavigationParams& request_params,
-                        bool is_view_source,
-                        scoped_refptr<ResourceRequestBody> post_data);
+                        bool is_view_source);
 
   // PlzNavigate
   // Indicates that a navigation failed and that this RenderFrame should display
@@ -653,8 +652,7 @@
                      blink::WebTextDirection title_direction);
   void OnUpdateEncoding(const std::string& encoding);
   void OnBeginNavigation(const CommonNavigationParams& common_params,
-                         const BeginNavigationParams& begin_params,
-                         scoped_refptr<ResourceRequestBody> body);
+                         const BeginNavigationParams& begin_params);
   void OnDispatchLoad();
   void OnAccessibilityEvents(
       const std::vector<AccessibilityHostMsg_EventParams>& params,