Skip to content

Commit

Permalink
[Headless] Split browser/renderer process dependencies
Browse files Browse the repository at this point in the history
This CL splits //headless:headless into 2 components:
- :headless_renderer
- :headless_non_renderer

:headless_renderer is intended for child processes, and has a smaller
footprint than :headless_non_renderer.

For release builds, this reduces the  size of chrome_child.dll by about
9.7MB.

Bug: 929408
Change-Id: I514a1bb5604f8ab8d6c6031970b5c7a965a8393d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1570654
Reviewed-by: John Budorick <jbudorick@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Commit-Queue: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656433}
  • Loading branch information
Nicolas Ouellet-Payeur authored and Commit Bot committed May 3, 2019
1 parent 4947e63 commit 9d58d48
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 139 deletions.
4 changes: 2 additions & 2 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ group("gn_all") {
":d8_fuchsia",
"//build/fuchsia/fidlgen_js:fidlgen_js_unittests",
"//fuchsia:gn_all",
"//headless",
"//headless:headless_non_renderer",
]
}

Expand Down Expand Up @@ -747,7 +747,7 @@ group("gn_all") {

if (is_linux && use_ozone) {
deps += [
"//headless",
"//headless:headless_non_renderer",
"//headless:headless_tests",
]
}
Expand Down
117 changes: 78 additions & 39 deletions headless/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ config("inside_headless_component") {

group("headless_lib") {
deps = [
":headless",
":headless_non_renderer",
]
}

Expand Down Expand Up @@ -227,7 +227,7 @@ if (headless_fontconfig_utils && !is_fuchsia) {
}

inspector_protocol_generate("protocol_sources") {
visibility = [ "//headless" ]
visibility = [ "//headless:headless_shared_sources" ]
deps = [
"//third_party/blink/renderer/core/inspector:protocol_version",
]
Expand Down Expand Up @@ -262,10 +262,75 @@ inspector_protocol_generate("protocol_sources") {
]
}

jumbo_component("headless") {
# Code needed in both processes. Only exposed through |headless| and
# |headless_non_renderer| components.
jumbo_source_set("headless_shared_sources") {
visibility = [
"//headless:headless_non_renderer",
"//headless:headless",
]

sources = [
"app/headless_shell_switches.cc",
"app/headless_shell_switches.h",
"lib/headless_content_client.cc",
"lib/headless_content_client.h",
"public/headless_browser.cc",
"public/headless_browser.h",
"public/headless_export.h",
"public/internal/headless_devtools_client_impl.h",
"public/internal/message_dispatcher.h",
"public/internal/value_conversions.h",
"public/util/error_reporter.cc",
"public/util/error_reporter.h",
"public/util/user_agent.cc",
"public/util/user_agent.h",
]

sources += generated_devtools_api
sources += get_target_outputs(":protocol_sources")

if (!is_fuchsia) {
sources += [
"lib/headless_crash_reporter_client.cc",
"lib/headless_crash_reporter_client.h",
]
}

deps = [
":gen_devtools_client_api",
":protocol_sources",
"//base:base_static",
"//content/public/common",
"//content/public/common:service_names",
"//ui/base",
"//url",
]

if (!is_fuchsia) {
deps += [ "//components/crash/content/browser" ]
}
if (is_win) {
deps += [ "//components/crash/content/app:crash_export_thunks" ]
}

if (is_linux && !is_chromeos) {
deps += [ "//components/os_crypt" ]
}

configs += [ ":inside_headless_component" ]
}

# Code that is needed in a renderer process.
jumbo_component("headless") {
deps = [
":headless_shared_sources",
]
}

# Code that is not needed in a renderer process.
jumbo_component("headless_non_renderer") {
sources = [
"lib/browser/headless_browser_context_impl.cc",
"lib/browser/headless_browser_context_impl.h",
"lib/browser/headless_browser_context_options.cc",
Expand Down Expand Up @@ -308,35 +373,13 @@ jumbo_component("headless") {
"lib/browser/protocol/page_handler.h",
"lib/browser/protocol/target_handler.cc",
"lib/browser/protocol/target_handler.h",
"lib/headless_content_client.cc",
"lib/headless_content_client.h",
"public/headless_browser.cc",
"public/headless_browser.h",
"public/headless_browser_context.h",
"public/headless_devtools_channel.h",
"public/headless_devtools_client.h",
"public/headless_devtools_target.h",
"public/headless_export.h",
"public/headless_web_contents.h",
"public/internal/headless_devtools_client_impl.h",
"public/internal/message_dispatcher.h",
"public/internal/value_conversions.h",
"public/util/error_reporter.cc",
"public/util/error_reporter.h",
"public/util/user_agent.cc",
"public/util/user_agent.h",
]

if (!is_fuchsia) {
sources += [
"lib/headless_crash_reporter_client.cc",
"lib/headless_crash_reporter_client.h",
]
}

sources += generated_devtools_api
sources += get_target_outputs(":protocol_sources")

if (use_aura) {
sources += [
"lib/browser/headless_browser_impl_aura.cc",
Expand Down Expand Up @@ -372,8 +415,7 @@ jumbo_component("headless") {
}

deps = [
":gen_devtools_client_api",
":protocol_sources",
":headless_shared_sources",
":version_header",
"//base:base_static",
"//components/cookie_config",
Expand All @@ -389,10 +431,6 @@ jumbo_component("headless") {
"//url",
]

if (is_linux && !is_chromeos) {
deps += [ "//components/os_crypt" ]
}

if (enable_basic_printing) {
deps += [ "//components/printing/browser" ]
}
Expand Down Expand Up @@ -534,7 +572,7 @@ if (!is_component_build) {
# component.
group("headless_renderer") {
deps = [
":headless",
":headless_non_renderer",
]
}
}
Expand Down Expand Up @@ -567,7 +605,7 @@ test("headless_unittests") {
}

deps = [
":headless_renderer",
":headless_shell_lib",
"//base/test:run_all_unittests",
"//base/test:test_support",
"//components/security_state/content",
Expand Down Expand Up @@ -719,7 +757,7 @@ test("headless_browsertests") {
}

deps = [
":headless_renderer",
":headless_shell_lib",
"//base",
"//components/security_state/content",
"//components/services/pdf_compositor/public/cpp:manifest",
Expand All @@ -732,10 +770,7 @@ test("headless_browsertests") {
]

if (!is_fuchsia) {
deps += [
"//components/crash/content/app:test_support",
"//components/crash/content/browser",
]
deps += [ "//components/crash/content/browser" ]
}

if (is_linux) {
Expand Down Expand Up @@ -774,7 +809,7 @@ if (is_win) {
"public/headless_shell.h",
]
deps = [
":headless",
":headless_non_renderer",
"//components/services/pdf_compositor/public/cpp:manifest",
"//components/services/pdf_compositor/public/interfaces",
"//content:sandbox_helper_win",
Expand Down Expand Up @@ -871,6 +906,10 @@ jumbo_static_library("headless_shell_lib") {
"//base",
]

if (!is_component_build) {
public_deps += [ ":headless_non_renderer" ]
}

if (!is_fuchsia) {
deps += [ "//components/crash/content/browser" ]
}
Expand Down
81 changes: 80 additions & 1 deletion headless/app/headless_shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "content/public/common/content_switches.h"
#include "headless/app/headless_shell.h"
#include "headless/app/headless_shell_switches.h"
#include "headless/lib/browser/headless_devtools.h"
#include "headless/lib/headless_content_main_delegate.h"
#include "headless/public/headless_devtools_target.h"
#include "net/base/filename_util.h"
#include "net/base/host_port_pair.h"
Expand All @@ -52,6 +52,11 @@
#include "sandbox/win/src/sandbox_types.h"
#endif

#if !defined(CHROME_MULTIPLE_DLL_CHILD)
#include "headless/lib/browser/headless_browser_impl.h"
#include "headless/lib/browser/headless_devtools.h"
#endif

namespace headless {

namespace {
Expand Down Expand Up @@ -137,6 +142,34 @@ base::FilePath GetSSLKeyLogFile(const base::CommandLine* command_line) {

#endif

int RunContentMain(
HeadlessBrowser::Options options,
base::OnceCallback<void(HeadlessBrowser*)> on_browser_start_callback) {
content::ContentMainParams params(nullptr);
#if defined(OS_WIN)
// Sandbox info has to be set and initialized.
CHECK(options.sandbox_info);
params.instance = options.instance;
params.sandbox_info = std::move(options.sandbox_info);
#elif !defined(OS_ANDROID)
params.argc = options.argc;
params.argv = options.argv;
#endif

// TODO(skyostil): Implement custom message pumps.
DCHECK(!options.message_pump);

#if !defined(CHROME_MULTIPLE_DLL_CHILD)
std::unique_ptr<HeadlessBrowserImpl> browser(new HeadlessBrowserImpl(
std::move(on_browser_start_callback), std::move(options)));
HeadlessContentMainDelegate delegate(std::move(browser));
#else
HeadlessContentMainDelegate delegate(std::move(options));
#endif
params.delegate = &delegate;
return content::ContentMain(params);
}

} // namespace

HeadlessShell::HeadlessShell()
Expand Down Expand Up @@ -776,4 +809,50 @@ int HeadlessShellMain(const content::ContentMainParams& params) {
#endif
}

#if defined(OS_WIN)
void RunChildProcessIfNeeded(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info) {
base::CommandLine::Init(0, nullptr);
HeadlessBrowser::Options::Builder builder(0, nullptr);
builder.SetInstance(instance);
builder.SetSandboxInfo(std::move(sandbox_info));
#else
void RunChildProcessIfNeeded(int argc, const char** argv) {
base::CommandLine::Init(argc, argv);
HeadlessBrowser::Options::Builder builder(argc, argv);
#endif // defined(OS_WIN)
const base::CommandLine& command_line(
*base::CommandLine::ForCurrentProcess());

if (!command_line.HasSwitch(::switches::kProcessType))
return;

if (command_line.HasSwitch(switches::kUserAgent)) {
std::string ua = command_line.GetSwitchValueASCII(switches::kUserAgent);
if (net::HttpUtil::IsValidHeaderValue(ua))
builder.SetUserAgent(ua);
}

exit(RunContentMain(builder.Build(),
base::OnceCallback<void(HeadlessBrowser*)>()));
}

int HeadlessBrowserMain(
HeadlessBrowser::Options options,
base::OnceCallback<void(HeadlessBrowser*)> on_browser_start_callback) {
DCHECK(!on_browser_start_callback.is_null());
#if DCHECK_IS_ON()
// The browser can only be initialized once.
static bool browser_was_initialized;
DCHECK(!browser_was_initialized);
browser_was_initialized = true;

// Child processes should not end up here.
DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
::switches::kProcessType));
#endif
return RunContentMain(std::move(options),
std::move(on_browser_start_callback));
}

} // namespace headless
Loading

0 comments on commit 9d58d48

Please sign in to comment.