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