Don't do autofocus process if the target node of 'autofocus' is in a cross-origin document.

Note that I've confirmed no-cross-origin-autofocus.html works fine with
enabling fission on w3c-test.org.

Differential Revision: https://phabricator.services.mozilla.com/D44950

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1578973
gecko-commit: 5a021b9f8790b0f6d39aefb29d9c65c2bc8f0ba3
gecko-integration-branch: autoland
gecko-reviewers: nika
diff --git a/html/semantics/forms/autofocus/resources/child-iframe.html b/html/semantics/forms/autofocus/resources/child-iframe.html
new file mode 100644
index 0000000..2645a18
--- /dev/null
+++ b/html/semantics/forms/autofocus/resources/child-iframe.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="/common/get-host-info.sub.js"></script>
+<iframe id="iframe" width="200" height="100"></iframe>
+
+<script>
+  iframe.src =
+    get_host_info().ORIGIN + "/html/semantics/forms/autofocus/resources/grand-child-autofocus.html";
+  window.addEventListener("message", event => {
+    if (event.data == "grand_child_loaded") {
+      parent.postMessage("ready", "*");
+    } else if (event.data == "report_focus_state") {
+      frames[0].postMessage("report_focus_state", "*");
+    } else if (event.data == "grand_child_is_focused" ||
+               event.data == "grand_child_is_not_focused") {
+      parent.postMessage(event.data, "*");
+    }
+  });
+</script>
diff --git a/html/semantics/forms/autofocus/resources/grand-child-autofocus.html b/html/semantics/forms/autofocus/resources/grand-child-autofocus.html
new file mode 100644
index 0000000..88be6e0
--- /dev/null
+++ b/html/semantics/forms/autofocus/resources/grand-child-autofocus.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<input id="target" value="This should be focused!" autofocus></input>
+
+<script>
+  let got_focus = false;
+  target.addEventListener("focus", () => got_focus = true);
+
+  window.addEventListener("load", () => {
+    parent.postMessage("grand_child_loaded", "*");
+  });
+
+  window.addEventListener("message", event => {
+    if (event.data == "report_focus_state") {
+      let msg = got_focus ? "grand_child_is_focused" : "grand_child_is_not_focused";
+      parent.postMessage(msg, "*");
+    }
+  });
+</script>
diff --git a/html/semantics/forms/autofocus/same-origin-autofocus.html b/html/semantics/forms/autofocus/same-origin-autofocus.html
new file mode 100644
index 0000000..9cfcdb9
--- /dev/null
+++ b/html/semantics/forms/autofocus/same-origin-autofocus.html
@@ -0,0 +1,46 @@
+<!doctype html>
+<html>
+<head>
+<meta charset=utf-8>
+<meta name="assert" content="`autofocus` should work in the same origin iframe even if there is a cross-origin iframe between the parent and the same origin iframe">
+<title>autofocus in the same origin grand child iframe</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/testdriver.js"></script>
+<script src="/resources/testdriver-vendor.js"></script>
+<script src="/common/get-host-info.sub.js"></script>
+</head>
+<body>
+  <h1>Autofocus should work in the same origin grand child iframe.</h1>
+  <iframe id="child" width="200" height="100"></iframe>
+  <script>
+    let parent_loaded = false;
+    let grand_child_loaded = false;
+
+    async_test(function(t) {
+      function pingChildIfBothFramesLoaded() {
+        if (parent_loaded && grand_child_loaded)
+          frames[0].postMessage("report_focus_state", "*");
+      }
+
+      window.addEventListener("load", t.step_func(event => {
+        parent_loaded = true;
+        pingChildIfBothFramesLoaded();
+      }));
+
+      window.addEventListener("message", t.step_func(event => {
+        if (event.data == "ready") {
+          grand_child_loaded = true;
+          pingChildIfBothFramesLoaded();
+        } else if (event.data == "grand_child_is_focused") {
+          t.done();
+        } else if (event.data == "grand_child_is_not_focused") {
+          assert_unreached("The iframe shouldn't get focus");
+        }
+      }));
+      document.getElementById("child").src =
+          get_host_info().HTTP_NOTSAMESITE_ORIGIN + "/html/semantics/forms/autofocus/resources/child-iframe.html";
+    }, "Autofocus should work in the same origin grand child iframe");
+  </script>
+</body>
+</html>