# Copyright 2014 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/util/process_version.gni")
import("//media/media_options.gni")
import("//remoting/build/config/remoting_build.gni")

group("all_tests") {
  testonly = true

  deps = [
    ":unit_tests",
  ]
}

group("all") {
  testonly = true

  deps = [
    ":host",
  ]
  if (enable_me2me_host) {
    deps += [ "//remoting/host:remoting_me2me_host" ]
  }

  if (is_chrome_branded) {
    deps += [ ":remoting_host_branded" ]
  }

  if (!is_chromeos && !is_android && !is_ios) {
    deps += [
      "//remoting/host:remoting_native_messaging_host",
      "//remoting/host:remoting_native_messaging_manifests",
      "//remoting/host:remoting_start_host",
      "//remoting/host/it2me:remote_assistance_host",
    ]
  }
}

process_version("remoting_version") {
  template_file = "//remoting/host/version.h.in"
  sources = [
    branding_path,
  ]
  output = "$target_gen_dir/version.h"
}

# This must be a static library instead of a source set because
# remoting_unittests requires that remoting_me2me_host.cc not be pulled in,
# which in turn depends on remoting_me2me_host_static which isn't part of that
# build.
#
# TODO fix this, successful builds should not depend on static libraries
# stripping code.
static_library("host") {
  sources = [
    "audio_capturer.cc",
    "audio_capturer.h",
    "audio_capturer_chromeos.cc",
    "audio_capturer_linux.cc",
    "audio_capturer_linux.h",
    "audio_capturer_mac.cc",
    "audio_capturer_win.cc",
    "audio_capturer_win.h",
    "audio_silence_detector.cc",
    "audio_silence_detector.h",
    "audio_volume_filter.cc",
    "audio_volume_filter.h",
    "backoff_timer.cc",
    "backoff_timer.h",
    "basic_desktop_environment.cc",
    "basic_desktop_environment.h",
    "branding.cc",
    "branding.h",
    "chromoting_host.cc",
    "chromoting_host.h",
    "chromoting_host_context.cc",
    "chromoting_host_context.h",
    "chromoting_messages.cc",
    "chromoting_messages.h",
    "chromoting_param_traits.cc",
    "chromoting_param_traits.h",
    "client_session.cc",
    "client_session.h",
    "client_session_control.h",
    "client_session_details.h",
    "clipboard.h",
    "clipboard_mac.mm",
    "clipboard_win.cc",
    "clipboard_x11.cc",
    "config_file_watcher.cc",
    "config_file_watcher.h",
    "config_watcher.h",
    "continue_window.cc",
    "continue_window.h",
    "continue_window_chromeos.cc",
    "continue_window_linux.cc",
    "continue_window_mac.mm",
    "continue_window_win.cc",
    "current_process_stats_agent.cc",
    "current_process_stats_agent.h",
    "curtain_mode.h",
    "curtain_mode_linux.cc",
    "curtain_mode_mac.cc",
    "curtain_mode_win.cc",
    "daemon_process.cc",
    "daemon_process.h",
    "daemon_process_win.cc",
    "desktop_capturer_proxy.cc",
    "desktop_capturer_proxy.h",
    "desktop_environment.h",
    "desktop_environment_options.cc",
    "desktop_environment_options.h",
    "desktop_process.cc",
    "desktop_process.h",
    "desktop_resizer.h",
    "desktop_resizer_mac.cc",
    "desktop_resizer_ozone.cc",
    "desktop_resizer_win.cc",
    "desktop_resizer_x11.cc",
    "desktop_session.cc",
    "desktop_session.h",
    "desktop_session_agent.cc",
    "desktop_session_agent.h",
    "desktop_session_connector.h",
    "desktop_session_proxy.cc",
    "desktop_session_proxy.h",
    "desktop_session_win.cc",
    "desktop_session_win.h",
    "disconnect_window_chromeos.cc",
    "disconnect_window_linux.cc",
    "disconnect_window_mac.h",
    "disconnect_window_mac.mm",
    "disconnect_window_win.cc",
    "dns_blackhole_checker.cc",
    "dns_blackhole_checker.h",
    "evaluate_capability.cc",
    "evaluate_capability.h",
    "file_proxy_wrapper.cc",
    "file_proxy_wrapper.h",
    "file_proxy_wrapper_linux.cc",
    "file_proxy_wrapper_mac.cc",
    "file_proxy_wrapper_win.cc",
    "file_transfer_message_handler.cc",
    "file_transfer_message_handler.h",
    "forward_process_stats_agent.cc",
    "forward_process_stats_agent.h",
    "gcd_rest_client.cc",
    "gcd_rest_client.h",
    "gcd_state_updater.cc",
    "gcd_state_updater.h",
    "heartbeat_sender.cc",
    "heartbeat_sender.h",
    "host_attributes.cc",
    "host_attributes.h",
    "host_change_notification_listener.cc",
    "host_change_notification_listener.h",
    "host_config.cc",
    "host_config.h",
    "host_details.cc",
    "host_details.h",
    "host_event_logger.h",
    "host_event_logger_posix.cc",
    "host_event_logger_win.cc",
    "host_exit_codes.cc",
    "host_exit_codes.h",
    "host_experiment_session_plugin.cc",
    "host_experiment_session_plugin.h",
    "host_export.h",
    "host_extension.h",
    "host_extension_session.h",
    "host_extension_session_manager.cc",
    "host_extension_session_manager.h",
    "host_power_save_blocker.cc",
    "host_power_save_blocker.h",
    "host_secret.cc",
    "host_secret.h",
    "host_status_logger.cc",
    "host_status_logger.h",
    "host_status_monitor.cc",
    "host_status_monitor.h",
    "host_status_observer.h",
    "host_window.cc",
    "host_window.h",
    "host_window_proxy.cc",
    "host_window_proxy.h",
    "input_injector.h",
    "input_injector_chromeos.cc",
    "input_injector_chromeos.h",
    "input_injector_mac.cc",
    "input_injector_win.cc",
    "input_injector_x11.cc",
    "ipc_audio_capturer.cc",
    "ipc_audio_capturer.h",
    "ipc_constants.cc",
    "ipc_constants.h",
    "ipc_desktop_environment.cc",
    "ipc_desktop_environment.h",
    "ipc_host_event_logger.cc",
    "ipc_host_event_logger.h",
    "ipc_input_injector.cc",
    "ipc_input_injector.h",
    "ipc_mouse_cursor_monitor.cc",
    "ipc_mouse_cursor_monitor.h",
    "ipc_screen_controls.cc",
    "ipc_screen_controls.h",
    "ipc_video_frame_capturer.cc",
    "ipc_video_frame_capturer.h",
    "it2me_desktop_environment.cc",
    "it2me_desktop_environment.h",
    "local_input_monitor.h",
    "local_input_monitor_chromeos.cc",
    "local_input_monitor_mac.mm",
    "local_input_monitor_win.cc",
    "local_input_monitor_x11.cc",
    "logging.h",
    "logging_linux.cc",
    "logging_mac.cc",
    "logging_win.cc",
    "me2me_desktop_environment.cc",
    "me2me_desktop_environment.h",
    "mouse_cursor_monitor_proxy.cc",
    "mouse_cursor_monitor_proxy.h",
    "mouse_shape_pump.cc",
    "mouse_shape_pump.h",
    "pairing_registry_delegate.cc",
    "pairing_registry_delegate.h",
    "pairing_registry_delegate_linux.cc",
    "pairing_registry_delegate_linux.h",
    "pairing_registry_delegate_mac.cc",
    "pairing_registry_delegate_win.cc",
    "pairing_registry_delegate_win.h",
    "pin_hash.cc",
    "pin_hash.h",
    "policy_watcher.cc",
    "policy_watcher.h",
    "posix/signal_handler.cc",
    "posix/signal_handler.h",
    "process_stats_agent.h",
    "process_stats_sender.cc",
    "process_stats_sender.h",
    "register_support_host_request.cc",
    "register_support_host_request.h",
    "remote_input_filter.cc",
    "remote_input_filter.h",
    "resizing_host_observer.cc",
    "resizing_host_observer.h",
    "resources.h",
    "resources_linux.cc",
    "resources_mac.cc",
    "resources_win.cc",
    "sas_injector.h",
    "sas_injector_win.cc",
    "screen_controls.h",
    "screen_resolution.cc",
    "screen_resolution.h",
    "server_log_entry_host.cc",
    "server_log_entry_host.h",
    "shutdown_watchdog.cc",
    "shutdown_watchdog.h",
    "signaling_connector.cc",
    "signaling_connector.h",
    "single_window_desktop_environment.cc",
    "single_window_desktop_environment.h",
    "single_window_input_injector.h",
    "single_window_input_injector_linux.cc",
    "single_window_input_injector_mac.cc",
    "single_window_input_injector_win.cc",
    "switches.cc",
    "switches.h",
    "test_echo_extension.cc",
    "test_echo_extension.h",
    "test_echo_extension_session.cc",
    "test_echo_extension_session.h",
    "third_party_auth_config.cc",
    "third_party_auth_config.h",
    "token_validator_base.cc",
    "token_validator_base.h",
    "token_validator_factory_impl.cc",
    "token_validator_factory_impl.h",
    "touch_injector_win.cc",
    "touch_injector_win.h",
    "usage_stats_consent.h",
    "usage_stats_consent_mac.cc",
    "usage_stats_consent_win.cc",
    "username.cc",
    "username.h",
  ]

  libs = []

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

  defines = [ "WEBRTC_CHROMIUM_BUILD" ]

  deps = [
    "//base:i18n",
    "//components/policy/core/common",
    "//crypto",
    "//google_apis",
    "//media",
    "//remoting/base",
    "//remoting/base:authorization",
    "//remoting/host/security_key",
    "//remoting/protocol",
    "//remoting/resources",

    # //remoting uses the power_save_blocker directly. See crbug.com/689423
    "//services/device/wake_lock/power_save_blocker",
    "//ui/base",
    "//ui/events:dom_keycode_converter",
    "//ui/events/platform",
  ]

  public_deps = [
    "//ipc",
  ]

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

  if (is_linux && !is_chromeos) {
    public_deps += [ "//remoting/host/linux" ]
    libs += [ "pam" ]
  }

  if (use_x11) {
    configs += [
      "//build/config/linux:x11",
      "//build/config/linux:xrandr",
    ]
    deps += [ "//remoting/host/linux:x11" ]
    if (is_desktop_linux) {
      deps += [ "//build/config/linux/gtk" ]
    }
  } else {
    sources -= [
      "clipboard_x11.cc",
      "desktop_resizer_x11.cc",
      "input_injector_x11.cc",
      "local_input_monitor_x11.cc",
    ]
  }

  if (!use_ozone) {
    sources -= [ "desktop_resizer_ozone.cc" ]
  }

  if (is_chromeos) {
    # TODO(GYP): crbug.com/481627. These should only be included
    # when enable_me2me_host is true.
    sources -= [
      "audio_capturer_linux.cc",
      "audio_capturer_linux.h",
      "continue_window_linux.cc",
      "curtain_mode_linux.cc",
      "disconnect_window_linux.cc",
      "me2me_desktop_environment.cc",
      "me2me_desktop_environment.h",
    ]
    deps += [
      "//ash",
      "//remoting/host/chromeos",
    ]

    if (use_ozone) {
      deps += [ "//ui/ozone" ]
      sources -= [ "desktop_resizer_ozone.cc" ]
    } else {
      sources -= [
        "clipboard_x11.cc",
        "desktop_resizer_x11.cc",
        "input_injector_chromeos.cc",
        "input_injector_chromeos.h",
        "local_input_monitor_x11.cc",
      ]
    }
  }

  if (is_mac) {
    libs += [
      "Accelerate.framework",
      "Carbon.framework",
    ]

    deps += [
      ":remoting_version",
      "//third_party/google_toolbox_for_mac",
    ]
  }

  if (is_win) {
    deps += [
      # On Windows, we use //media/gpu/MediaFoundationVideoEncodeAcceleratorWin
      # to detect whether HW encoder is supported by the system.
      "//media",
      "//remoting/host/win",
      "//remoting/host/win:messages",
      "//remoting/host/win:remoting_lib_idl",
    ]

    public_deps += [ "//remoting/host/win" ]
  }

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

static_library("test_support") {
  testonly = true

  sources = [
    "fake_desktop_environment.cc",
    "fake_desktop_environment.h",
    "fake_host_extension.cc",
    "fake_host_extension.h",
    "fake_mouse_cursor_monitor.cc",
    "fake_mouse_cursor_monitor.h",
    "host_mock_objects.cc",
    "host_mock_objects.h",
  ]

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

  deps = [
    "//remoting/proto",
    "//testing/gmock",
    "//testing/gtest",
  ]
  public_deps = [
    ":host",
    "//remoting/base:test_support",
    "//third_party/protobuf:protobuf_lite",
  ]

  if (enable_webrtc) {
    public_deps += [
      "//third_party/libjingle_xmpp",
      "//third_party/webrtc/modules/desktop_capture",
      "//third_party/webrtc_overrides:init_webrtc",
    ]
  }
}

# The host portions of the remoting unit tests.
source_set("unit_tests") {
  testonly = true

  sources = [
    "audio_silence_detector_unittest.cc",
    "audio_volume_filter_unittest.cc",
    "backoff_timer_unittest.cc",
    "chromoting_host_context_unittest.cc",
    "chromoting_host_unittest.cc",
    "client_session_unittest.cc",
    "config_file_watcher_unittest.cc",
    "daemon_process_unittest.cc",
    "desktop_process_unittest.cc",
    "desktop_session_agent_unittest.cc",
    "file_proxy_wrapper_linux_unittest.cc",
    "file_transfer_message_handler_unittest.cc",
    "gcd_rest_client_unittest.cc",
    "gcd_state_updater_unittest.cc",
    "heartbeat_sender_unittest.cc",
    "host_attributes_unittest.cc",
    "host_change_notification_listener_unittest.cc",
    "host_config_unittest.cc",
    "host_experiment_session_plugin_unittest.cc",
    "host_extension_session_manager_unittest.cc",
    "host_power_save_blocker_unittest.cc",
    "host_status_logger_unittest.cc",
    "ipc_desktop_environment_unittest.cc",
    "it2me/it2me_confirmation_dialog_proxy_unittest.cc",
    "it2me/it2me_host_unittest.cc",
    "it2me/it2me_native_messaging_host_unittest.cc",
    "local_input_monitor_unittest.cc",
    "mouse_cursor_monitor_proxy_unittest.cc",
    "mouse_shape_pump_unittest.cc",
    "native_messaging/native_messaging_reader_unittest.cc",
    "native_messaging/native_messaging_writer_unittest.cc",
    "pairing_registry_delegate_linux_unittest.cc",
    "pairing_registry_delegate_win_unittest.cc",
    "pin_hash_unittest.cc",
    "policy_watcher_unittest.cc",
    "process_stats_sender_unittest.cc",
    "register_support_host_request_unittest.cc",
    "remote_input_filter_unittest.cc",
    "resizing_host_observer_unittest.cc",
    "resources_unittest.cc",
    "screen_resolution_unittest.cc",
    "server_log_entry_host_unittest.cc",
    "setup/me2me_native_messaging_host_unittest.cc",
    "setup/pin_validator_unittest.cc",
    "third_party_auth_config_unittest.cc",
    "token_validator_base_unittest.cc",
    "token_validator_factory_impl_unittest.cc",
    "touch_injector_win_unittest.cc",
  ]

  if (use_ozone || is_chromeos) {
    sources -= [ "local_input_monitor_unittest.cc" ]
  }

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

  deps = [
    ":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",
  ]

  if (is_linux && !is_chromeos) {
    deps += [ "//remoting/host/linux:unit_tests" ]
  }

  if (is_chromeos) {
    deps += [
      "//remoting/host/chromeos:unit_tests",
      "//remoting/host/linux",  # for AudioPipeReader
    ]
  }

  if (is_win) {
    deps += [ "//remoting/host/win:unit_tests" ]
  }

  if (!is_ios) {
    deps += [ "//components/policy/core/browser:test_support" ]
  }

  if (enable_me2me_host) {
    sources += [ "evaluate_capability_unittest.cc" ]
    data_deps = [
      "//remoting/test:capability_test_stub",
    ]
  }
}

group("remoting_host_branded") {
  testonly = true
  deps = []
  if (enable_remoting_host) {
    deps += [ ":remoting_host_installation" ]
  }
  if (enable_me2me_host) {
    deps += [ "//remoting/host:remoting_me2me_host_archive" ]
  }
}

if (enable_remoting_host) {
  group("remoting_host_installation") {
    deps = []
    if (is_win) {
      deps += [ "//remoting/host/installer/win:remoting_host_installation" ]
    }
  }
}

if (enable_remoting_host) {
  executable("remoting_start_host") {
    sources = [
      "setup/start_host_entry_point.cc",
    ]

    deps = [
      "//build/config:exe_and_shlib_deps",
    ]

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

    if (is_win) {
      defines = host_predefines +
                [ "REMOTING_HOST_BINARY=BINARY_REMOTING_START_HOST" ]

      deps += [
        "//build/win:default_exe_manifest",
        "//remoting/host/win:remoting_core",
        "//remoting/host/win:remoting_windows_resources",
      ]
    } else {
      sources += [
        "setup/host_starter.cc",
        "setup/host_starter.h",
        "setup/start_host_main.cc",
        "setup/start_host_main.h",
      ]

      deps += [
        "//google_apis",
        "//net",
        "//remoting/host/setup",
      ]
    }

    if (enable_webrtc) {
      deps += [
        "//third_party/libjingle_xmpp",
        "//third_party/webrtc_overrides:init_webrtc",
      ]
    }
  }

  action_foreach("remoting_native_messaging_manifests") {
    if (is_mac) {
      me2me_host_path = "/Library/PrivilegedHelperTools/$me2me_host_bundle_name/Contents/MacOS/$native_messaging_host_bundle_name/Contents/MacOS/native_messaging_host"
      it2me_host_path = "/Library/PrivilegedHelperTools/$me2me_host_bundle_name/Contents/MacOS/$remote_assistance_host_bundle_name/Contents/MacOS/remote_assistance_host"
    } else if (is_win) {
      me2me_host_path = "remoting_native_messaging_host.exe"
      it2me_host_path = "remote_assistance_host.exe"
    } else {
      me2me_host_path =
          "/opt/google/chrome-remote-desktop/native-messaging-host"
      it2me_host_path =
          "/opt/google/chrome-remote-desktop/remote-assistance-host"
    }

    script = "../tools/build/remoting_localize.py"

    sources = [
      "it2me/com.google.chrome.remote_assistance.json.jinja2",
      "setup/com.google.chrome.remote_desktop.json.jinja2",
    ]

    inputs = [
      branding_path,
    ]

    outputs = [
      "$root_build_dir/remoting/{{source_name_part}}",
    ]

    args = [
      "--define",
      "ME2ME_HOST_PATH=$me2me_host_path",
      "--define",
      "IT2ME_HOST_PATH=$it2me_host_path",
      "--variables",
      rebase_path(branding_path, root_build_dir),
      "--template",
      "{{source}}",
      "--output",
      "remoting/{{source_name_part}}",
      "en",
    ]
  }

  action_foreach("remoting_infoplist_strings") {
    sources = [
      "installer/mac/uninstaller/remoting_uninstaller-InfoPlist.strings.jinja2",
      "it2me/remote_assistance_host-InfoPlist.strings.jinja2",
      "remoting_me2me_host-InfoPlist.strings.jinja2",
      "setup/native_messaging_host-InfoPlist.strings.jinja2",
    ]

    script = "//remoting/tools/build/remoting_localize.py"
    args = [
             "--locale_dir",
             rebase_path(webapp_locale_dir, root_build_dir),
             "--variables",
             rebase_path(branding_path, root_build_dir),
             "--template",
             "{{source}}",
             "--locale_output",
             rebase_path(
                 "$root_gen_dir/remoting/host/{{source_name_part}}/@{json_suffix}.lproj/InfoPlist.strings",
                 root_build_dir),
           ] + remoting_locales_with_underscores

    outputs = []
    foreach(locale, remoting_locales_with_underscores) {
      outputs += [ "$root_gen_dir/remoting/host/{{source_name_part}}/$locale.lproj/InfoPlist.strings" ]
    }

    deps = [
      "//remoting/resources",
      "//remoting/resources:strings",
    ]
  }
}

if (enable_me2me_host) {
  static_library("remoting_me2me_host_static") {
    sources = [
      "pam_authorization_factory_posix.cc",
      "pam_authorization_factory_posix.h",
      "remoting_me2me_host.cc",
    ]
    defines = []

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

    deps = [
      "//base",
      "//base:i18n",
      "//components/policy/core/common",
      "//ipc",
      "//net",
      "//remoting/base",
      "//remoting/host",
      "//remoting/proto",
      "//third_party/webrtc/modules/desktop_capture",
    ]

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

    if (enable_webrtc) {
      deps += [
        "//third_party/libjingle_xmpp",
        "//third_party/webrtc_overrides:init_webrtc",
      ]
    }

    if (is_desktop_linux) {
      deps += [ "//build/config/linux/gtk" ]
    }
    if ((is_linux && !is_chromeos) || is_mac) {
      libs = [ "pam" ]
    }

    if (is_mac && is_official_build) {
      sources += [ "internal/internal_mac-inl.h" ]
    }

    if (remoting_multi_process != 0) {
      deps += [
        "//mojo/edk/embedder:headers",
        "//mojo/edk/system",
      ]
    }
  }

  if (is_win) {
    group("remoting_me2me_host") {
      deps = [
        "//remoting/host/win:remoting_me2me_host",
      ]
    }
    group("remoting_native_messaging_host") {
      deps = [
        "//remoting/host/win:remoting_native_messaging_host",
      ]
    }
  } else {
    source_set("main") {
      sources = [
        "host_main.cc",
        "host_main.h",
      ]
    }
    if (is_mac) {
      group("remoting_me2me_host") {
        deps = [
          "//remoting/host/mac:remoting_me2me_host",
        ]
      }
    } else {
      target("executable", "remoting_me2me_host") {
        configs += [ "//remoting/build/config:version" ]

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

        deps = [
          ":remoting_me2me_host_static",
          "//build/config:exe_and_shlib_deps",
          "//remoting/base:breakpad",
          "//remoting/host/installer:credits",
          "//remoting/resources",
        ]
      }
    }

    if (is_mac) {
      group("remoting_native_messaging_host") {
        deps = [
          "//remoting/host/mac:remoting_native_messaging_host",
        ]
      }
    } else {
      group("remoting_native_messaging_host") {
        deps = [
          "//remoting/host/linux:remoting_native_messaging_host",
        ]
      }
    }
  }
  if (is_chrome_branded && enable_me2me_host && is_linux && !is_chromeos) {
    group("remoting_me2me_host_archive") {
      deps = [
        "//remoting/host/installer/linux:remoting_me2me_host_archive",
      ]
    }
  } else if (is_win) {
    group("remoting_me2me_host_archive") {
      deps = [
        "//remoting/host/installer/win:remoting_me2me_host_archive",
      ]
    }
  } else if (is_mac) {
    group("remoting_me2me_host_archive") {
      deps = [
        "//remoting/host/installer/mac:remoting_me2me_host_archive",
      ]
    }
  } else {
    group("remoting_me2me_host_archive") {
    }
  }
}