# Copyright 2026 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/rust/rust_static_library.gni")

# C++ transport client for OnePlatform `alt=proto` server-streaming RPCs:
# owns the network connection and fans complete (still serialized)
# messages out to observers. Framing of the untrusted stream bytes is
# behind the injected StreamFramer factory, so this target does not link
# any Rust; embedders bind :rust_stream_framer into the factory.
source_set("proto_stream_client") {
  sources = [
    "proto_stream_client.cc",
    "proto_stream_client.h",
  ]

  public_deps = [
    "//base",
    "//components/browser_actuator/internal/transport",
    "//net",
    "//services/network/public/cpp",
    "//url",
  ]

  deps = [
    "//components/browser_actuator/internal:features",
    "//services/network/public/mojom",
  ]
}

# StreamFramer implementation backed by the Rust StreamBody parser: the
# only production C++ that touches the cxx-generated bridge, so FFI types
# such as rust::Box do not leak into client code.
source_set("rust_stream_framer") {
  sources = [
    "rust_stream_framer.cc",
    "rust_stream_framer.h",
  ]

  public_deps = [
    "//base",
    "//components/browser_actuator/internal/transport",
  ]

  deps = [ ":stream_body_bridge" ]
}

# Pure-Rust StreamBody framing parser: the part that interprets untrusted
# network input. Keep this crate free of unsafe code and of dependencies.
rust_static_library("stream_body_parser") {
  crate_root = "rust/stream_body_parser.rs"
  sources = [ crate_root ]
  visibility = [ ":*" ]
}

# Rust gtest tests for the parser; linked into components_unittests via
# the //components/browser_actuator:unit_tests group.
rust_static_library("stream_body_parser_unittests") {
  testonly = true
  is_gtest_unittests = true
  crate_root = "rust/stream_body_parser_unittests.rs"
  sources = [ crate_root ]
  deps = [
    ":stream_body_parser",
    "//testing/rust_gtest_interop",
  ]
  visibility = [ "//components/browser_actuator:unit_tests" ]
}

# cxx FFI glue between RustStreamFramer and the parser crate. Contains no
# parsing logic, only type conversion; this is the only Rust target here
# that may use unsafe code.
rust_static_library("stream_body_bridge") {
  crate_root = "rust/stream_body_bridge.rs"
  sources = [ crate_root ]
  cxx_bindings = [ crate_root ]
  allow_unsafe = true  # Needed for FFI that underpins the `cxx` crate.
  deps = [ ":stream_body_parser" ]
  visibility = [ ":*" ]
}

# Includes a FuzzTest (listed in test("components_unittests")'s fuzztests)
# that fuzzes the Rust framing parser through the production StreamFramer
# surface.
source_set("unit_tests") {
  testonly = true

  sources = [
    "proto_stream_client_unittest.cc",
    "rust_stream_framer_unittest.cc",
  ]

  deps = [
    ":proto_stream_client",
    ":rust_stream_framer",
    "//base",
    "//base/test:test_support",
    "//components/browser_actuator/internal:features",
    "//components/browser_actuator/internal/transport",
    "//components/browser_actuator/internal/transport/test_support",
    "//mojo/public/cpp/system",
    "//net",
    "//net/traffic_annotation:test_support",
    "//services/network:test_support",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/fuzztest",
    "//url",
  ]

  visibility = [ "//components/browser_actuator/internal:unit_tests" ]
}
