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> |
| 11 | #include <lib/sys/cpp/component_context.h> |
| 12 | #include <zircon/processargs.h> |
| 13 | #include <utility> |
| 14 | |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 15 | #include "base/fuchsia/fuchsia_logging.h" |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 16 | #include "base/fuchsia/process_context.h" |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 17 | |
Kevin Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 18 | namespace cr_fuchsia { |
| 19 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 20 | namespace { |
| 21 | |
| 22 | // |is_for_logging_test| should only be true when testing WebEngine's logging |
| 23 | // behavior as it prevents WebEngine logs from being included in the test |
| 24 | // output. When false, WebEngine logs are not included in the Fuchsia system |
| 25 | // log. |
| 26 | fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTestsInternal( |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 27 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 28 | component_controller_request, |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 29 | const base::CommandLine& command_line, |
| 30 | bool is_for_logging_test) { |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 31 | fuchsia::sys::LaunchInfo launch_info; |
David Dorwin | 5df93ec | 2020-06-24 20:18:36 | [diff] [blame] | 32 | launch_info.url = |
| 33 | "fuchsia-pkg://fuchsia.com/web_engine#meta/context_provider.cmx"; |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 34 | launch_info.arguments = command_line.argv(); |
| 35 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 36 | if (!is_for_logging_test) { |
| 37 | // Clone stderr from the current process to WebEngine and ask it to |
| 38 | // redirects all logs to stderr. |
| 39 | launch_info.err = fuchsia::sys::FileDescriptor::New(); |
| 40 | launch_info.err->type0 = PA_FD; |
| 41 | zx_status_t status = fdio_fd_clone( |
| 42 | STDERR_FILENO, launch_info.err->handle0.reset_and_get_address()); |
| 43 | ZX_CHECK(status == ZX_OK, status); |
| 44 | launch_info.arguments->push_back("--enable-logging=stderr"); |
| 45 | } |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 46 | |
| 47 | fidl::InterfaceHandle<fuchsia::io::Directory> web_engine_services_dir; |
| 48 | launch_info.directory_request = |
| 49 | web_engine_services_dir.NewRequest().TakeChannel(); |
| 50 | |
| 51 | fuchsia::sys::LauncherPtr launcher; |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 52 | base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest()); |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 53 | launcher->CreateComponent(std::move(launch_info), |
| 54 | std::move(component_controller_request)); |
| 55 | |
Sergey Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 56 | return web_engine_services_dir; |
| 57 | } |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 58 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 59 | } // namespace |
| 60 | |
| 61 | fidl::InterfaceHandle<fuchsia::io::Directory> StartWebEngineForTests( |
| 62 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 63 | component_controller_request, |
| 64 | const base::CommandLine& command_line) { |
| 65 | return StartWebEngineForTestsInternal(std::move(component_controller_request), |
| 66 | command_line, false); |
| 67 | } |
| 68 | |
Sergey Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 69 | fuchsia::web::ContextProviderPtr ConnectContextProvider( |
| 70 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 71 | component_controller_request, |
| 72 | const base::CommandLine& command_line) { |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 73 | sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal( |
| 74 | std::move(component_controller_request), command_line, false)); |
Sergey Ulanov | f1d9f13c | 2020-04-09 22:46:45 | [diff] [blame] | 75 | return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>(); |
Kevin Marshall | 017f461 | 2019-12-10 01:03:35 | [diff] [blame] | 76 | } |
Kevin Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 77 | |
David Dorwin | 753eff8 | 2020-10-06 22:33:01 | [diff] [blame^] | 78 | fuchsia::web::ContextProviderPtr ConnectContextProviderForLoggingTest( |
| 79 | fidl::InterfaceRequest<fuchsia::sys::ComponentController> |
| 80 | component_controller_request, |
| 81 | const base::CommandLine& command_line) { |
| 82 | sys::ServiceDirectory web_engine_service_dir(StartWebEngineForTestsInternal( |
| 83 | std::move(component_controller_request), command_line, true)); |
| 84 | return web_engine_service_dir.Connect<fuchsia::web::ContextProvider>(); |
| 85 | } |
Kevin Marshall | b11af76b5 | 2020-01-29 19:47:26 | [diff] [blame] | 86 | } // namespace cr_fuchsia |