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 "InspectorBaseAgent.h"
35#include "InspectorFrontend.h"
36
37#include <wtf/HashSet.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/RefCounted.h>
40#include <wtf/Vector.h>
41#include <wtf/text/WTFString.h>
42
43#if ENABLE(INSPECTOR)
44
45namespace WTF {
46class String;
47}
48
49namespace WebCore {
50
51class CachedResource;
52class Document;
53class DocumentLoader;
54class FormData;
55class Frame;
56class HTTPHeaderMap;
57class InspectorArray;
58class InspectorClient;
59class InspectorFrontend;
60class InspectorObject;
61class InspectorPageAgent;
62class InspectorState;
63class InstrumentingAgents;
64class KURL;
65class NetworkResourcesData;
66class Page;
67class ResourceError;
68class ResourceLoader;
69class ResourceRequest;
70class ResourceResponse;
71class SharedBuffer;
72class ThreadableLoaderClient;
73class XHRReplayData;
74class XMLHttpRequest;
75
76#if ENABLE(WEB_SOCKETS)
77struct WebSocketFrame;
78#endif
79
80typedef String ErrorString;
81
82class InspectorResourceAgent : public InspectorBaseAgent<InspectorResourceAgent>, public InspectorBackendDispatcher::NetworkCommandHandler {
83public:
84    static PassOwnPtr<InspectorResourceAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorClient* client, InspectorCompositeState* state)
85    {
86        return adoptPtr(new InspectorResourceAgent(instrumentingAgents, pageAgent, client, state));
87    }
88
89    virtual void setFrontend(InspectorFrontend*);
90    virtual void clearFrontend();
91    virtual void restore();
92
93    static PassRefPtr<InspectorResourceAgent> restore(Page*, InspectorCompositeState*, InspectorFrontend*);
94
95    ~InspectorResourceAgent();
96
97    void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
98    void markResourceAsCached(unsigned long identifier);
99    void didReceiveResponse(unsigned long identifier, DocumentLoader* laoder, const ResourceResponse&, ResourceLoader*);
100    void didReceiveData(unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
101    void didFinishLoading(unsigned long identifier, DocumentLoader*, double finishTime);
102    void didFailLoading(unsigned long identifier, DocumentLoader*, const ResourceError&);
103    void didLoadResourceFromMemoryCache(DocumentLoader*, CachedResource*);
104    void mainFrameNavigated(DocumentLoader*);
105    void setInitialScriptContent(unsigned long identifier, const String& sourceString);
106    void didReceiveScriptResponse(unsigned long identifier);
107
108    void documentThreadableLoaderStartedLoadingForClient(unsigned long identifier, ThreadableLoaderClient*);
109    void willLoadXHR(ThreadableLoaderClient*, const String& method, const KURL&, bool async, PassRefPtr<FormData> body, const HTTPHeaderMap& headers, bool includeCrendentials);
110    void didFailXHRLoading(ThreadableLoaderClient*);
111    void didFinishXHRLoading(ThreadableLoaderClient*, unsigned long identifier, const String& sourceString);
112    void didReceiveXHRResponse(unsigned long identifier);
113    void willLoadXHRSynchronously();
114    void didLoadXHRSynchronously();
115
116    void willDestroyCachedResource(CachedResource*);
117
118    void applyUserAgentOverride(String* userAgent);
119
120    // FIXME: InspectorResourceAgent should now be aware of style recalculation.
121    void willRecalculateStyle();
122    void didRecalculateStyle();
123    void didScheduleStyleRecalculation(Document*);
124
125    PassRefPtr<TypeBuilder::Network::Initiator> buildInitiatorObject(Document*);
126
127#if ENABLE(WEB_SOCKETS)
128    void didCreateWebSocket(unsigned long identifier, const KURL& requestURL);
129    void willSendWebSocketHandshakeRequest(unsigned long identifier, const ResourceRequest&);
130    void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const ResourceResponse&);
131    void didCloseWebSocket(unsigned long identifier);
132    void didReceiveWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
133    void didSendWebSocketFrame(unsigned long identifier, const WebSocketFrame&);
134    void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
135#endif
136
137    // Called from Internals for layout test purposes.
138    void setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize);
139
140    // Called from frontend.
141    virtual void enable(ErrorString*);
142    virtual void disable(ErrorString*);
143    virtual void setUserAgentOverride(ErrorString*, const String& userAgent);
144    virtual void setExtraHTTPHeaders(ErrorString*, const RefPtr<InspectorObject>&);
145    virtual void getResponseBody(ErrorString*, const String& requestId, String* content, bool* base64Encoded);
146    virtual void replayXHR(ErrorString*, const String& requestId);
147    virtual void canClearBrowserCache(ErrorString*, bool*);
148    virtual void clearBrowserCache(ErrorString*);
149    virtual void canClearBrowserCookies(ErrorString*, bool*);
150    virtual void clearBrowserCookies(ErrorString*);
151    virtual void setCacheDisabled(ErrorString*, bool cacheDisabled);
152    virtual void loadResource(ErrorString*, const String& frameId, const String& url, PassRefPtr<LoadResourceCallback>) override;
153
154private:
155    InspectorResourceAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorClient*, InspectorCompositeState*);
156
157    void enable();
158
159    InspectorPageAgent* m_pageAgent;
160    InspectorClient* m_client;
161    InspectorFrontend::Network* m_frontend;
162    String m_userAgentOverride;
163    OwnPtr<NetworkResourcesData> m_resourcesData;
164    bool m_loadingXHRSynchronously;
165
166    HashSet<unsigned long> m_hiddenRequestIdentifiers;
167
168    typedef HashMap<ThreadableLoaderClient*, RefPtr<XHRReplayData> > PendingXHRReplayDataMap;
169    PendingXHRReplayDataMap m_pendingXHRReplayData;
170    // FIXME: InspectorResourceAgent should now be aware of style recalculation.
171    RefPtr<TypeBuilder::Network::Initiator> m_styleRecalculationInitiator;
172    bool m_isRecalculatingStyle;
173};
174
175} // namespace WebCore
176
177#endif // ENABLE(INSPECTOR)
178
179#endif // !defined(InspectorResourceAgent_h)
180