# Copyright 2014 The Chromium Authors
# 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/ui.gni")
import("//content/public/common/zygote/features.gni")
import("//ipc/features.gni")
import("//media/media_options.gni")
import("//mojo/public/tools/bindings/mojom.gni")
import("//sandbox/features.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("features.gni")
import("//arkweb/build/features/features.gni")
import("//arkweb/chromium_ext/chromium_ext.gni")

if (defined(is_arkweb_ext) && is_arkweb_ext) {
  import("//arkweb/ohos_nweb_ex/build/features/features.gni")
}

# For feature flags internal to content. See content/public/common:features
# for feature flags that clients of contents need to know about.
buildflag_header("buildflags") {
  header = "buildflags.h"

  _mbi_mode_per_render_process_host = mbi_mode == "per_render_process_host"
  _mbi_mode_per_site_instance = mbi_mode == "per_site_instance"

  flags = [
    "ALLOW_CRITICAL_MEMORY_PRESSURE_HANDLING_IN_FOREGROUND=$allow_critical_memory_pressure_handling_in_foreground",
    "MBI_MODE_PER_RENDER_PROCESS_HOST=$_mbi_mode_per_render_process_host",
    "MBI_MODE_PER_SITE_INSTANCE=$_mbi_mode_per_site_instance",
    "CONTENT_ENABLE_LEGACY_IPC=0",
  ]
}

if (is_linux || is_chromeos || arkweb_render_process_startup) {
  source_set("sandbox_support_linux") {
    public = [
      "zygote/zygote_commands_linux.h",
      "zygote/zygote_communication_linux.h",
    ]

    sources = [
      "zygote/sandbox_support_linux.cc",
      "zygote/send_zygote_child_ping_linux.cc",
      "zygote/zygote_communication_linux.cc",
    ]

    deps = [
      "//base",
      "//base:i18n",
      "//content/public/common:content_descriptors",
      "//content/public/common:result_codes",
      "//content/public/common:sandbox_support_linux",
      "//content/public/common:switches",
      "//sandbox/policy",
      "//third_party/icu",
    ]

    configs += [ "//content:content_implementation" ]
  }
}

source_set("common") {
  # Targets external to content should always link to the public API.
  # In addition, targets outside of the content component (shell and tests)
  # must not link to this because it will duplicate the code in the component
  # build.
  visibility = [ "//content/*" ]

  sources = [
    "background_fetch/background_fetch_types.cc",
    "background_fetch/background_fetch_types.h",
    "color_parser.cc",
    "common_param_traits.cc",
    "common_param_traits.h",
    "common_param_traits_macros.h",
    "content_constants_internal.h",
    "content_export.h",
    "content_navigation_policy.cc",
    "content_navigation_policy.h",
    "content_param_traits.cc",
    "content_param_traits.h",
    "content_param_traits_macros.h",
    "content_paths.cc",
    "content_switches_internal.cc",
    "content_switches_internal.h",
    "cursors/webcursor.cc",
    "cursors/webcursor.h",
    "cursors/webcursor_ash.cc",
    "cursors/webcursor_aura.cc",
    "debug_utils.cc",
    "debug_utils.h",
    "features.cc",
    "features.h",
    "fetch/fetch_api_request_proto.cc",
    "fetch/fetch_api_request_proto.h",
    "fetch/fetch_request_type_converters.cc",
    "fetch/fetch_request_type_converters.h",
    "font_list.cc",
    "font_list.h",
    "font_list_fontconfig.cc",
    "in_process_child_thread_params.cc",
    "in_process_child_thread_params.h",
    "input/actions_parser.cc",
    "input/actions_parser.h",
    "input/synthetic_gesture.cc",
    "input/synthetic_gesture.h",
    "input/synthetic_gesture_controller.cc",
    "input/synthetic_gesture_controller.h",
    "input/synthetic_gesture_params.cc",
    "input/synthetic_gesture_params.h",
    "input/synthetic_gesture_target.h",
    "input/synthetic_mouse_driver.cc",
    "input/synthetic_mouse_driver.h",
    "input/synthetic_pen_driver.cc",
    "input/synthetic_pen_driver.h",
    "input/synthetic_pinch_gesture.cc",
    "input/synthetic_pinch_gesture.h",
    "input/synthetic_pinch_gesture_params.cc",
    "input/synthetic_pinch_gesture_params.h",
    "input/synthetic_pointer_action.cc",
    "input/synthetic_pointer_action.h",
    "input/synthetic_pointer_action_list_params.cc",
    "input/synthetic_pointer_action_list_params.h",
    "input/synthetic_pointer_action_params.cc",
    "input/synthetic_pointer_action_params.h",
    "input/synthetic_pointer_driver.cc",
    "input/synthetic_pointer_driver.h",
    "input/synthetic_smooth_drag_gesture.cc",
    "input/synthetic_smooth_drag_gesture.h",
    "input/synthetic_smooth_drag_gesture_params.cc",
    "input/synthetic_smooth_drag_gesture_params.h",
    "input/synthetic_smooth_move_gesture.cc",
    "input/synthetic_smooth_move_gesture.h",
    "input/synthetic_smooth_scroll_gesture.cc",
    "input/synthetic_smooth_scroll_gesture.h",
    "input/synthetic_smooth_scroll_gesture_params.cc",
    "input/synthetic_smooth_scroll_gesture_params.h",
    "input/synthetic_tap_gesture.cc",
    "input/synthetic_tap_gesture.h",
    "input/synthetic_tap_gesture_params.cc",
    "input/synthetic_tap_gesture_params.h",
    "input/synthetic_touch_driver.cc",
    "input/synthetic_touch_driver.h",
    "input/synthetic_touchpad_pinch_gesture.cc",
    "input/synthetic_touchpad_pinch_gesture.h",
    "input/synthetic_touchscreen_pinch_gesture.cc",
    "input/synthetic_touchscreen_pinch_gesture.h",
    "media/cdm_info.cc",
    "navigation_gesture.h",
    "navigation_params_utils.h",
    "origin_util.cc",
    "process_type.cc",
    "process_visibility_tracker.cc",
    "process_visibility_tracker.h",
    "pseudonymization_salt.cc",
    "pseudonymization_salt.h",
    "scheduler_loop_quarantine_task_observer.cc",
    "scheduler_loop_quarantine_task_observer.h",
    "service_worker/forwarded_race_network_request_url_loader_factory.cc",
    "service_worker/forwarded_race_network_request_url_loader_factory.h",
    "service_worker/race_network_request_read_buffer_manager.cc",
    "service_worker/race_network_request_read_buffer_manager.h",
    "service_worker/race_network_request_simple_buffer_manager.cc",
    "service_worker/race_network_request_simple_buffer_manager.h",
    "service_worker/race_network_request_url_loader_client.cc",
    "service_worker/race_network_request_url_loader_client.h",
    "service_worker/race_network_request_write_buffer_manager.cc",
    "service_worker/race_network_request_write_buffer_manager.h",
    "service_worker/service_worker_resource_loader.cc",
    "service_worker/service_worker_resource_loader.h",
    "service_worker/service_worker_router_evaluator.cc",
    "service_worker/service_worker_router_evaluator.h",
    "skia_utils.cc",
    "skia_utils.h",
    "thread_pool_util.cc",
    "thread_pool_util.h",
    "trace_utils.h",
    "url_schemes.cc",
    "url_schemes.h",
    "user_level_memory_pressure_signal_features.cc",
    "user_level_memory_pressure_signal_features.h",
    "web_package/signed_exchange_utils.cc",
    "web_package/signed_exchange_utils.h",
    "web_ui_loading_util.cc",
    "web_ui_loading_util.h",
    "webid/identity_url_loader_throttle.cc",
    "webid/identity_url_loader_throttle.h",
  ]

  configs += [
    "//content:content_implementation",
    "//build/config:precompiled_headers",
  ]

  public_deps = [
    ":mojo_bindings",
    "//cc",
    "//cc/ipc",
    "//content/public/common:switches",
    "//gpu/command_buffer/common",
    "//ipc",
    "//sandbox/policy",
    "//services/network:network_service",
    "//services/service_manager/public/cpp",
    "//third_party/blink/public:blink_headers",
    "//third_party/blink/public/common",
    "//ui/accessibility",
    "//ui/accessibility/mojom",
    "//ui/base/cursor",
    "//ui/events/ipc",
  ]
  deps = [
    ":buildflags",
    "//base",
    "//base/allocator:buildflags",
    "//build:branding_buildflags",
    "//components/discardable_memory/common",
    "//components/input",
    "//components/services/filesystem/public/mojom",
    "//components/tracing:tracing_config",
    "//components/embedder_support:make_ohos_arkweb_version",
    "//content:content_resources",
    "//content/common/fetch:fetch_api_request_proto",
    "//content/common/memory_coordinator/mojom",
    "//content/public/common:interfaces",
    "//content/public/common/zygote:buildflags",
    "//device/base/synchronization",
    "//device/bluetooth",
    "//gpu/command_buffer/client:gles2_implementation",
    "//gpu/command_buffer/client:gles2_interface",
    "//gpu/command_buffer/service",
    "//gpu/ipc/client",
    "//gpu/ipc/common",
    "//gpu/skia_bindings",
    "//ipc",
    "//media",
    "//media/base/ipc",
    "//media/capture",
    "//media/gpu:buildflags",
    "//media/gpu/ipc/common",
    "//media/midi",
    "//media/midi:mojo",
    "//mojo/public/cpp/system",
    "//net",
    "//sandbox",
    "//sandbox:sandbox_buildflags",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
    "//services/resource_coordinator/public/cpp/memory_instrumentation",
    "//services/service_manager/public/mojom",
    "//services/video_capture/public/mojom",
    "//services/viz/public/mojom",
    "//skia",
    "//storage/common",
    "//third_party/angle:angle_gpu_info_util",
    "//third_party/boringssl",
    "//third_party/icu",
    "//third_party/re2",
    "//ui/base",
    "//ui/base/cursor/mojom:cursor_type",
    "//ui/base/ime",
    "//ui/color",
    "//ui/display",
    "//ui/events:gesture_detection",
    "//ui/events/blink",
    "//ui/gfx",
    "//ui/gfx/geometry",
    "//ui/gfx/geometry/mojom:mojom_traits",
    "//ui/gfx/ipc",
    "//ui/gfx/ipc/color",
    "//ui/gfx/ipc/geometry",
    "//ui/gfx/ipc/skia",
    "//ui/gl",
    "//ui/gl/init",
    "//ui/latency/ipc",
    "//ui/native_theme",
    "//ui/shell_dialogs",
    "//url",
    "//url/ipc:url_ipc",
  ]

  defines = []
  include_dirs = []
  libs = []
  ldflags = []

  allow_circular_includes_from = [
    ":mojo_bindings",
    "//content/public/common:interfaces",
  ]

  if (is_posix) {
    sources += [
      "shared_file_util.cc",
      "shared_file_util.h",
    ]
  }

  if (is_android) {
    sources += [
      "android/cpu_time_metrics.cc",
      "android/cpu_time_metrics.h",
      "android/cpu_time_metrics_internal.cc",
      "android/cpu_time_metrics_internal.h",
      "android/gin_java_bridge_errors.cc",
      "android/gin_java_bridge_errors.h",
      "android/gin_java_bridge_value.cc",
      "android/gin_java_bridge_value.h",
      "android/hash_set.cc",
      "android/hash_set.h",
      "android/surface_wrapper.cc",
      "android/surface_wrapper.h",
      "android/sync_compositor_statics.cc",
      "android/sync_compositor_statics.h",
      "cursors/webcursor_android.cc",
      "font_list_android.cc",
    ]

    if (use_seccomp_bpf) {
      deps += [ "//sandbox/policy" ]
    }
  }

  if (is_ios) {
    # TOOD: Create ios file instead of stealing android
    sources += [ "cursors/webcursor_android.cc" ]
  }

  if (is_mac) {
    sources += [
      "mac/attributed_string_type_converters.h",
      "mac/attributed_string_type_converters.mm",
      "mac/system_policy.cc",
      "mac/system_policy.h",
    ]
    deps += [ "//sandbox/mac:seatbelt" ]
  }

  if (is_apple) {
    sources += [ "font_list_mac.mm" ]
  }

  if (is_win) {
    sources += [
      "font_cache_dispatcher_win.cc",
      "font_list_win.cc",
      "sandbox_init_win.cc",
    ]
  }

  if (is_android) {
    deps += [
      "//content/public/android:common_aidl",
      "//content/public/android:common_jni",
      "//content/public/android:jar_jni",
    ]

    libs += [ "android" ]
  }

  if (!is_chromeos) {
    sources -= [ "cursors/webcursor_ash.cc" ]
  }

  if (use_aura) {
    deps += [ "//ui/wm" ]
  } else {
    sources -= [ "cursors/webcursor_aura.cc" ]
  }

  if (is_linux || is_chromeos) {
    sources += [
      "gpu_pre_sandbox_hook_linux.cc",
      "gpu_pre_sandbox_hook_linux.h",
    ]
    public_deps += [ "//sandbox/policy" ]
    deps += [
      ":sandbox_support_linux",
      "//media/gpu:buildflags",
      "//sandbox/linux:sandbox_services",
      "//sandbox/linux:seccomp_bpf",
      "//sandbox/policy:chromecast_sandbox_allowlist_buildflags",
      "//third_party/fontconfig",
    ]
    if (use_v4l2_codec) {
      deps += [ "//media/gpu/v4l2" ]
    }
  }

  if (use_zygote) {
    sources += [
      "zygote/zygote_handle_impl_linux.h",
      "zygote/zygote_handle_linux.cc",
    ]
  }

  if (is_mac || is_win || is_android || is_fuchsia || is_ios || is_ohos) {
    sources -= [ "font_list_fontconfig.cc" ]
  }

  if (is_arkweb) {
    sources += arkweb_content_common_font_list_sources
  }

  if (is_mac) {
    deps += [ "//media/gpu" ]
  }

  if (is_fuchsia) {
    sources += [ "font_list_fuchsia.cc" ]
    deps += [ "//third_party/fuchsia-sdk/sdk/pkg/fdio" ]
  }

  if (use_clang_profiling_inside_sandbox) {
    sources += [ "profiling_utils.cc" ]
  }

  if (arkweb_same_layer) {
    sources += arkweb_content_common_sources
  }
}

# This is used by content/shell as well, which we don't want to depend on the
# entire content/common.
component("main_frame_counter") {
  defines = [ "IS_MAIN_FRAME_COUNTER_IMPL" ]
  sources = [
    "main_frame_counter.cc",
    "main_frame_counter.h",
  ]
  deps = [ "//base" ]
}

# See comment at the top of //content/BUILD.gn for how this works.
group("for_content_tests") {
  visibility = [
    "//content/services/auction_worklet:tests",
    "//content/test/*",
  ]
  if (!is_component_build) {
    public_deps = [ ":common" ]
  }
}

mojom("mojo_bindings") {
  # This interface is internal to content.
  visibility = [ "//content/*" ]

  disable_variants = true

  sources = [
    "agent_scheduling_group.mojom",
    "associated_interfaces.mojom",
    "child_process.mojom",
    "dom_automation_controller.mojom",
    "download/mhtml_file_writer.mojom",
    "field_trial_recorder.mojom",
    "frame.mojom",
    "frame_messages.mojom",
    "input/input_injector.mojom",
    "media/media_log_records.mojom",
    "native_types.mojom",
    "navigation_client.mojom",
    "render_message_filter.mojom",
    "renderer.mojom",
    "renderer_host.mojom",
    "renderer_variations_configuration.mojom",
    "synthetic_trial_configuration.mojom",
    "web_ui.mojom",
  ]

  if (is_linux || is_chromeos) {
    sources += [ "thread_type_switcher.mojom" ]
  }

  if (is_android) {
    sources += [ "gin_java_bridge.mojom" ]
  }

  if (is_win) {
    sources += [ "sandbox_support.mojom" ]
  }

  enabled_features = []
  if (use_clang_profiling_inside_sandbox) {
    enabled_features += [ "clang_profiling_inside_sandbox" ]
  }
  if (is_posix && !is_android) {
    enabled_features += [ "is_non_android_posix" ]
  }

  if (arkweb_render_process_mode) {
    enabled_features += [ "arkweb_render_process_mode" ]
  }

  if (arkweb_menu) {
    enabled_features += [ "arkweb_menu" ]
  }

  if (arkweb_drag_drop) {
    enabled_features += [ "arkweb_drag_drop" ]
  }

  if (arkweb_ai) {
    enabled_features += [ "arkweb_ai" ]
  }

  if (enable_arkweb) {
    enabled_features += [ "arkweb_theme_font" ]
    enabled_features += [ "arkweb_i18n" ]
  }

  if (arkweb_pdf) {
    enabled_features += [ "arkweb_pdf" ]
  }

  if (arkweb_arkweb_extensions) {
    enabled_features += [ "arkweb_arkweb_extensions" ]
  }

  if (arkweb_renderer_anr_dump) {
    enabled_features += ["arkweb_renderer_anr_dump"]
  }

  if (arkweb_performance_scheduling) {
    enabled_features += ["arkweb_performance_scheduling"]
  }

  if (arkweb_multi_window) {
    enabled_features +=[ "arkweb_multi_window" ]
  }

  if (arkweb_blank_screen_detection) {
    enabled_features += [ "arkweb_blank_screen_detection" ]
  }

  if (arkweb_network_base) {
    enabled_features += [ "arkweb_network_base" ]
  }

  if (defined(arkweb_reader_mode) && arkweb_reader_mode) {
    enabled_features += [ "arkweb_reader_mode" ]
  }

  if (defined(arkweb_adblock) && arkweb_adblock) {
    enabled_features += [ "arkweb_adblock" ]
  }
  if (defined(arkweb_ext_video_load_optimization) && arkweb_ext_video_load_optimization) {
    enabled_features += [ "arkweb_ext_video_load_optimization" ]
  }

  if (is_arkweb && !is_component_build) {
    enabled_features += [ "arkweb_dfx_tracing" ]
  }

  if (is_arkweb) {
    enabled_features += [ "arkweb_js_on_document_end" ]
  }

  cpp_typemaps = [
    {
      types = [
        {
          mojom = "content.mojom.ChildFrameOwnerElementType"
          cpp = "::blink::FrameOwnerElementType"
        },
        {
          mojom = "content.mojom.NavigationGesture"
          cpp = "::content::NavigationGesture"
        },
        {
          mojom = "content.mojom.NetworkConnectionType"
          cpp = "::net::NetworkChangeNotifier::ConnectionType"
        },
        {
          mojom = "content.mojom.SyntheticPinch"
          cpp = "::content::SyntheticPinchGestureParams"
        },
        {
          mojom = "content.mojom.SyntheticPointerAction"
          cpp = "::content::SyntheticPointerActionListParams"
        },
        {
          mojom = "content.mojom.SyntheticSmoothDrag"
          cpp = "::content::SyntheticSmoothDragGestureParams"
        },
        {
          mojom = "content.mojom.SyntheticSmoothScroll"
          cpp = "::content::SyntheticSmoothScrollGestureParams"
        },
        {
          mojom = "content.mojom.SyntheticTap"
          cpp = "::content::SyntheticTapGestureParams"
        },
      ]

      traits_headers = [
        "//content/common/frame_owner_element_type_mojom_traits.h",
        "//content/common/input/input_injector_mojom_traits.h",
        "//content/common/input/synthetic_pinch_gesture_params.h",
        "//content/common/input/synthetic_pointer_action_list_params.h",
        "//content/common/input/synthetic_smooth_drag_gesture_params.h",
        "//content/common/input/synthetic_smooth_scroll_gesture_params.h",
        "//content/common/input/synthetic_tap_gesture_params.h",
        "//content/common/navigation_gesture.h",
        "//net/base/network_change_notifier.h",
        "//third_party/blink/public/common/navigation/impression.h",
        "//third_party/blink/public/common/widget/visual_properties.h",
        "//ui/events/types/scroll_types.h",
        "//ui/native_theme/native_theme.h",
      ]

      traits_private_headers = [
        "//content/common/content_param_traits_macros.h",
        "//content/public/common/common_param_traits.h",
        "//services/network/public/cpp/p2p_param_traits.h",
      ]

      traits_sources = [
        "//content/common/frame_owner_element_type_mojom_traits.cc",
        "//content/common/input/input_injector_mojom_traits.cc",
      ]

      traits_public_deps = [
        # NOTE: These dependencies are here to satisfy gn check because
        # common_param_traits_macros.h include their headers.
        # Although the mojo bindings target is configured to allow direct circular
        # includes from //content/common and //content/public/common, this isn't a
        # transitive allowance, so those targets' own public_deps aren't included in
        # the set of implied dependencies.
        "//content/common:buildflags",
        "//ui/events/blink",
      ]
    },
    {
      types = [
        {
          mojom = "content.mojom.PageState"
          cpp = "::blink::PageState"
        },
        {
          mojom = "content.mojom.PageTransition"
          cpp = "::ui::PageTransition"
        },
      ]
      traits_headers = [
        "//content/public/common/common_param_traits.h",
        "//third_party/blink/public/common/page_state/page_state.h",
        "//ui/base/page_transition_types.h",
      ]
    },
  ]

  public_deps = [
    "//cc/mojom",
    "//components/attribution_reporting:mojom",
    "//components/metrics/public/mojom:histogram_fetcher_mojo_bindings",
    "//components/variations:variations_mojom",
    "//content/public/common:interfaces",
    "//ipc:mojom",
    "//ipc:mojom_constants",
    "//media/capture/mojom:video_capture",
    "//media/mojo/mojom",
    "//mojo/public/mojom/base",
    "//services/audio/public/mojom",
    "//services/network/public/mojom",
    "//services/network/public/mojom:mojom_permissions_policy",
    "//services/service_manager/public/mojom",
    "//services/tracing/public/mojom",
    "//services/video_capture/public/mojom",
    "//services/viz/public/mojom",
    "//skia/public/mojom",
    "//third_party/blink/public/mojom:mojom_core",
    "//third_party/blink/public/mojom:mojom_modules",
    "//third_party/blink/public/mojom:web_feature_mojo_bindings",
    "//ui/accessibility:ax_enums_mojo",
    "//ui/accessibility/mojom",
    "//ui/base/ime/mojom",
    "//ui/base/mojom",
    "//ui/display/mojom",
    "//ui/events/mojom",
    "//ui/events/mojom:event_latency_metadata_mojom",
    "//ui/gfx/geometry/mojom",
    "//ui/gfx/image/mojom",
    "//ui/gfx/mojom",
    "//ui/gfx/range/mojom",
    "//ui/latency/mojom",
    "//url/mojom:url_mojom_gurl",
    "//url/mojom:url_mojom_origin",
  ]

  if (is_android) {
    public_deps += [ "//components/origin_matcher:mojom" ]
  }

  if (is_mac) {
    sources += [
      "render_widget_host_ns_view.mojom",
      "sandbox_support.mojom",
      "web_contents_ns_view_bridge.mojom",
    ]
    public_deps += [
      "//ui/base/cursor/mojom",
      "//ui/display/mojom",
    ]
    cpp_typemaps += [
      {
        types = [
          {
            mojom = "content.mojom.ScrollerStyle"
            cpp = "::blink::ScrollerStyle"
          },
        ]
        traits_headers =
            [ "//third_party/blink/public/platform/mac/web_scrollbar_theme.h" ]
        traits_public_deps = [ "//third_party/blink/public:blink_headers" ]
      },
    ]
  }

  overridden_deps = [
    "//third_party/blink/public/mojom:mojom_core",
    "//third_party/blink/public/mojom:mojom_modules",
  ]
  component_deps = [ "//third_party/blink/public/common" ]

  component_output_prefix = "content_common_mojo_bindings"
  export_class_attribute = "CONTENT_EXPORT"
  export_define = "CONTENT_IMPLEMENTATION=1"
  export_header = "content/common/content_export.h"
}

if (is_android) {
  java_cpp_features("java_features_srcjar") {
    # External code should depend on ":common_java" instead.
    visibility = [ ":*" ]
    sources = [ "//content/common/features.cc" ]
    class_name = "org.chromium.content.common.ContentInternalFeatures"
  }

  android_library("common_java") {
    # Right now, this only includes the Java features. But if we need more Java
    # files, they should be added here as necessary.
    srcjar_deps = [ ":java_features_srcjar" ]
  }
}