0
Files
src/headless
Leszek Swirski c062a07fbb Reland "[android] Re-adds ability to include both v8 snapshot types"
This is a reland of commit 87a4a1df82

The reland adds a dependency on v8_context_snapshot:buildflags in NaCL,
to fix lacros build issues.

Original change's description:
> [android] Re-adds ability to include both v8 snapshot types
>
> A reland of https://crrev.com/c/3209682, which allows selecting at
> runtime whether V8 uses the blink-generated context snapshot or not.
> This reland is both a rebase and an update, and as such has some
> mechanical differences with the original patch -- in particular, it
> switches to using BUILDFLAGS over `#if defined`.
>
> We're relanding this because we want to retry the experiment with
> various new differences (e.g. trying out enabling for just high-end
> Android, collecting metrics from partners rather than aggregated whole
> web performance, looking at metrics outside of CWV, experimenting with
> the impact on the trade-off space of alternative compression schemes
> for the snapshot).
>
> Original description:
>
> > And adds feature as to which one is used at runtime. Specific
> > parts:
> > . adds gn arg: include_both_v8_snapshots. At this time this is
> >   only supported on android. I will likely make it work on chromeos
> >   next.
> > . Adds feature kUseContextSnapshot. This feature is available if
> >   include_both_v8_snapshots is set.
> > . Adds switch kUseContextSnapshotSwitch. This is passed from browser
> >   to renderer if kUseContextSnapshot is enabled. A separate switch is
> >   used as at the time the v8 snapshot is loaded features have not been
> >   loaded.
> > And this updates a bunch of build targets accordingly.
>
> Bug: 40200623, 40539769
> Change-Id: I4e82581809321ec143b3c1484af4267279aa1e2f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5482109
> Reviewed-by: Richard (Torne) Coles <torne@chromium.org>
> Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
> Reviewed-by: John Abd-El-Malek <jam@chromium.org>
> Auto-Submit: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
> Commit-Queue: Leszek Swirski <leszeks@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1293558}

Bug: 40200623, 40539769
Cq-Include-Trybots: luci.chrome.try:linux-lacros-chrome
Change-Id: I5141e1a1ea488ffc3f539f941bf0c52297378e99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5491087
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Derek Schuff <dschuff@chromium.org>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1295360}
2024-05-02 10:51:09 +00:00
..

Headless Chromium

Headless Chromium allows running Chromium in a headless/server environment. Expected use cases include loading web pages, extracting metadata (e.g., the DOM) and generating bitmaps from page contents -- using all the modern web platform features provided by Chromium and Blink.

There are two ways to use Headless Chromium:

Usage via the DevTools remote debugging protocol

  1. Start a normal Chrome binary with the --headless command line flag:
$ chrome --headless --remote-debugging-port=9222 https://chromium.org/
  1. Navigate to http://localhost:9222/ in another browser to open the DevTools interface or use a tool such as Selenium to drive the headless browser.

Usage from Node.js

For example, the chrome-remote-interface Node.js package can be used to extract a page's DOM like this:

const CDP = require('chrome-remote-interface');

CDP((client) => {
  // Extract used DevTools domains.
  const {Page, Runtime} = client;

  // Enable events on domains we are interested in.
  Promise.all([
    Page.enable()
  ]).then(() => {
    return Page.navigate({url: 'https://example.com'});
  });

  // Evaluate outerHTML after page has loaded.
  Page.loadEventFired(() => {
    Runtime.evaluate({expression: 'document.body.outerHTML'}).then((result) => {
      console.log(result.result.value);
      client.close();
    });
  });
}).on('error', (err) => {
  console.error('Cannot connect to browser:', err);
});

Usage as a C++ library

Headless Chromium can be built as a library for embedding into a C++ application. This approach is otherwise similar to controlling the browser over a DevTools connection, but it provides more customization points, e.g., for networking and mojo services.

Headless Example is a small sample application which demonstrates the use of the headless C++ API. It loads a web page and outputs the resulting DOM. To run it, first initialize a headless build configuration:

$ mkdir -p out/Debug
$ echo 'import("//build/args/headless.gn")' > out/Debug/args.gn
$ gn gen out/Debug

Then build the example:

$ ninja -C out/Debug headless_example

After the build completes, the example can be run with the following command:

$ out/Debug/headless_example https://www.google.com/

Headless Shell is a more capable headless application. For instance, it supports remote debugging with the DevTools protocol. To do this, start the application with an argument specifying the debugging port:

$ ninja -C out/Debug headless_shell
$ out/Debug/headless_shell --remote-debugging-port=9222 https://youtube.com/

Then navigate to http://localhost:9222/ with your browser.

As of M118, precompiled headless_shell binaries are available for download under the name chrome-headless-shell via Chrome for Testing infrastructure.

Embedder API

The embedder API allows developers to integrate the headless library into their application. The API provides default implementations for low level adaptation points such as networking and the run loop.

The main embedder API classes are:

  • HeadlessBrowser::Options::Builder - Defines the embedding options, e.g.:
    • SetMessagePump - Replaces the default base message pump. See base::MessagePump.
    • SetProxyServer - Configures an HTTP/HTTPS proxy server to be used for accessing the network.

Client/DevTools API

The headless client API is used to drive the browser and interact with loaded web pages. Its main classes are:

  • HeadlessBrowser - Represents the global headless browser instance.
  • HeadlessWebContents - Represents a single "tab" within the browser.
  • HeadlessDevToolsClient - Provides a C++ interface for inspecting and controlling a tab. The API functions corresponds to DevTools commands. See the client API documentation for more information.

Resources and Documentation

Mailing list: headless-dev@chromium.org

Bug tracker: Internals>Headless

File a new bug (bit.ly/2pP6SBb)