OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <iostream> | 5 #include <iostream> |
6 #include <memory> | 6 #include <memory> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 using headless::HeadlessDevToolsClient; | 35 using headless::HeadlessDevToolsClient; |
36 using headless::HeadlessWebContents; | 36 using headless::HeadlessWebContents; |
37 namespace page = headless::page; | 37 namespace page = headless::page; |
38 namespace runtime = headless::runtime; | 38 namespace runtime = headless::runtime; |
39 | 39 |
40 namespace { | 40 namespace { |
41 // Address where to listen to incoming DevTools connections. | 41 // Address where to listen to incoming DevTools connections. |
42 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; | 42 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
43 // Default file name for screenshot. Can be overriden by "--screenshot" switch. | 43 // Default file name for screenshot. Can be overriden by "--screenshot" switch. |
44 const char kDefaultScreenshotFileName[] = "screenshot.png"; | 44 const char kDefaultScreenshotFileName[] = "screenshot.png"; |
| 45 |
| 46 bool ParseWindowSize(std::string window_size, gfx::Size* parsed_window_size) { |
| 47 int width, height = 0; |
| 48 if (sscanf(window_size.c_str(), "%dx%d", &width, &height) >= 2 && |
| 49 width >= 0 && height >= 0) { |
| 50 parsed_window_size->set_width(width); |
| 51 parsed_window_size->set_height(height); |
| 52 return true; |
| 53 } |
| 54 return false; |
45 } | 55 } |
| 56 } // namespace |
46 | 57 |
47 // A sample application which demonstrates the use of the headless API. | 58 // A sample application which demonstrates the use of the headless API. |
48 class HeadlessShell : public HeadlessWebContents::Observer, page::Observer { | 59 class HeadlessShell : public HeadlessWebContents::Observer, page::Observer { |
49 public: | 60 public: |
50 HeadlessShell() | 61 HeadlessShell() |
51 : browser_(nullptr), | 62 : browser_(nullptr), |
52 devtools_client_(HeadlessDevToolsClient::Create()), | 63 devtools_client_(HeadlessDevToolsClient::Create()), |
53 web_contents_(nullptr), | 64 web_contents_(nullptr), |
54 processed_page_ready_(false), | 65 processed_page_ready_(false), |
55 browser_context_(nullptr) {} | 66 browser_context_(nullptr) {} |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 builder.SetGLImplementation( | 356 builder.SetGLImplementation( |
346 command_line.GetSwitchValueASCII(headless::switches::kUseGL)); | 357 command_line.GetSwitchValueASCII(headless::switches::kUseGL)); |
347 } | 358 } |
348 | 359 |
349 if (command_line.HasSwitch(headless::switches::kUserDataDir)) { | 360 if (command_line.HasSwitch(headless::switches::kUserDataDir)) { |
350 builder.SetUserDataDir( | 361 builder.SetUserDataDir( |
351 command_line.GetSwitchValuePath(headless::switches::kUserDataDir)); | 362 command_line.GetSwitchValuePath(headless::switches::kUserDataDir)); |
352 builder.SetIncognitoMode(false); | 363 builder.SetIncognitoMode(false); |
353 } | 364 } |
354 | 365 |
| 366 if (command_line.HasSwitch(headless::switches::kWindowSize)) { |
| 367 std::string window_size = |
| 368 command_line.GetSwitchValueASCII(headless::switches::kWindowSize); |
| 369 gfx::Size parsed_window_size; |
| 370 if (!ParseWindowSize(window_size, &parsed_window_size)) { |
| 371 LOG(ERROR) << "Malformed window size"; |
| 372 return EXIT_FAILURE; |
| 373 } |
| 374 builder.SetWindowSize(parsed_window_size); |
| 375 } |
| 376 |
355 return HeadlessBrowserMain( | 377 return HeadlessBrowserMain( |
356 builder.Build(), | 378 builder.Build(), |
357 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 379 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
358 } | 380 } |
OLD | NEW |