1/*
2 * Copyright (C) 2011 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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. 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 *
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef NetworkResourcesData_h
30#define NetworkResourcesData_h
31
32#include "HTTPHeaderMap.h"
33#include "InspectorPageAgent.h"
34#include "URL.h"
35#include "TextResourceDecoder.h"
36#include <wtf/Deque.h>
37#include <wtf/HashMap.h>
38#include <wtf/RefCounted.h>
39#include <wtf/text/StringBuilder.h>
40#include <wtf/text/WTFString.h>
41
42#if ENABLE(INSPECTOR)
43
44namespace WebCore {
45
46class CachedResource;
47class FormData;
48class ResourceResponse;
49class SharedBuffer;
50class TextResourceDecoder;
51
52class XHRReplayData : public RefCounted<XHRReplayData> {
53public:
54    static PassRefPtr<XHRReplayData> create(const String &method, const URL&, bool async, PassRefPtr<FormData>, const HTTPHeaderMap& headers, bool includeCredentials);
55
56    const String& method() const { return m_method; }
57    const URL& url() const { return m_url; }
58    bool async() const { return m_async; }
59    PassRefPtr<FormData> formData() const { return m_formData; }
60    const HTTPHeaderMap& headers() const { return m_headers; }
61    bool includeCredentials() const { return m_includeCredentials; }
62private:
63    XHRReplayData(const String &method, const URL&, bool async, PassRefPtr<FormData>, const HTTPHeaderMap& headers, bool includeCredentials);
64
65    String m_method;
66    URL m_url;
67    bool m_async;
68    RefPtr<FormData> m_formData;
69    const HTTPHeaderMap m_headers;
70    bool m_includeCredentials;
71};
72
73class NetworkResourcesData {
74    WTF_MAKE_FAST_ALLOCATED;
75public:
76    class ResourceData {
77        WTF_MAKE_FAST_ALLOCATED;
78        friend class NetworkResourcesData;
79    public:
80        ResourceData(const String& requestId, const String& loaderId);
81
82        String requestId() const { return m_requestId; }
83        String loaderId() const { return m_loaderId; }
84
85        String frameId() const { return m_frameId; }
86        void setFrameId(const String& frameId) { m_frameId = frameId; }
87
88        String url() const { return m_url; }
89        void setUrl(const String& url) { m_url = url; }
90
91        bool hasContent() const { return !m_content.isNull(); }
92        String content() const { return m_content; }
93        void setContent(const String&, bool base64Encoded);
94
95        bool base64Encoded() const { return m_base64Encoded; }
96
97        unsigned removeContent();
98        bool isContentEvicted() const { return m_isContentEvicted; }
99        unsigned evictContent();
100
101        InspectorPageAgent::ResourceType type() const { return m_type; }
102        void setType(InspectorPageAgent::ResourceType type) { m_type = type; }
103
104        int httpStatusCode() const { return m_httpStatusCode; }
105        void setHTTPStatusCode(int httpStatusCode) { m_httpStatusCode = httpStatusCode; }
106
107        String textEncodingName() const { return m_textEncodingName; }
108        void setTextEncodingName(const String& textEncodingName) { m_textEncodingName = textEncodingName; }
109
110        PassRefPtr<TextResourceDecoder> decoder() const { return m_decoder; }
111        void setDecoder(PassRefPtr<TextResourceDecoder> decoder) { m_decoder = decoder; }
112
113        PassRefPtr<SharedBuffer> buffer() const { return m_buffer; }
114        void setBuffer(PassRefPtr<SharedBuffer> buffer) { m_buffer = buffer; }
115
116        CachedResource* cachedResource() const { return m_cachedResource; }
117        void setCachedResource(CachedResource* cachedResource) { m_cachedResource = cachedResource; }
118
119        XHRReplayData* xhrReplayData() const { return m_xhrReplayData.get(); }
120        void setXHRReplayData(XHRReplayData* xhrReplayData) { m_xhrReplayData = xhrReplayData; }
121
122    private:
123        bool hasData() const { return m_dataBuffer; }
124        size_t dataLength() const;
125        void appendData(const char* data, size_t dataLength);
126        size_t decodeDataToContent();
127
128        String m_requestId;
129        String m_loaderId;
130        String m_frameId;
131        String m_url;
132        String m_content;
133        RefPtr<XHRReplayData> m_xhrReplayData;
134        bool m_base64Encoded;
135        RefPtr<SharedBuffer> m_dataBuffer;
136        bool m_isContentEvicted;
137        InspectorPageAgent::ResourceType m_type;
138        int m_httpStatusCode;
139
140        String m_textEncodingName;
141        RefPtr<TextResourceDecoder> m_decoder;
142
143        RefPtr<SharedBuffer> m_buffer;
144        CachedResource* m_cachedResource;
145    };
146
147    NetworkResourcesData();
148
149    ~NetworkResourcesData();
150
151    void resourceCreated(const String& requestId, const String& loaderId);
152    void responseReceived(const String& requestId, const String& frameId, const ResourceResponse&);
153    void setResourceType(const String& requestId, InspectorPageAgent::ResourceType);
154    InspectorPageAgent::ResourceType resourceType(const String& requestId);
155    void setResourceContent(const String& requestId, const String& content, bool base64Encoded = false);
156    void maybeAddResourceData(const String& requestId, const char* data, size_t dataLength);
157    void maybeDecodeDataToContent(const String& requestId);
158    void addCachedResource(const String& requestId, CachedResource*);
159    void addResourceSharedBuffer(const String& requestId, PassRefPtr<SharedBuffer>, const String& textEncodingName);
160    ResourceData const* data(const String& requestId);
161    Vector<String> removeCachedResource(CachedResource*);
162    void clear(const String& preservedLoaderId = String());
163
164    void setXHRReplayData(const String& requestId, XHRReplayData*);
165    void reuseXHRReplayData(const String& requestId, const String& reusedRequestId);
166    XHRReplayData* xhrReplayData(const String& requestId);
167
168private:
169    ResourceData* resourceDataForRequestId(const String& requestId);
170    void ensureNoDataForRequestId(const String& requestId);
171    bool ensureFreeSpace(size_t);
172
173    Deque<String> m_requestIdsDeque;
174
175    typedef HashMap<String, String> ReusedRequestIds;
176    ReusedRequestIds m_reusedXHRReplayDataRequestIds;
177    typedef HashMap<String, ResourceData*> ResourceDataMap;
178    ResourceDataMap m_requestIdToResourceDataMap;
179    size_t m_contentSize;
180    size_t m_maximumResourcesContentSize;
181    size_t m_maximumSingleResourceContentSize;
182};
183
184} // namespace WebCore
185
186#endif // ENABLE(INSPECTOR)
187
188#endif // !defined(NetworkResourcesData_h)
189