# 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("//chrome/common/features.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"
}

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" ]
}
