Web Inspector: Include Beacon and Ping requests in Network tab
[WebKit-https.git] / Source / WebCore / inspector / 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 <inspector/InspectorBackendDispatchers.h>
38 #include <inspector/InspectorFrontendDispatchers.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/text/WTFString.h>
41 #include <yarr/RegularExpression.h>
42
43 namespace Inspector {
44 class InspectorObject;
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 URL;
61 class WebSocket;
62
63 struct WebSocketFrame;
64
65 typedef String ErrorString;
66
67 class InspectorNetworkAgent final : public InspectorAgentBase, public Inspector::NetworkBackendDispatcherHandler {
68     WTF_MAKE_FAST_ALLOCATED;
69 public:
70     InspectorNetworkAgent(WebAgentContext&, InspectorPageAgent*);
71     virtual ~InspectorNetworkAgent();
72
73     void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
74     void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
75
76     // InspectorInstrumentation
77     void willRecalculateStyle();
78     void didRecalculateStyle();
79     void willSendRequest(unsigned long identifier, DocumentLoader&, ResourceRequest&, const ResourceResponse& redirectResponse);
80     void willSendRequestOfType(unsigned long identifier, DocumentLoader&, ResourceRequest&, InspectorInstrumentation::LoadType);
81     void didReceiveResponse(unsigned long identifier, DocumentLoader&, const ResourceResponse&, ResourceLoader*);
82     void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
83     void didFinishLoading(unsigned long identifier, DocumentLoader&, const NetworkLoadMetrics&, ResourceLoader*);
84     void didFailLoading(unsigned long identifier, DocumentLoader&, const ResourceError&);
85     void didLoadResourceFromMemoryCache(DocumentLoader&, CachedResource&);
86     void didReceiveThreadableLoaderResponse(unsigned long identifier, DocumentThreadableLoader&);
87     void didFinishXHRLoading(unsigned long identifier, const String& decodedText);
88     void willLoadXHRSynchronously();
89     void didLoadXHRSynchronously();
90     void didReceiveScriptResponse(unsigned long identifier);
91     void willDestroyCachedResource(CachedResource&);
92     void didCreateWebSocket(unsigned long identifier, const URL& requestURL);
93     void willSendWebSocketHandshakeRequest(unsigned long identifier, const ResourceRequest&);
94     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const ResourceResponse&);
95     void didCloseWebSocket(unsigned long identifier);
96     void didReceiveWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
97     void didSendWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
98     void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
99     void mainFrameNavigated(DocumentLoader&);
100     void setInitialScriptContent(unsigned long identifier, const String& sourceString);
101     void didScheduleStyleRecalculation(Document&);
102
103     void searchOtherRequests(const JSC::Yarr::RegularExpression&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>>&);
104     void searchInRequest(ErrorString&, const String& requestId, const String& query, bool caseSensitive, bool isRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>>&);
105
106     RefPtr<Inspector::Protocol::Network::Initiator> buildInitiatorObject(Document*);
107
108     // Called from frontend.
109     void enable(ErrorString&) override;
110     void disable(ErrorString&) override;
111     void setExtraHTTPHeaders(ErrorString&, const Inspector::InspectorObject& headers) override;
112     void getResponseBody(ErrorString&, const String& requestId, String* content, bool* base64Encoded) override;
113     void setResourceCachingDisabled(ErrorString&, bool disabled) override;
114     void loadResource(ErrorString&, const String& frameId, const String& url, Ref<LoadResourceCallback>&&) override;
115     void resolveWebSocket(ErrorString&, const String& requestId, const String* const objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>&) override;
116
117 private:
118     void enable();
119
120     void willSendRequest(unsigned long identifier, DocumentLoader&, ResourceRequest&, const ResourceResponse& redirectResponse, InspectorPageAgent::ResourceType);
121
122     WebSocket* webSocketForRequestId(const String& requestId);
123
124     Ref<Inspector::Protocol::Network::ResourceTiming> buildObjectForTiming(const NetworkLoadMetrics&, ResourceLoader&);
125     Ref<Inspector::Protocol::Network::Metrics> buildObjectForMetrics(const NetworkLoadMetrics&);
126     RefPtr<Inspector::Protocol::Network::Response> buildObjectForResourceResponse(const ResourceResponse&, ResourceLoader*);
127     Ref<Inspector::Protocol::Network::CachedResource> buildObjectForCachedResource(CachedResource*);
128
129     double timestamp();
130
131     std::unique_ptr<Inspector::NetworkFrontendDispatcher> m_frontendDispatcher;
132     RefPtr<Inspector::NetworkBackendDispatcher> m_backendDispatcher;
133     Inspector::InjectedScriptManager& m_injectedScriptManager;
134     InspectorPageAgent* m_pageAgent { nullptr };
135
136     // FIXME: InspectorNetworkAgent should not be aware of style recalculation.
137     RefPtr<Inspector::Protocol::Network::Initiator> m_styleRecalculationInitiator;
138     bool m_isRecalculatingStyle { false };
139
140     std::unique_ptr<NetworkResourcesData> m_resourcesData;
141     bool m_enabled { false };
142     bool m_loadingXHRSynchronously { false };
143     HashMap<String, String> m_extraRequestHeaders;
144     HashSet<unsigned long> m_hiddenRequestIdentifiers;
145 };
146
147 } // namespace WebCore