Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 1 | // 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 Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 5 | #include "fuchsia/base/context_provider_test_connector.h" |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 6 | |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <fuchsia/sys/cpp/fidl.h> |
| 10 | #include <lib/fdio/directory.h> |
Wez | fb905ff | 2020-10-16 17:30:10 | [diff] [blame] | 11 | #include <lib/fdio/fd.h> |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 12 | #include <lib/sys/cpp/component_context.h> |
| 13 | #include <zircon/processargs.h> |
| 14 | #include <utility> |
| 15 | |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 16 | #include "base/fuchsia/fuchsia_logging.h" |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 17 | #include "base/fuchsia/process_context.h" |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 18 | |
Kevin Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 19 | namespace cr_fuchsia { |
| 20 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 21 | namespace { |
| 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. |
| 27 | fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTestsInternal( |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 28 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 29 | component_controller_request, |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 30 | const base::CommandLine& command_line, |
| 31 | bool is_for_logging_test) { |
David Dorwin | 016a2d8 | 2020-11-10 23:29:24 | [diff] [blame] | 32 | DCHECK(command_line.argv()[0].empty()) << "Must use NO_PROGRAM."; |
| 33 | |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 34 | fuchsia::sys::LaunchInfo launch_info; |
David Dorwin | 5df93ec | 2020-06-24 20:18:36 | [diff] [blame] | 35 | launch_info.url = |
| 36 | "fuchsia-pkg://fuchsia.com/web_engine#meta/context_provider.cmx"; |
David Dorwin | 016a2d8 | 2020-11-10 23:29:24 | [diff] [blame] | 37 | // 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 Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 40 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 41 | if (!is_for_logging_test) { |
| 42 | // Clone stderr from the current process to WebEngine and ask it to |
Wez | 6062659 | 2020-10-30 10:30:57 | [diff] [blame] | 43 | // redirect all logs to stderr. |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 44 | 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 Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 51 | |
Wez | cc6e818 | 2021-02-08 22:33:10 | [diff] [blame] | 52 | fuchsia::io::DirectorySyncPtr web_engine_services_dir; |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 53 | launch_info.directory_request = |
| 54 | web_engine_services_dir.NewRequest().TakeChannel(); |
| 55 | |
| 56 | fuchsia::sys::LauncherPtr launcher; |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 57 | base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest()); |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 58 | launcher->CreateComponent(std::move(launch_info), |
| 59 | std::move(component_controller_request)); |
| 60 | |
Wez | cc6e818 | 2021-02-08 22:33:10 | [diff] [blame] | 61 | // 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 Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 70 | } |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 71 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 72 | } // namespace |
| 73 | |
| 74 | fidl::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 Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 82 | fuchsia::web::ContextProviderPtr ConnectContextProvider( |
| 83 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 84 | component_controller_request, |
| 85 | const base::CommandLine& command_line) { |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 86 | sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal( |
| 87 | std::move(component_controller_request), command_line, false)); |
Sergey Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 88 | return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>(); |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 89 | } |
Kevin Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 90 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame] | 91 | fuchsia::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 Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 99 | } // namespace cr_fuchsia |