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