#include "content/browser/loader/navigation_early_hints_manager.h"
#include "base/memory/raw_ref.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/url_loader_throttles.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/referrer.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/system/data_pipe_drainer.h"
#include "net/base/load_flags.h"
#include "net/base/schemeful_site.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request_job.h"
#include "services/network/public/cpp/content_security_policy/content_security_policy.h"
#include "services/network/public/cpp/content_security_policy/csp_source_list.h"
#include "services/network/public/cpp/record_ontransfersizeupdate_utils.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/content_security_policy.mojom-shared.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/common/loader/network_utils.h"
#include "third_party/blink/public/common/loader/throttling_url_loader.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
#include "url/origin.h"
namespace content {
namespace {
const net::NetworkTrafficAnnotationTag kEarlyHintsPreloadTrafficAnnotation = …;
network::mojom::CSPDirectiveName LinkAsAttributeToCSPDirective(
network::mojom::LinkAsAttribute attr) { … }
bool CheckContentSecurityPolicyForPreload(
const network::mojom::LinkHeaderPtr& link,
const std::vector<network::mojom::ContentSecurityPolicyPtr>&
content_security_policies) { … }
std::optional<network::mojom::RequestDestination>
LinkAsAttributeToRequestDestination(const network::mojom::LinkHeaderPtr& link) { … }
network::mojom::RequestMode CalculateRequestMode(
const network::mojom::LinkHeaderPtr& link) { … }
network::mojom::CredentialsMode CalculateCredentialsMode(
const network::mojom::LinkHeaderPtr& link) { … }
}
NavigationEarlyHintsManagerParams::NavigationEarlyHintsManagerParams(
const url::Origin& origin,
net::IsolationInfo isolation_info,
mojo::Remote<network::mojom::URLLoaderFactory> loader_factory)
: … { … }
NavigationEarlyHintsManagerParams::~NavigationEarlyHintsManagerParams() =
default;
NavigationEarlyHintsManagerParams::NavigationEarlyHintsManagerParams(
NavigationEarlyHintsManagerParams&&) = default;
NavigationEarlyHintsManagerParams& NavigationEarlyHintsManagerParams::operator=(
NavigationEarlyHintsManagerParams&&) = default;
struct NavigationEarlyHintsManager::PreconnectEntry { … };
NavigationEarlyHintsManager::PreconnectEntry::PreconnectEntry(
const url::Origin& origin,
network::mojom::CrossOriginAttribute cross_origin)
: … { … }
NavigationEarlyHintsManager::PreconnectEntry::~PreconnectEntry() = default;
NavigationEarlyHintsManager::PreconnectEntry::PreconnectEntry(
const PreconnectEntry&) = default;
NavigationEarlyHintsManager::PreconnectEntry&
NavigationEarlyHintsManager::PreconnectEntry::operator=(
const PreconnectEntry&) = default;
bool NavigationEarlyHintsManager::PreconnectEntry::operator==(
const PreconnectEntry& other) { … }
bool NavigationEarlyHintsManager::PreconnectEntry::operator<(
const PreconnectEntry& other) const { … }
NavigationEarlyHintsManager::PreloadedResource::PreloadedResource() = default;
NavigationEarlyHintsManager::PreloadedResource::~PreloadedResource() = default;
NavigationEarlyHintsManager::PreloadedResource::PreloadedResource(
const PreloadedResource&) = default;
NavigationEarlyHintsManager::PreloadedResource&
NavigationEarlyHintsManager::PreloadedResource::operator=(
const PreloadedResource&) = default;
NavigationEarlyHintsManager::InflightPreload::InflightPreload(
std::unique_ptr<blink::ThrottlingURLLoader> loader,
std::unique_ptr<PreloadURLLoaderClient> client)
: … { … }
NavigationEarlyHintsManager::InflightPreload::~InflightPreload() = default;
class NavigationEarlyHintsManager::PreloadURLLoaderClient
: public network::mojom::URLLoaderClient,
public mojo::DataPipeDrainer::Client { … };
NavigationEarlyHintsManager::NavigationEarlyHintsManager(
BrowserContext& browser_context,
StoragePartition& storage_partition,
int frame_tree_node_id,
NavigationEarlyHintsManagerParams params)
: … { … }
NavigationEarlyHintsManager::~NavigationEarlyHintsManager() = default;
void NavigationEarlyHintsManager::HandleEarlyHints(
network::mojom::EarlyHintsPtr early_hints,
const network::ResourceRequest& request_for_navigation) { … }
bool NavigationEarlyHintsManager::WasResourceHintsReceived() const { … }
std::vector<GURL> NavigationEarlyHintsManager::TakePreloadedResourceURLs() { … }
bool NavigationEarlyHintsManager::HasInflightPreloads() const { … }
void NavigationEarlyHintsManager::WaitForPreloadsFinishedForTesting(
base::OnceCallback<void(PreloadedResources)> callback) { … }
void NavigationEarlyHintsManager::SetNetworkContextForTesting(
network::mojom::NetworkContext* network_context) { … }
network::mojom::NetworkContext*
NavigationEarlyHintsManager::GetNetworkContext() { … }
void NavigationEarlyHintsManager::MaybePreconnect(
const network::mojom::LinkHeaderPtr& link) { … }
void NavigationEarlyHintsManager::MaybePreloadHintedResource(
const network::mojom::LinkHeaderPtr& link,
const network::ResourceRequest& request_for_navigation,
const std::vector<network::mojom::ContentSecurityPolicyPtr>&
content_security_policies,
net::ReferrerPolicy referrer_policy) { … }
bool NavigationEarlyHintsManager::ShouldHandleResourceHints(
const network::mojom::LinkHeaderPtr& link) { … }
void NavigationEarlyHintsManager::OnPreloadComplete(
const GURL& url,
const PreloadedResource& result) { … }
net::RequestPriority NavigationEarlyHintsManager::CalculateRequestPriority(
const network::mojom::LinkHeaderPtr& link) { … }
}