| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 // A small example application showing the use of the C++ Headless Chrome | 5 // A small example application showing the use of the C++ Headless Chrome |
| 6 // library. It navigates to a web site given on the command line, waits for it | 6 // library. It navigates to a web site given on the command line, waits for it |
| 7 // to load and prints out the DOM. | 7 // to load and prints out the DOM. |
| 8 // | 8 // |
| 9 // Tip: start reading from the main() function below. | 9 // Tip: start reading from the main() function below. |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "build/build_config.h" | |
| 15 #include "headless/public/devtools/domains/page.h" | 14 #include "headless/public/devtools/domains/page.h" |
| 16 #include "headless/public/devtools/domains/runtime.h" | 15 #include "headless/public/devtools/domains/runtime.h" |
| 17 #include "headless/public/headless_browser.h" | 16 #include "headless/public/headless_browser.h" |
| 18 #include "headless/public/headless_devtools_client.h" | 17 #include "headless/public/headless_devtools_client.h" |
| 19 #include "headless/public/headless_devtools_target.h" | 18 #include "headless/public/headless_devtools_target.h" |
| 20 #include "headless/public/headless_web_contents.h" | 19 #include "headless/public/headless_web_contents.h" |
| 21 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 22 | 21 |
| 23 #if defined(OS_WIN) | |
| 24 #include "content/public/app/sandbox_helper_win.h" | |
| 25 #include "sandbox/win/src/sandbox_types.h" | |
| 26 #endif | |
| 27 | |
| 28 // This class contains the main application logic, i.e., waiting for a page to | 22 // This class contains the main application logic, i.e., waiting for a page to |
| 29 // load and printing its DOM. Note that browser initialization happens outside | 23 // load and printing its DOM. Note that browser initialization happens outside |
| 30 // this class. | 24 // this class. |
| 31 class HeadlessExample : public headless::HeadlessWebContents::Observer, | 25 class HeadlessExample : public headless::HeadlessWebContents::Observer, |
| 32 public headless::page::Observer { | 26 public headless::page::Observer { |
| 33 public: | 27 public: |
| 34 HeadlessExample(headless::HeadlessBrowser* browser, | 28 HeadlessExample(headless::HeadlessBrowser* browser, |
| 35 headless::HeadlessWebContents* web_contents); | 29 headless::HeadlessWebContents* web_contents); |
| 36 ~HeadlessExample() override; | 30 ~HeadlessExample() override; |
| 37 | 31 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // setting the initial URL to navigate to. | 151 // setting the initial URL to navigate to. |
| 158 tab_builder.SetInitialURL(url); | 152 tab_builder.SetInitialURL(url); |
| 159 | 153 |
| 160 // Create an instance of the example app, which will wait for the page to load | 154 // Create an instance of the example app, which will wait for the page to load |
| 161 // and print its DOM. | 155 // and print its DOM. |
| 162 headless::HeadlessWebContents* web_contents = tab_builder.Build(); | 156 headless::HeadlessWebContents* web_contents = tab_builder.Build(); |
| 163 g_example = new HeadlessExample(browser, web_contents); | 157 g_example = new HeadlessExample(browser, web_contents); |
| 164 } | 158 } |
| 165 | 159 |
| 166 int main(int argc, const char** argv) { | 160 int main(int argc, const char** argv) { |
| 167 #if !defined(OS_WIN) | |
| 168 // This function must be the first thing we call to make sure child processes | 161 // This function must be the first thing we call to make sure child processes |
| 169 // such as the renderer are started properly. The headless library starts | 162 // such as the renderer are started properly. The headless library starts |
| 170 // child processes by forking and exec'ing the main application. | 163 // child processes by forking and exec'ing the main application. |
| 171 headless::RunChildProcessIfNeeded(argc, argv); | 164 headless::RunChildProcessIfNeeded(argc, argv); |
| 172 #endif | |
| 173 | 165 |
| 174 // Create a headless browser instance. There can be one of these per process | 166 // Create a headless browser instance. There can be one of these per process |
| 175 // and it can only be initialized once. | 167 // and it can only be initialized once. |
| 176 headless::HeadlessBrowser::Options::Builder builder(argc, argv); | 168 headless::HeadlessBrowser::Options::Builder builder(argc, argv); |
| 177 | 169 |
| 178 #if defined(OS_WIN) | |
| 179 // In windows, you must initialize and set the sandbox, or pass it along | |
| 180 // if it has already been initialized. | |
| 181 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | |
| 182 content::InitializeSandboxInfo(&sandbox_info); | |
| 183 builder.SetSandboxInfo(&sandbox_info); | |
| 184 #endif | |
| 185 // Here you can customize browser options. As an example we set the window | 170 // Here you can customize browser options. As an example we set the window |
| 186 // size. | 171 // size. |
| 187 builder.SetWindowSize(gfx::Size(800, 600)); | 172 builder.SetWindowSize(gfx::Size(800, 600)); |
| 188 | 173 |
| 189 // Pass control to the headless library. It will bring up the browser and | 174 // Pass control to the headless library. It will bring up the browser and |
| 190 // invoke the given callback on the browser UI thread. Note: if you need to | 175 // invoke the given callback on the browser UI thread. Note: if you need to |
| 191 // pass more parameters to the callback, you can add them to the Bind() call | 176 // pass more parameters to the callback, you can add them to the Bind() call |
| 192 // below. | 177 // below. |
| 193 return headless::HeadlessBrowserMain(builder.Build(), | 178 return headless::HeadlessBrowserMain(builder.Build(), |
| 194 base::Bind(&OnHeadlessBrowserStarted)); | 179 base::Bind(&OnHeadlessBrowserStarted)); |
| 195 } | 180 } |
| OLD | NEW |