blob: 292dfeb52add16e3391a9f8310009cf5e354f078 [file] [log] [blame]
Kevin Marshall017f4612019-12-10 01:03:351// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Kevin Marshallb11af76b52020-01-29 19:47:265#include "fuchsia/base/context_provider_test_connector.h"
Kevin Marshall017f4612019-12-10 01:03:356
7#include <unistd.h>
8
9#include <fuchsia/sys/cpp/fidl.h>
10#include <lib/fdio/directory.h>
Wezfb905ff2020-10-16 17:30:1011#include <lib/fdio/fd.h>
Kevin Marshall017f4612019-12-10 01:03:3512#include <lib/sys/cpp/component_context.h>
13#include <zircon/processargs.h>
14#include <utility>
15
Kevin Marshall017f4612019-12-10 01:03:3516#include "base/fuchsia/fuchsia_logging.h"
Sharon Yangb2ff20e2020-06-19 12:54:0117#include "base/fuchsia/process_context.h"
Kevin Marshall017f4612019-12-10 01:03:3518
Kevin Marshallb11af76b52020-01-29 19:47:2619namespace cr_fuchsia {
20
David Dorwin753eff82020-10-06 22:33:0121namespace {
22
23// |is_for_logging_test| should only be true when testing WebEngine's logging
24// behavior as it prevents WebEngine logs from being included in the test
25// output. When false, WebEngine logs are not included in the Fuchsia system
26// log.
27fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTestsInternal(
Kevin Marshall017f4612019-12-10 01:03:3528 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
29 component_controller_request,
David Dorwin753eff82020-10-06 22:33:0130 const base::CommandLine& command_line,
31 bool is_for_logging_test) {
David Dorwin016a2d82020-11-10 23:29:2432 DCHECK(command_line.argv()[0].empty()) << "Must use NO_PROGRAM.";
33
Kevin Marshall017f4612019-12-10 01:03:3534 fuchsia::sys::LaunchInfo launch_info;
David Dorwin5df93ec2020-06-24 20:18:3635 launch_info.url =
36 "fuchsia-pkg://fuchsia.com/web_engine#meta/context_provider.cmx";
David Dorwin016a2d82020-11-10 23:29:2437 // Add all switches and arguments, skipping the program.
38 launch_info.arguments.emplace(std::vector<std::string>(
39 command_line.argv().begin() + 1, command_line.argv().end()));
Kevin Marshall017f4612019-12-10 01:03:3540
David Dorwin753eff82020-10-06 22:33:0141 if (!is_for_logging_test) {
42 // Clone stderr from the current process to WebEngine and ask it to
Wez60626592020-10-30 10:30:5743 // redirect all logs to stderr.
David Dorwin753eff82020-10-06 22:33:0144 launch_info.err = fuchsia::sys::FileDescriptor::New();
45 launch_info.err->type0 = PA_FD;
46 zx_status_t status = fdio_fd_clone(
47 STDERR_FILENO, launch_info.err->handle0.reset_and_get_address());
48 ZX_CHECK(status == ZX_OK, status);
49 launch_info.arguments->push_back("--enable-logging=stderr");
50 }
Kevin Marshall017f4612019-12-10 01:03:3551
Wezcc6e8182021-02-08 22:33:1052 fuchsia::io::DirectorySyncPtr web_engine_services_dir;
Kevin Marshall017f4612019-12-10 01:03:3553 launch_info.directory_request =
54 web_engine_services_dir.NewRequest().TakeChannel();
55
56 fuchsia::sys::LauncherPtr launcher;
Sharon Yangb2ff20e2020-06-19 12:54:0157 base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest());
Kevin Marshall017f4612019-12-10 01:03:3558 launcher->CreateComponent(std::move(launch_info),
59 std::move(component_controller_request));
60
Wezcc6e8182021-02-08 22:33:1061 // The WebEngine binary can take sufficiently long for blobfs to resolve that
62 // tests using it may timeout as a result. Wait for the ContextProvider to
63 // be responsive, by making a synchronous request to its service directory.
64 fuchsia::io::NodeAttributes attributes{};
65 zx_status_t stat{};
66 zx_status_t status = web_engine_services_dir->GetAttr(&stat, &attributes);
67 ZX_CHECK(status == ZX_OK, status);
68
69 return web_engine_services_dir.Unbind();
Sergey Ulanovf1d9f13c2020-04-09 22:46:4570}
Kevin Marshall017f4612019-12-10 01:03:3571
David Dorwin753eff82020-10-06 22:33:0172} // namespace
73
74fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTests(
75 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
76 component_controller_request,
77 const base::CommandLine& command_line) {
78 return StartWebEngineForTestsInternal(std::move(component_controller_request),
79 command_line, false);
80}
81
Sergey Ulanovf1d9f13c2020-04-09 22:46:4582fuchsia::web::ContextProviderPtr ConnectContextProvider(
83 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
84 component_controller_request,
85 const base::CommandLine& command_line) {
David Dorwin753eff82020-10-06 22:33:0186 sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal(
87 std::move(component_controller_request), command_line, false));
Sergey Ulanovf1d9f13c2020-04-09 22:46:4588 return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>();
Kevin Marshall017f4612019-12-10 01:03:3589}
Kevin Marshallb11af76b52020-01-29 19:47:2690
David Dorwin753eff82020-10-06 22:33:0191fuchsia::web::ContextProviderPtr ConnectContextProviderForLoggingTest(
92 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
93 component_controller_request,
94 const base::CommandLine& command_line) {
95 sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal(
96 std::move(component_controller_request), command_line, true));
97 return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>();
98}
Kevin Marshallb11af76b52020-01-29 19:47:2699} // namespace cr_fuchsia