Skip to content

Commit

Permalink
[DevTools] Adds newWindow and background params to Target.createTarget
Browse files Browse the repository at this point in the history
Adds newWindow and background params to Target.createTarget, exposing ability
to create a new window or tab, as well as whether to create it in the background
or foreground.

Design doc: http://bit.ly/chromedriver-new-window

Bug: chromium:966010
Change-Id: I5cb5875af668f5c37ecfb778392dd09315091ee0
Tested: Manually using ChromeDriver + WPTs (in a branch with the NewWindow endpoint implemented).
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1623131
Commit-Queue: Rohan Pavone <rohpavone@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664469}
  • Loading branch information
Rohan Pavone authored and Commit Bot committed May 29, 2019
1 parent acea1f5 commit 69efd94
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
64 changes: 48 additions & 16 deletions chrome/browser/devtools/protocol/target_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
#include "chrome/browser/ui/browser_navigator.h"
#include "content/public/browser/devtools_agent_host.h"

namespace {
NavigateParams CreateNavigateParams(Profile* profile,
const GURL& url,
ui::PageTransition transition,
bool new_window,
bool background,
Browser* browser) {
DCHECK(new_window || browser);
NavigateParams params(profile, url, transition);
if (new_window) {
params.disposition = WindowOpenDisposition::NEW_WINDOW;
if (background)
params.window_action = NavigateParams::WindowAction::SHOW_WINDOW_INACTIVE;
} else {
params.disposition = (background)
? WindowOpenDisposition::NEW_BACKGROUND_TAB
: WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.browser = browser;
}
return params;
}
} // namespace

TargetHandler::TargetHandler(protocol::UberDispatcher* dispatcher) {
protocol::Target::Dispatcher::wire(dispatcher, this);
}
Expand Down Expand Up @@ -49,6 +72,8 @@ protocol::Response TargetHandler::CreateTarget(
protocol::Maybe<int> height,
protocol::Maybe<std::string> browser_context_id,
protocol::Maybe<bool> enable_begin_frame_control,
protocol::Maybe<bool> new_window,
protocol::Maybe<bool> background,
std::string* out_target_id) {
Profile* profile = ProfileManager::GetActiveUserProfile();
if (browser_context_id.isJust()) {
Expand All @@ -60,25 +85,34 @@ protocol::Response TargetHandler::CreateTarget(
"Failed to find browser context with id " + profile_id);
}
}

NavigateParams params(profile, GURL(url), ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
bool create_new_window = new_window.fromMaybe(false);
bool create_in_background = background.fromMaybe(false);
Browser* target_browser = nullptr;
// Find a browser to open a new tab.
// We shouldn't use browser that is scheduled to close.
for (auto* browser : *BrowserList::GetInstance()) {
if (browser->profile() == profile &&
!browser->IsAttemptingToCloseBrowser()) {
target_browser = browser;
break;

// Must find target_browser if new_window not explicitly true.
if (!create_new_window) {
// Find a browser to open a new tab.
// We shouldn't use browser that is scheduled to close.
for (auto* browser : *BrowserList::GetInstance()) {
if (browser->profile() == profile &&
!browser->IsAttemptingToCloseBrowser()) {
target_browser = browser;
break;
}
}
}
if (target_browser) {
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.browser = target_browser;
} else {
params.disposition = WindowOpenDisposition::NEW_WINDOW;

bool explicit_old_window = !new_window.fromMaybe(true);
if (explicit_old_window && !target_browser) {
return protocol::Response::Error(
"Failed to open new tab - "
"no browser is open");
}

create_new_window = !target_browser;
NavigateParams params = CreateNavigateParams(
profile, GURL(url), ui::PAGE_TRANSITION_AUTO_TOPLEVEL, create_new_window,
create_in_background, target_browser);
Navigate(&params);
if (!params.navigated_or_inserted_contents)
return protocol::Response::Error("Failed to open a new tab");
Expand All @@ -88,5 +122,3 @@ protocol::Response TargetHandler::CreateTarget(
->GetId();
return protocol::Response::OK();
}


2 changes: 2 additions & 0 deletions chrome/browser/devtools/protocol/target_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TargetHandler : public protocol::Target::Backend {
protocol::Maybe<int> height,
protocol::Maybe<std::string> browser_context_id,
protocol::Maybe<bool> enable_begin_frame_control,
protocol::Maybe<bool> new_window,
protocol::Maybe<bool> background,
std::string* out_target_id) override;

private:
Expand Down
2 changes: 2 additions & 0 deletions content/browser/devtools/protocol/target_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ Response TargetHandler::CreateTarget(const std::string& url,
Maybe<int> height,
Maybe<std::string> context_id,
Maybe<bool> enable_begin_frame_control,
Maybe<bool> new_window,
Maybe<bool> background,
std::string* out_target_id) {
if (access_mode_ == AccessMode::kAutoAttachOnly)
return Response::Error(kNotAllowedError);
Expand Down
2 changes: 2 additions & 0 deletions content/browser/devtools/protocol/target_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class TargetHandler : public DevToolsDomainHandler,
Maybe<int> height,
Maybe<std::string> context_id,
Maybe<bool> enable_begin_frame_control,
Maybe<bool> new_window,
Maybe<bool> background,
std::string* out_target_id) override;
Response GetTargets(
std::unique_ptr<protocol::Array<Target::TargetInfo>>* target_infos)
Expand Down
2 changes: 2 additions & 0 deletions headless/lib/browser/protocol/target_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Response TargetHandler::CreateTarget(const std::string& url,
Maybe<int> height,
Maybe<std::string> context_id,
Maybe<bool> enable_begin_frame_control,
Maybe<bool> new_window,
Maybe<bool> background,
std::string* out_target_id) {
#if defined(OS_MACOSX)
if (enable_begin_frame_control.fromMaybe(false))
Expand Down
2 changes: 2 additions & 0 deletions headless/lib/browser/protocol/target_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class TargetHandler : public DomainHandler, public Target::Backend {
Maybe<int> height,
Maybe<std::string> context_id,
Maybe<bool> enable_begin_frame_control,
Maybe<bool> new_window,
Maybe<bool> background,
std::string* out_target_id) override;
Response CloseTarget(const std::string& target_id,
bool* out_success) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6266,6 +6266,11 @@ domain Target
# Whether BeginFrames for this target will be controlled via DevTools (headless chrome only,
# not supported on MacOS yet, false by default).
experimental optional boolean enableBeginFrameControl
# Whether to create a new Window or Tab (chrome-only, false by default).
optional boolean newWindow
# Whether to create the target in background or foreground (chrome-only,
# false by default).
optional boolean background
returns
# The id of the page opened.
TargetID targetId
Expand Down

0 comments on commit 69efd94

Please sign in to comment.