blob: d06cd9a32401b6de8c58c8fa23f6fdeee38f3eaa [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
52 fidl::InterfaceHandle<fuchsia::io::Directory> web_engine_services_dir;
53 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
Sergey Ulanovf1d9f13c2020-04-09 22:46:4561 return web_engine_services_dir;
62}
Kevin Marshall017f4612019-12-10 01:03:3563
David Dorwin753eff82020-10-06 22:33:0164} // namespace
65
66fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTests(
67 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
68 component_controller_request,
69 const base::CommandLine& command_line) {
70 return StartWebEngineForTestsInternal(std::move(component_controller_request),
71 command_line, false);
72}
73
Sergey Ulanovf1d9f13c2020-04-09 22:46:4574fuchsia::web::ContextProviderPtr ConnectContextProvider(
75 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
76 component_controller_request,
77 const base::CommandLine& command_line) {
David Dorwin753eff82020-10-06 22:33:0178 sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal(
79 std::move(component_controller_request), command_line, false));
Sergey Ulanovf1d9f13c2020-04-09 22:46:4580 return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>();
Kevin Marshall017f4612019-12-10 01:03:3581}
Kevin Marshallb11af76b52020-01-29 19:47:2682
David Dorwin753eff82020-10-06 22:33:0183fuchsia::web::ContextProviderPtr ConnectContextProviderForLoggingTest(
84 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
85 component_controller_request,
86 const base::CommandLine& command_line) {
87 sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal(
88 std::move(component_controller_request), command_line, true));
89 return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>();
90}
Kevin Marshallb11af76b52020-01-29 19:47:2691} // namespace cr_fuchsia