1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef InspectorResourceAgent_h
32#define InspectorResourceAgent_h
33
34#include "InspectorWebAgentBase.h"
35#include "InspectorWebBackendDispatchers.h"
36#include "InspectorWebFrontendDispatchers.h"
37#include <wtf/HashSet.h>
38#include <wtf/RefCounted.h>
39#include <wtf/Vector.h>
40#include <wtf/text/WTFString.h>
41
42#if ENABLE(INSPECTOR)
43
44namespace Inspector {
45class InspectorArray;
46class InspectorObject;
47}
48
49namespace WebCore {
50
51class CachedResource;
52class Document;
53class DocumentLoader;
54class FormData;
55class Frame;
56class HTTPHeaderMap;
57class InspectorClient;
58class InspectorPageAgent;
59class InstrumentingAgents;
60class NetworkResourcesData;
61class Page;
62class ResourceError;
63class ResourceLoader;
64class ResourceRequest;
65class ResourceResponse;
66class SharedBuffer;
67class ThreadableLoaderClient;
68class URL;
69class XHRReplayData;
70class XMLHttpRequest;
71
72#if ENABLE(WEB_SOCKETS)
73struct WebSocketFrame;
74#endif
75
76typedef String ErrorString;
77
78class InspectorResourceAgent : public InspectorAgentBase, public Inspector::InspectorNetworkBackendDispatcherHandler {
79    WTF_MAKE_FAST_ALLOCATED;
80public:
81    InspectorResourceAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorClient*);
82    ~InspectorResourceAgent();
83
84    virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
85    virtual void willDestroyFrontendAndBackend(Inspector::InspectorDisconnectReason) override;
86
87    void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
88    void markResourceAsCached(unsigned long identifier);
89    void didReceiveResponse(unsigned long identifier, DocumentLoader* laoder, const ResourceResponse&, ResourceLoader*);
90    void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
91    void didFinishLoading(unsigned long identifier, DocumentLoader*, double finishTime);
92    void didFailLoading(unsigned long identifier, DocumentLoader*, const ResourceError&);
93    void didLoadResourceFromMemoryCache(DocumentLoader*, CachedResource*);
94    void mainFrameNavigated(DocumentLoader*);
95    void setInitialScriptContent(unsigned long identifier, const String& sourceString);
96    void didReceiveScriptResponse(unsigned long identifier);
97
98    void documentThreadableLoaderStartedLoadingForClient(unsigned long identifier, ThreadableLoaderClient*);
99    void willLoadXHR(ThreadableLoaderClient*, const String& method, const URL&, bool async, PassRefPtr<FormData> body, const HTTPHeaderMap& headers, bool includeCrendentials);
100    void didFailXHRLoading(ThreadableLoaderClient*);
101    void didFinishXHRLoading(ThreadableLoaderClient*, unsigned long identifier, const String& sourceString);
102    void didReceiveXHRResponse(unsigned long identifier);
103    void willLoadXHRSynchronously();
104    void didLoadXHRSynchronously();
105
106    void willDestroyCachedResource(CachedResource*);
107
108    // FIXME: InspectorResourceAgent should now be aware of style recalculation.
109    void willRecalculateStyle();
110    void didRecalculateStyle();
111    void didScheduleStyleRecalculation(Document*);
112
113    PassRefPtr<Inspector::TypeBuilder::Network::Initiator> buildInitiatorObject(Document*);
114
115#if ENABLE(WEB_SOCKETS)
116    void didCreateWebSocket(unsigned long identifier, const URL& requestURL);
117    void willSendWebSocketHandshakeRequest(unsigned long identifier, const ResourceRequest&);
118    void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const ResourceResponse&);
119    void didCloseWebSocket(unsigned long identifier);
120    void didReceiveWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
121    void didSendWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
122    void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
123#endif
124
125    // Called from frontend.
126    virtual void enable(ErrorString*) override;
127    virtual void disable(ErrorString*) override;
128    virtual void setExtraHTTPHeaders(ErrorString*, const RefPtr<Inspector::InspectorObject>&) override;
129    virtual void getResponseBody(ErrorString*, const String& requestId, String* content, bool* base64Encoded) override;
130    virtual void replayXHR(ErrorString*, const String& requestId) override;
131    virtual void canClearBrowserCache(ErrorString*, bool*) override;
132    virtual void clearBrowserCache(ErrorString*) override;
133    virtual void canClearBrowserCookies(ErrorString*, bool*) override;
134    virtual void clearBrowserCookies(ErrorString*) override;
135    virtual void setCacheDisabled(ErrorString*, bool cacheDisabled) override;
136    virtual void loadResource(ErrorString*, const String& frameId, const String& url, PassRefPtr<LoadResourceCallback>) override;
137
138private:
139    void enable();
140
141    InspectorPageAgent* m_pageAgent;
142    InspectorClient* m_client;
143    std::unique_ptr<Inspector::InspectorNetworkFrontendDispatcher> m_frontendDispatcher;
144    RefPtr<Inspector::InspectorNetworkBackendDispatcher> m_backendDispatcher;
145    std::unique_ptr<NetworkResourcesData> m_resourcesData;
146    bool m_enabled;
147    bool m_cacheDisabled;
148    bool m_loadingXHRSynchronously;
149    RefPtr<Inspector::InspectorObject> m_extraRequestHeaders;
150
151    HashSet<unsigned long> m_hiddenRequestIdentifiers;
152
153    typedef HashMap<ThreadableLoaderClient*, RefPtr<XHRReplayData>> PendingXHRReplayDataMap;
154    PendingXHRReplayDataMap m_pendingXHRReplayData;
155    // FIXME: InspectorResourceAgent should now be aware of style recalculation.
156    RefPtr<Inspector::TypeBuilder::Network::Initiator> m_styleRecalculationInitiator;
157    bool m_isRecalculatingStyle;
158};
159
160} // namespace WebCore
161
162#endif // ENABLE(INSPECTOR)
163
164#endif // !defined(InspectorResourceAgent_h)
165