Commit a33dc824 authored by Pavel Feldman's avatar Pavel Feldman Committed by Commit Bot

DevTools: allow auto-attaching to the targets created with window.open

This change adds 'experimental optional boolean windowOpen' property to the
Target.setAutoAttach method. If it is true, targets created with window.open
are throttled and attached to as if they were OOPIFs or other dependent targets

Change-Id: I53ac8a879005920b7ffe29334c98d048a0d746ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1826874
Commit-Queue: Pavel Feldman <pfeldman@chromium.org>
Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#700516}
parent f053ed21
......@@ -199,9 +199,12 @@ std::vector<std::unique_ptr<NavigationThrottle>> CreateNavigationThrottles(
->GetFrameTree()
->root();
} else {
return result;
parent = frame_tree_node->original_opener();
}
}
if (!parent)
return result;
agent_host = RenderFrameDevToolsAgentHost::GetFor(parent);
if (agent_host) {
for (auto* target_handler :
......
......@@ -223,6 +223,20 @@ DevToolsAgentHost* TargetAutoAttacher::AutoAttachToFrame(
scoped_refptr<DevToolsAgentHost> agent_host =
RenderFrameDevToolsAgentHost::FindForDangling(frame_tree_node);
// Process the window.open auto-attaches for new targets.
if (frame_tree_node->original_opener()) {
if (!agent_host) {
agent_host =
RenderFrameDevToolsAgentHost::CreateForCrossProcessNavigation(
navigation_request);
}
if (auto_attached_hosts_.find(agent_host) != auto_attached_hosts_.end())
return nullptr;
attach_callback_.Run(agent_host.get(), wait_for_debugger_on_start_);
auto_attached_hosts_.insert(agent_host);
return wait_for_debugger_on_start_ ? agent_host.get() : nullptr;
}
bool old_cross_process = !!agent_host;
bool is_portal_main_frame =
frame_tree_node->IsMainFrame() &&
......@@ -237,7 +251,6 @@ DevToolsAgentHost* TargetAutoAttacher::AutoAttachToFrame(
if (new_cross_process) {
agent_host = RenderFrameDevToolsAgentHost::CreateForCrossProcessNavigation(
navigation_request);
DCHECK(auto_attached_hosts_.find(agent_host) == auto_attached_hosts_.end());
attach_callback_.Run(agent_host.get(), wait_for_debugger_on_start_);
auto_attached_hosts_.insert(agent_host);
return wait_for_debugger_on_start_ ? agent_host.get() : nullptr;
......
......@@ -472,7 +472,7 @@ void TargetHandler::SetRenderer(int process_host_id,
}
Response TargetHandler::Disable() {
SetAutoAttachInternal(false, false, false, base::DoNothing());
SetAutoAttachInternal(false, false, false, false, base::DoNothing());
SetDiscoverTargets(false);
auto_attached_sessions_.clear();
attached_sessions_.clear();
......@@ -487,6 +487,11 @@ std::unique_ptr<NavigationThrottle> TargetHandler::CreateThrottleForNavigation(
NavigationHandle* navigation_handle) {
if (!auto_attacher_.ShouldThrottleFramesNavigation())
return nullptr;
FrameTreeNode* frame_tree_node =
NavigationRequest::From(navigation_handle)->frame_tree_node();
bool is_window_open = frame_tree_node->original_opener();
if (is_window_open && !attach_to_window_open_)
return nullptr;
return std::make_unique<Throttle>(weak_factory_.GetWeakPtr(),
navigation_handle);
}
......@@ -505,8 +510,10 @@ void TargetHandler::ClearThrottles() {
void TargetHandler::SetAutoAttachInternal(bool auto_attach,
bool wait_for_debugger_on_start,
bool flatten,
bool window_open,
base::OnceClosure callback) {
flatten_auto_attach_ = flatten;
attach_to_window_open_ = window_open;
auto_attacher_.SetAutoAttach(auto_attach, wait_for_debugger_on_start,
std::move(callback));
if (!auto_attacher_.ShouldThrottleFramesNavigation())
......@@ -574,9 +581,11 @@ void TargetHandler::SetAutoAttach(
bool auto_attach,
bool wait_for_debugger_on_start,
Maybe<bool> flatten,
Maybe<bool> window_open,
std::unique_ptr<SetAutoAttachCallback> callback) {
SetAutoAttachInternal(
auto_attach, wait_for_debugger_on_start, flatten.fromMaybe(false),
window_open.fromMaybe(false),
base::BindOnce(&SetAutoAttachCallback::sendSuccess, std::move(callback)));
}
......
......@@ -63,6 +63,7 @@ class TargetHandler : public DevToolsDomainHandler,
void SetAutoAttach(bool auto_attach,
bool wait_for_debugger_on_start,
Maybe<bool> flatten,
Maybe<bool> window_open,
std::unique_ptr<SetAutoAttachCallback> callback) override;
Response SetRemoteLocations(
std::unique_ptr<protocol::Array<Target::RemoteLocation>>) override;
......@@ -114,6 +115,7 @@ class TargetHandler : public DevToolsDomainHandler,
void SetAutoAttachInternal(bool auto_attach,
bool wait_for_debugger_on_start,
bool flatten,
bool window_open,
base::OnceClosure callback);
// DevToolsAgentHostObserver implementation.
......@@ -129,6 +131,7 @@ class TargetHandler : public DevToolsDomainHandler,
std::unique_ptr<Target::Frontend> frontend_;
TargetAutoAttacher auto_attacher_;
bool flatten_auto_attach_ = false;
bool attach_to_window_open_ = false;
bool discover_;
std::map<std::string, std::unique_ptr<Session>> attached_sessions_;
std::map<DevToolsAgentHost*, Session*> auto_attached_sessions_;
......
......@@ -6652,6 +6652,8 @@ domain Target
# We plan to make this the default, deprecate non-flattened mode,
# and eventually retire it. See crbug.com/991325.
optional boolean flatten
# Auto-attach to the targets created via window.open from current target.
experimental optional boolean windowOpen
# Controls whether to discover available targets and notify via
# `targetCreated/targetInfoChanged/targetDestroyed` events.
......
Tests that Target.setAutoAttach(windowOpen=true) attaches to window.open targets.
Opened the window
Attached to window
Navigated the window
Target info changed
Closed the window
Detached from window
(async function(testRunner) {
var {page, session, dp} = await testRunner.startBlank(
`Tests that Target.setAutoAttach(windowOpen=true) attaches to window.open targets.`);
await dp.Target.setDiscoverTargets({discover: true});
await dp.Target.setAutoAttach({autoAttach: true, waitForDebuggerOnStart: true, flatten: true, windowOpen: true});
session.evaluate(`
window.myWindow = window.open('../resources/inspector-protocol-page.html'); undefined;
`);
testRunner.log('Opened the window');
await dp.Target.onceAttachedToTarget();
testRunner.log('Attached to window');
session.evaluate(`
window.myWindow.location.assign('../resources/inspector-protocol-page.html?foo'); undefined;
`);
testRunner.log('Navigated the window');
await dp.Target.onceTargetInfoChanged();
testRunner.log('Target info changed');
session.evaluate(`
window.myWindow.close(); undefined;
`);
testRunner.log('Closed the window');
await dp.Target.onceDetachedFromTarget();
testRunner.log('Detached from window');
testRunner.completeTest();
})
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