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 "bindings/core/v8/ScriptString.h"
35#include "core/InspectorFrontend.h"
36#include "core/inspector/InspectorBaseAgent.h"
37#include "platform/Timer.h"
38#include "platform/heap/Handle.h"
39#include "wtf/PassOwnPtr.h"
40#include "wtf/text/WTFString.h"
41
42namespace WTF {
43class String;
44}
45
46namespace blink {
47
48class Resource;
49struct FetchInitiatorInfo;
50class Document;
51class DocumentLoader;
52class FormData;
53class LocalFrame;
54class HTTPHeaderMap;
55class InspectorFrontend;
56class InspectorPageAgent;
57class InstrumentingAgents;
58class JSONObject;
59class KURL;
60class NetworkResourcesData;
61class Page;
62class ResourceError;
63class ResourceLoader;
64class ResourceRequest;
65class ResourceResponse;
66class ThreadableLoaderClient;
67class XHRReplayData;
68class XMLHttpRequest;
69
70class WebSocketHandshakeRequest;
71class WebSocketHandshakeResponse;
72
73typedef String ErrorString;
74
75class InspectorResourceAgent FINAL : public InspectorBaseAgent<InspectorResourceAgent>, public InspectorBackendDispatcher::NetworkCommandHandler {
76public:
77    static PassOwnPtrWillBeRawPtr<InspectorResourceAgent> create(InspectorPageAgent* pageAgent)
78    {
79        return adoptPtrWillBeNoop(new InspectorResourceAgent(pageAgent));
80    }
81
82    virtual void setFrontend(InspectorFrontend*) OVERRIDE;
83    virtual void clearFrontend() OVERRIDE;
84    virtual void restore() OVERRIDE;
85
86    virtual ~InspectorResourceAgent();
87    virtual void trace(Visitor*) OVERRIDE;
88
89    // Called from instrumentation.
90    void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse, const FetchInitiatorInfo&);
91    void markResourceAsCached(unsigned long identifier);
92    void didReceiveResourceResponse(LocalFrame*, unsigned long identifier, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
93    void didReceiveData(LocalFrame*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
94    void didFinishLoading(unsigned long identifier, DocumentLoader*, double monotonicFinishTime, int64_t encodedDataLength);
95    void didReceiveCORSRedirectResponse(LocalFrame*, unsigned long identifier, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
96    void didFailLoading(unsigned long identifier, const ResourceError&);
97    void didCommitLoad(LocalFrame*, DocumentLoader*);
98    void scriptImported(unsigned long identifier, const String& sourceString);
99    void didReceiveScriptResponse(unsigned long identifier);
100    bool shouldForceCORSPreflight();
101
102    void documentThreadableLoaderStartedLoadingForClient(unsigned long identifier, ThreadableLoaderClient*);
103    void willLoadXHR(XMLHttpRequest*, ThreadableLoaderClient*, const AtomicString& method, const KURL&, bool async, PassRefPtr<FormData> body, const HTTPHeaderMap& headers, bool includeCrendentials);
104    void didFailXHRLoading(XMLHttpRequest*, ThreadableLoaderClient*);
105    void didFinishXHRLoading(XMLHttpRequest*, ThreadableLoaderClient*, unsigned long identifier, ScriptString sourceString, const AtomicString&, const String&, const String&, unsigned);
106
107    void willDestroyResource(Resource*);
108
109    void applyUserAgentOverride(String* userAgent);
110
111    // FIXME: InspectorResourceAgent should not be aware of style recalculation.
112    void willRecalculateStyle(Document*);
113    void didRecalculateStyle(int);
114    void didScheduleStyleRecalculation(Document*);
115
116    void frameScheduledNavigation(LocalFrame*, double);
117    void frameClearedScheduledNavigation(LocalFrame*);
118
119    PassRefPtr<TypeBuilder::Network::Initiator> buildInitiatorObject(Document*, const FetchInitiatorInfo&);
120
121    void didCreateWebSocket(Document*, unsigned long identifier, const KURL& requestURL, const String&);
122    void willSendWebSocketHandshakeRequest(Document*, unsigned long identifier, const WebSocketHandshakeRequest*);
123    void didReceiveWebSocketHandshakeResponse(Document*, unsigned long identifier, const WebSocketHandshakeRequest*, const WebSocketHandshakeResponse*);
124    void didCloseWebSocket(Document*, unsigned long identifier);
125    void didReceiveWebSocketFrame(unsigned long identifier, int opCode, bool masked, const char* payload, size_t payloadLength);
126    void didSendWebSocketFrame(unsigned long identifier, int opCode, bool masked, const char* payload, size_t payloadLength);
127    void didReceiveWebSocketFrameError(unsigned long identifier, const String&);
128
129    // called from Internals for layout test purposes.
130    void setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize);
131
132    // Called from frontend
133    virtual void enable(ErrorString*) OVERRIDE;
134    virtual void disable(ErrorString*) OVERRIDE;
135    virtual void setUserAgentOverride(ErrorString*, const String& userAgent) OVERRIDE;
136    virtual void setExtraHTTPHeaders(ErrorString*, const RefPtr<JSONObject>&) OVERRIDE;
137    virtual void getResponseBody(ErrorString*, const String& requestId, String* content, bool* base64Encoded) OVERRIDE;
138
139    virtual void replayXHR(ErrorString*, const String& requestId) OVERRIDE;
140
141    virtual void canClearBrowserCache(ErrorString*, bool*) OVERRIDE;
142    virtual void canClearBrowserCookies(ErrorString*, bool*) OVERRIDE;
143    virtual void emulateNetworkConditions(ErrorString*, bool, double, double, double) OVERRIDE;
144    virtual void setCacheDisabled(ErrorString*, bool cacheDisabled) OVERRIDE;
145
146    virtual void loadResourceForFrontend(ErrorString*, const String& frameId, const String& url, const RefPtr<JSONObject>* requestHeaders, PassRefPtrWillBeRawPtr<LoadResourceForFrontendCallback>) OVERRIDE;
147
148    // Called from other agents.
149    void setHostId(const String&);
150    bool fetchResourceContent(Document*, const KURL&, String* content, bool* base64Encoded);
151
152private:
153    explicit InspectorResourceAgent(InspectorPageAgent*);
154
155    void enable();
156    void delayedRemoveReplayXHR(XMLHttpRequest*);
157    void removeFinishedReplayXHRFired(Timer<InspectorResourceAgent>*);
158
159    RawPtrWillBeMember<InspectorPageAgent> m_pageAgent;
160    InspectorFrontend::Network* m_frontend;
161    String m_userAgentOverride;
162    String m_hostId;
163    OwnPtr<NetworkResourcesData> m_resourcesData;
164
165    typedef WillBeHeapHashMap<ThreadableLoaderClient*, RefPtrWillBeMember<XHRReplayData> > PendingXHRReplayDataMap;
166    PendingXHRReplayDataMap m_pendingXHRReplayData;
167
168    typedef HashMap<String, RefPtr<TypeBuilder::Network::Initiator> > FrameNavigationInitiatorMap;
169    FrameNavigationInitiatorMap m_frameNavigationInitiatorMap;
170
171    // FIXME: InspectorResourceAgent should now be aware of style recalculation.
172    RefPtr<TypeBuilder::Network::Initiator> m_styleRecalculationInitiator;
173    bool m_isRecalculatingStyle;
174
175    WillBeHeapHashSet<RefPtrWillBeMember<XMLHttpRequest> > m_replayXHRs;
176    WillBeHeapHashSet<RefPtrWillBeMember<XMLHttpRequest> > m_replayXHRsToBeDeleted;
177    Timer<InspectorResourceAgent> m_removeFinishedReplayXHRTimer;
178};
179
180} // namespace blink
181
182
183#endif // !defined(InspectorResourceAgent_h)
184