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