#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "content/browser/service_worker/service_worker_test_utils.h"
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/barrier_closure.h"
#include "base/containers/span.h"
#include "base/memory/raw_ref.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_consts.h"
#include "content/browser/service_worker/service_worker_container_host.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/common/frame.mojom.h"
#include "content/common/frame_messages.mojom.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/common/alternative_error_page_override_info.mojom.h"
#include "content/public/test/policy_container_utils.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_response_info.h"
#include "net/http/http_util.h"
#include "third_party/blink/public/common/loader/throttling_url_loader.h"
#include "third_party/blink/public/common/navigation/navigation_params.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/back_forward_cache_not_restored_reasons.mojom-blink.h"
#include "third_party/blink/public/mojom/loader/referrer.mojom.h"
#include "third_party/blink/public/mojom/loader/transferrable_url_loader.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
namespace content {
namespace {
mojom::DidCommitProvisionalLoadParamsPtr
MinimalDidCommitNavigationLoadParams() { … }
class FakeNavigationClient : public mojom::NavigationClient { … };
class ResourceWriter { … };
void OnWriteToDiskCacheFinished(
std::unique_ptr<ResourceWriter> self_owned_writer,
WriteToDiskCacheCallback callback,
storage::mojom::ServiceWorkerResourceRecordPtr record) { … }
}
CommittedServiceWorkerClient::CommittedServiceWorkerClient(
CommittedServiceWorkerClient&& other) = default;
CommittedServiceWorkerClient::~CommittedServiceWorkerClient() = default;
CommittedServiceWorkerClient::CommittedServiceWorkerClient(
ScopedServiceWorkerClient service_worker_client,
const GlobalRenderFrameHostId& render_frame_host_id)
: … { … }
CommittedServiceWorkerClient::CommittedServiceWorkerClient(
ScopedServiceWorkerClient service_worker_client)
: … { … }
ServiceWorkerContainerHost& CommittedServiceWorkerClient::container_host()
const { … }
base::OnceCallback<void(blink::ServiceWorkerStatusCode)>
ReceiveServiceWorkerStatus(std::optional<blink::ServiceWorkerStatusCode>* out,
base::OnceClosure quit_closure) { … }
blink::ServiceWorkerStatusCode WarmUpServiceWorker(
ServiceWorkerVersion* version) { … }
bool WarmUpServiceWorker(ServiceWorkerContext& service_worker_context,
const GURL& url) { … }
blink::ServiceWorkerStatusCode StartServiceWorker(
ServiceWorkerVersion* version) { … }
void StopServiceWorker(ServiceWorkerVersion* version) { … }
ScopedServiceWorkerClient CreateServiceWorkerClient(
ServiceWorkerContextCore* context,
bool are_ancestors_secure,
int frame_tree_node_id) { … }
ScopedServiceWorkerClient CreateServiceWorkerClient(
ServiceWorkerContextCore* context,
const GURL& document_url,
const url::Origin& top_frame_origin,
bool are_ancestors_secure,
int frame_tree_node_id) { … }
ScopedServiceWorkerClient CreateServiceWorkerClient(
ServiceWorkerContextCore* context,
const GURL& document_url,
bool are_ancestors_secure,
int frame_tree_node_id) { … }
std::unique_ptr<ServiceWorkerHost> CreateServiceWorkerHost(
int process_id,
bool is_parent_frame_secure,
ServiceWorkerVersion& hosted_version,
base::WeakPtr<ServiceWorkerContextCore> context) { … }
scoped_refptr<ServiceWorkerRegistration> CreateNewServiceWorkerRegistration(
ServiceWorkerRegistry* registry,
const blink::mojom::ServiceWorkerRegistrationOptions& options,
const blink::StorageKey& key) { … }
scoped_refptr<ServiceWorkerVersion> CreateNewServiceWorkerVersion(
ServiceWorkerRegistry* registry,
scoped_refptr<ServiceWorkerRegistration> registration,
const GURL& script_url,
blink::mojom::ScriptType script_type) { … }
scoped_refptr<ServiceWorkerRegistration>
CreateServiceWorkerRegistrationAndVersion(ServiceWorkerContextCore* context,
const GURL& scope,
const GURL& script,
const blink::StorageKey& key,
int64_t resource_id) { … }
storage::mojom::ServiceWorkerResourceRecordPtr WriteToDiskCacheWithIdSync(
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const GURL& script_url,
int64_t resource_id,
const std::vector<std::pair<std::string, std::string>>& headers,
const std::string& body,
const std::string& meta_data) { … }
storage::mojom::ServiceWorkerResourceRecordPtr WriteToDiskCacheSync(
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const GURL& script_url,
const std::vector<std::pair<std::string, std::string>>& headers,
const std::string& body,
const std::string& meta_data) { … }
void WriteToDiskCacheAsync(
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const GURL& script_url,
const std::vector<std::pair<std::string, std::string>>& headers,
const std::string& body,
const std::string& meta_data,
WriteToDiskCacheCallback callback) { … }
int64_t GetNewResourceIdSync(
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage) { … }
MockServiceWorkerResourceReader::MockServiceWorkerResourceReader() = default;
MockServiceWorkerResourceReader::~MockServiceWorkerResourceReader() = default;
mojo::PendingRemote<storage::mojom::ServiceWorkerResourceReader>
MockServiceWorkerResourceReader::BindNewPipeAndPassRemote(
base::OnceClosure disconnect_handler) { … }
void MockServiceWorkerResourceReader::ReadResponseHead(
storage::mojom::ServiceWorkerResourceReader::ReadResponseHeadCallback
callback) { … }
void MockServiceWorkerResourceReader::PrepareReadData(
int64_t,
PrepareReadDataCallback callback) { … }
void MockServiceWorkerResourceReader::ReadData(ReadDataCallback callback) { … }
void MockServiceWorkerResourceReader::ExpectReadResponseHead(size_t len,
int result) { … }
void MockServiceWorkerResourceReader::ExpectReadResponseHeadOk(size_t len) { … }
void MockServiceWorkerResourceReader::ExpectReadData(const char* data,
size_t len,
int result) { … }
void MockServiceWorkerResourceReader::ExpectReadDataOk(
const std::string& data) { … }
void MockServiceWorkerResourceReader::ExpectReadOk(
const std::vector<std::string>& stored_data,
const size_t bytes_stored) { … }
void MockServiceWorkerResourceReader::CompletePendingRead() { … }
MockServiceWorkerResourceWriter::MockServiceWorkerResourceWriter() = default;
MockServiceWorkerResourceWriter::~MockServiceWorkerResourceWriter() = default;
mojo::PendingRemote<storage::mojom::ServiceWorkerResourceWriter>
MockServiceWorkerResourceWriter::BindNewPipeAndPassRemote(
base::OnceClosure disconnect_handler) { … }
void MockServiceWorkerResourceWriter::WriteResponseHead(
network::mojom::URLResponseHeadPtr response_head,
WriteResponseHeadCallback callback) { … }
void MockServiceWorkerResourceWriter::WriteData(mojo_base::BigBuffer data,
WriteDataCallback callback) { … }
void MockServiceWorkerResourceWriter::ExpectWriteResponseHeadOk(size_t length) { … }
void MockServiceWorkerResourceWriter::ExpectWriteDataOk(size_t length) { … }
void MockServiceWorkerResourceWriter::ExpectWriteResponseHead(size_t length,
int result) { … }
void MockServiceWorkerResourceWriter::ExpectWriteData(size_t length,
int result) { … }
void MockServiceWorkerResourceWriter::CompletePendingWrite() { … }
ServiceWorkerUpdateCheckTestUtils::ServiceWorkerUpdateCheckTestUtils() =
default;
ServiceWorkerUpdateCheckTestUtils::~ServiceWorkerUpdateCheckTestUtils() =
default;
std::unique_ptr<ServiceWorkerCacheWriter>
ServiceWorkerUpdateCheckTestUtils::CreatePausedCacheWriter(
EmbeddedWorkerTestHelper* worker_test_helper,
size_t bytes_compared,
const std::string& new_headers,
scoped_refptr<network::MojoToNetPendingBuffer> pending_network_buffer,
uint32_t consumed_size,
int64_t old_resource_id,
int64_t new_resource_id) { … }
std::unique_ptr<ServiceWorkerSingleScriptUpdateChecker::PausedState>
ServiceWorkerUpdateCheckTestUtils::CreateUpdateCheckerPausedState(
std::unique_ptr<ServiceWorkerCacheWriter> cache_writer,
ServiceWorkerUpdatedScriptLoader::LoaderState network_loader_state,
ServiceWorkerUpdatedScriptLoader::WriterState body_writer_state,
scoped_refptr<network::MojoToNetPendingBuffer> pending_network_buffer,
uint32_t consumed_size) { … }
void ServiceWorkerUpdateCheckTestUtils::SetComparedScriptInfoForVersion(
const GURL& script_url,
int64_t resource_id,
ServiceWorkerSingleScriptUpdateChecker::Result compare_result,
std::unique_ptr<ServiceWorkerSingleScriptUpdateChecker::PausedState>
paused_state,
ServiceWorkerVersion* version) { … }
void ServiceWorkerUpdateCheckTestUtils::
CreateAndSetComparedScriptInfoForVersion(
const GURL& script_url,
size_t bytes_compared,
const std::string& new_headers,
const std::string& diff_data_block,
int64_t old_resource_id,
int64_t new_resource_id,
EmbeddedWorkerTestHelper* worker_test_helper,
ServiceWorkerUpdatedScriptLoader::LoaderState network_loader_state,
ServiceWorkerUpdatedScriptLoader::WriterState body_writer_state,
ServiceWorkerSingleScriptUpdateChecker::Result compare_result,
ServiceWorkerVersion* version,
mojo::ScopedDataPipeProducerHandle* out_body_handle) { … }
bool ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse(
int64_t resource_id,
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const std::string& expected_body) { … }
void ReadDataPipeInternal(mojo::DataPipeConsumerHandle handle,
std::string* result,
base::OnceClosure quit_closure) { … }
std::string ReadDataPipe(mojo::ScopedDataPipeConsumerHandle handle) { … }
}