#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/bindings/core/v8/script_streamer.h"
#include <memory>
#include <utility>
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/web_url_request_extra_data.h"
#include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h"
#include "third_party/blink/renderer/bindings/core/v8/script_evaluation_result.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_code_cache.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_compile_hints_consumer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_local_compile_hints_consumer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_local_compile_hints_producer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/script/classic_script.h"
#include "third_party/blink/renderer/core/script/mock_script_element_base.h"
#include "third_party/blink/renderer/core/testing/dummy_page_holder.h"
#include "third_party/blink/renderer/platform/exported/wrapped_resource_response.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata.h"
#include "third_party/blink/renderer/platform/loader/fetch/cross_origin_attribute_value.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader.h"
#include "third_party/blink/renderer/platform/loader/fetch/response_body_loader.h"
#include "third_party/blink/renderer/platform/loader/fetch/script_cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/loader/fetch/script_fetch_options.h"
#include "third_party/blink/renderer/platform/loader/fetch/url_loader/url_loader.h"
#include "third_party/blink/renderer/platform/loader/testing/mock_fetch_context.h"
#include "third_party/blink/renderer/platform/loader/testing/test_resource_fetcher_properties.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_pool.h"
#include "third_party/blink/renderer/platform/testing/mock_context_lifecycle_notifier.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_mock_scheduler.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_loader_mock_factory.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
#include "v8/include/v8.h"
namespace blink {
namespace {
class TestResourceClient final : public GarbageCollected<TestResourceClient>,
public ResourceClient { … };
class NoopLoaderFactory final : public ResourceFetcher::LoaderFactory { … };
void AppendDataToDataPipe(std::string_view data,
mojo::ScopedDataPipeProducerHandle& producer_handle) { … }
const uint32_t kDataPipeSize = …;
}
class ScriptStreamingTest : public testing::Test { … };
int ScriptStreamingTest::url_counter_ = …;
TEST_F(ScriptStreamingTest, CompilingStreamedScript) { … }
TEST_F(ScriptStreamingTest, CompilingStreamedScriptWithParseError) { … }
TEST_F(ScriptStreamingTest, CancellingStreaming) { … }
TEST_F(ScriptStreamingTest, DataAfterCancelling) { … }
TEST_F(ScriptStreamingTest, SuppressingStreaming) { … }
TEST_F(ScriptStreamingTest, ConsumeLocalCompileHints) { … }
TEST_F(ScriptStreamingTest, EmptyScripts) { … }
TEST_F(ScriptStreamingTest, SmallScripts) { … }
TEST_F(ScriptStreamingTest, ScriptsWithSmallFirstChunk) { … }
TEST_F(ScriptStreamingTest, EncodingChanges) { … }
TEST_F(ScriptStreamingTest, EncodingFromBOM) { … }
TEST_F(ScriptStreamingTest, GarbageCollectDuringStreaming) { … }
TEST_F(ScriptStreamingTest, ResourceSetRevalidatingRequest) { … }
class InlineScriptStreamingTest
: public ScriptStreamingTest,
public ::testing::WithParamInterface<
std::pair<bool ,
v8::ScriptCompiler::CompileOptions>> { … };
TEST_P(InlineScriptStreamingTest, InlineScript) { … }
TEST_F(ScriptStreamingTest, ProduceLocalCompileHintsForStreamedScript) { … }
TEST_F(ScriptStreamingTest, NullCacheHandler) { … }
INSTANTIATE_TEST_SUITE_P(…);
namespace {
const char kTooSmallScript[] = …;
const char kLargeEnoughScript[] = …;
const char kScriptWithBOM[] = …;
class DummyLoaderFactory final : public ResourceFetcher::LoaderFactory { … };
class DummyBackgroundResponseProcessorClient
: public BackgroundResponseProcessor::Client { … };
class DummyCachedMetadataSender : public CachedMetadataSender { … };
mojo_base::BigBuffer CreateDummyCodeCacheData() { … }
mojo_base::BigBuffer CreateDummyTimeStampData() { … }
network::mojom::URLResponseHeadPtr CreateURLResponseHead(
const std::string& content_type = "text/javascript") { … }
}
class BackgroundResourceScriptStreamerTest : public testing::Test { … };
int BackgroundResourceScriptStreamerTest::url_counter_ = …;
TEST_F(BackgroundResourceScriptStreamerTest, UnsupportedModuleMimeType) { … }
TEST_F(BackgroundResourceScriptStreamerTest, HasCodeCache) { … }
class BackgroundResourceScriptStreamerCodeCacheDecodeStartTest
: public BackgroundResourceScriptStreamerTest { … };
TEST_F(BackgroundResourceScriptStreamerCodeCacheDecodeStartTest, HasCodeCache) { … }
TEST_F(BackgroundResourceScriptStreamerTest, HasTimeStampData) { … }
TEST_F(BackgroundResourceScriptStreamerTest, InvalidCachedMetadata) { … }
TEST_F(BackgroundResourceScriptStreamerTest, SmallScript) { … }
TEST_F(BackgroundResourceScriptStreamerTest, SmallScriptInFirstChunk) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EmptyScript) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EmptyScriptSyncCheckable) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EnoughData) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EnoughDataInFirstChunk) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EnoughDataModuleScript) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EncodingNotSupported) { … }
TEST_F(BackgroundResourceScriptStreamerTest, EncodingFromBOM) { … }
TEST_F(BackgroundResourceScriptStreamerTest, ScriptTypeMismatch) { … }
TEST_F(BackgroundResourceScriptStreamerTest, CancelWhileWaitingForDataPipe) { … }
TEST_F(BackgroundResourceScriptStreamerTest, CancelBeforeReceiveResponse) { … }
TEST_F(BackgroundResourceScriptStreamerTest, CancelWhileRuningStreamingTask) { … }
TEST_F(BackgroundResourceScriptStreamerTest, CompilingStreamedScript) { … }
TEST_F(BackgroundResourceScriptStreamerTest,
CompilingStreamedScriptWithParseError) { … }
TEST_F(BackgroundResourceScriptStreamerTest, DataPipeReadableAfterGC) { … }
TEST_F(BackgroundResourceScriptStreamerTest,
DataPipeReadableAfterProcessorIsDeleted) { … }
TEST_F(BackgroundResourceScriptStreamerTest,
DeletingBackgroundProcessorWhileParsingShouldNotCrash) { … }
}