test_fonts_fuchsia_cfv1.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2022 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. #include "skia/ext/test_fonts_fuchsia.h"
  5. #include <fuchsia/fonts/cpp/fidl.h>
  6. #include <fuchsia/io/cpp/fidl.h>
  7. #include <fuchsia/sys/cpp/fidl.h>
  8. #include <lib/fidl/cpp/interface_handle.h>
  9. #include <lib/sys/cpp/component_context.h>
  10. #include <utility>
  11. #include "base/check.h"
  12. #include "base/files/file_path.h"
  13. #include "base/fuchsia/file_utils.h"
  14. #include "base/fuchsia/fuchsia_logging.h"
  15. #include "base/fuchsia/process_context.h"
  16. #include "base/no_destructor.h"
  17. #include "base/path_service.h"
  18. #include "skia/ext/test_fonts.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace skia {
  21. namespace {
  22. // Runs the fonts component via fuchsia.sys.Launcher.
  23. class TestFontsProvider {
  24. public:
  25. TestFontsProvider();
  26. TestFontsProvider(const TestFontsProvider&) = delete;
  27. TestFontsProvider& operator=(const TestFontsProvider&) = delete;
  28. ~TestFontsProvider();
  29. fuchsia::fonts::ProviderHandle GetProvider();
  30. private:
  31. fidl::InterfaceHandle<fuchsia::sys::ComponentController> controller_;
  32. absl::optional<sys::ServiceDirectory> services_client_;
  33. };
  34. TestFontsProvider::TestFontsProvider() {
  35. // Start a fuchsia.fonts.Provider instance and configure it to load the test
  36. // fonts, which must be bundled in the calling process' package.
  37. fuchsia::sys::LaunchInfo launch_info;
  38. launch_info.url = "fuchsia-pkg://fuchsia.com/fonts#meta/fonts.cmx";
  39. // Note: the manifest name here matches the default used by the Fuchsia fonts
  40. // component so that the file can be found automagically by the modern (cfv2)
  41. // variant.
  42. launch_info.arguments.emplace(
  43. {"--font-manifest", "/test_fonts/all.font_manifest.json"});
  44. launch_info.flat_namespace = fuchsia::sys::FlatNamespace::New();
  45. launch_info.flat_namespace->paths.push_back("/test_fonts");
  46. base::FilePath assets_path;
  47. CHECK(base::PathService::Get(base::DIR_ASSETS, &assets_path))
  48. << "Can't get DIR_ASSETS";
  49. launch_info.flat_namespace->directories.push_back(
  50. base::OpenDirectoryHandle(assets_path.AppendASCII("test_fonts"))
  51. .TakeChannel());
  52. fidl::InterfaceHandle<fuchsia::io::Directory> font_provider_services_dir;
  53. launch_info.directory_request =
  54. font_provider_services_dir.NewRequest().TakeChannel();
  55. fuchsia::sys::LauncherSyncPtr launcher;
  56. auto status =
  57. base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest());
  58. ZX_CHECK(status == ZX_OK, status) << "Connect to fuchsia.sys.Launcher";
  59. launcher->CreateComponent(std::move(launch_info), controller_.NewRequest());
  60. services_client_.emplace(std::move(font_provider_services_dir));
  61. }
  62. TestFontsProvider::~TestFontsProvider() = default;
  63. fuchsia::fonts::ProviderHandle TestFontsProvider::GetProvider() {
  64. fuchsia::fonts::ProviderHandle font_provider;
  65. services_client_->Connect(font_provider.NewRequest());
  66. return font_provider;
  67. }
  68. } // namespace
  69. fuchsia::fonts::ProviderHandle GetTestFontsProvider() {
  70. static base::NoDestructor<TestFontsProvider> test_fonts_provider;
  71. return test_fonts_provider->GetProvider();
  72. }
  73. } // namespace skia