Home | History | Annotate | Download | only in inspector
      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/inspector/InspectorPageAgent.h"
     33 #include "core/loader/TextResourceDecoder.h"
     34 #include "core/platform/network/HTTPHeaderMap.h"
     35 #include "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 String &method, const KURL&, bool async, PassRefPtr<FormData>, bool includeCredentials);
     53 
     54     void addHeader(const AtomicString& key, const String& value);
     55     const String& method() const { return m_method; }
     56     const KURL& url() const { return m_url; }
     57     bool async() const { return m_async; }
     58     PassRefPtr<FormData> formData() const { return m_formData; }
     59     const HTTPHeaderMap& headers() const { return m_headers; }
     60     bool includeCredentials() const { return m_includeCredentials; }
     61 
     62 private:
     63     XHRReplayData(const String &method, const KURL&, bool async, PassRefPtr<FormData>, bool includeCredentials);
     64 
     65     String 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 
     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         Resource* cachedResource() const { return m_cachedResource; }
    117         void setResource(Resource* 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         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 
    169 private:
    170     ResourceData* resourceDataForRequestId(const String& requestId);
    171     void ensureNoDataForRequestId(const String& requestId);
    172     bool ensureFreeSpace(size_t);
    173 
    174     Deque<String> m_requestIdsDeque;
    175 
    176     typedef HashMap<String, String> ReusedRequestIds;
    177     ReusedRequestIds m_reusedXHRReplayDataRequestIds;
    178     typedef HashMap<String, ResourceData*> ResourceDataMap;
    179     ResourceDataMap m_requestIdToResourceDataMap;
    180     size_t m_contentSize;
    181     size_t m_maximumResourcesContentSize;
    182     size_t m_maximumSingleResourceContentSize;
    183 };
    184 
    185 } // namespace WebCore
    186 
    187 
    188 #endif // !defined(NetworkResourcesData_h)
    189