# Copyright (c) 2013 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/config/crypto.gni")
import("//build/config/features.gni")
import("//build/config/ui.gni")
import("//tools/grit/grit_rule.gni")
import("//url/config.gni")

if (is_android) {
  import("//build/config/android/config.gni")
  import("//build/config/android/rules.gni")
} else if (is_mac) {
  import("//build/config/mac/mac_sdk.gni")
}

# The list of net files is kept in net.gypi. Read it.
gypi_values = exec_script(
    "//build/gypi_to_gn.py",
    [ rebase_path("net.gypi") ],
    "scope",
    [ "net.gypi" ])

# Disable Kerberos on ChromeOS, Android and iOS, at least for now. It needs
# configuration (krb5.conf and so on).
use_kerberos = !is_chromeos && !is_android && !is_ios

# The way the cache uses mmap() is inefficient on some Android devices. If
# this flag is set, we hackily avoid using mmap() in the disk cache. We are
# pretty confident that mmap-ing the index would not hurt any existing x86
# android devices, but we cannot be so sure about the variety of ARM devices.
# So enable it for x86 only for now.
posix_avoid_mmap = is_android && cpu_arch != "x86"

# WebSockets and socket stream code are used everywhere except iOS.
enable_websockets = !is_ios
use_v8_in_net = !is_ios
enable_built_in_dns = !is_ios
disable_ftp_support = is_ios

declare_args() {
  # Disables support for file URLs.  File URL support requires use of icu.
  disable_file_support = false
}

config("net_config") {
  defines = []
  if (posix_avoid_mmap) {
    defines += [ "POSIX_AVOID_MMAP" ]
  }
  if (disable_file_support) {
    defines += [ "DISABLE_FILE_SUPPORT" ]
  }
}

# Disables Windows warning about size to int truncations.
# TODO(jschuh): crbug.com/167187 fix this and delete this config.
config("net_win_size_truncation") {
  if (is_win) {
    cflags = [ "/wd4267" ]
  }
}

component("net") {
  sources =
    gypi_values.net_nacl_common_sources +
    gypi_values.net_non_nacl_sources

  cflags = []
  defines = [
    # TODO(GYP) Note that he GYP file supports linux_link_kerberos (defaults to
    # 0) which implies that we run pkg_config on kerberos and link to that
    # rather than setting this define which will dynamically open it. That
    # doesn't seem to be set in the regular builds, so we're skipping this
    # capability here.
    "DLOPEN_KERBEROS",
    "NET_IMPLEMENTATION"
  ]
  configs += [ ":net_win_size_truncation" ]
  direct_dependent_configs = [ ":net_config" ]
  include_dirs = []

  deps = [
   ":net_resources",
    "//base",
    "//base:i18n",
    "//base/third_party/dynamic_annotations",
    "//crypto",
    "//crypto:platform",
    "//net/base/registry_controlled_domains",
    "//sdch",
    "//third_party/icu",
    "//third_party/zlib",
    "//url",
  ]
  forward_dependent_configs_from = [
    "//crypto",
    "//crypto:platform"
  ]

  if (use_kerberos) {
    defines += [ "USE_KERBEROS" ]
    if (is_android) {
      include_dirs += [ "/usr/include/kerberosV" ]
    }
  } else {
    sources -= [
      "http/http_auth_gssapi_posix.cc",
      "http/http_auth_gssapi_posix.h",
      "http/http_auth_handler_negotiate.h",
      "http/http_auth_handler_negotiate.cc",
    ]
  }

  if (is_posix) {
    if (posix_avoid_mmap) {
      sources -= [ "disk_cache/blockfile/mapped_file_posix.cc" ]
    } else {
      sources -= [ "disk_cache/blockfile/mapped_file_avoid_mmap_posix.cc" ]
    }
  }

  if (disable_file_support) {
    sources -= [
      "base/directory_lister.cc",
      "base/directory_lister.h",
      "url_request/url_request_file_dir_job.cc",
      "url_request/url_request_file_dir_job.h",
      "url_request/url_request_file_job.cc",
      "url_request/url_request_file_job.h",
      "url_request/file_protocol_handler.cc",
      "url_request/file_protocol_handler.h",
    ]
  }

  if (disable_ftp_support) {
    sources -= [
      "ftp/ftp_auth_cache.cc",
      "ftp/ftp_auth_cache.h",
      "ftp/ftp_ctrl_response_buffer.cc",
      "ftp/ftp_ctrl_response_buffer.h",
      "ftp/ftp_directory_listing_parser.cc",
      "ftp/ftp_directory_listing_parser.h",
      "ftp/ftp_directory_listing_parser_ls.cc",
      "ftp/ftp_directory_listing_parser_ls.h",
      "ftp/ftp_directory_listing_parser_netware.cc",
      "ftp/ftp_directory_listing_parser_netware.h",
      "ftp/ftp_directory_listing_parser_os2.cc",
      "ftp/ftp_directory_listing_parser_os2.h",
      "ftp/ftp_directory_listing_parser_vms.cc",
      "ftp/ftp_directory_listing_parser_vms.h",
      "ftp/ftp_directory_listing_parser_windows.cc",
      "ftp/ftp_directory_listing_parser_windows.h",
      "ftp/ftp_network_layer.cc",
      "ftp/ftp_network_layer.h",
      "ftp/ftp_network_session.cc",
      "ftp/ftp_network_session.h",
      "ftp/ftp_network_transaction.cc",
      "ftp/ftp_network_transaction.h",
      "ftp/ftp_request_info.h",
      "ftp/ftp_response_info.cc",
      "ftp/ftp_response_info.h",
      "ftp/ftp_server_type_histograms.cc",
      "ftp/ftp_server_type_histograms.h",
      "ftp/ftp_transaction.h",
      "ftp/ftp_transaction_factory.h",
      "ftp/ftp_util.cc",
      "ftp/ftp_util.h",
      "url_request/ftp_protocol_handler.cc",
      "url_request/ftp_protocol_handler.h",
      "url_request/url_request_ftp_job.cc",
      "url_request/url_request_ftp_job.h",
    ]
  }

  if (enable_built_in_dns) {
    defines += [ "ENABLE_BUILT_IN_DNS" ]
  } else {
    sources -= [
      "dns/address_sorter_posix.cc",
      "dns/address_sorter_posix.h",
      "dns/dns_client.cc",
    ]
  }

  if (use_openssl) {
    sources -= [
      "base/nss_memio.c",
      "base/nss_memio.h",
      "cert/ct_log_verifier_nss.cc",
      "cert/ct_objects_extractor_nss.cc",
      "cert/jwk_serializer_nss.cc",
      "cert/scoped_nss_types.h",
      "cert/x509_util_nss.cc",
      "cert/x509_util_nss.h",
      "quic/crypto/aead_base_decrypter_nss.cc",
      "quic/crypto/aead_base_encrypter_nss.cc",
      "quic/crypto/aes_128_gcm_12_decrypter_nss.cc",
      "quic/crypto/aes_128_gcm_12_encrypter_nss.cc",
      "quic/crypto/chacha20_poly1305_decrypter_nss.cc",
      "quic/crypto/chacha20_poly1305_encrypter_nss.cc",
      "quic/crypto/channel_id_nss.cc",
      "quic/crypto/p256_key_exchange_nss.cc",
      "socket/nss_ssl_util.cc",
      "socket/nss_ssl_util.h",
      "socket/ssl_client_socket_nss.cc",
      "socket/ssl_client_socket_nss.h",
      "socket/ssl_server_socket_nss.cc",
      "socket/ssl_server_socket_nss.h",
    ]
    if (is_chromeos) {
      sources -= [
        "cert/nss_cert_database_chromeos.cc",
        "cert/nss_cert_database_chromeos.h",
        "cert/nss_profile_filter_chromeos.cc",
        "cert/nss_profile_filter_chromeos.h",
      ]
    }
    if (is_linux) {
      # These are always removed for non-Linux cases below.
      sources -= [
        "base/crypto_module_nss.cc",
        "base/keygen_handler_nss.cc",
        "cert/cert_database_nss.cc",
        "cert/nss_cert_database.cc",
        "cert/nss_cert_database.h",
        "cert/x509_certificate_nss.cc",
        "third_party/mozilla_security_manager/nsKeygenHandler.cpp",
        "third_party/mozilla_security_manager/nsKeygenHandler.h",
        "third_party/mozilla_security_manager/nsNSSCertificateDB.cpp",
        "third_party/mozilla_security_manager/nsNSSCertificateDB.h",
        "third_party/mozilla_security_manager/nsPKCS12Blob.cpp",
        "third_party/mozilla_security_manager/nsPKCS12Blob.h",
      ]
    }
    if (is_ios) {
      # Always removed for !ios below.
      sources -= [
        "cert/cert_verify_proc_nss.cc",
        "cert/cert_verify_proc_nss.h",
        "cert/test_root_certs_nss.cc",
        "ocsp/nss_ocsp.cc",
        "ocsp/nss_ocsp.h",
      ]
    }
  } else {
    sources -= [
      "base/crypto_module_openssl.cc",
      "base/keygen_handler_openssl.cc",
      "cert/ct_log_verifier_openssl.cc",
      "cert/ct_objects_extractor_openssl.cc",
      "cert/jwk_serializer_openssl.cc",
      "cert/x509_util_openssl.cc",
      "cert/x509_util_openssl.h",
      "quic/crypto/aead_base_decrypter_openssl.cc",
      "quic/crypto/aead_base_encrypter_openssl.cc",
      "quic/crypto/aes_128_gcm_12_decrypter_openssl.cc",
      "quic/crypto/aes_128_gcm_12_encrypter_openssl.cc",
      "quic/crypto/chacha20_poly1305_decrypter_openssl.cc",
      "quic/crypto/chacha20_poly1305_encrypter_openssl.cc",
      "quic/crypto/channel_id_openssl.cc",
      "quic/crypto/p256_key_exchange_openssl.cc",
      "quic/crypto/scoped_evp_aead_ctx.cc",
      "quic/crypto/scoped_evp_aead_ctx.h",
      "socket/openssl_ssl_util.cc",
      "socket/openssl_ssl_util.h",
      "socket/ssl_client_socket_openssl.cc",
      "socket/ssl_client_socket_openssl.h",
      "socket/ssl_server_socket_openssl.cc",
      "socket/ssl_server_socket_openssl.h",
      "socket/ssl_session_cache_openssl.cc",
      "socket/ssl_session_cache_openssl.h",
    ]
  }

  if (!use_openssl_certs) {
    sources -= [
      "base/openssl_private_key_store.h",
      "base/openssl_private_key_store_memory.cc",
      "cert/cert_database_openssl.cc",
      "cert/cert_verify_proc_openssl.cc",
      "cert/cert_verify_proc_openssl.h",
      "cert/test_root_certs_openssl.cc",
      "cert/x509_certificate_openssl.cc",
      "ssl/openssl_client_key_store.cc",
      "ssl/openssl_client_key_store.h",
    ]
    if (is_android) {
      sources -= [
        "base/openssl_private_key_store_android.cc",
      ]
    }
  } else if (is_android) {
    # Android doesn't use these even when using OpenSSL.
    sources -= [
      "base/openssl_private_key_store_memory.cc",
      "cert/cert_database_openssl.cc",
      "cert/cert_verify_proc_openssl.cc",
      "cert/test_root_certs_openssl.cc",
    ]
  }

  if (use_glib) {
    configs += [ "//build/config/linux:gconf" ]
    deps += [ "//build/config/linux:gio" ]
  }

  if (is_linux) {
    configs += [ "//build/config/linux:libresolv" ]
  } else {
    sources -= [
      "base/crypto_module_nss.cc",
      "base/keygen_handler_nss.cc",
      "cert/cert_database_nss.cc",
      "cert/nss_cert_database.cc",
      "cert/nss_cert_database.h",
      "cert/x509_certificate_nss.cc",
      "third_party/mozilla_security_manager/nsKeygenHandler.cpp",
      "third_party/mozilla_security_manager/nsKeygenHandler.h",
      "third_party/mozilla_security_manager/nsNSSCertificateDB.cpp",
      "third_party/mozilla_security_manager/nsNSSCertificateDB.h",
      "third_party/mozilla_security_manager/nsPKCS12Blob.cpp",
      "third_party/mozilla_security_manager/nsPKCS12Blob.h",
    ]

    if (!is_ios) {
      # These files are part of the partial implementation of NSS on iOS so
      # keep them in that case.
      sources -= [
        "cert/test_root_certs_nss.cc",
        "ocsp/nss_ocsp.cc",
        "ocsp/nss_ocsp.h",
      ]
    }
  }

  if (!use_nss_certs) {
    sources -= [
      "ssl/client_cert_store_nss.cc",
      "ssl/client_cert_store_nss.h",
    ]
    if (!is_ios) {
      # These files are part of the partial implementation of NSS on iOS so
      # keep them in that case (even though use_nss_certs is not set).
      sources -= [
        "cert/cert_verify_proc_nss.cc",
        "cert/cert_verify_proc_nss.h",
      ]
    }
    if (is_chromeos) {
      # These were already removed on non-ChromeOS.
      sources -= [
        "ssl/client_cert_store_chromeos.cc",
        "ssl/client_cert_store_chromeos.h",
      ]
    }
  }

  if (!enable_websockets) {
    sources -= [
      "socket_stream/socket_stream.cc",
      "socket_stream/socket_stream.h",
      "socket_stream/socket_stream_job.cc",
      "socket_stream/socket_stream_job.h",
      "socket_stream/socket_stream_job_manager.cc",
      "socket_stream/socket_stream_job_manager.h",
      "socket_stream/socket_stream_metrics.cc",
      "socket_stream/socket_stream_metrics.h",
      "spdy/spdy_websocket_stream.cc",
      "spdy/spdy_websocket_stream.h",
      "websockets/websocket_basic_handshake_stream.cc",
      "websockets/websocket_basic_handshake_stream.h",
      "websockets/websocket_basic_stream.cc",
      "websockets/websocket_basic_stream.h",
      "websockets/websocket_channel.cc",
      "websockets/websocket_channel.h",
      "websockets/websocket_deflate_predictor.h",
      "websockets/websocket_deflate_predictor_impl.cc",
      "websockets/websocket_deflate_predictor_impl.h",
      "websockets/websocket_deflate_stream.cc",
      "websockets/websocket_deflate_stream.h",
      "websockets/websocket_deflater.cc",
      "websockets/websocket_deflater.h",
      "websockets/websocket_errors.cc",
      "websockets/websocket_errors.h",
      "websockets/websocket_extension.cc",
      "websockets/websocket_extension.h",
      "websockets/websocket_extension_parser.cc",
      "websockets/websocket_extension_parser.h",
      "websockets/websocket_frame.cc",
      "websockets/websocket_frame.h",
      "websockets/websocket_frame_parser.cc",
      "websockets/websocket_frame_parser.h",
      "websockets/websocket_handshake_constants.cc",
      "websockets/websocket_handshake_constants.h",
      "websockets/websocket_handshake_handler.cc",
      "websockets/websocket_handshake_handler.h",
      "websockets/websocket_handshake_request_info.cc",
      "websockets/websocket_handshake_request_info.h",
      "websockets/websocket_handshake_response_info.cc",
      "websockets/websocket_handshake_response_info.h",
      "websockets/websocket_handshake_stream_base.h",
      "websockets/websocket_handshake_stream_create_helper.cc",
      "websockets/websocket_handshake_stream_create_helper.h",
      "websockets/websocket_inflater.cc",
      "websockets/websocket_inflater.h",
      "websockets/websocket_job.cc",
      "websockets/websocket_job.h",
      "websockets/websocket_mux.h",
      "websockets/websocket_net_log_params.cc",
      "websockets/websocket_net_log_params.h",
      "websockets/websocket_stream.cc",
      "websockets/websocket_stream.h",
      "websockets/websocket_throttle.cc",
      "websockets/websocket_throttle.h",
    ]
  }

  if (!enable_mdns) {
    sources -= [
      "dns/mdns_cache.cc",
      "dns/mdns_cache.h",
      "dns/mdns_client.cc",
      "dns/mdns_client.h",
      "dns/mdns_client_impl.cc",
      "dns/mdns_client_impl.h",
      "dns/record_parsed.cc",
      "dns/record_parsed.h",
      "dns/record_rdata.cc",
      "dns/record_rdata.h",
    ]
  }

  if (is_win) {
    sources -= [
      "http/http_auth_handler_ntlm_portable.cc",
      "socket/tcp_socket_libevent.cc",
      "socket/tcp_socket_libevent.h",
      "udp/udp_socket_libevent.cc",
      "udp/udp_socket_libevent.h",
    ]
    deps += [
      "//third_party/nss:nspr",
      "//third_party/nss",
    ]
  } else {  # !is_win
    sources -= [
      "base/winsock_init.cc",
      "base/winsock_init.h",
      "base/winsock_util.cc",
      "base/winsock_util.h",
      "proxy/proxy_resolver_winhttp.cc",
      "proxy/proxy_resolver_winhttp.h",
    ]
  }

  if (is_mac) {
    deps += [
      "//third_party/nss:nspr",
      "//third_party/nss",
    ]
    libs = [
      "Foundation.framework",
      "Security.framework",
      "SystemConfiguration.framework",
      "resolv",
    ]
  }

  if (is_ios) {
    # Add back some sources that were otherwise filtered out. iOS additionally
    # doesn't set USE_NSS but needs some of the files.
    set_sources_assignment_filter([])
    sources += [
      "base/network_change_notifier_mac.cc",
      "base/network_config_watcher_mac.cc",
      "base/platform_mime_util_mac.mm",
      "cert/cert_verify_proc_nss.cc",
      "cert/cert_verify_proc_nss.h",
      "cert/test_root_certs_nss.cc",
      "cert/x509_util_nss.cc",
      "cert/x509_util_nss.h",
      "proxy/proxy_resolver_mac.cc",
      "proxy/proxy_server_mac.cc",
      "ocsp/nss_ocsp.cc",
      "ocsp/nss_ocsp.h",
    ]
    set_sources_assignment_filter(sources_assignment_filter)

    sources -= [ "disk_cache/blockfile/file_posix.cc" ]
    deps += [
      "//third_party/nss",
    ]
    libs = [
      "CFNetwork.framework",
      "MobileCoreServices.framework",
      "Security.framework",
      "SystemConfiguration.framework",
      "resolv",
    ]
  }

  if (is_android) {
    # Add some Linux sources that were excluded by the filter, but which
    # are needed.
    set_sources_assignment_filter([])
    sources += [
      "base/platform_mime_util_linux.cc",
      "base/address_tracker_linux.cc",
      "base/address_tracker_linux.h",
    ]
    set_sources_assignment_filter(sources_assignment_filter)

    if (!is_android_webview_build) {
      deps += [ ":net_jni_headers" ]

      # The net/android/keystore_openssl.cc source file needs to access an
      # OpenSSL-internal header.
      include_dirs = [ "//third_party/openssl" ]
    }
  }
  
  if (use_icu_alternatives_on_android) {
    deps -= [
      "//base:i18n",
      "//third_party/icu",
    ]
    sources -= [
      "base/filename_util_icu.cc",
      "base/net_string_util_icu.cc",
      "base/net_util_icu.cc",
    ]
    sources += [
      "base/net_string_util_icu_alternatives_android.cc",
      "base/net_string_util_icu_alternatives_android.h",
    ]
  }
}

grit("net_resources") {
  source = "base/net_resources.grd"
}

static_library("http_server") {
  sources = [
    "server/http_connection.cc",
    "server/http_connection.h",
    "server/http_server.cc",
    "server/http_server.h",
    "server/http_server_request_info.cc",
    "server/http_server_request_info.h",
    "server/http_server_response_info.cc",
    "server/http_server_response_info.h",
    "server/web_socket.cc",
    "server/web_socket.h",
  ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
  deps = [
    ":net",
    "//base",
  ]
}

executable("dump_cache") {
  sources = [
    "tools/dump_cache/cache_dumper.cc",
    "tools/dump_cache/cache_dumper.h",
    "tools/dump_cache/dump_cache.cc",
    "tools/dump_cache/dump_files.cc",
    "tools/dump_cache/dump_files.h",
    "tools/dump_cache/simple_cache_dumper.cc",
    "tools/dump_cache/simple_cache_dumper.h",
    "tools/dump_cache/upgrade_win.cc",
    "tools/dump_cache/upgrade_win.h",
    "tools/dump_cache/url_to_filename_encoder.cc",
    "tools/dump_cache/url_to_filename_encoder.h",
    "tools/dump_cache/url_utilities.h",
    "tools/dump_cache/url_utilities.cc",
  ]

  configs += [ ":net_win_size_truncation" ]

  deps = [
    "//base",
    ":net",
    ":test_support",
  ]
}

source_set("test_support") {
  sources = [
    "base/capturing_net_log.cc",
    "base/capturing_net_log.h",
    "base/load_timing_info_test_util.cc",
    "base/load_timing_info_test_util.h",
    "base/mock_file_stream.cc",
    "base/mock_file_stream.h",
    "base/test_completion_callback.cc",
    "base/test_completion_callback.h",
    "base/test_data_directory.cc",
    "base/test_data_directory.h",
    "cert/mock_cert_verifier.cc",
    "cert/mock_cert_verifier.h",
    "cookies/cookie_monster_store_test.cc",
    "cookies/cookie_monster_store_test.h",
    "cookies/cookie_store_test_callbacks.cc",
    "cookies/cookie_store_test_callbacks.h",
    "cookies/cookie_store_test_helpers.cc",
    "cookies/cookie_store_test_helpers.h",
    "disk_cache/disk_cache_test_base.cc",
    "disk_cache/disk_cache_test_base.h",
    "disk_cache/disk_cache_test_util.cc",
    "disk_cache/disk_cache_test_util.h",
    "dns/dns_test_util.cc",
    "dns/dns_test_util.h",
    "dns/mock_host_resolver.cc",
    "dns/mock_host_resolver.h",
    "dns/mock_mdns_socket_factory.cc",
    "dns/mock_mdns_socket_factory.h",
    "http/http_transaction_test_util.cc",
    "http/http_transaction_test_util.h",
    "proxy/mock_proxy_resolver.cc",
    "proxy/mock_proxy_resolver.h",
    "proxy/mock_proxy_script_fetcher.cc",
    "proxy/mock_proxy_script_fetcher.h",
    "proxy/proxy_config_service_common_unittest.cc",
    "proxy/proxy_config_service_common_unittest.h",
    "socket/socket_test_util.cc",
    "socket/socket_test_util.h",
    "test/cert_test_util.cc",
    "test/cert_test_util.h",
    "test/ct_test_util.cc",
    "test/ct_test_util.h",
    "test/embedded_test_server/embedded_test_server.cc",
    "test/embedded_test_server/embedded_test_server.h",
    "test/embedded_test_server/http_connection.cc",
    "test/embedded_test_server/http_connection.h",
    "test/embedded_test_server/http_request.cc",
    "test/embedded_test_server/http_request.h",
    "test/embedded_test_server/http_response.cc",
    "test/embedded_test_server/http_response.h",
    "test/net_test_suite.cc",
    "test/net_test_suite.h",
    "test/python_utils.cc",
    "test/python_utils.h",
    "test/spawned_test_server/base_test_server.cc",
    "test/spawned_test_server/base_test_server.h",
    "test/spawned_test_server/local_test_server_posix.cc",
    "test/spawned_test_server/local_test_server_win.cc",
    "test/spawned_test_server/local_test_server.cc",
    "test/spawned_test_server/local_test_server.h",
    "test/spawned_test_server/remote_test_server.cc",
    "test/spawned_test_server/remote_test_server.h",
    "test/spawned_test_server/spawned_test_server.h",
    "test/spawned_test_server/spawner_communicator.cc",
    "test/spawned_test_server/spawner_communicator.h",
    "url_request/test_url_fetcher_factory.cc",
    "url_request/test_url_fetcher_factory.h",
    "url_request/url_request_test_util.cc",
    "url_request/url_request_test_util.h",
  ]

  configs += [ ":net_win_size_truncation" ]

  deps = [
    "//base",
    "//base/test:test_support",
    "//crypto:platform",
    "//net",
    "//net/tools/tld_cleanup",
    "//testing/gmock",
    "//testing/gtest",
    "//url",
  ]

  if (is_ios) {
    deps += [ "//third_party/nss" ]
  }

  if (!is_android) {
    sources -= [
      "test/spawned_test_server/remote_test_server.cc",
      "test/spawned_test_server/remote_test_server.h",
      "test/spawned_test_server/spawner_communicator.cc",
      "test/spawned_test_server/spawner_communicator.h",
    ]
  }

  if (use_v8_in_net) {
    deps += [ ":net_with_v8" ]
  }

  if (!enable_mdns) {
    sources -= [
      "dns/mock_mdns_socket_factory.cc",
      "dns/mock_mdns_socket_factory.h",
    ]
  }

  forward_dependent_configs_from = deps
}

if (use_v8_in_net) {
  component("net_with_v8") {
    sources = [
      "proxy/proxy_resolver_v8.cc",
      "proxy/proxy_resolver_v8.h",
      "proxy/proxy_resolver_v8_tracing.cc",
      "proxy/proxy_resolver_v8_tracing.h",
      "proxy/proxy_service_v8.cc",
      "proxy/proxy_service_v8.h",
    ]

    defines = [ "NET_IMPLEMENTATION" ]
    configs += [
      ":net_win_size_truncation",
      "//build/config/compiler:wexit_time_destructors",
    ]

    deps = [
      ":net",
      "//base",
      "//gin",
      "//url",
      "//v8",
    ]
  }
}

if (!is_ios && !is_android) {
  executable("crash_cache") {
    sources = [ "tools/crash_cache/crash_cache.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      ":net",
      ":test_support",
      "//base",
    ]
  }

  executable("crl_set_dump") {
    sources = [ "tools/crl_set_dump/crl_set_dump.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      ":net",
      "//base",
    ]
  }

  executable("dns_fuzz_stub") {
    sources = [ "tools/dns_fuzz_stub/dns_fuzz_stub.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      ":net",
      "//base",
    ]
  }

  executable("gdig") {
    sources = [
      "tools/gdig/file_net_log.cc",
      "tools/gdig/gdig.cc",
    ]
    deps = [
      ":net",
      "//base",
    ]
  }

  executable("get_server_time") {
    sources = [ "tools/get_server_time/get_server_time.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      ":net",
      "//base",
      "//base:i18n",
      "//url",
    ]
  }

  if (use_v8_in_net) {
    executable("net_watcher") {
      sources = [ "tools/net_watcher/net_watcher.cc" ]
      deps = [
        ":net",
        ":net_with_v8",
        "//base",
      ]

      if (is_linux) {
        configs += [ "//build/config/linux:gconf" ]
        deps += [ "//build/config/linux:gio" ]
      }
    }
  }

  executable("run_testserver") {
    sources = [ "tools/testserver/run_testserver.cc" ]
    deps = [
      ":net",  # TODO(brettw) bug 363749: this shouldn't be necessary. It's not
               # in the GYP build, and can be removed when the bug is fixed.
      ":test_support",
      "//base",
      "//base/test:test_support",
      "//testing/gtest",
    ]
  }

  executable("stress_cache") {
    sources = [ "disk_cache/blockfile/stress_cache.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      ":net",
      ":test_support",
      "//base",
    ]
  }

  executable("tld_cleanup") {
    sources = [ "tools/tld_cleanup/tld_cleanup.cc" ]
    configs += [ ":net_win_size_truncation" ]
    deps = [
      "//base",
      "//base:i18n",
      "//net/tools/tld_cleanup",
    ]
  }
}

if (is_linux) {
  source_set("balsa") {
    sources = [
      "tools/balsa/balsa_enums.h",
      "tools/balsa/balsa_frame.cc",
      "tools/balsa/balsa_frame.h",
      "tools/balsa/balsa_headers.cc",
      "tools/balsa/balsa_headers.h",
      "tools/balsa/balsa_headers_token_utils.cc",
      "tools/balsa/balsa_headers_token_utils.h",
      "tools/balsa/balsa_visitor_interface.h",
      "tools/balsa/http_message_constants.cc",
      "tools/balsa/http_message_constants.h",
      "tools/balsa/noop_balsa_visitor.h",
      "tools/balsa/simple_buffer.cc",
      "tools/balsa/simple_buffer.h",
      "tools/balsa/split.cc",
      "tools/balsa/split.h",
      "tools/balsa/string_piece_utils.h",
    ]
    deps = [
      ":net",
      "//base",
    ]
  }

  static_library("epoll_server") {
    sources = [
      "tools/epoll_server/epoll_server.cc",
      "tools/epoll_server/epoll_server.h",
    ]
    deps = [
      ":net",
      "//base",
    ]
  }

  static_library("flip_in_mem_edsm_server_base") {
    sources = [
      "tools/dump_cache/url_to_filename_encoder.cc",
      "tools/dump_cache/url_to_filename_encoder.h",
      "tools/dump_cache/url_utilities.h",
      "tools/dump_cache/url_utilities.cc",
      "tools/flip_server/acceptor_thread.h",
      "tools/flip_server/acceptor_thread.cc",
      "tools/flip_server/create_listener.cc",
      "tools/flip_server/create_listener.h",
      "tools/flip_server/constants.h",
      "tools/flip_server/flip_config.cc",
      "tools/flip_server/flip_config.h",
      "tools/flip_server/http_interface.cc",
      "tools/flip_server/http_interface.h",
      "tools/flip_server/loadtime_measurement.h",
      "tools/flip_server/mem_cache.h",
      "tools/flip_server/mem_cache.cc",
      "tools/flip_server/output_ordering.cc",
      "tools/flip_server/output_ordering.h",
      "tools/flip_server/ring_buffer.cc",
      "tools/flip_server/ring_buffer.h",
      "tools/flip_server/sm_connection.cc",
      "tools/flip_server/sm_connection.h",
      "tools/flip_server/sm_interface.h",
      "tools/flip_server/spdy_ssl.cc",
      "tools/flip_server/spdy_ssl.h",
      "tools/flip_server/spdy_interface.cc",
      "tools/flip_server/spdy_interface.h",
      "tools/flip_server/spdy_util.cc",
      "tools/flip_server/spdy_util.h",
      "tools/flip_server/streamer_interface.cc",
      "tools/flip_server/streamer_interface.h",
    ]
    deps = [
      ":balsa",
      ":epoll_server",
      ":net",
      "//base",
      "//third_party/openssl",
    ]
  }

  executable("flip_in_mem_edsm_server_unittests") {
    sources = [
      "tools/flip_server/flip_test_utils.cc",
      "tools/flip_server/flip_test_utils.h",
      "tools/flip_server/http_interface_test.cc",
      "tools/flip_server/mem_cache_test.cc",
      "tools/flip_server/run_all_tests.cc",
      "tools/flip_server/spdy_interface_test.cc",
    ]
    deps = [
      ":flip_in_mem_edsm_server_base",
      ":net",
      ":test_support",
      "//testing/gtest",
      "//testing/gmock",
      "//third_party/openssl",
    ]
  }

  executable("flip_in_mem_edsm_server") {
    sources = [ "tools/flip_server/flip_in_mem_edsm_server.cc" ]
    deps = [
      ":flip_in_mem_edsm_server_base",
      ":net",
      "//base",
    ]
  }

  source_set("quic_base") {
    sources = [
      "tools/quic/quic_client.cc",
      "tools/quic/quic_client.h",
      "tools/quic/quic_client_session.cc",
      "tools/quic/quic_client_session.h",
      "tools/quic/quic_default_packet_writer.cc",
      "tools/quic/quic_default_packet_writer.h",
      "tools/quic/quic_dispatcher.h",
      "tools/quic/quic_dispatcher.cc",
      "tools/quic/quic_epoll_clock.cc",
      "tools/quic/quic_epoll_clock.h",
      "tools/quic/quic_epoll_connection_helper.cc",
      "tools/quic/quic_epoll_connection_helper.h",
      "tools/quic/quic_in_memory_cache.cc",
      "tools/quic/quic_in_memory_cache.h",
      "tools/quic/quic_packet_writer_wrapper.cc",
      "tools/quic/quic_packet_writer_wrapper.h",
      "tools/quic/quic_server.cc",
      "tools/quic/quic_server.h",
      "tools/quic/quic_server_session.cc",
      "tools/quic/quic_server_session.h",
      "tools/quic/quic_socket_utils.cc",
      "tools/quic/quic_socket_utils.h",
      "tools/quic/quic_spdy_client_stream.cc",
      "tools/quic/quic_spdy_client_stream.h",
      "tools/quic/quic_spdy_server_stream.cc",
      "tools/quic/quic_spdy_server_stream.h",
      "tools/quic/quic_time_wait_list_manager.h",
      "tools/quic/quic_time_wait_list_manager.cc",
      "tools/quic/spdy_utils.cc",
      "tools/quic/spdy_utils.h",
    ]
    deps = [
      ":balsa",
      ":epoll_server",
      ":net",
      "//base",
      "//base/third_party/dynamic_annotations",
      "//crypto",
      "//third_party/openssl",
      "//url",
    ]
  }

  executable("quic_client") {
    sources = [ "tools/quic/quic_client_bin.cc" ]
    deps = [
      ":quic_base",
      ":net",
      "//base",
      "//third_party/openssl",
    ]
  }

  executable("quic_server") {
    sources = [ "tools/quic/quic_server_bin.cc" ]
    deps = [
      ":quic_base",
      ":net",
      "//base",
      "//third_party/openssl",
    ]
  }
}

if (is_android) {
  generate_jni("net_jni_headers") {
    sources = [
      "android/java/src/org/chromium/net/AndroidCertVerifyResult.java",
      "android/java/src/org/chromium/net/AndroidKeyStore.java",
      "android/java/src/org/chromium/net/AndroidNetworkLibrary.java",
      "android/java/src/org/chromium/net/AndroidPrivateKey.java",
      "android/java/src/org/chromium/net/GURLUtils.java",
      "android/java/src/org/chromium/net/NetworkChangeNotifier.java",
      "android/java/src/org/chromium/net/ProxyChangeListener.java",
      "android/java/src/org/chromium/net/X509Util.java",
    ]
    jni_package = "net"
  }
}

if (is_android || is_linux) {
  executable("disk_cache_memory_test") {
    sources = [ "tools/disk_cache_memory_test/disk_cache_memory_test.cc" ]
    deps = [
      ":net",
      "//base",
    ]
  }
}

# TODO(GYP) make this compile on Android, we need some native test deps done.
if (!is_android) {

test("net_unittests") {
  sources = gypi_values.net_test_sources

  configs += [ ":net_win_size_truncation" ]
  defines = []

  deps = [
    ":http_server",
    ":net",
    ":test_support",
    "//base",
    "//base:i18n",
    "//base/allocator",
    "//base/third_party/dynamic_annotations",
    "//crypto",
    "//crypto:platform",
    "//net/base/registry_controlled_domains",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/zlib",
    "//url",
  ]

  if (is_linux) {
    sources += gypi_values.net_linux_test_sources
    deps += [
      ":balsa",
      ":epoll_server",
      ":flip_in_mem_edsm_server_base",
      ":quic_base",
    ]
  }

  if (is_chromeos) {
    sources -= [
      "base/network_change_notifier_linux_unittest.cc",
      "proxy/proxy_config_service_linux_unittest.cc",
    ]
  }

  if (is_android) {
    sources -= [
      # See bug http://crbug.com/344533.
      "disk_cache/blockfile/index_table_v3_unittest.cc",
      # No res_ninit() et al on Android, so this doesn't make a lot of
      # sense.
      "dns/dns_config_service_posix_unittest.cc",
    ]
    deps += [
      ":net_javatests",  # FIXME(brettw)
      ":net_test_jni_headers",
    ]
  }

  if (!use_nss_certs) {
    sources -= [
      "ssl/client_cert_store_nss_unittest.cc",
    ]
    if (is_chromeos) {  # Already removed for all non-ChromeOS builds.
      sources -= [
        "ssl/client_cert_store_chromeos_unittest.cc",
      ]
    }
  }

  if (use_openssl) {
    # When building for OpenSSL, we need to exclude NSS specific tests
    # or functionality not supported by OpenSSL yet.
    # TODO(bulach): Add equivalent tests when the underlying
    #               functionality is ported to OpenSSL.
    sources -= [
      "cert/ct_objects_extractor_unittest.cc",
      "cert/multi_log_ct_verifier_unittest.cc",
      "cert/nss_cert_database_unittest.cc",
      "cert/nss_cert_database_chromeos_unittest.cc",
      "cert/nss_profile_filter_chromeos_unittest.cc",
      "cert/x509_util_nss_unittest.cc",
      "quic/test_tools/crypto_test_utils_nss.cc",
    ]
  } else {
    sources -= [
      "cert/x509_util_openssl_unittest.cc",
      "quic/test_tools/crypto_test_utils_openssl.cc",
      "socket/ssl_client_socket_openssl_unittest.cc",
      "socket/ssl_session_cache_openssl_unittest.cc",
    ]
    if (!is_desktop_linux && !is_chromeos) {
      sources -= [ "cert/nss_cert_database_unittest.cc" ]
    }
  }

  if (use_kerberos) {
    defines += [ "USE_KERBEROS" ]
  } else {
    sources -= [
      "http/http_auth_gssapi_posix_unittest.cc",
      "http/http_auth_handler_negotiate_unittest.cc",
      "http/mock_gssapi_library_posix.cc",
      "http/mock_gssapi_library_posix.h",
    ]
  }

  if (use_openssl || (!is_desktop_linux && !is_chromeos && !is_ios)) {
    # Only include this test when on Posix and using NSS for
    # cert verification or on iOS (which also uses NSS for certs).
    sources -= [ "ocsp/nss_ocsp_unittest.cc" ]
  }

  if (!use_openssl_certs) {
    sources -= [ "ssl/openssl_client_key_store_unittest.cc" ]
  }

  if (!enable_websockets) {
    sources -= [
      "socket_stream/socket_stream_metrics_unittest.cc",
      "socket_stream/socket_stream_unittest.cc",
      "spdy/spdy_websocket_stream_unittest.cc",
      "websockets/websocket_basic_stream_test.cc",
      "websockets/websocket_channel_test.cc",
      "websockets/websocket_deflate_predictor_impl_test.cc",
      "websockets/websocket_deflate_stream_test.cc",
      "websockets/websocket_deflater_test.cc",
      "websockets/websocket_errors_test.cc",
      "websockets/websocket_extension_parser_test.cc",
      "websockets/websocket_frame_parser_test.cc",
      "websockets/websocket_frame_test.cc",
      "websockets/websocket_handshake_handler_spdy_test.cc",
      "websockets/websocket_handshake_handler_test.cc",
      "websockets/websocket_handshake_stream_create_helper_test.cc",
      "websockets/websocket_inflater_test.cc",
      "websockets/websocket_job_test.cc",
      "websockets/websocket_net_log_params_test.cc",
      "websockets/websocket_stream_test.cc",
      "websockets/websocket_test_util.cc",
      "websockets/websocket_test_util.h",
      "websockets/websocket_throttle_test.cc",
    ]
  }

  if (disable_file_support) {
    sources -= [
      "base/directory_lister_unittest.cc",
      "url_request/url_request_file_job_unittest.cc",
    ]
  }

  if (disable_ftp_support) {
    sources -= [
      "ftp/ftp_auth_cache_unittest.cc",
      "ftp/ftp_ctrl_response_buffer_unittest.cc",
      "ftp/ftp_directory_listing_parser_ls_unittest.cc",
      "ftp/ftp_directory_listing_parser_netware_unittest.cc",
      "ftp/ftp_directory_listing_parser_os2_unittest.cc",
      "ftp/ftp_directory_listing_parser_unittest.cc",
      "ftp/ftp_directory_listing_parser_unittest.h",
      "ftp/ftp_directory_listing_parser_vms_unittest.cc",
      "ftp/ftp_directory_listing_parser_windows_unittest.cc",
      "ftp/ftp_network_transaction_unittest.cc",
      "ftp/ftp_util_unittest.cc",
      "url_request/url_request_ftp_job_unittest.cc",
    ]
  }

  if (!enable_built_in_dns) {
    sources -= [
      "dns/address_sorter_posix_unittest.cc",
      "dns/address_sorter_unittest.cc",
    ]
  }

  # Always need use_v8_in_net to be 1 to run on Android, so just remove
  # net_unittest's dependency on v8 when using icu alternatives instead of
  # setting use_v8_in_net to 0.
  if (use_v8_in_net && !use_icu_alternatives_on_android) {
    deps += [ ":net_with_v8" ]
  } else {
    sources -= [
      "proxy/proxy_resolver_v8_unittest.cc",
      "proxy/proxy_resolver_v8_tracing_unittest.cc",
    ]
  }

  if (!enable_mdns) {
    sources -= [
      "dns/mdns_cache_unittest.cc",
      "dns/mdns_client_unittest.cc",
      "dns/record_parsed_unittest.cc",
      "dns/record_rdata_unittest.cc",
    ]
  }

  if (is_ios) {
    # TODO(GYP)
    #  'actions': [
    #    {
    #      'action_name': 'copy_test_data',
    #      'variables': {
    #        'test_data_files': [
    #          'data/ssl/certificates/',
    #          'data/test.html',
    #          'data/url_request_unittest/',
    #        ],
    #        'test_data_prefix': 'net',
    #      },
    #      'includes': [ '../build/copy_test_data_ios.gypi' ],
    #    },
    #  ],
    sources -= [
      # TODO(droger): The following tests are disabled because the
      # implementation is missing or incomplete.
      # KeygenHandler::GenKeyAndSignChallenge() is not ported to iOS.
      "base/keygen_handler_unittest.cc",
      "disk_cache/backend_unittest.cc",
      "disk_cache/blockfile/block_files_unittest.cc",
      # Need to read input data files.
      "filter/gzip_filter_unittest.cc",
      "socket/ssl_server_socket_unittest.cc",
      "spdy/fuzzing/hpack_fuzz_util_test.cc",
      # Need TestServer.
      "proxy/proxy_script_fetcher_impl_unittest.cc",
      "socket/ssl_client_socket_unittest.cc",
      "url_request/url_fetcher_impl_unittest.cc",
      "url_request/url_request_context_builder_unittest.cc",
      # Needs GetAppOutput().
      "test/python_utils_unittest.cc",

      # The following tests are disabled because they don't apply to
      # iOS.
      # OS is not "linux" or "freebsd" or "openbsd".
      "socket/unix_domain_socket_posix_unittest.cc",

      # See bug http://crbug.com/344533.
      "disk_cache/blockfile/index_table_v3_unittest.cc",
    ]
  }

  if (is_android) {
    sources -= [
      "dns/dns_config_service_posix_unittest.cc",
    ]
    # TODO(GYP)
    # # TODO(mmenke):  This depends on test_support_base, which depends on
    # #                icu.  Figure out a way to remove that dependency.
    # 'dependencies': [
    #   '../testing/android/native_test.gyp:native_test_native_code',
    # ]

    set_sources_assignment_filter([])
    sources += [ "base/address_tracker_linux_unittest.cc" ]
    set_sources_assignment_filter(sources_assignment_filter)
  }

  if (use_icu_alternatives_on_android) {
    sources -= [
      "base/filename_util_unittest.cc",
      "base/net_util_icu_unittest.cc",
    ]
    deps -= [ "//base:i18n" ]
  }
}

}  # !is_android