Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / inspector / agents / InspectorNetworkAgent.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2015 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #pragma once
33
34 #include "InspectorInstrumentation.h"
35 #include "InspectorPageAgent.h"
36 #include "InspectorWebAgentBase.h"
37 #include <JavaScriptCore/InspectorBackendDispatchers.h>
38 #include <JavaScriptCore/InspectorFrontendDispatchers.h>
39 #include <JavaScriptCore/RegularExpression.h>
40 #include <wtf/HashSet.h>
41 #include <wtf/JSONValues.h>
42 #include <wtf/text/WTFString.h>
43
44 namespace Inspector {
45 class InjectedScriptManager;
46 }
47
48 namespace WebCore {
49
50 class CachedResource;
51 class Document;
52 class DocumentLoader;
53 class DocumentThreadableLoader;
54 class NetworkLoadMetrics;
55 class NetworkResourcesData;
56 class ResourceError;
57 class ResourceLoader;
58 class ResourceRequest;
59 class ResourceResponse;
60 class TextResourceDecoder;
61 class WebSocket;
62
63 struct WebSocketFrame;
64
65 typedef String ErrorString;
66
67 class InspectorNetworkAgent : public InspectorAgentBase, public Inspector::NetworkBackendDispatcherHandler {
68     WTF_MAKE_NONCOPYABLE(InspectorNetworkAgent);
69     WTF_MAKE_FAST_ALLOCATED;
70 public:
71     explicit InspectorNetworkAgent(WebAgentContext&);
72     virtual ~InspectorNetworkAgent();
73
74     static bool shouldTreatAsText(const String& mimeType);
75     static Ref<TextResourceDecoder> createTextDecoder(const String& mimeType, const String& textEncodingName);
76     static std::optional<String> textContentForCachedResource(CachedResource&);
77     static bool cachedResourceContent(CachedResource&, String* result, bool* base64Encoded);
78
79     void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
80     void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
81
82     // InspectorInstrumentation
83     void willRecalculateStyle();
84     void didRecalculateStyle();
85     void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
86     void willSendRequestOfType(unsigned long identifier, DocumentLoader*, ResourceRequest&, InspectorInstrumentation::LoadType);
87     void didReceiveResponse(unsigned long identifier, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
88     void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
89     void didFinishLoading(unsigned long identifier, DocumentLoader*, const NetworkLoadMetrics&, ResourceLoader*);
90     void didFailLoading(unsigned long identifier, DocumentLoader*, const ResourceError&);
91     void didLoadResourceFromMemoryCache(DocumentLoader*, CachedResource&);
92     void didReceiveThreadableLoaderResponse(unsigned long identifier, DocumentThreadableLoader&);
93     void willLoadXHRSynchronously();
94     void didLoadXHRSynchronously();
95     void didReceiveScriptResponse(unsigned long identifier);
96     void willDestroyCachedResource(CachedResource&);
97     void didCreateWebSocket(unsigned long identifier, const URL& requestURL);
98     void willSendWebSocketHandshakeRequest(unsigned long identifier, const ResourceRequest&);
99     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const ResourceResponse&);
100     void didCloseWebSocket(unsigned long identifier);
101     void didReceiveWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
102     void didSendWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
103     void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
104     void mainFrameNavigated(DocumentLoader&);
105     void setInitialScriptContent(unsigned long identifier, const String& sourceString);
106     void didScheduleStyleRecalculation(Document&);
107
108     void searchOtherRequests(const JSC::Yarr::RegularExpression&, RefPtr<JSON::ArrayOf<Inspector::Protocol::Page::SearchResult>>&);
109     void searchInRequest(ErrorString&, const String& requestId, const String& query, bool caseSensitive, bool isRegex, RefPtr<JSON::ArrayOf<Inspector::Protocol::GenericTypes::SearchMatch>>&);
110
111     // Called from frontend.
112     void enable(ErrorString&) final;
113     void disable(ErrorString&) final;
114     void setExtraHTTPHeaders(ErrorString&, const JSON::Object& headers) final;
115     void getResponseBody(ErrorString&, const String& requestId, String* content, bool* base64Encoded) final;
116     void setResourceCachingDisabled(ErrorString&, bool disabled) final;
117     void loadResource(const String& frameId, const String& url, Ref<LoadResourceCallback>&&) final;
118     void getSerializedCertificate(ErrorString&, const String& requestId, String* serializedCertificate) final;
119     void resolveWebSocket(ErrorString&, const String& requestId, const String* objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>&) final;
120
121     virtual String loaderIdentifier(DocumentLoader*) = 0;
122     virtual String frameIdentifier(DocumentLoader*) = 0;
123     virtual Vector<WebSocket*> activeWebSockets(const LockHolder&) = 0;
124     virtual void setResourceCachingDisabled(bool) = 0;
125     virtual ScriptExecutionContext* scriptExecutionContext(ErrorString&, const String& frameId) = 0;
126     virtual bool shouldForceBufferingNetworkResourceData() const = 0;
127
128 private:
129     void enable();
130
131     void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse, InspectorPageAgent::ResourceType);
132
133     WebSocket* webSocketForRequestId(const String& requestId);
134
135     RefPtr<Inspector::Protocol::Network::Initiator> buildInitiatorObject(Document*, std::optional<const ResourceRequest&> = std::nullopt);
136     Ref<Inspector::Protocol::Network::ResourceTiming> buildObjectForTiming(const NetworkLoadMetrics&, ResourceLoader&);
137     Ref<Inspector::Protocol::Network::Metrics> buildObjectForMetrics(const NetworkLoadMetrics&);
138     RefPtr<Inspector::Protocol::Network::Response> buildObjectForResourceResponse(const ResourceResponse&, ResourceLoader*);
139     Ref<Inspector::Protocol::Network::CachedResource> buildObjectForCachedResource(CachedResource*);
140
141     double timestamp();
142
143     std::unique_ptr<Inspector::NetworkFrontendDispatcher> m_frontendDispatcher;
144     RefPtr<Inspector::NetworkBackendDispatcher> m_backendDispatcher;
145     Inspector::InjectedScriptManager& m_injectedScriptManager;
146
147     // FIXME: InspectorNetworkAgent should not be aware of style recalculation.
148     RefPtr<Inspector::Protocol::Network::Initiator> m_styleRecalculationInitiator;
149     bool m_isRecalculatingStyle { false };
150
151     std::unique_ptr<NetworkResourcesData> m_resourcesData;
152     bool m_enabled { false };
153     bool m_loadingXHRSynchronously { false };
154     HashMap<String, String> m_extraRequestHeaders;
155     HashSet<unsigned long> m_hiddenRequestIdentifiers;
156 };
157
158 } // namespace WebCore