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

import("//remoting/build/config/remoting_build.gni")

if (icu_use_data_file) {
  bundle_data("icu_data") {
    sources = [ "$root_out_dir/icudtl.dat" ]

    outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]

    public_deps = [ "//third_party/icu:icudata" ]
  }
}

if (is_chrome_branded && is_official_build) {
  bundle_data("me2me_provisionprofile") {
    sources = [ "//remoting/internal/mac/Google_Chrome_Remote_Desktop_Remote_Access.provisionprofile" ]
    outputs = [ "{{bundle_contents_dir}}/embedded.provisionprofile" ]
  }
}

source_set("constants") {
  defines = [
    "HOST_BUNDLE_NAME=\"" + me2me_host_bundle_name + "\"",
    "HOST_LEGACY_BUNDLE_NAME=\"" + me2me_host_legacy_bundle_name + "\"",
    "HOST_BUNDLE_ID=\"" + host_bundle_id + "\"",
  ]

  sources = [
    "constants_mac.cc",
    "constants_mac.h",
  ]

  deps = [ "//remoting/host:remoting_version" ]
}

source_set("permission_checking") {
  sources = [
    "permission_checker.cc",
    "permission_checker.h",
    "permission_process_utils.cc",
    "permission_process_utils.h",
    "permission_utils.h",
    "permission_utils.mm",
    "permission_wizard.h",
    "permission_wizard.mm",
  ]

  public_deps = [ "//base" ]

  deps = [
    ":constants",
    "//remoting/host:remoting_version",
    "//remoting/resources",
    "//ui/base",
  ]

  frameworks = [ "AVFoundation.framework" ]
}

source_set("trust_util") {
  sources = [
    "trust_util.cc",
    "trust_util.h",
  ]
  deps = [
    "//base",
    "//remoting/host:remoting_version",
  ]
  frameworks = [
    "Security.framework",
    "Carbon.framework",
  ]
}

source_set("agent_process_broker_constants") {
  sources = [ "agent_process_broker_constants.h" ]
}

source_set("agent_process_broker_client") {
  sources = [
    "agent_process_broker_client.cc",
    "agent_process_broker_client.h",
  ]
  deps = [
    ":agent_process_broker_constants",
    "//base",
    "//components/named_mojo_ipc_server",
    "//mojo/public/cpp/platform",
    "//remoting/host:ipc_constants",
    "//remoting/host/mojom",
  ]
}

source_set("host_service_main_headers") {
  sources = [ "host_service_main.h" ]
  deps = [ "//remoting/host:host_main_headers" ]
}

source_set("host_service_main") {
  sources = [ "host_service_main.cc" ]
  configs += [
    "//remoting/build/config:host_implementation",
    "//remoting/build/config:version",
  ]
  deps = [
    ":constants",
    ":host_service_main_headers",
    "//base",
    "//remoting/base",
    "//remoting/host:remoting_version",
    "//remoting/host/base",
  ]
}

executable("remoting_me2me_host_service") {
  sources = [ "host_service_entry_point.cc" ]
  deps = [
    ":host_service_main_headers",
    "//remoting/host:remoting_core",
  ]
}

bundle_data("remoting_me2me_host_service_bundle_data") {
  sources = [ "$root_out_dir/remoting_me2me_host_service" ]
  outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
  public_deps = [ ":remoting_me2me_host_service" ]
}

source_set("agent_process_broker") {
  sources = [
    "agent_process_broker.cc",
    "agent_process_broker.h",
  ]
  deps = [
    ":agent_process_broker_constants",
    "//base",
    "//components/named_mojo_ipc_server",
    "//mojo/public/cpp/platform",
    "//remoting/base",
    "//remoting/host:chromoting_host_services_server",
    "//remoting/host:ipc_constants",
    "//remoting/host:mojo_caller_security_checker",
    "//remoting/host/base",
    "//remoting/host/mojom",
  ]
}

source_set("agent_process_broker_main_headers") {
  sources = [ "agent_process_broker_main.h" ]
  deps = [ "//remoting/host:host_main_headers" ]
}

source_set("agent_process_broker_main") {
  sources = [ "agent_process_broker_main.cc" ]
  configs += [
    "//remoting/build/config:host_implementation",
    "//remoting/build/config:version",
  ]
  deps = [
    ":agent_process_broker",
    ":agent_process_broker_main_headers",
    "//base",
    "//mojo/core/embedder",
    "//remoting/base",
    "//remoting/base:logging",
    "//third_party/webrtc_overrides:webrtc_component",
  ]
}

executable("remoting_agent_process_broker") {
  sources = [ "agent_process_broker_entry_point.cc" ]
  deps = [
    ":agent_process_broker_main_headers",
    "//remoting/host:remoting_core",
  ]
}

bundle_data("remoting_agent_process_broker_bundle_data") {
  sources = [ "$root_out_dir/remoting_agent_process_broker" ]
  outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
  public_deps = [ ":remoting_agent_process_broker" ]
}

bundle_data("remoting_core_bundle_data") {
  sources = [ "$root_out_dir/libremoting_core.dylib" ]
  outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
  public_deps = [ "//remoting/host:remoting_core" ]
}

bundle_data("remote_webauthn_bundle_data") {
  sources = [ "$root_out_dir/remote_webauthn" ]
  outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
  public_deps = [ "//remoting/host/webauthn:remote_webauthn" ]
}

# remoting_me2me_host-InfoPlist.strings
foreach(locale, remoting_locales_with_underscores) {
  bundle_data("remoting_me2me_host_strings_${locale}_bundle_data") {
    sources = [ "$root_gen_dir/remoting/host/remoting_me2me_host-InfoPlist.strings/$locale.lproj/InfoPlist.strings" ]
    outputs = [ "{{bundle_resources_dir}}/$locale.lproj/{{source_file_part}}" ]
    deps = [ "//remoting/host:remoting_infoplist_strings" ]
  }
}

bundle_data("remoting_host_resources") {
  sources = [
    "$root_gen_dir/remoting/CREDITS.txt",
    "remoting_host.icns",
  ]

  outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]

  public_deps = [ "//remoting/host/installer/mac:credits" ]
}

target("mac_app_bundle", "remoting_me2me_host") {
  configs += [ "//remoting/build/config:version" ]
  info_plist = "remoting_me2me_host-Info.plist"
  extra_substitutions = [
    "BUNDLE_ID=$host_bundle_id",
    "VERSION_FULL=$remoting_version_full",
    "VERSION_SHORT=$remoting_version_short",
    "MACOSX_DEPLOYMENT_TARGET=10.15",
  ]

  # TODO(joedow): Re-enable or replace with Crashpad: crbug.com/637884.
  # if (is_chrome_branded && is_official_build) {
  #   defines = [ "REMOTING_ENABLE_CRASH_REPORTING" ]
  # }

  deps = [
    "//remoting/host:entry_point",
    "//remoting/host/installer/mac:credits",
    "//remoting/resources",
  ]

  foreach(locale, remoting_locales_with_underscores) {
    deps += [
      ":remoting_host_locale_${locale}_bundle_data",
      ":remoting_me2me_host_strings_${locale}_bundle_data",
    ]
  }
  deps += [
    ":remote_webauthn_bundle_data",
    ":remoting_agent_process_broker_bundle_data",
    ":remoting_core_bundle_data",
    ":remoting_host_resources",
    ":remoting_me2me_host_service_bundle_data",
    "//remoting/host:remoting_infoplist_strings",
    "//remoting/resources:copy_locales",
  ]

  if (icu_use_data_file) {
    deps += [ ":icu_data" ]
  }

  if (is_chrome_branded && is_official_build) {
    deps += [ ":me2me_provisionprofile" ]
  }
}

foreach(locale, remoting_locales_with_underscores) {
  bundle_data("remoting_host_locale_${locale}_bundle_data") {
    sources = [ "$root_build_dir/remoting/resources/$locale.lproj/locale.pak" ]
    outputs = [ "{{bundle_resources_dir}}/$locale.lproj/{{source_file_part}}" ]
    deps = [ "//remoting/resources:copy_locales" ]
  }
}

# native_messaging_host-InfoPlist.strings
foreach(locale, remoting_locales_with_underscores) {
  bundle_data("native_messaging_host_strings_${locale}_bundle_data") {
    sources = [ "$root_gen_dir/remoting/host/native_messaging_host-InfoPlist.strings/$locale.lproj/InfoPlist.strings" ]
    outputs = [ "{{bundle_resources_dir}}/$locale.lproj/{{source_file_part}}" ]
    deps = [ "//remoting/host:remoting_infoplist_strings" ]
  }
}

bundle_data("remoting_native_messaging_host_resources") {
  sources = [
    "remoting_host.icns",

    # This image is used to badge the lock icon in the authentication dialogs,
    # used for elevating privileges to set up the host. The exact filename is
    # required by base::mac::GetAuthorizationRightsWithPrompt().
    # It needs to exist as a file on disk and not just something in a
    # resource bundle because that's the interface that Authorization Services
    # uses. Also, Authorization Services can't deal with .icns files.
    "//remoting/resources/product_logo_32.png",
  ]
  outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]

  if (icu_use_data_file) {
    deps = [ ":icu_data" ]
  }
}

target("mac_app_bundle", "remoting_native_messaging_host") {
  output_name = "native_messaging_host"
  info_plist = "../setup/native_messaging_host-Info.plist"
  extra_substitutions = [
    "BUNDLE_ID=$native_messaging_host_bundle_id",
    "VERSION_FULL=$remoting_version_full",
    "VERSION_SHORT=$remoting_version_short",
    "MACOSX_DEPLOYMENT_TARGET=10.15",
    "HOST_NAME=$host_name",
  ]

  sources = [ "../setup/me2me_native_messaging_host_entry_point.cc" ]

  deps = [
    ":remoting_native_messaging_host_resources",
    "//remoting/host:remoting_core",
    "//remoting/host:remoting_infoplist_strings",
    "//remoting/host/setup:native_messaging_host_main_headers",
  ]
  foreach(locale, remoting_locales_with_underscores) {
    deps += [
      ":native_messaging_host_strings_${locale}_bundle_data",
      "//remoting/host/mac:remoting_host_locale_${locale}_bundle_data",
    ]
  }
  deps += [ "//remoting/resources:copy_locales" ]
  if (!is_official_build) {
    deps += [ ":remoting_core_bundle_data" ]
  }
}

source_set("unit_tests") {
  testonly = true

  sources = [ "agent_process_broker_unittest.cc" ]

  deps = [
    ":agent_process_broker",
    ":agent_process_broker_client",
    "//base",
    "//base/test:test_support",
    "//components/named_mojo_ipc_server",
    "//mojo/public/cpp/platform",
    "//remoting/host:chromoting_host_services_client",
    "//remoting/host:chromoting_host_services_server",
    "//remoting/host/mojom",
    "//testing/gmock",
    "//testing/gtest",
  ]
}
