Commit 9da0291b authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Use adjusted relatedTarget on WindowEventContext

In event capturing phase, when the event is dispatched on window, the
related target was not set properly (it uses the state of the event
at the target, which means related target is the same as when it was
at the target). This might cause leaks when the event's relatedTarget
is inside a shadow tree. This change makes it so at window context,
the relatedTarget used is the relatedTarget of the last
NodeEventContext.

Bug: 817222
Change-Id: I1edea3977202b0a05e3e8ea46a5b8c1edb4b834d
Reviewed-on: https://chromium-review.googlesource.com/1248244Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594606}
parent 1934d5c0
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!--
This test is adopted from Olli Pettay's test case at
http://mozilla.pettay.fi/shadow_focus.html
-->
<div id="host"></div>
<input id="lightInput">
<script>
const root = host.attachShadow({ mode: "closed" });
root.innerHTML = "<input id='shadowInput'>";
async_test((test) => {
root.getElementById("shadowInput").focus();
window.addEventListener("focus", test.step_func_done((e) => {
assert_equals(e.relatedTarget, host);
}, "relatedTarget should be pointing to shadow host."), true);
lightInput.focus();
}, "relatedTarget should not leak at capturing phase, at window object.");
async_test((test) => {
root.getElementById("shadowInput").focus();
lightInput.addEventListener("focus", test.step_func_done((e) => {
assert_equals(e.relatedTarget, host);
}, "relatedTarget should be pointing to shadow host."), true);
lightInput.focus();
}, "relatedTarget should not leak at target.");
</script>
...@@ -45,6 +45,7 @@ WindowEventContext::WindowEventContext( ...@@ -45,6 +45,7 @@ WindowEventContext::WindowEventContext(
return; return;
window_ = ToDocument(top_node_event_context.GetNode())->domWindow(); window_ = ToDocument(top_node_event_context.GetNode())->domWindow();
target_ = top_node_event_context.Target(); target_ = top_node_event_context.Target();
related_target_ = top_node_event_context.RelatedTarget();
} }
bool WindowEventContext::HandleLocalEvents(Event& event) { bool WindowEventContext::HandleLocalEvents(Event& event) {
...@@ -53,6 +54,8 @@ bool WindowEventContext::HandleLocalEvents(Event& event) { ...@@ -53,6 +54,8 @@ bool WindowEventContext::HandleLocalEvents(Event& event) {
event.SetTarget(Target()); event.SetTarget(Target());
event.SetCurrentTarget(Window()); event.SetCurrentTarget(Window());
if (RelatedTarget())
event.SetRelatedTargetIfExists(RelatedTarget());
window_->FireEventListeners(event); window_->FireEventListeners(event);
return true; return true;
} }
...@@ -60,6 +63,7 @@ bool WindowEventContext::HandleLocalEvents(Event& event) { ...@@ -60,6 +63,7 @@ bool WindowEventContext::HandleLocalEvents(Event& event) {
void WindowEventContext::Trace(blink::Visitor* visitor) { void WindowEventContext::Trace(blink::Visitor* visitor) {
visitor->Trace(window_); visitor->Trace(window_);
visitor->Trace(target_); visitor->Trace(target_);
visitor->Trace(related_target_);
} }
} // namespace blink } // namespace blink
...@@ -43,6 +43,7 @@ class WindowEventContext : public GarbageCollected<WindowEventContext> { ...@@ -43,6 +43,7 @@ class WindowEventContext : public GarbageCollected<WindowEventContext> {
LocalDOMWindow* Window() const; LocalDOMWindow* Window() const;
EventTarget* Target() const; EventTarget* Target() const;
EventTarget* RelatedTarget() const;
bool HandleLocalEvents(Event&); bool HandleLocalEvents(Event&);
void Trace(blink::Visitor*); void Trace(blink::Visitor*);
...@@ -50,6 +51,7 @@ class WindowEventContext : public GarbageCollected<WindowEventContext> { ...@@ -50,6 +51,7 @@ class WindowEventContext : public GarbageCollected<WindowEventContext> {
private: private:
Member<LocalDOMWindow> window_; Member<LocalDOMWindow> window_;
Member<EventTarget> target_; Member<EventTarget> target_;
Member<EventTarget> related_target_;
DISALLOW_COPY_AND_ASSIGN(WindowEventContext); DISALLOW_COPY_AND_ASSIGN(WindowEventContext);
}; };
...@@ -61,6 +63,10 @@ inline EventTarget* WindowEventContext::Target() const { ...@@ -61,6 +63,10 @@ inline EventTarget* WindowEventContext::Target() const {
return target_.Get(); return target_.Get();
} }
inline EventTarget* WindowEventContext::RelatedTarget() const {
return related_target_.Get();
}
} // namespace blink } // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_EVENTS_WINDOW_EVENT_CONTEXT_H_ #endif // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_EVENTS_WINDOW_EVENT_CONTEXT_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment