Web Inspector: Network: add button to show system certificate dialog
[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 URL;
62 class WebSocket;
63
64 struct WebSocketFrame;
65
66 typedef String ErrorString;
67
68 class InspectorNetworkAgent : public InspectorAgentBase, public Inspector::NetworkBackendDispatcherHandler {
69     WTF_MAKE_NONCOPYABLE(InspectorNetworkAgent);
70     WTF_MAKE_FAST_ALLOCATED;
71 public:
72     explicit InspectorNetworkAgent(WebAgentContext&);
73     virtual ~InspectorNetworkAgent();
74
75     static bool shouldTreatAsText(const String& mimeType);
76     static Ref<TextResourceDecoder> createTextDecoder(const String& mimeType, const String& textEncodingName);
77     static std::optional<String> textContentForCachedResource(CachedResource&);
78     static bool cachedResourceContent(CachedResource&, String* result, bool* base64Encoded);
79
80     void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
81     void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
82
83     // InspectorInstrumentation
84     void willRecalculateStyle();
85     void didRecalculateStyle();
86     void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
87     void willSendRequestOfType(unsigned long identifier, DocumentLoader*, ResourceRequest&, InspectorInstrumentation::LoadType);
88     void didReceiveResponse(unsigned long identifier, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
89     void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
90     void didFinishLoading(unsigned long identifier, DocumentLoader*, const NetworkLoadMetrics&, ResourceLoader*);
91     void didFailLoading(unsigned long identifier, DocumentLoader*, const ResourceError&);
92     void didLoadResourceFromMemoryCache(DocumentLoader*, CachedResource&);
93     void didReceiveThreadableLoaderResponse(unsigned long identifier, DocumentThreadableLoader&);
94     void willLoadXHRSynchronously();
95     void didLoadXHRSynchronously();
96     void didReceiveScriptResponse(unsigned long identifier);
97     void willDestroyCachedResource(CachedResource&);
98     void didCreateWebSocket(unsigned long identifier, const URL& requestURL);
99     void willSendWebSocketHandshakeRequest(unsigned long identifier, const ResourceRequest&);
100     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const ResourceResponse&);
101     void didCloseWebSocket(unsigned long identifier);
102     void didReceiveWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
103     void didSendWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
104     void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
105     void mainFrameNavigated(DocumentLoader&);
106     void setInitialScriptContent(unsigned long identifier, const String& sourceString);
107     void didScheduleStyleRecalculation(Document&);
108
109     void searchOtherRequests(const JSC::Yarr::RegularExpression&, RefPtr<JSON::ArrayOf<Inspector::Protocol::Page::SearchResult>>&);
110     void searchInRequest(ErrorString&, const String& requestId, const String& query, bool caseSensitive, bool isRegex, RefPtr<JSON::ArrayOf<Inspector::Protocol::GenericTypes::SearchMatch>>&);
111
112     // Called from frontend.
113     void enable(ErrorString&) final;
114     void disable(ErrorString&) final;
115     void setExtraHTTPHeaders(ErrorString&, const JSON::Object& headers) final;
116     void getResponseBody(ErrorString&, const String& requestId, String* content, bool* base64Encoded) final;
117     void setResourceCachingDisabled(ErrorString&, bool disabled) final;
118     void loadResource(const String& frameId, const String& url, Ref<LoadResourceCallback>&&) final;
119     void getSerializedCertificate(ErrorString&, const String& requestId, String* serializedCertificate) final;
120     void resolveWebSocket(ErrorString&, const String& requestId, const String* objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>&) final;
121
122     virtual String loaderIdentifier(DocumentLoader*) = 0;
123     virtual String frameIdentifier(DocumentLoader*) = 0;
124     virtual Vector<WebSocket*> activeWebSockets(const LockHolder&) = 0;
125     virtual void setResourceCachingDisabled(bool) = 0;
126     virtual ScriptExecutionContext* scriptExecutionContext(ErrorString&, const String& frameId) = 0;
127     virtual bool shouldForceBufferingNetworkResourceData() const = 0;
128
129 private:
130     void enable();
131
132     void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse, InspectorPageAgent::ResourceType);
133
134     WebSocket* webSocketForRequestId(const String& requestId);
135
136     RefPtr<Inspector::Protocol::Network::Initiator> buildInitiatorObject(Document*, std::optional<const ResourceRequest&> = std::nullopt);
137     Ref<Inspector::Protocol::Network::ResourceTiming> buildObjectForTiming(const NetworkLoadMetrics&, ResourceLoader&);
138     Ref<Inspector::Protocol::Network::Metrics> buildObjectForMetrics(const NetworkLoadMetrics&);
139     RefPtr<Inspector::Protocol::Network::Response> buildObjectForResourceResponse(const ResourceResponse&, ResourceLoader*);
140     Ref<Inspector::Protocol::Network::CachedResource> buildObjectForCachedResource(CachedResource*);
141
142     double timestamp();
143
144     std::unique_ptr<Inspector::NetworkFrontendDispatcher> m_frontendDispatcher;
145     RefPtr<Inspector::NetworkBackendDispatcher> m_backendDispatcher;
146     Inspector::InjectedScriptManager& m_injectedScriptManager;
147
148     // FIXME: InspectorNetworkAgent should not be aware of style recalculation.
149     RefPtr<Inspector::Protocol::Network::Initiator> m_styleRecalculationInitiator;
150     bool m_isRecalculatingStyle { false };
151
152     std::unique_ptr<NetworkResourcesData> m_resourcesData;
153     bool m_enabled { false };
154     bool m_loadingXHRSynchronously { false };
155     HashMap<String, String> m_extraRequestHeaders;
156     HashSet<unsigned long> m_hiddenRequestIdentifiers;
157 };
158
159 } // namespace WebCore