1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
21 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#pragma once
31
32#include "InspectorPageAgent.h"
33#include <wtf/Deque.h>
34#include <wtf/HashMap.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39class CachedResource;
40class ResourceResponse;
41class TextResourceDecoder;
42class SharedBuffer;
43
44class NetworkResourcesData {
45 WTF_MAKE_FAST_ALLOCATED;
46public:
47 class ResourceData {
48 WTF_MAKE_FAST_ALLOCATED;
49 friend class NetworkResourcesData;
50 public:
51 ResourceData(const String& requestId, const String& loaderId);
52
53 const String& requestId() const { return m_requestId; }
54 const String& loaderId() const { return m_loaderId; }
55
56 const String& frameId() const { return m_frameId; }
57 void setFrameId(const String& frameId) { m_frameId = frameId; }
58
59 const String& url() const { return m_url; }
60 void setURL(const String& url) { m_url = url; }
61
62 bool hasContent() const { return !m_content.isNull(); }
63 const String& content() const { return m_content; }
64 void setContent(const String&, bool base64Encoded);
65
66 bool base64Encoded() const { return m_base64Encoded; }
67
68 unsigned removeContent();
69 unsigned evictContent();
70 bool isContentEvicted() const { return m_isContentEvicted; }
71
72 InspectorPageAgent::ResourceType type() const { return m_type; }
73 void setType(InspectorPageAgent::ResourceType type) { m_type = type; }
74
75 int httpStatusCode() const { return m_httpStatusCode; }
76 void setHTTPStatusCode(int httpStatusCode) { m_httpStatusCode = httpStatusCode; }
77
78 const String& textEncodingName() const { return m_textEncodingName; }
79 void setTextEncodingName(const String& textEncodingName) { m_textEncodingName = textEncodingName; }
80
81 RefPtr<TextResourceDecoder> decoder() const { return m_decoder.copyRef(); }
82 void setDecoder(RefPtr<TextResourceDecoder>&& decoder) { m_decoder = WTFMove(decoder); }
83
84 RefPtr<SharedBuffer> buffer() const { return m_buffer.copyRef(); }
85 void setBuffer(RefPtr<SharedBuffer>&& buffer) { m_buffer = WTFMove(buffer); }
86
87 const Optional<CertificateInfo>& certificateInfo() const { return m_certificateInfo; }
88 void setCertificateInfo(const Optional<CertificateInfo>& certificateInfo) { m_certificateInfo = certificateInfo; }
89
90 CachedResource* cachedResource() const { return m_cachedResource; }
91 void setCachedResource(CachedResource* cachedResource) { m_cachedResource = cachedResource; }
92
93 bool forceBufferData() const { return m_forceBufferData; }
94 void setForceBufferData(bool force) { m_forceBufferData = force; }
95
96 bool hasBufferedData() const { return m_dataBuffer; }
97
98 private:
99 bool hasData() const { return m_dataBuffer; }
100 size_t dataLength() const;
101 void appendData(const char* data, size_t dataLength);
102 size_t decodeDataToContent();
103
104 String m_requestId;
105 String m_loaderId;
106 String m_frameId;
107 String m_url;
108 String m_content;
109 String m_textEncodingName;
110 RefPtr<TextResourceDecoder> m_decoder;
111 RefPtr<SharedBuffer> m_dataBuffer;
112 RefPtr<SharedBuffer> m_buffer;
113 Optional<CertificateInfo> m_certificateInfo;
114 CachedResource* m_cachedResource { nullptr };
115 InspectorPageAgent::ResourceType m_type { InspectorPageAgent::OtherResource };
116 int m_httpStatusCode { 0 };
117 bool m_isContentEvicted { false };
118 bool m_base64Encoded { false };
119 bool m_forceBufferData { false };
120 };
121
122 NetworkResourcesData();
123 ~NetworkResourcesData();
124
125 void resourceCreated(const String& requestId, const String& loaderId, InspectorPageAgent::ResourceType);
126 void resourceCreated(const String& requestId, const String& loaderId, CachedResource&);
127 void responseReceived(const String& requestId, const String& frameId, const ResourceResponse&, InspectorPageAgent::ResourceType, bool forceBufferData);
128 void setResourceType(const String& requestId, InspectorPageAgent::ResourceType);
129 InspectorPageAgent::ResourceType resourceType(const String& requestId);
130 void setResourceContent(const String& requestId, const String& content, bool base64Encoded = false);
131 ResourceData const* maybeAddResourceData(const String& requestId, const char* data, size_t dataLength);
132 void maybeDecodeDataToContent(const String& requestId);
133 void addCachedResource(const String& requestId, CachedResource*);
134 void addResourceSharedBuffer(const String& requestId, RefPtr<SharedBuffer>&&, const String& textEncodingName);
135 ResourceData const* data(const String& requestId);
136 Vector<String> removeCachedResource(CachedResource*);
137 void clear(Optional<String> preservedLoaderId = WTF::nullopt);
138 Vector<ResourceData*> resources();
139
140private:
141 ResourceData* resourceDataForRequestId(const String& requestId);
142 void ensureNoDataForRequestId(const String& requestId);
143 bool ensureFreeSpace(size_t);
144
145 Deque<String> m_requestIdsDeque;
146 HashMap<String, std::unique_ptr<ResourceData>> m_requestIdToResourceDataMap;
147 size_t m_contentSize { 0 };
148 size_t m_maximumResourcesContentSize;
149 size_t m_maximumSingleResourceContentSize;
150};
151
152} // namespace WebCore
153