#include "content/browser/service_worker/service_worker_single_script_update_checker.h"
#include <utility>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "content/browser/loader/browser_initiated_resource_request.h"
#include "content/browser/service_worker/service_worker_cache_writer.h"
#include "content/browser/service_worker/service_worker_consts.h"
#include "content/browser/service_worker/service_worker_loader_helpers.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/url_loader_throttles.h"
#include "content/public/common/content_client.h"
#include "content/public/common/referrer.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/ip_endpoint.h"
#include "net/base/isolation_info.h"
#include "net/base/load_flags.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_response_info.h"
#include "services/network/public/cpp/net_adapters.h"
#include "services/network/public/cpp/record_ontransfersizeupdate_utils.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/early_hints.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.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/fetch/fetch_api_request.mojom.h"
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
namespace content {
namespace {
constexpr net::NetworkTrafficAnnotationTag kUpdateCheckTrafficAnnotation = …;
}
class ServiceWorkerSingleScriptUpdateChecker::WrappedIOBuffer
: public net::WrappedIOBuffer { … };
ServiceWorkerSingleScriptUpdateChecker::ServiceWorkerSingleScriptUpdateChecker(
const GURL& script_url,
bool is_main_script,
const GURL& main_script_url,
const GURL& scope,
bool force_bypass_cache,
blink::mojom::ScriptType worker_script_type,
blink::mojom::ServiceWorkerUpdateViaCache update_via_cache,
const blink::mojom::FetchClientSettingsObjectPtr&
fetch_client_settings_object,
base::TimeDelta time_since_last_check,
BrowserContext* browser_context,
scoped_refptr<network::SharedURLLoaderFactory> loader_factory,
mojo::Remote<storage::mojom::ServiceWorkerResourceReader> compare_reader,
mojo::Remote<storage::mojom::ServiceWorkerResourceReader> copy_reader,
mojo::Remote<storage::mojom::ServiceWorkerResourceWriter> writer,
int64_t writer_resource_id,
ScriptChecksumUpdateOption script_checksum_update_option,
const blink::StorageKey& storage_key,
ResultCallback callback)
: … { … }
ServiceWorkerSingleScriptUpdateChecker::
~ServiceWorkerSingleScriptUpdateChecker() = default;
void ServiceWorkerSingleScriptUpdateChecker::OnReceiveEarlyHints(
network::mojom::EarlyHintsPtr early_hints) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnReceiveResponse(
network::mojom::URLResponseHeadPtr response_head,
mojo::ScopedDataPipeConsumerHandle consumer,
std::optional<mojo_base::BigBuffer> cached_metadata) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnReceiveRedirect(
const net::RedirectInfo& redirect_info,
network::mojom::URLResponseHeadPtr response_head) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnUploadProgress(
int64_t current_position,
int64_t total_size,
OnUploadProgressCallback ack_callback) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnTransferSizeUpdated(
int32_t transfer_size_diff) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnComplete(
const network::URLLoaderCompletionStatus& status) { … }
const char* ServiceWorkerSingleScriptUpdateChecker::ResultToString(
Result result) { … }
void ServiceWorkerSingleScriptUpdateChecker::WriteHeaders(
network::mojom::URLResponseHeadPtr response_head) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnWriteHeadersComplete(
net::Error error) { … }
void ServiceWorkerSingleScriptUpdateChecker::
MaybeStartNetworkConsumerHandleWatcher() { … }
void ServiceWorkerSingleScriptUpdateChecker::OnNetworkDataAvailable(
MojoResult,
const mojo::HandleSignalsState& state) { … }
void ServiceWorkerSingleScriptUpdateChecker::CompareData(
scoped_refptr<network::MojoToNetPendingBuffer> pending_buffer,
uint32_t bytes_to_compare) { … }
void ServiceWorkerSingleScriptUpdateChecker::OnCompareDataComplete(
scoped_refptr<network::MojoToNetPendingBuffer> pending_buffer,
uint32_t bytes_written,
net::Error error) { … }
void ServiceWorkerSingleScriptUpdateChecker::Fail(
blink::ServiceWorkerStatusCode status,
const std::string& error_message,
network::URLLoaderCompletionStatus network_status) { … }
void ServiceWorkerSingleScriptUpdateChecker::Succeed(
Result result,
std::unique_ptr<PausedState> paused_state) { … }
void ServiceWorkerSingleScriptUpdateChecker::Finish(
Result result,
std::unique_ptr<PausedState> paused_state,
std::unique_ptr<FailureInfo> failure_info,
const std::optional<std::string>& sha256_checksum) { … }
ServiceWorkerSingleScriptUpdateChecker::PausedState::PausedState(
std::unique_ptr<ServiceWorkerCacheWriter> cache_writer,
std::unique_ptr<blink::ThrottlingURLLoader> network_loader,
mojo::Remote<network::mojom::URLLoaderClient> network_client_remote,
mojo::PendingReceiver<network::mojom::URLLoaderClient>
network_client_receiver,
scoped_refptr<network::MojoToNetPendingBuffer> pending_network_buffer,
uint32_t consumed_bytes,
ServiceWorkerUpdatedScriptLoader::LoaderState network_loader_state,
ServiceWorkerUpdatedScriptLoader::WriterState body_writer_state)
: … { … }
ServiceWorkerSingleScriptUpdateChecker::PausedState::~PausedState() = default;
ServiceWorkerSingleScriptUpdateChecker::FailureInfo::FailureInfo(
blink::ServiceWorkerStatusCode status,
const std::string& error_message,
network::URLLoaderCompletionStatus network_status)
: … { … }
ServiceWorkerSingleScriptUpdateChecker::FailureInfo::~FailureInfo() = default;
}