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

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

import("//build/config/chromeos/ui_mode.gni")
import("//chrome/common/features.gni")
import("//third_party/closure_compiler/compile_js.gni")
import("//tools/grit/grit_rule.gni")
import("//tools/grit/preprocess_if_expr.gni")
import("//ui/webui/resources/tools/generate_grd.gni")
import("//ui/webui/resources/tools/optimize_webui.gni")

grd_prefix = "gaia_auth_host"
manifest_existing_files = "gaia_auth_host_existing_manifest.json"

generate_grd("build_grd") {
  out_grd = "$target_gen_dir/resources.grd"
  deps = [
    ":build_injected_grdp",
    ":preprocess_src",
  ]

  # Resources are expected to be registered with paths like:
  # gaia_auth_host/resource_name.js
  resource_path_prefix = "gaia_auth_host"

  manifest_files = [ "$target_gen_dir/$manifest_existing_files" ]

  grdp_files = [ "$target_gen_dir/injected_resources.grdp" ]
}

generate_grd("build_injected_grdp") {
  # Resources are expected to be registered with paths like:
  # gaia_auth_host/resource_name.js
  resource_path_prefix = "gaia_auth_host"
  out_grd = "$target_gen_dir/injected_resources.grdp"

  input_files_base_dir = rebase_path(target_gen_dir, root_build_dir)

  input_files = [ "saml_injected.rollup.js" ]

  deps = [ ":build_injected_scripts" ]
}

# Preprocess existing files. Must preprocess directly to target_gen_dir to
# accommodate Closure Compiler, as a large number of external targets expect to
# import these files from relative paths.
preprocess_folder = target_gen_dir
preprocess_if_expr("preprocess_src") {
  out_folder = preprocess_folder
  out_manifest = "$target_gen_dir/$manifest_existing_files"
  in_files = [
    "authenticator.js",
    "channel.js",
    "okta_detect_success_injected.js",
    "password_change_authenticator.js",
    "post_message_channel.js",
    "scroll_helper_injected.js",
    "safe_xml_utils.js",
    "saml_handler.js",
    "saml_injected.js",
    "saml_password_attributes.js",
    "saml_timestamps.js",
    "saml_username_autofill.js",
    "webview_event_manager.js",
  ]
}

grit("resources") {
  defines = chrome_grit_defines

  # These arguments are needed since the grd is generated at build time.
  enable_input_discovery_for_gn_analyze = false
  source = "$target_gen_dir/resources.grd"
  deps = [ ":build_grd" ]
  outputs = [
    "grit/gaia_auth_host_resources.h",
    "grit/gaia_auth_host_resources_map.cc",
    "grit/gaia_auth_host_resources_map.h",
    "gaia_auth_host_resources.pak",
  ]
  output_dir = "$root_gen_dir/chrome"
}

# Limit closure targets to ChromeOS, since closure is deprecated on Desktop.
if (is_chromeos_ash) {
  js_type_check("closure_compile") {
    deps = [
      ":authenticator",
      ":channel",
      ":password_change_authenticator",
      ":post_message_channel",
      ":saml_handler",
      ":saml_password_attributes",
      ":saml_timestamps",
      ":saml_username_autofill",
      ":scroll_helper_injected",
      ":webview_event_manager",
    ]
  }

  js_library("safe_xml_utils") {
    sources = [ "$preprocess_folder/safe_xml_utils.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("channel") {
    sources = [ "$preprocess_folder/channel.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("webview_event_manager") {
    sources = [ "$preprocess_folder/webview_event_manager.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("post_message_channel") {
    sources = [ "$preprocess_folder/post_message_channel.js" ]
    deps = [ ":channel" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("saml_password_attributes") {
    sources = [ "$preprocess_folder/saml_password_attributes.js" ]
    deps = [
      ":safe_xml_utils",
      ":saml_timestamps",
    ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("saml_timestamps") {
    sources = [ "$preprocess_folder/saml_timestamps.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("scroll_helper_injected") {
    sources = [ "$preprocess_folder/scroll_helper_injected.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("saml_username_autofill") {
    sources = [ "$preprocess_folder/scroll_helper_injected.js" ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("saml_handler") {
    sources = [ "$preprocess_folder/saml_handler.js" ]
    deps = [
      ":channel",
      ":post_message_channel",
      ":safe_xml_utils",
      ":saml_password_attributes",
      ":saml_username_autofill",
      ":webview_event_manager",
      "//ash/webui/common/resources:event_target",
    ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("saml_injected") {
    sources = [ "$preprocess_folder/saml_injected.js" ]
    deps = [
      ":channel",
      ":post_message_channel",
      ":scroll_helper_injected",
    ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("authenticator") {
    sources = [ "$preprocess_folder/authenticator.js" ]
    deps = [
      ":saml_handler",
      ":saml_password_attributes",
      "//ash/webui/common/resources:assert",
      "//ash/webui/common/resources:cr.m",
      "//ash/webui/common/resources:event_target",
      "//ash/webui/common/resources:util",
    ]
    externs_list = [
      "$externs_path/chrome_extensions.js",
      "$externs_path/chrome_send.js",
      "$externs_path/webview_tag.js",
    ]
    extra_deps = [ ":preprocess_src" ]
  }

  js_library("password_change_authenticator") {
    sources = [ "$preprocess_folder/password_change_authenticator.js" ]
    deps = [
      ":saml_handler",
      "//ash/webui/common/resources:assert",
      "//ash/webui/common/resources:event_target",
      "//ash/webui/common/resources:util",
    ]
    externs_list = [
      "$externs_path/chrome_extensions.js",
      "$externs_path/webview_tag.js",
    ]
    extra_deps = [ ":preprocess_src" ]
  }
}

stage_folder = "$target_gen_dir/stage"

copy("saml_injected_copy") {
  deps = [ ":preprocess_src" ]
  sources = [
    "$preprocess_folder/channel.js",
    "$preprocess_folder/post_message_channel.js",
    "$preprocess_folder/saml_injected.js",
    "$preprocess_folder/scroll_helper_injected.js",
  ]
  outputs = [ stage_folder + "/{{source_file_part}}" ]
}

# This script is injected in webview by saml_handler.js.
# Consequently, it must always be rolled up by `optimize_webui()`.
optimize_webui("build_injected_scripts") {
  host = "about:blank"
  input = rebase_path(stage_folder, root_build_dir)
  js_module_in_files = [ "saml_injected.js" ]
  deps = [ ":saml_injected_copy" ]
}