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

import("//build/buildflag_header.gni")
import("//build/config/ios/ios_sdk.gni")
import("//build/config/ios/rules.gni")
import("//build/config/locales.gni")
import("//components/cronet/native/include/headers.gni")
import("//components/grpc_support/include/headers.gni")
import("//ios/build/config.gni")
import("//ios/web/js_compile.gni")
import("//ios/web_view/features.gni")
import("//ios/web_view/repack.gni")
import("//testing/test.gni")
import("//tools/grit/repack.gni")

buildflag_header("web_view_features") {
  header = "cwv_web_view_features.h"

  flags = [
    "IOS_WEB_VIEW_ENABLE_SYNC=$ios_web_view_enable_sync",
    "IOS_WEB_VIEW_ENABLE_AUTOFILL=$ios_web_view_enable_autofill",
  ]
}

group("all_tests") {
  testonly = true
  deps = [
    ":ios_web_view_inttests",
    ":ios_web_view_unittests",
  ]
}

config("config") {
  defines = [ "CWV_IMPLEMENTATION" ]
  libs = [
    "CoreGraphics.framework",
    "Foundation.framework",
    "MobileCoreServices.framework",
    "UIKit.framework",
  ]
}

# These are defined as vars so they can be shared with different targets below.
ios_web_view_public_headers = [
  "public/cwv_export.h",
  "public/cwv_favicon.h",
  "public/cwv_html_element.h",
  "public/cwv_navigation_action.h",
  "public/cwv_navigation_delegate.h",
  "public/cwv_navigation_type.h",
  "public/cwv_preferences.h",
  "public/cwv_preview_element_info.h",
  "public/cwv_script_command.h",
  "public/cwv_scroll_view.h",
  "public/cwv_scroll_view_delegate.h",
  "public/cwv_translation_controller.h",
  "public/cwv_translation_controller_delegate.h",
  "public/cwv_translation_language.h",
  "public/cwv_translation_policy.h",
  "public/cwv_ui_delegate.h",
  "public/cwv_user_content_controller.h",
  "public/cwv_user_script.h",
  "public/cwv_web_view.h",
  "public/cwv_web_view_configuration.h",
]
if (ios_web_view_enable_sync) {
  ios_web_view_public_headers += [ "public/cwv_identity.h" ]
}
if (ios_web_view_enable_autofill) {
  ios_web_view_public_headers += [
    "public/cwv_autofill_controller.h",
    "public/cwv_autofill_controller_delegate.h",
    "public/cwv_autofill_data_manager.h",
    "public/cwv_autofill_data_manager_delegate.h",
    "public/cwv_autofill_profile.h",
    "public/cwv_autofill_suggestion.h",
    "public/cwv_credit_card.h",
    "public/cwv_credit_card_verifier.h",
    "public/cwv_preferences_autofill.h",
    "public/cwv_web_view_autofill.h",
    "public/cwv_web_view_configuration_autofill.h",
  ]
}

ios_web_view_sources = [
  "internal/app/application_context.cc",
  "internal/app/application_context.h",
  "internal/app/web_view_io_thread.h",
  "internal/app/web_view_io_thread.mm",
  "internal/autofill/web_view_autofill_client_ios.h",
  "internal/autofill/web_view_autofill_client_ios.mm",
  "internal/autofill/web_view_personal_data_manager_factory.cc",
  "internal/autofill/web_view_personal_data_manager_factory.h",
  "internal/content_settings/web_view_cookie_settings_factory.cc",
  "internal/content_settings/web_view_cookie_settings_factory.h",
  "internal/content_settings/web_view_host_content_settings_map_factory.cc",
  "internal/content_settings/web_view_host_content_settings_map_factory.h",
  "internal/cwv_favicon.mm",
  "internal/cwv_favicon_internal.h",
  "internal/cwv_html_element.mm",
  "internal/cwv_html_element_internal.h",
  "internal/cwv_navigation_action.mm",
  "internal/cwv_navigation_action_internal.h",
  "internal/cwv_navigation_type.mm",
  "internal/cwv_navigation_type_internal.h",
  "internal/cwv_preferences.mm",
  "internal/cwv_preferences_internal.h",
  "internal/cwv_preview_element_info.mm",
  "internal/cwv_preview_element_info_internal.h",
  "internal/cwv_script_command.mm",
  "internal/cwv_script_command_internal.h",
  "internal/cwv_scroll_view.mm",
  "internal/cwv_scroll_view_internal.h",
  "internal/cwv_user_content_controller.mm",
  "internal/cwv_user_content_controller_internal.h",
  "internal/cwv_user_script.mm",
  "internal/cwv_web_view.mm",
  "internal/cwv_web_view_configuration.mm",
  "internal/cwv_web_view_configuration_internal.h",
  "internal/cwv_web_view_internal.h",
  "internal/ios_global_state_web_view_configuration.cc",
  "internal/language/web_view_language_model_manager_factory.cc",
  "internal/language/web_view_language_model_manager_factory.h",
  "internal/language/web_view_url_language_histogram_factory.cc",
  "internal/language/web_view_url_language_histogram_factory.h",
  "internal/pref_names.cc",
  "internal/pref_names.h",
  "internal/signin/ios_web_view_signin_client.h",
  "internal/signin/ios_web_view_signin_client.mm",
  "internal/signin/web_view_account_fetcher_service_factory.h",
  "internal/signin/web_view_account_fetcher_service_factory.mm",
  "internal/signin/web_view_account_tracker_service_factory.h",
  "internal/signin/web_view_account_tracker_service_factory.mm",
  "internal/signin/web_view_gaia_cookie_manager_service_factory.h",
  "internal/signin/web_view_gaia_cookie_manager_service_factory.mm",
  "internal/signin/web_view_identity_manager_factory.h",
  "internal/signin/web_view_identity_manager_factory.mm",
  "internal/signin/web_view_oauth2_token_service_factory.h",
  "internal/signin/web_view_oauth2_token_service_factory.mm",
  "internal/signin/web_view_profile_oauth2_token_service_ios_provider_impl.h",
  "internal/signin/web_view_profile_oauth2_token_service_ios_provider_impl.mm",
  "internal/signin/web_view_signin_client_factory.h",
  "internal/signin/web_view_signin_client_factory.mm",
  "internal/signin/web_view_signin_error_controller_factory.h",
  "internal/signin/web_view_signin_error_controller_factory.mm",
  "internal/signin/web_view_signin_manager_factory.h",
  "internal/signin/web_view_signin_manager_factory.mm",
  "internal/sync/web_view_gcm_profile_service_factory.h",
  "internal/sync/web_view_gcm_profile_service_factory.mm",
  "internal/sync/web_view_profile_invalidation_provider_factory.h",
  "internal/sync/web_view_profile_invalidation_provider_factory.mm",
  "internal/translate/cwv_translation_controller.mm",
  "internal/translate/cwv_translation_controller_internal.h",
  "internal/translate/cwv_translation_language_internal.h",
  "internal/translate/cwv_translation_language.mm",
  "internal/translate/cwv_translation_policy.mm",
  "internal/translate/web_view_translate_accept_languages_factory.cc",
  "internal/translate/web_view_translate_accept_languages_factory.h",
  "internal/translate/web_view_translate_client.h",
  "internal/translate/web_view_translate_client.mm",
  "internal/translate/web_view_translate_ranker_factory.cc",
  "internal/translate/web_view_translate_ranker_factory.h",
  "internal/translate/web_view_translate_service.cc",
  "internal/translate/web_view_translate_service.h",
  "internal/web_view_browser_state.h",
  "internal/web_view_browser_state.mm",
  "internal/web_view_early_page_script_provider.h",
  "internal/web_view_early_page_script_provider.mm",
  "internal/web_view_global_state_util.h",
  "internal/web_view_global_state_util.mm",
  "internal/web_view_java_script_dialog_presenter.h",
  "internal/web_view_java_script_dialog_presenter.mm",
  "internal/web_view_network_delegate.cc",
  "internal/web_view_network_delegate.h",
  "internal/web_view_url_request_context_getter.h",
  "internal/web_view_url_request_context_getter.mm",
  "internal/web_view_web_client.h",
  "internal/web_view_web_client.mm",
  "internal/web_view_web_main_delegate.h",
  "internal/web_view_web_main_delegate.mm",
  "internal/web_view_web_main_parts.h",
  "internal/web_view_web_main_parts.mm",
  "internal/web_view_web_state_policy_decider.h",
  "internal/web_view_web_state_policy_decider.mm",
  "internal/webdata_services/web_view_web_data_service_wrapper_factory.cc",
  "internal/webdata_services/web_view_web_data_service_wrapper_factory.h",
]
ios_web_view_sources += ios_web_view_public_headers
if (ios_web_view_enable_sync) {
  ios_web_view_sources += [ "internal/signin/cwv_identity.mm" ]
}
if (ios_web_view_enable_autofill) {
  ios_web_view_sources += [
    "internal/autofill/cwv_autofill_client_ios_bridge.h",
    "internal/autofill/cwv_autofill_controller.mm",
    "internal/autofill/cwv_autofill_controller_internal.h",
    "internal/autofill/cwv_autofill_data_manager.mm",
    "internal/autofill/cwv_autofill_data_manager_internal.h",
    "internal/autofill/cwv_autofill_profile.mm",
    "internal/autofill/cwv_autofill_profile_internal.h",
    "internal/autofill/cwv_autofill_suggestion.mm",
    "internal/autofill/cwv_autofill_suggestion_internal.h",
    "internal/autofill/cwv_credit_card.mm",
    "internal/autofill/cwv_credit_card_internal.h",
    "internal/autofill/cwv_credit_card_verifier.mm",
    "internal/autofill/cwv_credit_card_verifier_internal.h",
  ]
}

ios_web_view_deps = [
  ":web_view_bundle",
  ":web_view_features",
  ":web_view_resources",
  "//base",
  "//components/autofill/core/browser",
  "//components/autofill/core/common",
  "//components/autofill/ios/browser",
  "//components/autofill/ios/form_util",
  "//components/content_settings/core/browser",
  "//components/flags_ui",
  "//components/gcm_driver",
  "//components/image_fetcher/ios",
  "//components/invalidation/impl",
  "//components/infobars/core",
  "//components/keyed_service/core",
  "//components/keyed_service/ios",
  "//components/language/core/browser",
  "//components/language/core/common",
  "//components/net_log",
  "//components/pref_registry",
  "//components/prefs",
  "//components/proxy_config",
  "//components/signin/core/browser",
  "//components/signin/ios/browser",
  "//components/signin/ios/browser:active_state_manager",
  "//components/strings:components_strings_grit",
  "//components/sync",
  "//components/translate/core/browser",
  "//components/translate/core/common",
  "//components/translate/ios/browser",
  "//components/web_resource",
  "//components/webdata_services",
  "//google_apis",
  "//ios/components/io_thread",
  "//ios/net",
  "//ios/web",
  "//ios/web/public",
  "//ios/web/public/app",
  "//ios/web/public/global_state",
  "//net",
  "//net:extras",
  "//services/identity/public/cpp",
  "//ui/base",
  "//url",
]

# Generate the umbrella header to exclude headers for disabled features.
# This is preferable to guarding header imports using flags defined in
# :web_view_features because:
# 1. Confusing to see headers for unsupported features in umbrella header.
# 2. A need to include cwv_web_view_features.h as part of public headers and
#    any headers it itself includes, despite not being part of the public API.
action("web_view_umbrella_header") {
  script = "//build/config/ios/generate_umbrella_header.py"

  full_header_path = target_gen_dir + "/ChromeWebView.h"
  outputs = [
    full_header_path,
  ]

  args = [
    "--output-path",
    rebase_path(full_header_path, root_build_dir),
  ]

  args += rebase_path(ios_web_view_public_headers, root_build_dir)
}

ios_framework_bundle("web_view") {
  output_name = ios_web_view_output_name
  info_plist = "Info.plist"

  public_headers = ios_web_view_public_headers
  public_headers += get_target_outputs(":web_view_umbrella_header")
  if (ios_web_view_include_cronet) {
    public_headers += [ "//components/cronet/ios/Cronet.h" ]
    public_headers += grpc_public_headers
    public_headers += cronet_native_public_headers
  }

  sources = ios_web_view_sources
  if (ios_web_view_include_cronet) {
    sources += [ "//components/cronet/ios/Cronet.h" ]
  }

  deps = ios_web_view_deps
  deps += [ ":web_view_umbrella_header" ]
  if (ios_web_view_include_cronet) {
    deps += [ "//components/cronet/ios:cronet_sources" ]
  }

  configs += [
    ":config",
    "//build/config/compiler:enable_arc",
  ]
  if (ios_web_view_include_cronet) {
    configs -= [ "//build/config/compiler:default_symbols" ]
    configs += [ "//build/config/compiler:symbols" ]
  }
}

ios_web_view_test_sources = [
  "internal/translate/fake_web_view_translate_client.h",
  "internal/translate/fake_web_view_translate_client.mm",
]

source_set("run_all_unittests") {
  testonly = true
  sources = [
    "test/run_all_unittests.cc",
  ]
  deps = [
    "//base",
    "//base/test:test_support",
    "//mojo/core/embedder",
  ]
}

test("ios_web_view_unittests") {
  testonly = true
  sources = [
    "internal/autofill/cwv_autofill_controller_unittest.mm",
    "internal/autofill/cwv_autofill_data_manager_unittest.mm",
    "internal/autofill/cwv_autofill_profile_unittest.mm",
    "internal/autofill/cwv_autofill_suggestion_unittest.mm",
    "internal/autofill/cwv_credit_card_unittest.mm",
    "internal/autofill/cwv_credit_card_verifier_unittest.mm",
    "internal/cwv_favicon_unittest.mm",
    "internal/cwv_html_element_unittest.mm",
    "internal/cwv_preferences_unittest.mm",
    "internal/cwv_preview_element_info_unittest.mm",
    "internal/signin/cwv_identity_unittest.mm",
    "internal/translate/cwv_translation_controller_unittest.mm",
    "internal/translate/cwv_translation_language_unittest.mm",
    "internal/translate/cwv_translation_policy_unittest.mm",
    "internal/web_view_web_client_unittest.mm",
  ]
  sources += ios_web_view_sources
  sources += ios_web_view_test_sources

  deps = [
    ":run_all_unittests",
    "test:test_support",
    "//base/test:test_support",
    "//components/autofill/core/browser:test_support",
    "//components/autofill/ios/browser:test_support",
    "//components/autofill/ios/form_util:test_support",
    "//components/prefs:test_support",
    "//ios/web/public/test",
    "//ios/web/public/test/fakes",
    "//testing/gtest",
    "//third_party/ocmock",
  ]
  deps += ios_web_view_deps

  configs += [ "//build/config/compiler:enable_arc" ]

  assert_no_deps = ios_assert_no_deps
}

test("ios_web_view_inttests") {
  testonly = true

  deps = [
    "//ios/web_view/test:inttests",
  ]

  bundle_deps = [ ":web_view+bundle" ]

  configs += [ "//build/config/compiler:enable_arc" ]

  assert_no_deps = ios_assert_no_deps
}

js_compile_bundle("web_view_bundle") {
  closure_entry_point = "__crWeb.webViewBundle"
  sources = [
    "//components/autofill/ios/browser/resources/autofill_controller.js",
    "//components/autofill/ios/form_util/resources/fill.js",
    "//components/autofill/ios/form_util/resources/form.js",
    "//ios/chrome/browser/passwords/resources/password_controller.js",
    "resources/web_view_bundle.js",
  ]
}

repack_locales("repack_locales") {
  visibility = [ ":web_view_resources" ]
  source_patterns = [ "${root_gen_dir}/components/strings/components_strings_" ]

  deps = [
    "//components/strings:components_strings",
  ]
  input_locales = ios_packed_locales
  output_locales = ios_packed_locales_as_mac_outputs
  copy_data_to_bundle = true
}

repack("repack_resources") {
  visibility = [ ":web_view_resources" ]
  deps = [
    "//components/resources:components_resources",
    "//ios/web:resources",
  ]
  sources = [
    "$root_gen_dir/components/components_resources.pak",
    "$root_gen_dir/ios/web/ios_web_resources.pak",
  ]
  output = "$target_gen_dir/web_view_resources.pak"
  copy_data_to_bundle = true
}

ios_web_view_repack_all_scales("repack_scalable_resources") {
  visibility = [ ":web_view_resources" ]
  scales = [
    "100",
    "200",
    "300",
  ]
}

group("web_view_resources") {
  visibility = [ "//ios/web_view:*" ]
  deps = [
    ":repack_locales",
    ":repack_resources",
  ]
  if (ios_web_view_enable_autofill) {
    deps += [ ":repack_scalable_resources" ]
  }
}

if (additional_toolchains == [] || current_toolchain == default_toolchain) {
  _package_dir = "$root_out_dir/ios_web_view"

  action("ios_web_view_generate_license") {
    _license_path = "$_package_dir/LICENSE"

    script = "//tools/licenses.py"
    inputs = [
      "//buildtools/$host_os/gn",
    ]
    outputs = [
      _license_path,
    ]
    args = [
      "license_file",
      rebase_path(_license_path, root_build_dir),
      "--gn-target",
      "//ios/web_view",
      "--gn-out-dir",
      ".",
    ]
  }

  copy("ios_web_view_package_copy") {
    sources = [
      "$root_out_dir/$ios_web_view_output_name.framework",
      "//AUTHORS",
      "//chrome/VERSION",
    ]
    outputs = [
      "$_package_dir/{{source_file_part}}",
    ]

    deps = [
      ":web_view",
    ]
  }

  if (enable_dsyms) {
    action("ios_web_view_dsym_archive") {
      script = "//chrome/tools/build/mac/archive_symbols.py"

      # These are the dSYMs that will be archived. The sources list must be
      # the target outputs that correspond to the dSYMs (since a dSYM is a
      # directory it cannot be listed as a source file). The targets that
      # generate both the dSYM and binary image are listed in deps.
      _dsyms = [ "$root_out_dir/$ios_web_view_output_name.dSYM" ]

      sources = [
        "$root_out_dir/$ios_web_view_output_name.framework",
      ]

      _output = "$_package_dir/$ios_web_view_output_name.dSYM.tar.bz2"

      outputs = [
        _output,
      ]

      args = [ rebase_path(_output, root_out_dir) ] +
             rebase_path(_dsyms, root_out_dir)

      deps = [
        ":web_view",
      ]
    }
  } else {
    group("ios_web_view_dsym_archive") {
    }
  }

  group("ios_web_view_package") {
    deps = [
      ":ios_web_view_dsym_archive",
      ":ios_web_view_generate_license",
      ":ios_web_view_package_copy",
    ]
  }
}
