chromium/third_party/blink/renderer/core/inspector/inspector_network_agent.cc

/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/core/inspector/inspector_network_agent.h"

#include <memory>
#include <utility>

#include "base/containers/span.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/cert/ct_sct_to_string.h"
#include "net/cert/x509_util.h"
#include "net/http/http_status_code.h"
#include "net/ssl/ssl_cipher_suite_names.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "services/network/public/cpp/cors/cors_error_status.h"
#include "services/network/public/mojom/referrer_policy.mojom-blink.h"
#include "services/network/public/mojom/trust_tokens.mojom-blink.h"
#include "services/network/public/mojom/websocket.mojom-blink.h"
#include "third_party/blink/public/common/loader/referrer_utils.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/mojom/loader/mixed_content.mojom-blink.h"
#include "third_party/blink/public/mojom/loader/request_context_frame_type.mojom-blink.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_effective_connection_type.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/bindings/core/v8/capture_source_location.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/scriptable_document_parser.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_client.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_loader.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/html/html_frame_owner_element.h"
#include "third_party/blink/renderer/core/inspector/identifiers_factory.h"
#include "third_party/blink/renderer/core/inspector/inspected_frames.h"
#include "third_party/blink/renderer/core/inspector/network_resources_data.h"
#include "third_party/blink/renderer/core/inspector/protocol/network.h"
#include "third_party/blink/renderer/core/inspector/request_debug_header_scope.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/loader/frame_loader.h"
#include "third_party/blink/renderer/core/loader/mixed_content_checker.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/source_location.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_info.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h"
#include "third_party/blink/renderer/platform/loader/fetch/render_blocking_behavior.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_load_timing.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/loader/fetch/service_worker_router_info.h"
#include "third_party/blink/renderer/platform/loader/fetch/unique_identifier.h"
#include "third_party/blink/renderer/platform/loader/fetch/url_loader/url_loader_client.h"
#include "third_party/blink/renderer/platform/network/http_header_map.h"
#include "third_party/blink/renderer/platform/network/network_state_notifier.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.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/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/text/base64.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "third_party/boringssl/src/include/openssl/ssl.h"
#include "third_party/inspector_protocol/crdtp/json.h"

SpanFrom;
ConvertCBORToJSON;

namespace blink {

GetRequestPostDataCallback;
GetResponseBodyCallback;

namespace {

#if BUILDFLAG(IS_ANDROID)
constexpr int kDefaultTotalBufferSize = 10 * 1000 * 1000;    // 10 MB
constexpr int kDefaultResourceBufferSize = 5 * 1000 * 1000;  // 5 MB
#else
constexpr int kDefaultTotalBufferSize =;    // 200 MB
constexpr int kDefaultResourceBufferSize =;  // 20 MB
#endif

// Pattern may contain stars ('*') which match to any (possibly empty) string.
// Stars implicitly assumed at the begin/end of pattern.
bool Matches(const String& url, const String& pattern) {}

bool LoadsFromCacheOnly(const ResourceRequest& request) {}

protocol::Network::CertificateTransparencyCompliance
SerializeCTPolicyCompliance(net::ct::CTPolicyCompliance ct_compliance) {}

static std::unique_ptr<protocol::Network::Headers> BuildObjectForHeaders(
    const HTTPHeaderMap& headers) {}

class InspectorFileReaderLoaderClient final
    : public GarbageCollected<InspectorFileReaderLoaderClient>,
      public FileReaderClient {};

static void ResponseBodyFileReaderLoaderDone(
    const String& mime_type,
    const String& text_encoding_name,
    std::unique_ptr<GetResponseBodyCallback> callback,
    std::optional<SegmentedBuffer> raw_data) {}

class InspectorPostBodyParser
    : public WTF::RefCounted<InspectorPostBodyParser> {};

KURL UrlWithoutFragment(const KURL& url) {}

String MixedContentTypeForContextType(
    mojom::blink::MixedContentContextType context_type) {}

String ResourcePriorityJSON(ResourceLoadPriority priority) {}

String BuildBlockedReason(ResourceRequestBlockedReason reason) {}

Maybe<String> BuildBlockedReason(const ResourceError& error) {}

String BuildCorsError(network::mojom::CorsError cors_error) {}

std::unique_ptr<protocol::Network::CorsErrorStatus> BuildCorsErrorStatus(
    const network::CorsErrorStatus& status) {}

String BuildServiceWorkerResponseSource(const ResourceResponse& response) {}

String BuildServiceWorkerRouterSourceType(
    const network::mojom::ServiceWorkerRouterSourceType& type) {}

WebConnectionType ToWebConnectionType(const String& connection_type) {}

String GetReferrerPolicy(network::mojom::ReferrerPolicy policy) {}

std::unique_ptr<protocol::Network::WebSocketFrame> WebSocketMessageToProtocol(
    int op_code,
    bool masked,
    const char* payload,
    size_t payload_length) {}

String GetTrustTokenOperationType(
    network::mojom::TrustTokenOperationType operation) {}

String GetTrustTokenRefreshPolicy(
    network::mojom::TrustTokenRefreshPolicy policy) {}

std::unique_ptr<protocol::Network::TrustTokenParams> BuildTrustTokenParams(
    const network::mojom::blink::TrustTokenParams& params) {}

void SetNetworkStateOverride(bool offline,
                             double latency,
                             double download_throughput,
                             double upload_throughput,
                             WebConnectionType type) {}

String IPAddressToString(const net::IPAddress& address) {}

ContentEncodingEnum;

String AcceptedEncodingFromProtocol(
    const protocol::Network::ContentEncoding& encoding) {}

SourceTypeEnum;
SourceTypeEnum SourceTypeFromString(const String& type) {}

}  // namespace

void InspectorNetworkAgent::Restore() {}

static std::unique_ptr<protocol::Network::ResourceTiming> BuildObjectForTiming(
    const ResourceLoadTiming& timing) {}

static bool FormDataToString(
    scoped_refptr<EncodedFormData> body,
    size_t max_body_size,
    protocol::Array<protocol::Network::PostDataEntry>* data_entries,
    String* content) {}

static String StringFromASCII(const std::string& str) {}

static std::unique_ptr<protocol::Network::SecurityDetails> BuildSecurityDetails(
    const net::SSLInfo& ssl_info) {}

static std::unique_ptr<protocol::Network::Request>
BuildObjectForResourceRequest(const ResourceRequest& request,
                              scoped_refptr<EncodedFormData> post_data,
                              size_t max_body_size) {}

String AlternateProtocolUsageToString(
    net::AlternateProtocolUsage alternate_protocol_usage) {}

static std::unique_ptr<protocol::Network::Response>
BuildObjectForResourceResponse(const ResourceResponse& response,
                               const Resource* cached_resource = nullptr,
                               bool* is_empty = nullptr) {}

InspectorNetworkAgent::~InspectorNetworkAgent() = default;

void InspectorNetworkAgent::Trace(Visitor* visitor) const {}

void InspectorNetworkAgent::ShouldBlockRequest(const KURL& url, bool* result) {}

void InspectorNetworkAgent::ShouldBypassServiceWorker(bool* result) {}

void InspectorNetworkAgent::DidBlockRequest(
    const ResourceRequest& request,
    DocumentLoader* loader,
    const KURL& fetch_context_url,
    const ResourceLoaderOptions& options,
    ResourceRequestBlockedReason reason,
    ResourceType resource_type) {}

void InspectorNetworkAgent::DidChangeResourcePriority(
    DocumentLoader* loader,
    uint64_t identifier,
    ResourceLoadPriority load_priority) {}

String InspectorNetworkAgent::RequestId(DocumentLoader* loader,
                                        uint64_t identifier) {}

void InspectorNetworkAgent::WillSendRequestInternal(
    DocumentLoader* loader,
    const KURL& fetch_context_url,
    const ResourceRequest& request,
    const ResourceResponse& redirect_response,
    const ResourceLoaderOptions& options,
    InspectorPageAgent::ResourceType type,
    base::TimeTicks timestamp) {}

void InspectorNetworkAgent::WillSendNavigationRequest(
    uint64_t identifier,
    DocumentLoader* loader,
    const KURL& url,
    const AtomicString& http_method,
    EncodedFormData* http_body) {}

// This method was pulled out of PrepareRequest(), because we want to be able
// to create DevTools issues before the PrepareRequest() call. We need these
// IDs to be set, to properly create a DevTools issue.
void InspectorNetworkAgent::SetDevToolsIds(
    ResourceRequest& request,
    const FetchInitiatorInfo& initiator_info) {}

void InspectorNetworkAgent::PrepareRequest(DocumentLoader* loader,
                                           ResourceRequest& request,
                                           ResourceLoaderOptions& options,
                                           ResourceType resource_type) {}

void InspectorNetworkAgent::WillSendRequest(
    ExecutionContext*,
    DocumentLoader* loader,
    const KURL& fetch_context_url,
    const ResourceRequest& request,
    const ResourceResponse& redirect_response,
    const ResourceLoaderOptions& options,
    ResourceType resource_type,
    RenderBlockingBehavior render_blocking_behavior,
    base::TimeTicks timestamp) {}

void InspectorNetworkAgent::MarkResourceAsCached(DocumentLoader* loader,
                                                 uint64_t identifier) {}

void InspectorNetworkAgent::DidReceiveResourceResponse(
    uint64_t identifier,
    DocumentLoader* loader,
    const ResourceResponse& response,
    const Resource* cached_resource) {}

static bool IsErrorStatusCode(int status_code) {}

protocol::Response InspectorNetworkAgent::streamResourceContent(
    const String& request_id,
    protocol::Binary* buffered_data) {}

void InspectorNetworkAgent::DidReceiveData(uint64_t identifier,
                                           DocumentLoader* loader,
                                           const char* data,
                                           uint64_t data_length) {}

void InspectorNetworkAgent::DidReceiveBlob(uint64_t identifier,
                                           DocumentLoader* loader,
                                           scoped_refptr<BlobDataHandle> blob) {}

void InspectorNetworkAgent::DidReceiveEncodedDataLength(
    DocumentLoader* loader,
    uint64_t identifier,
    size_t encoded_data_length) {}

void InspectorNetworkAgent::DidFinishLoading(
    uint64_t identifier,
    DocumentLoader* loader,
    base::TimeTicks monotonic_finish_time,
    int64_t encoded_data_length,
    int64_t decoded_body_length) {}

void InspectorNetworkAgent::DidReceiveCorsRedirectResponse(
    uint64_t identifier,
    DocumentLoader* loader,
    const ResourceResponse& response,
    Resource* resource) {}

void InspectorNetworkAgent::DidFailLoading(
    CoreProbeSink* sink,
    uint64_t identifier,
    DocumentLoader* loader,
    const ResourceError& error,
    const base::UnguessableToken& devtools_frame_or_worker_token) {}

void InspectorNetworkAgent::ScriptImported(uint64_t identifier,
                                           const String& source_string) {}

void InspectorNetworkAgent::DidReceiveScriptResponse(uint64_t identifier) {}

// static
bool InspectorNetworkAgent::IsNavigation(DocumentLoader* loader,
                                         uint64_t identifier) {}

void InspectorNetworkAgent::WillLoadXHR(ExecutionContext* execution_context,
                                        const AtomicString& method,
                                        const KURL& url,
                                        bool async,
                                        const HTTPHeaderMap& headers,
                                        bool include_credentials) {}

void InspectorNetworkAgent::DidFinishXHR(XMLHttpRequest* xhr) {}

void InspectorNetworkAgent::WillSendEventSourceRequest() {}

void InspectorNetworkAgent::WillDispatchEventSourceEvent(
    uint64_t identifier,
    const AtomicString& event_name,
    const AtomicString& event_id,
    const String& data) {}

std::unique_ptr<protocol::Network::Initiator>
InspectorNetworkAgent::BuildInitiatorObject(
    Document* document,
    const FetchInitiatorInfo& initiator_info,
    int max_async_depth) {}

String InspectorNetworkAgent::GetProtocolAsString(
    const ResourceResponse& response) {}

void InspectorNetworkAgent::WillCreateP2PSocketUdp(
    std::optional<base::UnguessableToken>* devtools_token) {}

void InspectorNetworkAgent::WillCreateWebSocket(
    ExecutionContext* execution_context,
    uint64_t identifier,
    const KURL& request_url,
    const String&,
    std::optional<base::UnguessableToken>* devtools_token) {}

void InspectorNetworkAgent::WillSendWebSocketHandshakeRequest(
    ExecutionContext*,
    uint64_t identifier,
    network::mojom::blink::WebSocketHandshakeRequest* request) {}

void InspectorNetworkAgent::DidReceiveWebSocketHandshakeResponse(
    ExecutionContext*,
    uint64_t identifier,
    network::mojom::blink::WebSocketHandshakeRequest* request,
    network::mojom::blink::WebSocketHandshakeResponse* response) {}

void InspectorNetworkAgent::DidCloseWebSocket(ExecutionContext*,
                                              uint64_t identifier) {}

void InspectorNetworkAgent::DidReceiveWebSocketMessage(
    uint64_t identifier,
    int op_code,
    bool masked,
    const Vector<base::span<const char>>& data) {}

void InspectorNetworkAgent::DidSendWebSocketMessage(uint64_t identifier,
                                                    int op_code,
                                                    bool masked,
                                                    const char* payload,
                                                    size_t payload_length) {}

void InspectorNetworkAgent::DidReceiveWebSocketMessageError(
    uint64_t identifier,
    const String& error_message) {}

void InspectorNetworkAgent::WebTransportCreated(
    ExecutionContext* execution_context,
    uint64_t transport_id,
    const KURL& request_url) {}

void InspectorNetworkAgent::WebTransportConnectionEstablished(
    uint64_t transport_id) {}

void InspectorNetworkAgent::WebTransportClosed(uint64_t transport_id) {}

protocol::Response InspectorNetworkAgent::enable(
    Maybe<int> total_buffer_size,
    Maybe<int> resource_buffer_size,
    Maybe<int> max_post_data_size) {}

void InspectorNetworkAgent::Enable() {}

protocol::Response InspectorNetworkAgent::disable() {}

protocol::Response InspectorNetworkAgent::setExtraHTTPHeaders(
    std::unique_ptr<protocol::Network::Headers> headers) {}

protocol::Response InspectorNetworkAgent::setAttachDebugStack(bool enabled) {}

bool InspectorNetworkAgent::CanGetResponseBodyBlob(const String& request_id) {}

void InspectorNetworkAgent::GetResponseBodyBlob(
    const String& request_id,
    std::unique_ptr<GetResponseBodyCallback> callback) {}

void InspectorNetworkAgent::getResponseBody(
    const String& request_id,
    std::unique_ptr<GetResponseBodyCallback> callback) {}

protocol::Response InspectorNetworkAgent::setBlockedURLs(
    std::unique_ptr<protocol::Array<String>> urls) {}

protocol::Response InspectorNetworkAgent::replayXHR(const String& request_id) {}

protocol::Response InspectorNetworkAgent::canClearBrowserCache(bool* result) {}

protocol::Response InspectorNetworkAgent::canClearBrowserCookies(bool* result) {}

protocol::Response InspectorNetworkAgent::setAcceptedEncodings(
    std::unique_ptr<protocol::Array<protocol::Network::ContentEncoding>>
        encodings) {}

protocol::Response InspectorNetworkAgent::clearAcceptedEncodingsOverride() {}

protocol::Response InspectorNetworkAgent::emulateNetworkConditions(
    bool offline,
    double latency,
    double download_throughput,
    double upload_throughput,
    Maybe<String> connection_type,
    Maybe<double> packet_loss,
    Maybe<int> packet_queue_length,
    Maybe<bool> packet_reordering) {}

protocol::Response InspectorNetworkAgent::setCacheDisabled(
    bool cache_disabled) {}

protocol::Response InspectorNetworkAgent::setBypassServiceWorker(bool bypass) {}

protocol::Response InspectorNetworkAgent::getCertificate(
    const String& origin,
    std::unique_ptr<protocol::Array<String>>* certificate) {}

void InspectorNetworkAgent::DidCommitLoad(LocalFrame* frame,
                                          DocumentLoader* loader) {}

void InspectorNetworkAgent::FrameScheduledNavigation(LocalFrame* frame,
                                                     const KURL&,
                                                     base::TimeDelta,
                                                     ClientNavigationReason) {}

void InspectorNetworkAgent::FrameClearedScheduledNavigation(LocalFrame* frame) {}

protocol::Response InspectorNetworkAgent::GetResponseBody(
    const String& request_id,
    String* content,
    bool* base64_encoded) {}

protocol::Response InspectorNetworkAgent::searchInResponseBody(
    const String& request_id,
    const String& query,
    Maybe<bool> case_sensitive,
    Maybe<bool> is_regex,
    std::unique_ptr<
        protocol::Array<v8_inspector::protocol::Debugger::API::SearchMatch>>*
        matches) {}

bool InspectorNetworkAgent::FetchResourceContent(Document* document,
                                                 const KURL& url,
                                                 String* content,
                                                 bool* base64_encoded,
                                                 bool* loadingFailed) {}

String InspectorNetworkAgent::NavigationInitiatorInfo(LocalFrame* frame) {}

InspectorNetworkAgent::InspectorNetworkAgent(
    InspectedFrames* inspected_frames,
    WorkerOrWorkletGlobalScope* worker_or_worklet_global_scope,
    v8_inspector::V8InspectorSession* v8_session)
    :{}

void InspectorNetworkAgent::ShouldForceCorsPreflight(bool* result) {}

void InspectorNetworkAgent::getRequestPostData(
    const String& request_id,
    std::unique_ptr<GetRequestPostDataCallback> callback) {}

ExecutionContext* InspectorNetworkAgent::GetTargetExecutionContext() const {}

void InspectorNetworkAgent::IsCacheDisabled(bool* is_cache_disabled) const {}

}  // namespace blink