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" |
14 #include "headless/public/devtools/domains/page.h" | 15 #include "headless/public/devtools/domains/page.h" |
15 #include "headless/public/devtools/domains/runtime.h" | 16 #include "headless/public/devtools/domains/runtime.h" |
16 #include "headless/public/headless_browser.h" | 17 #include "headless/public/headless_browser.h" |
17 #include "headless/public/headless_devtools_client.h" | 18 #include "headless/public/headless_devtools_client.h" |
18 #include "headless/public/headless_devtools_target.h" | 19 #include "headless/public/headless_devtools_target.h" |
19 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
20 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
21 | 22 |
| 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 |
22 // This class contains the main application logic, i.e., waiting for a page to | 28 // This class contains the main application logic, i.e., waiting for a page to |
23 // load and printing its DOM. Note that browser initialization happens outside | 29 // load and printing its DOM. Note that browser initialization happens outside |
24 // this class. | 30 // this class. |
25 class HeadlessExample : public headless::HeadlessWebContents::Observer, | 31 class HeadlessExample : public headless::HeadlessWebContents::Observer, |
26 public headless::page::Observer { | 32 public headless::page::Observer { |
27 public: | 33 public: |
28 HeadlessExample(headless::HeadlessBrowser* browser, | 34 HeadlessExample(headless::HeadlessBrowser* browser, |
29 headless::HeadlessWebContents* web_contents); | 35 headless::HeadlessWebContents* web_contents); |
30 ~HeadlessExample() override; | 36 ~HeadlessExample() override; |
31 | 37 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // setting the initial URL to navigate to. | 157 // setting the initial URL to navigate to. |
152 tab_builder.SetInitialURL(url); | 158 tab_builder.SetInitialURL(url); |
153 | 159 |
154 // Create an instance of the example app, which will wait for the page to load | 160 // Create an instance of the example app, which will wait for the page to load |
155 // and print its DOM. | 161 // and print its DOM. |
156 headless::HeadlessWebContents* web_contents = tab_builder.Build(); | 162 headless::HeadlessWebContents* web_contents = tab_builder.Build(); |
157 g_example = new HeadlessExample(browser, web_contents); | 163 g_example = new HeadlessExample(browser, web_contents); |
158 } | 164 } |
159 | 165 |
160 int main(int argc, const char** argv) { | 166 int main(int argc, const char** argv) { |
| 167 #if !defined(OS_WIN) |
161 // This function must be the first thing we call to make sure child processes | 168 // This function must be the first thing we call to make sure child processes |
162 // such as the renderer are started properly. The headless library starts | 169 // such as the renderer are started properly. The headless library starts |
163 // child processes by forking and exec'ing the main application. | 170 // child processes by forking and exec'ing the main application. |
164 headless::RunChildProcessIfNeeded(argc, argv); | 171 headless::RunChildProcessIfNeeded(argc, argv); |
| 172 #endif |
165 | 173 |
166 // Create a headless browser instance. There can be one of these per process | 174 // Create a headless browser instance. There can be one of these per process |
167 // and it can only be initialized once. | 175 // and it can only be initialized once. |
168 headless::HeadlessBrowser::Options::Builder builder(argc, argv); | 176 headless::HeadlessBrowser::Options::Builder builder(argc, argv); |
169 | 177 |
| 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 |
170 // Here you can customize browser options. As an example we set the window | 185 // Here you can customize browser options. As an example we set the window |
171 // size. | 186 // size. |
172 builder.SetWindowSize(gfx::Size(800, 600)); | 187 builder.SetWindowSize(gfx::Size(800, 600)); |
173 | 188 |
174 // Pass control to the headless library. It will bring up the browser and | 189 // Pass control to the headless library. It will bring up the browser and |
175 // invoke the given callback on the browser UI thread. Note: if you need to | 190 // invoke the given callback on the browser UI thread. Note: if you need to |
176 // pass more parameters to the callback, you can add them to the Bind() call | 191 // pass more parameters to the callback, you can add them to the Bind() call |
177 // below. | 192 // below. |
178 return headless::HeadlessBrowserMain(builder.Build(), | 193 return headless::HeadlessBrowserMain(builder.Build(), |
179 base::Bind(&OnHeadlessBrowserStarted)); | 194 base::Bind(&OnHeadlessBrowserStarted)); |
180 } | 195 } |
OLD | NEW |