Assert navigations specifying a FrameTreeNode use correct NavigationController

If a caller of LoadURLWithParams targets a specific frame, but calls
the method on the wrong NavigationController, we don't hit any asserts
until possibly when the renderer commits the navigation and sees
conflicting parameters. We add an assert to catch this earlier.

A few existing call sites mistakenly/accidentally pass an unrelated
frame, but they're fine since they intend to navigate the main frame
anyway.

Bug: 40202416
Change-Id: Ib764bff725ea97a9fc3c04f744c48569faaf0ca9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6448882
Reviewed-by: Charlie Reis <[email protected]>
Reviewed-by: Phillis Tang <[email protected]>
Commit-Queue: Kevin McNee <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1446766}
diff --git a/content/browser/renderer_host/navigation_controller_impl.cc b/content/browser/renderer_host/navigation_controller_impl.cc
index 924d7433..887f1ab0 100644
--- a/content/browser/renderer_host/navigation_controller_impl.cc
+++ b/content/browser/renderer_host/navigation_controller_impl.cc
@@ -44,6 +44,7 @@
 #include "base/functional/bind.h"
 #include "base/logging.h"
 #include "base/metrics/histogram_macros.h"
+#include "base/not_fatal_until.h"
 #include "base/numerics/safe_conversions.h"
 #include "base/strings/escape.h"
 #include "base/strings/string_util.h"
@@ -3663,6 +3664,12 @@
                ? frame_tree_->FindByID(params.frame_tree_node_id)
                : frame_tree_->FindByName(params.frame_name);
     DCHECK(!node || &node->frame_tree() == &frame_tree());
+    if (!node && params.frame_tree_node_id) {
+      // If the specified FrameTreeNode exists in another FrameTree, the caller
+      // is using the wrong NavigationController.
+      CHECK(!FrameTreeNode::GloballyFindByID(params.frame_tree_node_id),
+            base::NotFatalUntil::M140);
+    }
   }
 
   // If no FrameTreeNode was specified, navigate the main frame.