#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "content/web_test/browser/web_test_control_host.h"
#include <stddef.h>
#include <string.h>
#include <iostream>
#include <memory>
#include <queue>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/barrier_closure.h"
#include "base/base64.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "cc/paint/skia_paint_canvas.h"
#include "components/custom_handlers/protocol_handler_registry.h"
#include "components/custom_handlers/simple_protocol_handler_registry_factory.h"
#include "content/browser/aggregation_service/aggregation_service.h"
#include "content/browser/attribution_reporting/attribution_manager.h"
#include "content/browser/in_memory_federated_permission_context.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/child_process_termination_info.h"
#include "content/public/browser/client_hints_controller_delegate.h"
#include "content/public/browser/content_index_context.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/blink_test_browser_support.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_browser_context.h"
#include "content/shell/browser/shell_content_browser_client.h"
#include "content/shell/browser/shell_content_index_provider.h"
#include "content/shell/browser/shell_devtools_frontend.h"
#include "content/test/mock_platform_notification_service.h"
#include "content/test/storage_partition_test_helpers.h"
#include "content/web_test/browser/devtools_protocol_test_bindings.h"
#include "content/web_test/browser/fake_bluetooth_chooser.h"
#include "content/web_test/browser/test_info_extractor.h"
#include "content/web_test/browser/web_test_bluetooth_chooser_factory.h"
#include "content/web_test/browser/web_test_browser_context.h"
#include "content/web_test/browser/web_test_content_browser_client.h"
#include "content/web_test/browser/web_test_devtools_bindings.h"
#include "content/web_test/browser/web_test_first_device_bluetooth_chooser.h"
#include "content/web_test/browser/web_test_permission_manager.h"
#include "content/web_test/browser/web_test_pressure_manager.h"
#include "content/web_test/common/web_test_constants.h"
#include "content/web_test/common/web_test_string_util.h"
#include "content/web_test/common/web_test_switches.h"
#include "ipc/ipc_channel_proxy.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "services/device/public/cpp/compute_pressure/buildflags.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/clear_data_filter.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "storage/browser/database/database_tracker.h"
#include "storage/browser/file_system/isolated_context.h"
#include "storage/browser/quota/quota_manager.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/page_state/page_state.h"
#include "third_party/blink/public/common/page_state/page_state_serialization.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/blink/public/common/unique_name/unique_name_helper.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_dialog_factory.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "url/gurl.h"
#include "url/url_constants.h"
#if BUILDFLAG(IS_MAC)
#include "base/apple/foundation_util.h"
#endif
namespace content {
namespace {
const char kAboutBlankResetWebTest[] = …;
std::string DumpFrameState(const blink::ExplodedFrameState& frame_state,
size_t indent,
bool is_current_index) { … }
std::string DumpNavigationEntry(NavigationEntry* navigation_entry,
bool is_current_index) { … }
std::string DumpHistoryForWebContents(WebContents* web_contents) { … }
std::vector<std::string> DumpTitleWasSet(WebContents* web_contents) { … }
std::string DumpFailLoad(WebContents* web_contents,
RenderFrameHost* render_frame_host) { … }
void DrawSelectionRect(const SkBitmap& bitmap, const gfx::Rect& wr) { … }
void ApplyWebTestDefaultPreferences(blink::web_pref::WebPreferences* prefs) { … }
}
WebTestResultPrinter::WebTestResultPrinter(std::ostream* output,
std::ostream* error)
: … { … }
void WebTestResultPrinter::StartStateDump() { … }
void WebTestResultPrinter::PrintTextHeader() { … }
void WebTestResultPrinter::PrintTextBlock(const std::string& block) { … }
void WebTestResultPrinter::PrintTextFooter() { … }
void WebTestResultPrinter::PrintImageHeader(const std::string& actual_hash,
const std::string& expected_hash) { … }
void WebTestResultPrinter::PrintImageBlock(
const std::vector<unsigned char>& png_image) { … }
void WebTestResultPrinter::PrintImageFooter() { … }
void WebTestResultPrinter::PrintAudioHeader() { … }
void WebTestResultPrinter::PrintAudioBlock(
const std::vector<unsigned char>& audio_data) { … }
void WebTestResultPrinter::PrintAudioFooter() { … }
void WebTestResultPrinter::AddMessageToStderr(const std::string& message) { … }
void WebTestResultPrinter::AddMessage(const std::string& message) { … }
void WebTestResultPrinter::AddMessageRaw(const std::string& message) { … }
void WebTestResultPrinter::AddErrorMessage(const std::string& message) { … }
void WebTestResultPrinter::PrintEncodedBinaryData(
const std::vector<unsigned char>& data) { … }
void WebTestResultPrinter::CloseStderr() { … }
class WebTestControlHost::WebTestWindowObserver : WebContentsObserver { … };
WebTestControlHost* WebTestControlHost::instance_ = …;
WebTestControlHost* WebTestControlHost::Get() { … }
WebTestControlHost::WebTestControlHost() { … }
WebTestControlHost::~WebTestControlHost() { … }
void WebTestControlHost::PrepareForWebTest(const TestInfo& test_info) { … }
void WebTestControlHost::ResetBrowserAfterWebTest() { … }
void WebTestControlHost::DidCreateOrAttachWebContents(
WebContents* web_contents) { … }
void WebTestControlHost::SetTempPath(const base::FilePath& temp_path) { … }
void WebTestControlHost::OverrideWebkitPrefs(
blink::web_pref::WebPreferences* prefs) { … }
void WebTestControlHost::OpenURL(const GURL& url) { … }
void WebTestControlHost::InitiateCaptureDump(
mojom::WebTestRendererDumpResultPtr renderer_dump_result,
bool capture_navigation_history,
bool capture_pixels) { … }
void WebTestControlHost::TestFinishedInSecondaryRenderer() { … }
void WebTestControlHost::EnqueueSurfaceCopyRequest() { … }
void WebTestControlHost::CompositeAllFramesThen(
base::OnceCallback<void()> callback) { … }
void WebTestControlHost::CompositeNodeQueueThen(
base::OnceCallback<void()> callback) { … }
void WebTestControlHost::BuildDepthFirstQueue(Node* node) { … }
WebTestControlHost::Node* WebTestControlHost::BuildFrameTree(
WebContents* web_contents) { … }
bool WebTestControlHost::IsMainWindow(WebContents* web_contents) const { … }
std::unique_ptr<BluetoothChooser> WebTestControlHost::RunBluetoothChooser(
RenderFrameHost* frame,
const BluetoothChooser::EventHandler& event_handler) { … }
void WebTestControlHost::RequestPointerLock(WebContents* web_contents) { … }
void WebTestControlHost::PluginCrashed(const base::FilePath& plugin_path,
base::ProcessId plugin_pid) { … }
void WebTestControlHost::TitleWasSet(NavigationEntry* entry) { … }
void WebTestControlHost::DidFailLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code) { … }
void WebTestControlHost::WebContentsDestroyed() { … }
void WebTestControlHost::DidUpdateFaviconURL(
RenderFrameHost* render_frame_host,
const std::vector<blink::mojom::FaviconURLPtr>& candidates) { … }
void WebTestControlHost::RenderFrameHostChanged(RenderFrameHost* old_host,
RenderFrameHost* new_host) { … }
void WebTestControlHost::RenderViewDeleted(RenderViewHost* render_view_host) { … }
void WebTestControlHost::DidStartNavigation(
NavigationHandle* navigation_handle) { … }
void WebTestControlHost::ReadyToCommitNavigation(
NavigationHandle* navigation_handle) { … }
void WebTestControlHost::RenderProcessHostDestroyed(
RenderProcessHost* render_process_host) { … }
void WebTestControlHost::RenderProcessExited(
RenderProcessHost* render_process_host,
const ChildProcessTerminationInfo& info) { … }
void WebTestControlHost::OnGpuProcessCrashed() { … }
void WebTestControlHost::DiscardMainWindow() { … }
void WebTestControlHost::HandleNewRenderFrameHost(RenderFrameHost* frame) { … }
void WebTestControlHost::OnTestFinished() { … }
void WebTestControlHost::OnDumpFrameLayoutResponse(int frame_tree_node_id,
const std::string& dump) { … }
void WebTestControlHost::OnPixelDumpCaptured(const SkBitmap& snapshot) { … }
void WebTestControlHost::ReportResults() { … }
void WebTestControlHost::OnImageDump(const std::string& actual_pixel_hash,
const SkBitmap& image) { … }
void WebTestControlHost::OnAudioDump(const std::vector<unsigned char>& dump) { … }
void WebTestControlHost::OnTextDump(const std::string& dump) { … }
void WebTestControlHost::PrintMessageToStderr(const std::string& message) { … }
void WebTestControlHost::PrintMessage(const std::string& message) { … }
void WebTestControlHost::OverridePreferences(
const blink::web_pref::WebPreferences& prefs) { … }
void WebTestControlHost::SetPopupBlockingEnabled(bool block_popups) { … }
void WebTestControlHost::SimulateScreenOrientationChanged() { … }
void WebTestControlHost::SetPermission(const std::string& name,
blink::mojom::PermissionStatus status,
const GURL& origin,
const GURL& embedding_origin) { … }
void WebTestControlHost::GetWritableDirectory(
GetWritableDirectoryCallback reply) { … }
namespace {
class FakeSelectFileDialog : public ui::SelectFileDialog { … };
class FakeSelectFileDialogFactory : public ui::SelectFileDialogFactory { … };
}
void WebTestControlHost::SetFilePathForMockFileDialog(
const base::FilePath& path) { … }
void WebTestControlHost::FocusDevtoolsSecondaryWindow() { … }
void WebTestControlHost::SetTrustTokenKeyCommitments(
const std::string& raw_commitments,
base::OnceClosure callback) { … }
void WebTestControlHost::ClearTrustTokenState(base::OnceClosure callback) { … }
void WebTestControlHost::SetDatabaseQuota(int32_t quota) { … }
void WebTestControlHost::ClearAllDatabases() { … }
void WebTestControlHost::SimulateWebNotificationClick(
const std::string& title,
int32_t action_index,
const std::optional<std::u16string>& reply) { … }
void WebTestControlHost::SimulateWebNotificationClose(const std::string& title,
bool by_user) { … }
void WebTestControlHost::SimulateWebContentIndexDelete(const std::string& id) { … }
void WebTestControlHost::WebTestRuntimeFlagsChanged(
base::Value::Dict changed_web_test_runtime_flags) { … }
void WebTestControlHost::RegisterIsolatedFileSystem(
const std::vector<base::FilePath>& file_paths,
RegisterIsolatedFileSystemCallback callback) { … }
void WebTestControlHost::DropPointerLock() { … }
void WebTestControlHost::SetPointerLockWillFail() { … }
void WebTestControlHost::SetPointerLockWillRespondAsynchronously() { … }
void WebTestControlHost::AllowPointerLock() { … }
void WebTestControlHost::WorkItemAdded(mojom::WorkItemPtr work_item) { … }
void WebTestControlHost::RequestWorkItem() { … }
void WebTestControlHost::WorkQueueStatesChanged(
base::Value::Dict changed_work_queue_states) { … }
void WebTestControlHost::SetAcceptLanguages(
const std::string& accept_languages) { … }
void WebTestControlHost::EnableAutoResize(const gfx::Size& min_size,
const gfx::Size& max_size) { … }
void WebTestControlHost::DisableAutoResize(const gfx::Size& new_size) { … }
void WebTestControlHost::SetLCPPNavigationHint(
blink::mojom::LCPCriticalPathPredictorNavigationTimeHintPtr hint) { … }
void WebTestControlHost::SetRegisterProtocolHandlerMode(
mojom::WebTestControlHost::AutoResponseMode mode) { … }
void WebTestControlHost::GoToOffset(int offset) { … }
void WebTestControlHost::Reload() { … }
void WebTestControlHost::LoadURLForFrame(const GURL& url,
const std::string& frame_name) { … }
void WebTestControlHost::SetMainWindowHidden(bool hidden) { … }
void WebTestControlHost::SetFrameWindowHidden(
const blink::LocalFrameToken& frame_token,
bool hidden) { … }
WebContents* WebTestControlHost::GetWebContentsFromCurrentContext(
const blink::LocalFrameToken& frame_token) { … }
void WebTestControlHost::CheckForLeakedWindows() { … }
void WebTestControlHost::PrepareRendererForNextWebTest() { … }
void WebTestControlHost::FlushInputAndStartTest(WeakDocumentPtr doc) { … }
void WebTestControlHost::DidFinishNavigation(NavigationHandle* navigation) { … }
void WebTestControlHost::PrepareRendererForNextWebTestDone() { … }
void WebTestControlHost::OnLeakDetectionDone(
int pid,
const LeakDetector::LeakDetectionReport& report) { … }
void WebTestControlHost::CloseTestOpenedWindows() { … }
void WebTestControlHost::CloseAllWindows() { … }
void WebTestControlHost::SetBluetoothManualChooser(bool enable) { … }
void WebTestControlHost::GetBluetoothManualChooserEvents(
GetBluetoothManualChooserEventsCallback reply) { … }
void WebTestControlHost::SendBluetoothManualChooserEvent(
const std::string& event_name,
const std::string& argument) { … }
void WebTestControlHost::BlockThirdPartyCookies(bool block) { … }
void WebTestControlHost::BindWebTestControlHostForRenderer(
int render_process_id,
mojo::PendingAssociatedReceiver<mojom::WebTestControlHost> receiver) { … }
void WebTestControlHost::BindNonAssociatedWebTestControlHost(
mojo::PendingReceiver<mojom::NonAssociatedWebTestControlHost> receiver) { … }
mojo::AssociatedRemote<mojom::WebTestRenderFrame>&
WebTestControlHost::GetWebTestRenderFrameRemote(RenderFrameHost* frame) { … }
void WebTestControlHost::HandleWebTestRenderFrameRemoteError(
const GlobalRenderFrameHostId& key) { … }
WebTestControlHost::Node::Node(RenderFrameHost* host)
: … { … }
WebTestControlHost::Node::Node(Node&& other) = default;
WebTestControlHost::Node& WebTestControlHost::Node::operator=(Node&& other) =
default;
WebTestControlHost::Node::~Node() = default;
}