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 "core/fetch/TextResourceDecoder.h" 33 #include "core/inspector/InspectorPageAgent.h" 34 #include "platform/network/HTTPHeaderMap.h" 35 #include "platform/weborigin/KURL.h" 36 #include "wtf/Deque.h" 37 #include "wtf/HashMap.h" 38 #include "wtf/RefCounted.h" 39 #include "wtf/text/WTFString.h" 40 41 42 namespace WebCore { 43 44 class Resource; 45 class FormData; 46 class ResourceResponse; 47 class SharedBuffer; 48 class TextResourceDecoder; 49 50 class XHRReplayData : public RefCounted<XHRReplayData> { 51 public: 52 static PassRefPtr<XHRReplayData> create(const AtomicString& method, const KURL&, bool async, PassRefPtr<FormData>, bool includeCredentials); 53 54 void addHeader(const AtomicString& key, const AtomicString& value); method()55 const AtomicString& method() const { return m_method; } url()56 const KURL& url() const { return m_url; } async()57 bool async() const { return m_async; } formData()58 PassRefPtr<FormData> formData() const { return m_formData; } headers()59 const HTTPHeaderMap& headers() const { return m_headers; } includeCredentials()60 bool includeCredentials() const { return m_includeCredentials; } 61 62 private: 63 XHRReplayData(const AtomicString& method, const KURL&, bool async, PassRefPtr<FormData>, bool includeCredentials); 64 65 AtomicString m_method; 66 KURL m_url; 67 bool m_async; 68 RefPtr<FormData> m_formData; 69 HTTPHeaderMap m_headers; 70 bool m_includeCredentials; 71 }; 72 73 class NetworkResourcesData { 74 WTF_MAKE_FAST_ALLOCATED; 75 public: 76 class ResourceData { 77 WTF_MAKE_FAST_ALLOCATED; 78 friend class NetworkResourcesData; 79 public: 80 ResourceData(const String& requestId, const String& loaderId); 81 requestId()82 String requestId() const { return m_requestId; } loaderId()83 String loaderId() const { return m_loaderId; } 84 frameId()85 String frameId() const { return m_frameId; } setFrameId(const String & frameId)86 void setFrameId(const String& frameId) { m_frameId = frameId; } 87 url()88 KURL url() const { return m_url; } setUrl(const KURL & url)89 void setUrl(const KURL& url) { m_url = url; } 90 hasContent()91 bool hasContent() const { return !m_content.isNull(); } content()92 String content() const { return m_content; } 93 void setContent(const String&, bool base64Encoded); 94 base64Encoded()95 bool base64Encoded() const { return m_base64Encoded; } 96 97 unsigned removeContent(); isContentEvicted()98 bool isContentEvicted() const { return m_isContentEvicted; } 99 unsigned evictContent(); 100 type()101 InspectorPageAgent::ResourceType type() const { return m_type; } setType(InspectorPageAgent::ResourceType type)102 void setType(InspectorPageAgent::ResourceType type) { m_type = type; } 103 httpStatusCode()104 int httpStatusCode() const { return m_httpStatusCode; } setHTTPStatusCode(int httpStatusCode)105 void setHTTPStatusCode(int httpStatusCode) { m_httpStatusCode = httpStatusCode; } 106 textEncodingName()107 String textEncodingName() const { return m_textEncodingName; } setTextEncodingName(const String & textEncodingName)108 void setTextEncodingName(const String& textEncodingName) { m_textEncodingName = textEncodingName; } 109 decoder()110 TextResourceDecoder* decoder() const { return m_decoder.get(); } setDecoder(PassOwnPtr<TextResourceDecoder> decoder)111 void setDecoder(PassOwnPtr<TextResourceDecoder> decoder) { m_decoder = decoder; } 112 buffer()113 PassRefPtr<SharedBuffer> buffer() const { return m_buffer; } setBuffer(PassRefPtr<SharedBuffer> buffer)114 void setBuffer(PassRefPtr<SharedBuffer> buffer) { m_buffer = buffer; } 115 cachedResource()116 Resource* cachedResource() const { return m_cachedResource; } setResource(Resource * cachedResource)117 void setResource(Resource* cachedResource) { m_cachedResource = cachedResource; } 118 xhrReplayData()119 XHRReplayData* xhrReplayData() const { return m_xhrReplayData.get(); } setXHRReplayData(XHRReplayData * xhrReplayData)120 void setXHRReplayData(XHRReplayData* xhrReplayData) { m_xhrReplayData = xhrReplayData; } 121 122 private: hasData()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 KURL 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 OwnPtr<TextResourceDecoder> m_decoder; 142 143 RefPtr<SharedBuffer> m_buffer; 144 Resource* 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 addResource(const String& requestId, Resource*); 159 void addResourceSharedBuffer(const String& requestId, PassRefPtr<SharedBuffer>, const String& textEncodingName); 160 ResourceData const* data(const String& requestId); 161 Vector<String> removeResource(Resource*); 162 void clear(const String& preservedLoaderId = String()); 163 164 void setResourcesDataSizeLimits(size_t maximumResourcesContentSize, size_t maximumSingleResourceContentSize); 165 void setXHRReplayData(const String& requestId, XHRReplayData*); 166 void reuseXHRReplayData(const String& requestId, const String& reusedRequestId); 167 XHRReplayData* xhrReplayData(const String& requestId); 168 Vector<ResourceData*> resources(); 169 170 private: 171 ResourceData* resourceDataForRequestId(const String& requestId); 172 void ensureNoDataForRequestId(const String& requestId); 173 bool ensureFreeSpace(size_t); 174 175 Deque<String> m_requestIdsDeque; 176 177 typedef HashMap<String, String> ReusedRequestIds; 178 ReusedRequestIds m_reusedXHRReplayDataRequestIds; 179 typedef HashMap<String, ResourceData*> ResourceDataMap; 180 ResourceDataMap m_requestIdToResourceDataMap; 181 size_t m_contentSize; 182 size_t m_maximumResourcesContentSize; 183 size_t m_maximumSingleResourceContentSize; 184 }; 185 186 } // namespace WebCore 187 188 189 #endif // !defined(NetworkResourcesData_h) 190