blob: e3ceff78b220c6da586cd901b5b88460d7324dff [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>
11#include <lib/sys/cpp/component_context.h>
12#include <zircon/processargs.h>
13#include <utility>
14
Kevin Marshall017f4612019-12-10 01:03:3515#include "base/fuchsia/fuchsia_logging.h"
Sharon Yangb2ff20e2020-06-19 12:54:0116#include "base/fuchsia/process_context.h"
Kevin Marshallb11af76b52020-01-29 19:47:2617#include "base/strings/strcat.h"
18#include "fuchsia/base/release_channel.h"
Kevin Marshall017f4612019-12-10 01:03:3519
Kevin Marshallb11af76b52020-01-29 19:47:2620namespace cr_fuchsia {
21
Sergey Ulanovf1d9f13c2020-04-09 22:46:4522fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTests(
Kevin Marshall017f4612019-12-10 01:03:3523 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
24 component_controller_request,
25 const base::CommandLine& command_line) {
26 fuchsia::sys::LaunchInfo launch_info;
Kevin Marshallb11af76b52020-01-29 19:47:2627 launch_info.url = base::StrCat({"fuchsia-pkg://fuchsia.com/web_engine",
28 BUILDFLAG(FUCHSIA_RELEASE_CHANNEL_SUFFIX),
29 "#meta/context_provider.cmx"});
Kevin Marshall017f4612019-12-10 01:03:3530 launch_info.arguments = command_line.argv();
31
32 // Clone stderr from the current process to WebEngine and ask it to
33 // redirects all logs to stderr.
34 launch_info.err = fuchsia::sys::FileDescriptor::New();
35 launch_info.err->type0 = PA_FD;
36 zx_status_t status = fdio_fd_clone(
37 STDERR_FILENO, launch_info.err->handle0.reset_and_get_address());
38 ZX_CHECK(status == ZX_OK, status);
39 launch_info.arguments->push_back("--enable-logging=stderr");
40
41 fidl::InterfaceHandle<fuchsia::io::Directory> web_engine_services_dir;
42 launch_info.directory_request =
43 web_engine_services_dir.NewRequest().TakeChannel();
44
45 fuchsia::sys::LauncherPtr launcher;
Sharon Yangb2ff20e2020-06-19 12:54:0146 base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest());
Kevin Marshall017f4612019-12-10 01:03:3547 launcher->CreateComponent(std::move(launch_info),
48 std::move(component_controller_request));
49
Sergey Ulanovf1d9f13c2020-04-09 22:46:4550 return web_engine_services_dir;
51}
Kevin Marshall017f4612019-12-10 01:03:3552
Sergey Ulanovf1d9f13c2020-04-09 22:46:4553fuchsia::web::ContextProviderPtr ConnectContextProvider(
54 fidl::InterfaceRequest<fuchsia::sys::ComponentController>
55 component_controller_request,
56 const base::CommandLine& command_line) {
57 sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTests(
58 std::move(component_controller_request), command_line));
59 return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>();
Kevin Marshall017f4612019-12-10 01:03:3560}
Kevin Marshallb11af76b52020-01-29 19:47:2661
62} // namespace cr_fuchsia