[Resource Timing] Gather timing information with reliable responseEnd time
[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 }
44
45 namespace WebCore {
46
47 class CachedResource;
48 class Document;
49 class DocumentLoader;
50 class DocumentThreadableLoader;
51 class InspectorPageAgent;
52 class NetworkLoadMetrics;
53 class NetworkResourcesData;
54 class ResourceError;
55 class ResourceLoader;
56 class ResourceRequest;
57 class ResourceResponse;
58 class URL;
59
60 #if ENABLE(WEB_SOCKETS)
61 struct WebSocketFrame;
62 #endif
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 markResourceAsCached(unsigned long identifier);
80     void didReceiveResponse(unsigned long identifier, DocumentLoader&, const ResourceResponse&, ResourceLoader*);
81     void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
82     void didFinishLoading(unsigned long identifier, DocumentLoader&);
83     void didFailLoading(unsigned long identifier, DocumentLoader&, const ResourceError&);
84     void didLoadResourceFromMemoryCache(DocumentLoader&, CachedResource&);
85     void didReceiveThreadableLoaderResponse(unsigned long identifier, DocumentThreadableLoader&);
86     void didFinishXHRLoading(unsigned long identifier, const String& decodedText);
87     void willLoadXHRSynchronously();
88     void didLoadXHRSynchronously();
89     void didReceiveScriptResponse(unsigned long identifier);
90     void willDestroyCachedResource(CachedResource&);
91 #if ENABLE(WEB_SOCKETS)
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 #endif
100     void mainFrameNavigated(DocumentLoader&);
101     void setInitialScriptContent(unsigned long identifier, const String& sourceString);
102     void didScheduleStyleRecalculation(Document&);
103
104     void searchOtherRequests(const JSC::Yarr::RegularExpression&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>>&);
105     void searchInRequest(ErrorString&, const String& requestId, const String& query, bool caseSensitive, bool isRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>>&);
106
107     RefPtr<Inspector::Protocol::Network::Initiator> buildInitiatorObject(Document*);
108
109     // Called from frontend.
110     void enable(ErrorString&) override;
111     void disable(ErrorString&) override;
112     void setExtraHTTPHeaders(ErrorString&, const Inspector::InspectorObject& headers) override;
113     void getResponseBody(ErrorString&, const String& requestId, String* content, bool* base64Encoded) override;
114     void setCacheDisabled(ErrorString&, bool cacheDisabled) override;
115     void loadResource(ErrorString&, const String& frameId, const String& url, Ref<LoadResourceCallback>&&) override;
116
117 private:
118     void enable();
119
120     Ref<Inspector::Protocol::Network::ResourceTiming> buildObjectForTiming(const NetworkLoadMetrics&, ResourceLoader&);
121     RefPtr<Inspector::Protocol::Network::Response> buildObjectForResourceResponse(const ResourceResponse&, ResourceLoader*);
122     Ref<Inspector::Protocol::Network::CachedResource> buildObjectForCachedResource(CachedResource*);
123
124     double timestamp();
125
126     std::unique_ptr<Inspector::NetworkFrontendDispatcher> m_frontendDispatcher;
127     RefPtr<Inspector::NetworkBackendDispatcher> m_backendDispatcher;
128     InspectorPageAgent* m_pageAgent { nullptr };
129
130     // FIXME: InspectorNetworkAgent should not be aware of style recalculation.
131     RefPtr<Inspector::Protocol::Network::Initiator> m_styleRecalculationInitiator;
132     bool m_isRecalculatingStyle { false };
133
134     std::unique_ptr<NetworkResourcesData> m_resourcesData;
135     bool m_enabled { false };
136     bool m_cacheDisabled { false };
137     bool m_loadingXHRSynchronously { false };
138     HashMap<String, String> m_extraRequestHeaders;
139     HashSet<unsigned long> m_hiddenRequestIdentifiers;
140 };
141
142 } // namespace WebCore