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 "InspectorFrontend.h"
35 #include "PlatformString.h"
36 
37 #include <wtf/OwnPtr.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/Vector.h>
40 
41 #if ENABLE(INSPECTOR)
42 
43 namespace WTF {
44 class String;
45 }
46 
47 namespace WebCore {
48 
49 class CachedResource;
50 class Document;
51 class DocumentLoader;
52 class Frame;
53 class InspectorArray;
54 class InspectorFrontend;
55 class InspectorFrontendProxy;
56 class InspectorObject;
57 class InspectorPageAgent;
58 class InspectorState;
59 class InstrumentingAgents;
60 class KURL;
61 class EventsCollector;
62 class Page;
63 class ResourceError;
64 class ResourceRequest;
65 class ResourceResponse;
66 class SharedBuffer;
67 
68 #if ENABLE(WEB_SOCKETS)
69 class WebSocketHandshakeRequest;
70 class WebSocketHandshakeResponse;
71 #endif
72 
73 typedef String ErrorString;
74 
75 class InspectorResourceAgent : public RefCounted<InspectorResourceAgent> {
76 public:
create(InstrumentingAgents * instrumentingAgents,InspectorPageAgent * pageAgent,InspectorState * state)77     static PassRefPtr<InspectorResourceAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorState* state)
78     {
79         return adoptRef(new InspectorResourceAgent(instrumentingAgents, pageAgent, state));
80     }
81 
82     void setFrontend(InspectorFrontend*);
83     void clearFrontend();
84     void restore();
85 
86     static PassRefPtr<InspectorResourceAgent> restore(Page*, InspectorState*, InspectorFrontend*);
87 
88     ~InspectorResourceAgent();
89 
90     void willSendRequest(unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
91     void markResourceAsCached(unsigned long identifier);
92     void didReceiveResponse(unsigned long identifier, DocumentLoader* laoder, const ResourceResponse&);
93     void didReceiveContentLength(unsigned long identifier, int dataLength, int encodedDataLength);
94     void didFinishLoading(unsigned long identifier, double finishTime);
95     void didFailLoading(unsigned long identifier, const ResourceError&);
96     void didLoadResourceFromMemoryCache(DocumentLoader*, const CachedResource*);
97     void setInitialScriptContent(unsigned long identifier, const String& sourceString);
98     void setInitialXHRContent(unsigned long identifier, const String& sourceString);
99     void applyUserAgentOverride(String* userAgent);
100 
101 #if ENABLE(WEB_SOCKETS)
102     void didCreateWebSocket(unsigned long identifier, const KURL& requestURL);
103     void willSendWebSocketHandshakeRequest(unsigned long identifier, const WebSocketHandshakeRequest&);
104     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const WebSocketHandshakeResponse&);
105     void didCloseWebSocket(unsigned long identifier);
106 #endif
107 
108     bool backgroundEventsCollectionEnabled();
109 
110     // Called from frontend
111     void enable(ErrorString*);
112     void disable(ErrorString*);
113     void setUserAgentOverride(ErrorString*, const String& userAgent);
114     void setExtraHeaders(ErrorString*, PassRefPtr<InspectorObject>);
115 
116 
117 private:
118     InspectorResourceAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorState*);
119 
120     void enable();
121 
122     InstrumentingAgents* m_instrumentingAgents;
123     InspectorPageAgent* m_pageAgent;
124     InspectorState* m_state;
125     OwnPtr<EventsCollector> m_eventsCollector;
126     InspectorFrontend::Network* m_frontend;
127     OwnPtr<InspectorFrontend::Network> m_mockFrontend;
128     OwnPtr<InspectorFrontendProxy> m_inspectorFrontendProxy;
129     String m_userAgentOverride;
130 };
131 
132 } // namespace WebCore
133 
134 #endif // ENABLE(INSPECTOR)
135 
136 #endif // !defined(InspectorResourceAgent_h)
137