Skip to content

Commit

Permalink
Fenced Frames: Temporarily allow window.fence.reportEvent from iframes
Browse files Browse the repository at this point in the history
Associated with the FLEDGE origin trial, there is a flag that allows iframes to load urn:uuids. This CL also enables window.fence.reportEvent in that case, to make it easier to test the new APIs. This will be removed when the iframe urn:uuid flag is removed.

For implementation reasons, the behavior right now is:
- If the invoking frame is in a fenced frame tree, the behavior is the same as before.
- If the invoking frame isn't in a fenced frame tree, reportEvent is available only when the invoking frame is an iframe whose document was navigated to a urn:uuid with attached reporting metadata.

For some examples:

* embedder > iframe1 (urn1) > iframe2 (https)
  fence.reportEvent works from iframe1 only.
* embedder > iframe1 (urn1) > iframe2 (urn2)
  fence.reportEvent works from iframe1 (to urn1) and iframe2 (to urn2)
* embedder > fencedframe1 (urn1) > iframe2 (https)
  fence.reportEvent works from fencedframe1 and iframe2 (both to urn1)
* embedder > fencedframe1 (urn1) > iframe2 (urn2)
  fence.reportEvent works from fencedframe1 and iframe2 (BOTH to urn1)

WICG/turtledove#309

Change-Id: Ie85bfb5264eb1ae78769533b2b8939f3168c8656
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3690741
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Shivani Sharma <[email protected]>
Commit-Queue: Garrett Tanzer <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1014688}
NOKEYCHECK=True
GitOrigin-RevId: 95b0232d46b725c457f406db36de6461a9250e60
  • Loading branch information
Garrett Tanzer authored and copybara-github committed Jun 16, 2022
1 parent 0e8661f commit e4530a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
11 changes: 10 additions & 1 deletion blink/renderer/core/frame/local_dom_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2314,9 +2314,18 @@ void LocalDOMWindow::DidBufferLoadWhileInBackForwardCache(size_t num_bytes) {

Fence* LocalDOMWindow::fence() {
// Return nullptr if we aren't in a fenced subtree.
if (!GetFrame() || !GetFrame()->IsInFencedFrameTree()) {
if (!GetFrame()) {
return nullptr;
}
if (!GetFrame()->IsInFencedFrameTree()) {
// We temporarily allow window.fence in iframes with fenced frame reporting
// metadata (navigated by urn:uuids).
// If we are in an iframe that doesn't qualify, return nullptr.
if (!blink::features::IsAllowURNsInIframeEnabled() ||
!GetFrame()->GetDocument()->Loader()->FencedFrameReporting()) {
return nullptr;
}
}

if (!fence_) {
fence_ = MakeGarbageCollected<Fence>(*this);
Expand Down
34 changes: 26 additions & 8 deletions blink/renderer/core/html/fenced_frame/fence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "third_party/blink/renderer/core/html/fenced_frame/fence.h"

#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/frame/frame_policy.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom-blink.h"
#include "third_party/blink/public/mojom/fenced_frame/fenced_frame.mojom-blink.h"
Expand Down Expand Up @@ -56,17 +57,34 @@ void Fence::reportEvent(ScriptState* script_state,

LocalFrame* frame = DomWindow()->GetFrame();
DCHECK(frame);
DCHECK(frame->IsInFencedFrameTree());

if (frame->GetFencedFrameMode() !=
mojom::blink::FencedFrameMode::kOpaqueAds) {
AddConsoleMessage(
"fence.reportEvent is only available in the 'opaque-ads' mode.");
return;
LocalFrame* fenced_frame = nullptr;
if (blink::features::IsAllowURNsInIframeEnabled() &&
!frame->IsInFencedFrameTree()) {
// The only way to get a Fence outside a fenced frame is from
// LocalDOMWindow::fence(), when both:
// - blink::features::IsAllowURNsInIframeEnabled() is true
// - the Document itself was loaded from a urn:uuid
// In that case, pretend that the frame is a fenced frame root for this
// temporary experiment.
// TODO(crbug.com/1123606): Disable window.fence.reportEvent in iframes.
// In order to disable, run the else branch unconditionally.
// Also remove the features.h include above.
fenced_frame = frame;
} else {
DCHECK(frame->IsInFencedFrameTree());

if (frame->GetFencedFrameMode() !=
mojom::blink::FencedFrameMode::kOpaqueAds) {
AddConsoleMessage(
"fence.reportEvent is only available in the 'opaque-ads' mode.");
return;
}

fenced_frame = DynamicTo<LocalFrame>(
DomWindow()->GetFrame()->Top(FrameTreeBoundary::kFenced));
}

LocalFrame* fenced_frame = DynamicTo<LocalFrame>(
DomWindow()->GetFrame()->Top(FrameTreeBoundary::kFenced));
DCHECK(fenced_frame);
DCHECK(fenced_frame->GetDocument());

Expand Down

0 comments on commit e4530a4

Please sign in to comment.