# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/ohos.gni")
import("../config.gni")
import("../web_aafwk.gni")
import("//build/config/components/ets_frontend/ets2abc_config.gni")

webview_root_path = webview_path
if (target_cpu == "arm64") {
  web_source = "prebuilts/arm64/ArkWebCore.hap"
  defines = [ "webview_arm64" ]
} else if (target_cpu == "arm") {
  web_source = "prebuilts/arm/ArkWebCore.hap"
  defines = []
} else if (target_cpu == "x86_64") {
  web_source = "prebuilts/arm64/ArkWebCore.hap"
  defines = [ "webview_x86_64" ]
}

defines += [
  "WEBVIEW_PACKAGE_NAME=\"${webview_package_name}\"",
  "WEBVIEW_SANDBOX_LIB_PATH=\"${webview_sandbox_lib_path}\"",
  "LEGACY_WEBVIEW_SANDBOX_LIB_PATH=\"${legacy_webview_sandbox_lib_path}\"",
  "WEBVIEW_CRASHPAD_HANDLER_SO=\"${webview_crashpad_handler_so}\"",
  "WEBVIEW_SANDBOX_RELATIVE_LIB_PATH=\"${webview_sandbox_relative_lib_path}\"",
  "WEBVIEW_ENGINE_SO=\"${webview_engine_so}\"",
]

if (use_hwasan) {
  defines += [
    "IS_ASAN",
    "WEBVIEW_SANDBOX_LIB_PATH_ASAN=\"${asan_webview_sandbox_lib_path}\"",
  ]
}

config("nweb_config") {
  include_dirs = [ "${target_gen_dir}/include" ]
  include_dirs += [ "include" ]

  cflags = [
    "-Wall",
    "-Werror",
    "-g3",
  ]
}

config("nweb_public_interface") {
  include_dirs = [ "${target_gen_dir}/include" ]
  include_dirs += [ "include" ]
}

config("nweb_glue_interface") {
  include_dirs = [
    "${target_gen_dir}/../ohos_glue",
    "${target_gen_dir}/../ohos_glue/ohos_nweb/bridge",
  ]
}

ohos_shared_library("web_configs") {
  include_dirs = [
    "${target_gen_dir}/include",
    "../ohos_interface/include/ohos_nweb",
    "include",
  ]
  sources = [ "src/nweb_config_helper.cpp" ]

  deps = [
    ":web_config",
    "${webview_path}/arkweb_utils:libarkweb_utils",
  ]

  external_deps = [
    "ability_runtime:app_context",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "config_policy:configpolicy_util",
    "c_utils:utils",
    "hilog:libhilog",
    "init:libbegetutil",
    "ipc:ipc_core",
    "libxml2:libxml2",
    "ability_runtime:ability_connect_callback_stub",
    "samgr:samgr_proxy",
  ]

  part_name = "webview"
  subsystem_name = "web"
}

ohos_shared_library("libnweb") {
  output_name = "arkweb_core_loader"
  if (target_cpu == "arm64") {
    branch_protector_ret = "pac_ret"
  }

  configs = [
    ":nweb_config",
    ":nweb_glue_interface",
  ]
  public_configs = [ ":nweb_public_interface" ]
  sources = [
    "src/nweb_enhance_surface_adapter.cpp",
    "src/nweb_helper.cpp",
    "src/nweb_hisysevent.cpp",
    "src/nweb_surface_adapter.cpp",
    "src/webview_value.cpp",
  ]

  deps = [
    ":alias",
    ":context-menu",
    ":web.para",
    ":web.para.dac",
    ":web_config",
    ":web_configs",
    "${webview_path}/ohos_glue:ohos_adapter_glue_source",
    "${webview_path}/ohos_glue:ohos_base_glue_source",
    "${webview_path}/ohos_glue:ohos_nweb_glue_source",
    "${webview_path}/arkweb_utils:libarkweb_utils",
  ]

  if (webview_app_fwk_update_enable) {
    deps += [ "${webview_path}/sa/app_fwk_update:app_fwk_update" ]
  }

  external_deps = [
    "ability_runtime:ability_manager",
    "ability_runtime:app_context",
    "ability_runtime:app_manager",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "c_utils:utils",
    "config_policy:configpolicy_util",
    "graphic_2d:libcomposer",
    "graphic_2d:librender_service_client",
    "graphic_surface:surface",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "i18n:intl_util",
    "init:libbegetutil",
    "input:libmmi-client",
    "ipc:ipc_core",
    "libxml2:libxml2",
    "window_manager:libwm",
  ]

  innerapi_tags = [ "platformsdk" ]
  part_name = "webview"
  subsystem_name = "web"
}

ohos_prebuilt_etc("nweb_hap") {
  source = web_source
  module_install_dir = "app/${webview_package_name}"
  part_name = "webview"
  subsystem_name = "web"
  license_file = "./prebuilts/NOTICE"
}

ohos_prebuilt_etc("web_config") {
  source = "${webview_root_path}/ohos_nweb/etc/web_config.xml"
  subsystem_name = "web"
  part_name = "webview"
  module_install_dir = "etc/web"
}

ohos_prebuilt_etc("web.para") {
  source = "${webview_root_path}/ohos_nweb/etc/para/web.para"
  subsystem_name = "web"
  part_name = "webview"
  module_install_dir = "etc/param"
}

ohos_prebuilt_etc("web.para.dac") {
  source = "${webview_root_path}/ohos_nweb/etc/para/web.para.dac"
  subsystem_name = "web"
  part_name = "webview"
  module_install_dir = "etc/param"
}

ohos_prebuilt_etc("alias") {
  source = "${webview_root_path}/ohos_nweb/etc/alias.svg"
  subsystem_name = "web"
  part_name = "webview"
  relative_install_dir = "webview/ohos_nweb"
}

ohos_prebuilt_etc("context-menu") {
  source = "${webview_root_path}/ohos_nweb/etc/context-menu.svg"
  subsystem_name = "web"
  part_name = "webview"
  relative_install_dir = "webview/ohos_nweb"
}

ohos_executable("chrome_crashpad_handler") {
  output_name = "arkweb_crashpad_handler"

  install_enable = true

  include_dirs = [ "${target_gen_dir}/include" ]
  include_dirs += [ "include" ]

  sources = [ "src/nweb_crashpad_handler_main.cpp" ]

  external_deps = [ 
    "hilog:libhilog",
    "bundle_framework:appexecfwk_base", 
  ]

  if (target_cpu == "arm64") {
    branch_protector_ret = "pac_ret"
    defines += [ "webview_arm64" ]
  } else if (target_cpu == "arm") {
    defines += [ "webview_arm" ]
  } else if (target_cpu == "x86_64") {
    defines += [ "webview_x86_64" ]
  }

  subsystem_name = "web"
  part_name = "webview"
}