2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef ResourceResponseBase_h
28 #define ResourceResponseBase_h
30 #include "HTTPHeaderMap.h"
32 #include "ResourceLoadInfo.h"
33 #include "ResourceLoadTiming.h"
35 #include <wtf/PassOwnPtr.h>
36 #include <wtf/RefPtr.h>
39 #include <sys/time.h> // For time_t structure.
44 class ResourceResponse;
45 struct CrossThreadResourceResponseData;
47 // Do not use this class directly, use the class ResponseResponse instead
48 class ResourceResponseBase {
49 WTF_MAKE_FAST_ALLOCATED;
51 static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
53 // Gets a copy of the data suitable for passing to another thread.
54 PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
56 bool isNull() const { return m_isNull; }
59 const KURL& url() const;
60 void setURL(const KURL& url);
62 const String& mimeType() const;
63 void setMimeType(const String& mimeType);
65 long long expectedContentLength() const;
66 void setExpectedContentLength(long long expectedContentLength);
68 const String& textEncodingName() const;
69 void setTextEncodingName(const String& name);
71 // FIXME: Should compute this on the fly.
72 // There should not be a setter exposed, as suggested file name is determined based on other headers in a manner that WebCore does not necessarily know about.
73 const String& suggestedFilename() const;
74 void setSuggestedFilename(const String&);
76 int httpStatusCode() const;
77 void setHTTPStatusCode(int);
79 const String& httpStatusText() const;
80 void setHTTPStatusText(const String&);
82 String httpHeaderField(const AtomicString& name) const;
83 String httpHeaderField(const char* name) const;
84 void setHTTPHeaderField(const AtomicString& name, const String& value);
85 void addHTTPHeaderField(const AtomicString& name, const String& value);
86 const HTTPHeaderMap& httpHeaderFields() const;
88 bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
90 bool isAttachment() const;
92 // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
93 // Leaving it for now but this should go away in favor of generic solution.
94 void setLastModifiedDate(time_t);
95 time_t lastModifiedDate() const;
97 // These functions return parsed values of the corresponding response headers.
98 // NaN means that the header was not present or had invalid value.
99 bool cacheControlContainsNoCache() const;
100 bool cacheControlContainsNoStore() const;
101 bool cacheControlContainsMustRevalidate() const;
102 bool hasCacheValidatorFields() const;
103 double cacheControlMaxAge() const;
106 double expires() const;
107 double lastModified() const;
109 unsigned connectionID() const;
110 void setConnectionID(unsigned);
112 bool connectionReused() const;
113 void setConnectionReused(bool);
115 bool wasCached() const;
116 void setWasCached(bool);
118 ResourceLoadTiming* resourceLoadTiming() const;
119 void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
121 PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
122 void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
124 // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
125 unsigned memoryUsage() const
127 // average size, mostly due to URL and Header Map strings
131 void reportMemoryUsage(MemoryObjectInfo*) const;
133 static bool compare(const ResourceResponse&, const ResourceResponse&);
139 CommonAndUncommonFields,
143 ResourceResponseBase();
144 ResourceResponseBase(const KURL& url, const String& mimeType, long long expectedLength, const String& textEncodingName, const String& filename);
146 void lazyInit(InitLevel) const;
148 // The ResourceResponse subclass may "shadow" this method to lazily initialize platform specific fields
149 void platformLazyInit(InitLevel) { }
151 // The ResourceResponse subclass may "shadow" this method to compare platform specific fields
152 static bool platformCompare(const ResourceResponse&, const ResourceResponse&) { return true; }
156 long long m_expectedContentLength;
157 String m_textEncodingName;
158 String m_suggestedFilename;
159 int m_httpStatusCode;
160 String m_httpStatusText;
161 HTTPHeaderMap m_httpHeaderFields;
162 time_t m_lastModifiedDate;
163 bool m_wasCached : 1;
164 unsigned m_connectionID;
165 bool m_connectionReused : 1;
166 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
167 RefPtr<ResourceLoadInfo> m_resourceLoadInfo;
172 const ResourceResponse& asResourceResponse() const;
173 void parseCacheControlDirectives() const;
174 void updateHeaderParsedState(const AtomicString& name);
176 mutable bool m_haveParsedCacheControlHeader : 1;
177 mutable bool m_haveParsedAgeHeader : 1;
178 mutable bool m_haveParsedDateHeader : 1;
179 mutable bool m_haveParsedExpiresHeader : 1;
180 mutable bool m_haveParsedLastModifiedHeader : 1;
182 mutable bool m_cacheControlContainsNoCache : 1;
183 mutable bool m_cacheControlContainsNoStore : 1;
184 mutable bool m_cacheControlContainsMustRevalidate : 1;
185 mutable double m_cacheControlMaxAge;
187 mutable double m_age;
188 mutable double m_date;
189 mutable double m_expires;
190 mutable double m_lastModified;
193 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); }
194 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
196 struct CrossThreadResourceResponseDataBase {
197 WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseDataBase); WTF_MAKE_FAST_ALLOCATED;
199 CrossThreadResourceResponseDataBase() { }
202 long long m_expectedContentLength;
203 String m_textEncodingName;
204 String m_suggestedFilename;
205 int m_httpStatusCode;
206 String m_httpStatusText;
207 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
208 time_t m_lastModifiedDate;
209 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
212 } // namespace WebCore
214 #endif // ResourceResponseBase_h