#include "chrome/browser/preloading/prefetch/search_prefetch/streaming_search_prefetch_url_loader.h"
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/preloading/prefetch/search_prefetch/field_trial_settings.h"
#include "chrome/browser/preloading/prerender/prerender_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/buildflags/buildflags.h"
#include "mojo/public/c/system/data_pipe.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/features.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/url_loader_factory_builder.h"
#include "services/network/public/mojom/early_hints.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/api/web_request/web_request_api.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#endif
namespace {
bool CanServePrefetchRequest(const net::HttpResponseHeaders* const headers,
const mojo::ScopedDataPipeConsumerHandle& body) { … }
MojoResult CreateDataPipeForServingData(
mojo::ScopedDataPipeProducerHandle& producer_handle,
mojo::ScopedDataPipeConsumerHandle& consumer_handle) { … }
}
StreamingSearchPrefetchURLLoader::ResponseReader::ResponseReader(
mojo::PendingReceiver<network::mojom::URLLoader> forward_receiver,
mojo::PendingRemote<network::mojom::URLLoaderClient> forwarding_client,
base::OnceCallback<void(ResponseReader*)> forwarding_disconnection_callback,
std::optional<network::URLLoaderCompletionStatus> status,
scoped_refptr<StreamingSearchPrefetchURLLoader> loader)
: … { … }
StreamingSearchPrefetchURLLoader::ResponseReader::~ResponseReader() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::OnStatusCodeReady(
const network::URLLoaderCompletionStatus& status) { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::
StartReadingResponseFromData(
network::mojom::URLResponseHeadPtr& resource_response) { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::PushData() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::OnDataHandleReady(
MojoResult result,
const mojo::HandleSignalsState& state) { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::
MaybeSendCompletionSignal() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::
OnForwardingDisconnection() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::OnDestroyed() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::ReleaseSelfReference() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::FollowRedirect(
const std::vector<std::string>& removed_headers,
const net::HttpRequestHeaders& modified_headers,
const net::HttpRequestHeaders& modified_cors_exempt_headers,
const std::optional<GURL>& new_url) { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::SetPriority(
net::RequestPriority priority,
int32_t intra_priority_value) { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::
PauseReadingBodyFromNet() { … }
void StreamingSearchPrefetchURLLoader::ResponseReader::
ResumeReadingBodyFromNet() { … }
StreamingSearchPrefetchURLLoader::StreamingSearchPrefetchURLLoader(
SearchPrefetchRequest* streaming_prefetch_request,
Profile* profile,
bool navigation_prefetch,
std::unique_ptr<network::ResourceRequest> resource_request,
const net::NetworkTrafficAnnotationTag& network_traffic_annotation,
base::OnceCallback<void(bool)> report_error_callback)
: … { … }
StreamingSearchPrefetchURLLoader::~StreamingSearchPrefetchURLLoader() { … }
SearchPrefetchURLLoader::RequestHandler
StreamingSearchPrefetchURLLoader::GetCallbackForReadingViaResponseReader(
scoped_refptr<StreamingSearchPrefetchURLLoader> loader) { … }
SearchPrefetchURLLoader::RequestHandler
StreamingSearchPrefetchURLLoader::GetServingResponseHandler(
scoped_refptr<StreamingSearchPrefetchURLLoader> loader) { … }
void StreamingSearchPrefetchURLLoader::MarkPrefetchAsServable() { … }
void StreamingSearchPrefetchURLLoader::OnServableResponseCodeReceived() { … }
void StreamingSearchPrefetchURLLoader::RecordNavigationURLHistogram(
const GURL& navigation_url) { … }
void StreamingSearchPrefetchURLLoader::SetUpForwardingClient(
const network::ResourceRequest& resource_request,
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
mojo::PendingRemote<network::mojom::URLLoaderClient> forwarding_client) { … }
void StreamingSearchPrefetchURLLoader::CreateResponseReaderForPrerender(
const network::ResourceRequest& resource_request,
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
mojo::PendingRemote<network::mojom::URLLoaderClient> forwarding_client) { … }
void StreamingSearchPrefetchURLLoader::OnPrerenderForwardingDisconnect(
ResponseReader* reader) { … }
void StreamingSearchPrefetchURLLoader::OnReceiveEarlyHints(
network::mojom::EarlyHintsPtr early_hints) { … }
void StreamingSearchPrefetchURLLoader::OnReceiveResponse(
network::mojom::URLResponseHeadPtr head,
mojo::ScopedDataPipeConsumerHandle body,
std::optional<mojo_base::BigBuffer> cached_metadata) { … }
void StreamingSearchPrefetchURLLoader::OnReceiveRedirect(
const net::RedirectInfo& redirect_info,
network::mojom::URLResponseHeadPtr head) { … }
void StreamingSearchPrefetchURLLoader::OnUploadProgress(
int64_t current_position,
int64_t total_size,
OnUploadProgressCallback callback) { … }
void StreamingSearchPrefetchURLLoader::OnTransferSizeUpdated(
int32_t transfer_size_diff) { … }
void StreamingSearchPrefetchURLLoader::OnDataAvailable(
base::span<const uint8_t> data) { … }
void StreamingSearchPrefetchURLLoader::OnDataComplete() { … }
void StreamingSearchPrefetchURLLoader::OnStartLoadingResponseBodyFromData() { … }
void StreamingSearchPrefetchURLLoader::OnHandleReady(
MojoResult result,
const mojo::HandleSignalsState& state) { … }
std::string_view StreamingSearchPrefetchURLLoader::GetMoreDataFromCache(
size_t writing_position) const { … }
void StreamingSearchPrefetchURLLoader::PushData() { … }
void StreamingSearchPrefetchURLLoader::Finish() { … }
void StreamingSearchPrefetchURLLoader::OnComplete(
const network::URLLoaderCompletionStatus& status) { … }
void StreamingSearchPrefetchURLLoader::RunEventQueue() { … }
void StreamingSearchPrefetchURLLoader::FollowRedirect(
const std::vector<std::string>& removed_headers,
const net::HttpRequestHeaders& modified_headers,
const net::HttpRequestHeaders& modified_cors_exempt_headers,
const std::optional<GURL>& new_url) { … }
void StreamingSearchPrefetchURLLoader::SetPriority(
net::RequestPriority priority,
int32_t intra_priority_value) { … }
void StreamingSearchPrefetchURLLoader::PauseReadingBodyFromNet() { … }
void StreamingSearchPrefetchURLLoader::ResumeReadingBodyFromNet() { … }
void StreamingSearchPrefetchURLLoader::OnURLLoaderMojoDisconnect() { … }
void StreamingSearchPrefetchURLLoader::OnURLLoaderClientMojoDisconnect() { … }
void StreamingSearchPrefetchURLLoader::ClearOwnerPointer() { … }
void StreamingSearchPrefetchURLLoader::PostTaskToReleaseOwnership() { … }
void StreamingSearchPrefetchURLLoader::Fallback() { … }
void StreamingSearchPrefetchURLLoader::OnURLLoaderMojoDisconnectInFallback() { … }