chromium/chrome/browser/resources/pdf/BUILD.gn

# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//pdf/features.gni")
import("//ui/webui/resources/tools/build_webui.gni")
import("//ui/webui/resources/tools/generate_grd.gni")
import("//ui/webui/resources/tools/optimize_webui.gni")

assert(enable_pdf, "enable_pdf check failed")

build_webui("build") {
  static_files = [
    "index.css",
    "index.html",
    "index_print.html",
  ]
  if (enable_ink) {
    static_files += [ "ink/index.html" ]
  }

  # TODO(crbug.com/362783117): Check in the .html.ts files for these two
  # elements, once the bug with <if expr> breaking UIs when JS code coverage
  # is enabled has been fixed.
  web_component_files = [
    "pdf_viewer.ts",
    "elements/viewer_toolbar.ts",
  ]

  if (enable_ink) {
    web_component_files += [
      "elements/viewer_annotations_bar.ts",
      "elements/viewer_annotations_mode_dialog.ts",
      "elements/viewer_ink_host.ts",
      "elements/viewer_pen_options.ts",
      "elements/viewer_toolbar_dropdown.ts",
    ]
  }

  non_web_component_files = [
    "before_unload_proxy.ts",
    "bookmark_type.ts",
    "browser_api.ts",
    "constants.ts",
    "controller.ts",
    "elements/viewer_attachment.html.ts",
    "elements/viewer_attachment.ts",
    "elements/viewer_attachment_bar.html.ts",
    "elements/viewer_attachment_bar.ts",
    "elements/viewer_bookmark.html.ts",
    "elements/viewer_bookmark.ts",
    "elements/viewer_document_outline.html.ts",
    "elements/viewer_document_outline.ts",
    "elements/viewer_download_controls.html.ts",
    "elements/viewer_download_controls.ts",
    "elements/viewer_error_dialog.html.ts",
    "elements/viewer_error_dialog.ts",
    "elements/viewer_page_selector.html.ts",
    "elements/viewer_page_selector.ts",
    "elements/viewer_password_dialog.html.ts",
    "elements/viewer_password_dialog.ts",
    "elements/viewer_pdf_sidenav.html.ts",
    "elements/viewer_pdf_sidenav.ts",
    "elements/viewer_properties_dialog.html.ts",
    "elements/viewer_properties_dialog.ts",
    "elements/viewer_thumbnail_bar.html.ts",
    "elements/viewer_thumbnail_bar.ts",
    "elements/viewer_thumbnail.html.ts",
    "elements/viewer_thumbnail.ts",
    "gesture_detector.ts",
    "internal_plugin.ts",
    "local_storage_proxy.ts",
    "main.ts",
    "metrics.ts",
    "navigator.ts",
    "open_pdf_params_parser.ts",
    "pdf_print_wrapper.ts",
    "pdf_scripting_api.ts",
    "pdf_viewer_base.ts",
    "pdf_viewer_private_proxy.ts",
    "pdf_viewer_utils.ts",
    "pdf_viewer_wrapper.ts",
    "pdf_internal_plugin_wrapper.ts",
    "swipe_detector.ts",
    "viewport.ts",
    "zoom_manager.ts",

    # Files only used in Print Preview's PDF viewer UI.
    "elements/viewer_page_indicator.html.ts",
    "elements/viewer_page_indicator.ts",
    "elements/viewer_zoom_button.html.ts",
    "elements/viewer_zoom_button.ts",
    "elements/viewer_zoom_toolbar.html.ts",
    "elements/viewer_zoom_toolbar.ts",
    "main_print.ts",
    "pdf_viewer_print.html.ts",
    "pdf_viewer_print.ts",
    "toolbar_manager.ts",
  ]

  if (enable_ink) {
    non_web_component_files += [
      "annotation_tool.ts",
      "ink/ink_api.ts",
      "ink_controller.ts",
    ]
  }

  if (enable_pdf_ink2) {
    non_web_component_files += [
      "elements/viewer_side_panel.html.ts",
      "elements/viewer_side_panel.ts",
    ]
  }

  css_files = [
    "elements/pdf_shared.css",
    "elements/shared_vars.css",
    "elements/viewer_attachment.css",
    "elements/viewer_attachment_bar.css",
    "elements/viewer_bookmark.css",
    "elements/viewer_document_outline.css",
    "elements/viewer_download_controls.css",
    "elements/viewer_page_selector.css",
    "elements/viewer_password_dialog.css",
    "elements/viewer_pdf_sidenav.css",
    "elements/viewer_properties_dialog.css",
    "elements/viewer_thumbnail.css",
    "elements/viewer_thumbnail_bar.css",
    "elements/viewer_toolbar.css",
    "pdf_viewer.css",
    "pdf_viewer_shared_style.css",

    # Files only used in Print Preview's PDF Viewer UI.
    "elements/viewer_page_indicator.css",
    "elements/viewer_zoom_button.css",
    "elements/viewer_zoom_toolbar.css",
    "pdf_viewer_print.css",
  ]

  if (enable_pdf_ink2) {
    css_files += [ "elements/viewer_side_panel.css" ]
  }

  icons_html_files = [ "elements/icons.html" ]

  html_to_wrapper_scheme = "chrome"
  html_to_wrapper_template = "detect"

  # TypeScript Compiler parameters
  ts_composite = true
  ts_definitions = [
    "//tools/typescript/definitions/chrome_event.d.ts",
    "//tools/typescript/definitions/content_settings.d.ts",
    "//tools/typescript/definitions/file_system.d.ts",
    "//tools/typescript/definitions/metrics_private.d.ts",
    "//tools/typescript/definitions/mime_handler_private.d.ts",
    "//tools/typescript/definitions/pdf_viewer_private.d.ts",
    "//tools/typescript/definitions/pending.d.ts",
    "//tools/typescript/definitions/resources_private.d.ts",
    "//tools/typescript/definitions/runtime.d.ts",
    "//tools/typescript/definitions/tabs.d.ts",
    "//tools/typescript/definitions/windows.d.ts",
  ]
  if (enable_ink) {
    ts_definitions +=
        [ "//chrome/browser/resources/pdf/ink/drawing_canvas.d.ts" ]
  }
  ts_deps = [
    "//third_party/lit/v3_0:build_ts",
    "//ui/webui/resources/cr_elements:build_ts",
    "//ui/webui/resources/js:build_ts",
  ]
  if (enable_ink) {
    ts_deps += [ "//third_party/polymer/v3_0:library" ]
  }

  # grd file generation parameters
  grd_prefix = "pdf"
  extra_grdp_deps = [ ":build_internal_plugin_grdp" ]
  extra_grdp_files = [ "$target_gen_dir/internal_plugin_resources.grdp" ]
  grd_resource_path_prefix = "pdf"

  # Optimization parameters
  webui_context_type = "trusted_only"
  optimize = optimize_webui
  if (optimize) {
    optimize_webui_host = "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"
    optimize_webui_in_files = [
      "pdf_viewer_wrapper.js",
      "pdf_print_wrapper.js",
    ]
    optimize_webui_excludes = [
      "browser_api.js",

      # Exclude pdf_scripting_api so that Print Preview can import
      # pdfCreateOutOfProcessPlugin without importing the entire PDF bundle.
      "pdf_scripting_api.js",
    ]
    if (enable_ink) {
      optimize_webui_excludes += [
        "ink/drawing_canvas_externs.js",
        "ink/ink_api.js",
      ]
    }
    extra_grdp_deps += [ ":build_excluded_grdp" ]
    extra_grdp_files += [ "$target_gen_dir/excluded_resources.grdp" ]
  }
}

# Extra grdp build targets

tsc_folder = "tsc"

# This script is injected by `pdf::PluginResponseWriter`, and must stand alone.
# Consequently, it must always be rolled up by `optimize_webui()`.
build_internal_plugin_manifest = "build_internal_plugin_manifest.json"
optimize_webui("build_internal_plugin") {
  host = "about:blank"
  input = rebase_path("$target_gen_dir/$tsc_folder", root_build_dir)
  js_module_in_files = [ "pdf_internal_plugin_wrapper.js" ]
  out_manifest = "$target_gen_dir/$build_internal_plugin_manifest"

  deps = [ ":build_ts" ]
  excludes = [ "browser_api.js" ]
}

generate_grd("build_internal_plugin_grdp") {
  manifest_files = [ "$target_gen_dir/$build_internal_plugin_manifest" ]
  deps = [ ":build_internal_plugin" ]
  grd_prefix = "pdf"
  resource_path_prefix = "pdf"
  out_grd = "$target_gen_dir/internal_plugin_resources.grdp"
}

if (optimize_webui) {
  # Need to manually register files that are excluded from the optimized bundle.
  generate_grd("build_excluded_grdp") {
    input_files = [
      "browser_api.js",
      "main.js",
      "main_print.js",
      "pdf_scripting_api.js",
    ]
    if (enable_ink) {
      input_files += [ "ink/ink_api.js" ]
    }
    input_files_base_dir =
        rebase_path("$target_gen_dir/$tsc_folder", root_build_dir)
    deps = [ ":build_ts" ]

    grd_prefix = "pdf"
    out_grd = "$target_gen_dir/excluded_resources.grdp"
    resource_path_prefix = "pdf"
  }
}