#include "chrome/browser/media/router/discovery/dial/dial_service_impl.h"
#include <stdint.h>
#include <algorithm>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/media/router/discovery/dial/dial_device_data.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "net/base/address_family.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_interfaces.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "net/log/net_log.h"
#include "net/log/net_log_source.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#endif
Time;
BrowserThread;
HttpResponseHeaders;
HttpUtil;
IOBufferWithSize;
IPAddress;
NetworkInterface;
NetworkInterfaceList;
StringIOBuffer;
UDPSocket;
namespace media_router {
#if !BUILDFLAG(IS_CHROMEOS_ASH)
void PostSendNetworkList(
base::WeakPtr<DialServiceImpl> impl,
scoped_refptr<base::SequencedTaskRunner> task_runner,
const std::optional<net::NetworkInterfaceList>& networks) { … }
#endif
namespace {
const int kDialMaxRequests = …;
const int kDialRequestIntervalMillis = …;
const int kDialMaxResponseDelaySecs = …;
const int kDialResponseTimeoutSecs = …;
const char kDialRequestAddress[] = …;
const uint16_t kDialRequestPort = …;
const char kDialSearchType[] = …;
const char kSsdpLocationHeader[] = …;
const char kSsdpCacheControlHeader[] = …;
const char kSsdpConfigIdHeader[] = …;
const char kSsdpUsnHeader[] = …;
constexpr char kSsdpMaxAgeDirective[] = …;
constexpr int kSsdpMaxMaxAge = …;
constexpr int kSsdpMaxConfigId = …;
const int kDialRecvBufferSize = …;
bool GetHeader(HttpResponseHeaders* headers,
const char* name,
std::string* value) { … }
std::string BuildRequest() { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
void InsertBestBindAddressChromeOS(const ash::NetworkTypePattern& type,
net::IPAddressList* bind_address_list) {
const ash::NetworkState* state = ash::NetworkHandler::Get()
->network_state_handler()
->ConnectedNetworkByType(type);
if (!state)
return;
std::string state_ip_address = state->GetIpAddress();
IPAddress bind_ip_address;
if (bind_ip_address.AssignFromIPLiteral(state_ip_address) &&
bind_ip_address.IsIPv4()) {
bind_address_list->push_back(bind_ip_address);
}
}
net::IPAddressList GetBestBindAddressOnUIThread() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
net::IPAddressList bind_address_list;
if (ash::NetworkHandler::IsInitialized()) {
InsertBestBindAddressChromeOS(ash::NetworkTypePattern::Ethernet(),
&bind_address_list);
InsertBestBindAddressChromeOS(ash::NetworkTypePattern::WiFi(),
&bind_address_list);
}
return bind_address_list;
}
#else
void GetNetworkListOnUIThread(
base::WeakPtr<DialServiceImpl> impl,
scoped_refptr<base::SequencedTaskRunner> task_runner) { … }
#endif
}
DialServiceImpl::DialSocket::DialSocket(DialServiceImpl* dial_service)
: … { … }
DialServiceImpl::DialSocket::~DialSocket() { … }
bool DialServiceImpl::DialSocket::CreateAndBindSocket(
const IPAddress& bind_ip_address,
net::NetLog* net_log) { … }
void DialServiceImpl::DialSocket::SendOneRequest(
const net::IPEndPoint& send_address,
const scoped_refptr<net::StringIOBuffer>& send_buffer) { … }
bool DialServiceImpl::DialSocket::IsClosed() { … }
bool DialServiceImpl::DialSocket::CheckResult(const char* operation,
int result) { … }
void DialServiceImpl::DialSocket::Close() { … }
void DialServiceImpl::DialSocket::OnSocketWrite(int send_buffer_size,
int result) { … }
bool DialServiceImpl::DialSocket::ReadSocket() { … }
void DialServiceImpl::DialSocket::OnSocketRead(int result) { … }
void DialServiceImpl::DialSocket::HandleResponse(int bytes_read) { … }
bool DialServiceImpl::DialSocket::ParseResponse(const std::string& response,
const base::Time& response_time,
DialDeviceData* device) { … }
DialServiceImpl::DialServiceImpl(
DialService::Client& client,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
net::NetLog* net_log)
: … { … }
DialServiceImpl::~DialServiceImpl() { … }
bool DialServiceImpl::Discover() { … }
void DialServiceImpl::StartDiscovery() { … }
void DialServiceImpl::SendNetworkList(
const std::optional<NetworkInterfaceList>& networks) { … }
void DialServiceImpl::DiscoverOnAddresses(
const net::IPAddressList& ip_addresses) { … }
void DialServiceImpl::BindAndAddSocket(const IPAddress& bind_ip_address) { … }
std::unique_ptr<DialServiceImpl::DialSocket>
DialServiceImpl::CreateDialSocket() { … }
void DialServiceImpl::SendOneRequest() { … }
void DialServiceImpl::NotifyOnDiscoveryRequest() { … }
void DialServiceImpl::NotifyOnDeviceDiscovered(
const DialDeviceData& device_data) { … }
void DialServiceImpl::NotifyOnError() { … }
void DialServiceImpl::FinishDiscovery() { … }
bool DialServiceImpl::HasOpenSockets() { … }
}