Skip to content

Commit

Permalink
headless: make initial screen/window sizes configurable.
Browse files Browse the repository at this point in the history
BUG=546953

Review-Url: https://codereview.chromium.org/2199773002
Cr-Commit-Position: refs/heads/master@{#409261}
  • Loading branch information
betasheet authored and Commit bot committed Aug 2, 2016
1 parent fa6c9e8 commit 73c4ead
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 25 deletions.
4 changes: 1 addition & 3 deletions headless/lib/browser/headless_browser_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ void HeadlessBrowserImpl::set_browser_main_parts(

void HeadlessBrowserImpl::RunOnStartCallback() {
DCHECK(aura::Env::GetInstance());
// TODO(eseckler): allow configuration of window (viewport) size by embedder.
const gfx::Size kDefaultSize(800, 600);
window_tree_host_.reset(
aura::WindowTreeHost::Create(gfx::Rect(kDefaultSize)));
aura::WindowTreeHost::Create(gfx::Rect(options()->window_size)));
window_tree_host_->InitHost();

window_tree_client_.reset(
Expand Down
6 changes: 3 additions & 3 deletions headless/lib/browser/headless_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace headless {

namespace {

void PlatformInitialize() {
HeadlessScreen* screen = HeadlessScreen::Create(gfx::Size());
void PlatformInitialize(const gfx::Size& screen_size) {
HeadlessScreen* screen = HeadlessScreen::Create(screen_size);
display::Screen::SetScreenInstance(screen);
}

Expand All @@ -38,7 +38,7 @@ void HeadlessBrowserMainParts::PreMainMessageLoopRun() {
devtools_http_handler_ =
CreateLocalDevToolsHttpHandler(browser_context_.get());
}
PlatformInitialize();
PlatformInitialize(browser_->options()->window_size);
}

void HeadlessBrowserMainParts::PostMainMessageLoopRun() {
Expand Down
4 changes: 2 additions & 2 deletions headless/lib/browser/headless_devtools_manager_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CreateTarget(
const base::DictionaryValue* params) {
std::string url;
std::string browser_context_id;
int width = 800;
int height = 600;
int width = browser_->options()->window_size.width();
int height = browser_->options()->window_size.height();
params->GetString("url", &url);
params->GetString("browserContextId", &browser_context_id);
params->GetInteger("width", &width);
Expand Down
3 changes: 1 addition & 2 deletions headless/lib/browser/headless_screen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ bool IsRotationPortrait(display::Display::Rotation rotation) {

// static
HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) {
const gfx::Size kDefaultSize(800, 600);
return new HeadlessScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size));
return new HeadlessScreen(gfx::Rect(size));
}

HeadlessScreen::~HeadlessScreen() {}
Expand Down
3 changes: 1 addition & 2 deletions headless/lib/browser/headless_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace headless {

class HeadlessScreen : public display::Screen, public aura::WindowObserver {
public:
// Creates a display::Screen of the specified size. If no size is specified,
// then creates a 800x600 screen. |size| is in physical pixels.
// Creates a display::Screen of the specified size (physical pixels).
static HeadlessScreen* Create(const gfx::Size& size);
~HeadlessScreen() override;

Expand Down
1 change: 1 addition & 0 deletions headless/lib/browser/headless_web_contents_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ content::WebContents* HeadlessWebContentsImpl::web_contents() const {

HeadlessWebContents::Builder::Builder(HeadlessBrowserImpl* browser)
: browser_(browser),
window_size_(browser_->options()->window_size),
browser_context_(
browser->browser_main_parts()->default_browser_context()) {}

Expand Down
47 changes: 41 additions & 6 deletions headless/lib/headless_browser_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,49 @@ IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, WebGLSupported) {
browser()->CreateWebContentsBuilder().Build();

bool webgl_supported;
EXPECT_TRUE(EvaluateScript(
web_contents,
"(document.createElement('canvas').getContext('webgl')"
" instanceof WebGLRenderingContext)")
EXPECT_TRUE(
EvaluateScript(web_contents,
"(document.createElement('canvas').getContext('webgl')"
" instanceof WebGLRenderingContext)")
->GetResult()
->GetValue()
->GetAsBoolean(&webgl_supported));
EXPECT_TRUE(webgl_supported);
}

IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, DefaultSizes) {
HeadlessWebContents* web_contents =
browser()->CreateWebContentsBuilder().Build();

HeadlessBrowser::Options::Builder builder;
const HeadlessBrowser::Options kDefaultOptions = builder.Build();

int screen_width;
int screen_height;
int window_width;
int window_height;

EXPECT_TRUE(EvaluateScript(web_contents, "screen.width")
->GetResult()
->GetValue()
->GetAsBoolean(&webgl_supported));
EXPECT_TRUE(webgl_supported);
->GetAsInteger(&screen_width));
EXPECT_TRUE(EvaluateScript(web_contents, "screen.height")
->GetResult()
->GetValue()
->GetAsInteger(&screen_height));
EXPECT_TRUE(EvaluateScript(web_contents, "window.innerWidth")
->GetResult()
->GetValue()
->GetAsInteger(&window_width));
EXPECT_TRUE(EvaluateScript(web_contents, "window.innerHeight")
->GetResult()
->GetValue()
->GetAsInteger(&window_height));

EXPECT_EQ(kDefaultOptions.window_size.width(), screen_width);
EXPECT_EQ(kDefaultOptions.window_size.height(), screen_height);
EXPECT_EQ(kDefaultOptions.window_size.width(), window_width);
EXPECT_EQ(kDefaultOptions.window_size.height(), window_height);
}

} // namespace headless
9 changes: 8 additions & 1 deletion headless/public/headless_browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace headless {
// Product name for building the default user agent string.
namespace {
const char kProductName[] = "HeadlessChrome";
constexpr gfx::Size kDefaultWindowSize(800, 600);
}

Options::Options(int argc, const char** argv)
Expand All @@ -22,7 +23,8 @@ Options::Options(int argc, const char** argv)
message_pump(nullptr),
single_process_mode(false),
disable_sandbox(false),
gl_implementation("osmesa") {}
gl_implementation("osmesa"),
window_size(kDefaultWindowSize) {}

Options::Options(Options&& options) = default;

Expand Down Expand Up @@ -81,6 +83,11 @@ Builder& Builder::SetGLImplementation(const std::string& gl_implementation) {
return *this;
}

Builder& Builder::SetWindowSize(const gfx::Size& window_size) {
options_.window_size = window_size;
return *this;
}

Options Builder::Build() {
return std::move(options_);
}
Expand Down
10 changes: 6 additions & 4 deletions headless/public/headless_browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
#include "headless/public/headless_web_contents.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_endpoint.h"
#include "ui/gfx/geometry/size.h"

namespace base {
class MessagePump;
class SingleThreadTaskRunner;
}

namespace gfx {
class Size;
}

namespace headless {

// This class represents the global headless browser instance. To get a pointer
Expand Down Expand Up @@ -126,6 +123,10 @@ struct HeadlessBrowser::Options {
// string can be used to disable GL rendering (e.g., WebGL support).
std::string gl_implementation;

// Default window size. This is also used to create the window tree host and
// as initial screen size. Defaults to 800x600.
gfx::Size window_size;

private:
Options(int argc, const char** argv);

Expand All @@ -147,6 +148,7 @@ class HeadlessBrowser::Options::Builder {
Builder& SetDisableSandbox(bool disable_sandbox);
Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers);
Builder& SetGLImplementation(const std::string& gl_implementation);
Builder& SetWindowSize(const gfx::Size& window_size);

Options Build();

Expand Down
4 changes: 2 additions & 2 deletions headless/public/headless_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class HEADLESS_EXPORT HeadlessWebContents::Builder {
// about:blank.
Builder& SetInitialURL(const GURL& initial_url);

// Specify the initial window size (default is 800x600).
// Specify the initial window size (default is configured in browser options).
Builder& SetWindowSize(const gfx::Size& size);

// Set a browser context for storing session data (e.g., cookies, cache, local
Expand Down Expand Up @@ -135,7 +135,7 @@ class HEADLESS_EXPORT HeadlessWebContents::Builder {

HeadlessBrowserImpl* browser_;
GURL initial_url_ = GURL("about:blank");
gfx::Size window_size_ = gfx::Size(800, 600);
gfx::Size window_size_;
HeadlessBrowserContext* browser_context_;
std::list<MojoService> mojo_services_;

Expand Down

0 comments on commit 73c4ead

Please sign in to comment.