# 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("//remoting/build/config/remoting_build.gni")
import("//remoting/host/installer/win/generate_clsids.gni")
import("//build/toolchain/win/midl.gni")
import("//build/win/message_compiler.gni")

group("all") {
  testonly = true

  deps = [
    ":remoting_console",
    ":remoting_desktop",
    ":remoting_me2me_host",
    ":remoting_native_messaging_host",
    "//remoting/host/security_key:remote_security_key",
  ]
}

# Reference this manifest to indicate that a process is per-monitor DPI aware.
dpi_aware_manifest = "//remoting/host/win/dpi_aware.manifest"

# Reference this manifest to give the binary the uiAccess privilege.
enable_uiaccess_manifest = "//remoting/host/win/enable_uiaccess.manifest"
enable_uiaccess_require_admin_manifest =
    "//remoting/host/win/enable_uiaccess_require_admin.manifest"

# Depending on this target gives a default executable manifest with the addition
# of the DPI aware tag.
windows_manifest("dpi_aware_exe_manifest") {
  sources = [
    as_invoker_manifest,
    common_controls_manifest,
    default_compatibility_manifest,
    dpi_aware_manifest,
  ]
  type = "exe"
}

# Depending on this target gives a default executable manifest with the addition
# of the DPI aware tag and a requestedExecutionLevel of requireAdministrator.
windows_manifest("dpi_aware_elevated_exe_manifest") {
  sources = [
    common_controls_manifest,
    default_compatibility_manifest,
    dpi_aware_manifest,
    require_administrator_manifest,
  ]
  type = "exe"
}

# Depending on this target gives the executable a default manifest with the
# addition of the DPI aware tag and enables uiAccess.
windows_manifest("dpi_aware_uiaccess_exe_manifest") {
  sources = [
    common_controls_manifest,
    default_compatibility_manifest,
    dpi_aware_manifest,
    enable_uiaccess_manifest,
  ]
  type = "exe"
}

# Depending on this target gives the executable a default manifest with the
# addition of the DPI aware tag and enables uiAccess.
windows_manifest("dpi_aware_uiaccess_require_admin_exe_manifest") {
  sources = [
    common_controls_manifest,
    default_compatibility_manifest,
    dpi_aware_manifest,
    enable_uiaccess_require_admin_manifest,
  ]
  type = "exe"
}

source_set("win") {
  sources = [
    "com_imported_mstscax.tlh",
    "com_security.cc",
    "com_security.h",
    "elevation_helpers.cc",
    "elevation_helpers.h",
    "launch_process_with_token.cc",
    "launch_process_with_token.h",
    "omaha.cc",
    "omaha.h",
    "rdp_client.cc",
    "rdp_client.h",
    "rdp_client_window.cc",
    "rdp_client_window.h",
    "security_descriptor.cc",
    "security_descriptor.h",
    "session_desktop_environment.cc",
    "session_desktop_environment.h",
    "session_input_injector.cc",
    "session_input_injector.h",
    "window_station_and_desktop.cc",
    "window_station_and_desktop.h",
    "worker_process_launcher.cc",
    "worker_process_launcher.h",
    "wts_terminal_monitor.cc",
    "wts_terminal_monitor.h",
    "wts_terminal_observer.h",
  ]

  configs += [
    "//build/config/compiler:wexit_time_destructors",
    "//remoting/build/config:version",
  ]

  defines = [ "WEBRTC_CHROMIUM_BUILD" ]

  deps = [
    "//base:i18n",
    "//components/policy/core/common",
    "//content/public/common",
    "//crypto",
    "//device/power_save_blocker",
    "//google_apis",
    "//ipc",
    "//remoting/base",
    "//remoting/host/security_key",
    "//remoting/host/win:messages",
    "//remoting/host/win:remoting_lib_idl",
    "//remoting/protocol",
    "//remoting/resources",
    "//ui/base",
    "//ui/events:dom_keycode_converter",
    "//ui/events/platform",
  ]

  if (enable_configuration_policy) {
    deps += [ "//components/policy:generated" ]
  }

  if (enable_webrtc) {
    deps += [ "//third_party/webrtc/modules/desktop_capture" ]
  }
}

source_set("unit_tests") {
  testonly = true

  sources = [
    "elevated_native_messaging_host.cc",
    "elevated_native_messaging_host.h",
    "launch_native_messaging_host_process.cc",
    "launch_native_messaging_host_process.h",
    "rdp_client_unittest.cc",
    "worker_process_launcher_unittest.cc",
  ]

  deps = [
    "//remoting/host",
    "//remoting/host:test_support",
    "//remoting/host/it2me:common",
    "//remoting/host/native_messaging",
    "//remoting/host/security_key:unit_tests",
    "//remoting/host/setup",
    "//remoting/proto",
    "//remoting/resources",
    "//skia",
    "//testing/gmock",
    "//testing/gtest",
  ]
}

action("generate_idl") {
  script = "//build/util/version.py"

  inputs = [
    "chromoting_lib_idl.templ",
  ]
  outputs = [
    "$target_gen_dir/chromoting_lib.idl",
  ]

  args = [
    "-e",
    "DAEMON_CONTROLLER_CLSID='$daemon_controller_clsid'",
    "-e",
    "RDP_DESKTOP_SESSION_CLSID='$rdp_desktop_session_clsid'",
    rebase_path(inputs[0], root_build_dir),
    rebase_path(outputs[0], root_build_dir),
  ]
}

midl("remoting_lib_idl") {
  sources = get_target_outputs(":generate_idl")
  deps = [
    ":generate_idl",
  ]
}

config("MIDL_config") {
  if (is_clang) {
    cflags = [
      # MIDL generated code has a habit of omitting optional braces.
      "-Wno-missing-braces",

      # Source files generated by the MIDL compiler trigger warnings with
      # -Wincompatible-pointer-types enabled.
      "-Wno-incompatible-pointer-types",

      # Generated code contains unused variables.
      "-Wno-unused-variable",

      # PROXYFILE_LIST_START is an extern with initializer.
      "-Wno-extern-initializer",
    ]
  }
}

static_library("remoting_lib_ps") {
  configs += [ ":MIDL_config" ]

  defines = [
    "ENTRY_PREFIX=Ps",
    "REGISTER_PROXY_DLL",
  ]

  deps = [
    ":remoting_lib_idl",
  ]

  sources = [
    "$root_gen_dir/remoting/host/win/chromoting_lib.dlldata.c",
    "$root_gen_dir/remoting/host/win/chromoting_lib_p.c",
  ]
}

# Makes the .mc file from the .mc.jinja file.
remoting_localize("messages_localizing") {
  sources = [
    "host_messages.mc.jinja2",
  ]
  locales = remoting_locales
  locale_dir = webapp_locale_dir
  encoding = "utf-16"

  # This target is funny. It only produces one file and the output doesn't
  # match the input. We want to generate remoting_host_messages.mc from
  # host_messages.mg.jinja2. GN complains if it doesn't see a pattern in the
  # output, so the following pattern produces the name we want with a template
  # based on the input.
  #
  # TODO: This is for GYP compat. We should just make the names match instead.
  output = "$target_gen_dir/remoting_{{source_name_part}}"
}

# Makes the .h/.rc files from the .mc file.
message_compiler("messages") {
  compile_generated_code = false
  sources = get_target_outputs(":messages_localizing")
  deps = [
    ":messages_localizing",
  ]
}

executable("remoting_console") {
  configs += [ "//build/config/compiler:wexit_time_destructors" ]

  defines = host_predefines + [ "BINARY=BINARY_HOST_ME2ME" ]

  deps = [
    ":dpi_aware_exe_manifest",
    ":remoting_core",
    ":remoting_windows_resources",
  ]

  sources = [
    "$root_gen_dir/remoting/version.rc",
    "entry_point.cc",
  ]

  ldflags = [
    "/ENTRY:HostEntryPoint",
    # "/NODEFAULTLIB",
  ]
}

executable("remoting_me2me_host") {
  configs += [
    "//build/config/compiler:wexit_time_destructors",
    "//build/config/win:windowed",
    "//remoting/build/config:remoting_me2me_host",
  ]

  defines = host_predefines + [ "BINARY=BINARY_HOST_ME2ME" ]

  deps = [
    ":dpi_aware_exe_manifest",
    ":remoting_core",
    ":remoting_windows_resources",
  ]

  sources = [
    "$root_gen_dir/remoting/version.rc",
    "entry_point.cc",
  ]

  output_name = "remoting_host"

  ldflags = [ "/ENTRY:HostEntryPoint" ]
}

shared_library("remoting_core") {
  configs += [ "//build/config/compiler:wexit_time_destructors" ]

  defines = host_predefines + [
              "_ATL_APARTMENT_THREADED",
              "_ATL_CSTRING_EXPLICIT_CONSTRUCTORS",
              "_ATL_NO_AUTOMATIC_NAMESPACE",
              "_ATL_NO_EXCEPTIONS",
              "BINARY=BINARY_CORE",
              "DAEMON_CONTROLLER_CLSID=\"$daemon_controller_clsid\"",
              "RDP_DESKTOP_SESSION_CLSID=\"$rdp_desktop_session_clsid\"",
              "HOST_IMPLEMENTATION",
              "ISOLATION_AWARE_ENABLED=1",
              "STRICT",
              "VERSION=$chrome_version_full",
            ]

  if (remoting_multi_process != 0 && remoting_rdp_session != 0) {
    defines += [ "REMOTING_RDP_SESSION" ]
  }

  if (remoting_multi_process != 0) {
    defines += [ "REMOTING_MULTI_PROCESS" ]
  }

  sources = [
    "$root_gen_dir/remoting/core.rc",
    "$root_gen_dir/remoting/host/win/remoting_host_messages.rc",
    "$root_gen_dir/remoting/version.rc",
    "//remoting/host/desktop_process_main.cc",
    "//remoting/host/host_main.cc",
    "//remoting/host/host_main.h",
    "//remoting/host/it2me/it2me_native_messaging_host_main.cc",
    "//remoting/host/it2me/it2me_native_messaging_host_main.h",
    "//remoting/host/setup/host_starter.cc",
    "//remoting/host/setup/host_starter.h",
    "//remoting/host/setup/me2me_native_messaging_host_main.cc",
    "//remoting/host/setup/me2me_native_messaging_host_main.h",
    "//remoting/host/setup/start_host_main.cc",
    "//remoting/host/setup/start_host_main.h",
    "//remoting/host/worker_process_ipc_delegate.h",
    "chromoting_lib.rc",
    "chromoting_module.cc",
    "chromoting_module.h",
    "core.cc",
    "core_resource.h",
    "elevated_native_messaging_host.cc",
    "elevated_native_messaging_host.h",
    "host_service.cc",
    "host_service.h",
    "launch_native_messaging_host_process.cc",
    "launch_native_messaging_host_process.h",
    "rdp_desktop_session.cc",
    "rdp_desktop_session.h",
    "unprivileged_process_delegate.cc",
    "unprivileged_process_delegate.h",
    "wts_session_process_delegate.cc",
    "wts_session_process_delegate.h",
  ]

  deps = [
    ":messages",
    ":remoting_lib_idl",
    ":remoting_lib_ps",
    ":remoting_windows_resources",
    ":win",
    "//base",
    "//base:base_static",
    "//base/allocator",
    "//base/third_party/dynamic_annotations",
    "//build/win:default_exe_manifest",
    "//ipc",
    "//mojo/edk/system",
    "//net",
    "//remoting/base",
    "//remoting/base:breakpad",
    "//remoting/codec",
    "//remoting/host:host",
    "//remoting/host:remoting_me2me_host_static",
    "//remoting/host/it2me:common",
    "//remoting/host/native_messaging",
    "//remoting/host/security_key:main",
    "//remoting/host/setup",
    "//remoting/protocol",
    "//sandbox/win:sandbox",  # Should always use Windows version
    "//third_party/webrtc/modules/desktop_capture",
  ]

  ldflags = [
    "/EXPORT:DllGetClassObject=PsDllGetClassObject,PRIVATE",
    "/EXPORT:DllCanUnloadNow=PsDllCanUnloadNow,PRIVATE",
    "/EXPORT:DllRegisterServer=PsDllRegisterServer,PRIVATE",
    "/EXPORT:DllUnregisterServer=PsDllUnregisterServer,PRIVATE",
  ]

  libs = [
    "comctl32.lib",
    "rpcns4.lib",
    "rpcrt4.lib",
    "sas.lib",
    "uuid.lib",
    "wtsapi32.lib",
  ]

  if (is_clang) {
    cflags = [ "-Wno-header-hygiene" ]
  }
}

executable("remoting_desktop") {
  configs += [
    "//build/config/compiler:wexit_time_destructors",
    "//build/config/win:windowed",
  ]

  defines = host_predefines + [ "BINARY=BINARY_DESKTOP" ]

  deps = [
    ":remoting_core",
    ":remoting_windows_resources",
  ]

  if (is_official_build) {
    deps += [ ":dpi_aware_uiaccess_require_admin_exe_manifest" ]
  } else {
    deps += [ ":dpi_aware_exe_manifest" ]
  }

  sources = [
    "$root_gen_dir/remoting/version.rc",
    "entry_point.cc",
  ]

  ldflags = [
    "/ENTRY:HostEntryPoint",
    # "/NODEFAULTLIB",
  ]
}

executable("remoting_native_messaging_host") {
  configs += [ "//build/config/compiler:wexit_time_destructors" ]

  defines = host_predefines + [ "BINARY=BINARY_NATIVE_MESSAGING_HOST" ]

  deps = [
    ":remoting_core",
    ":remoting_windows_resources",
    "//build/win:default_exe_manifest",
  ]

  sources = [
    "$root_gen_dir/remoting/version.rc",
    "../setup/me2me_native_messaging_host_entry_point.cc",
  ]
}

remoting_localize("remoting_windows_resources") {
  deps = [
    "//remoting/resources",
  ]

  sources = [
    "core.rc.jinja2",
    "version.rc.jinja2",
  ]

  variables = [
    rebase_path(chrome_version_file),
    rebase_path(remoting_version_file),
    rebase_path("//build/util/LASTCHANGE"),
  ]

  output = "$root_gen_dir/remoting/{{source_name_part}}"

  locale_dir = webapp_locale_dir

  encoding = "utf-16"

  locales = remoting_locales
}
# TODO(GYP) More Windows remoting targets from remoting_host_win.gypi
