1 /* 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Google 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 7 * are met: 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. 13 * 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. 25 */ 26 27 #ifndef ResourceResponseBase_h 28 #define ResourceResponseBase_h 29 30 #include "HTTPHeaderMap.h" 31 #include "KURL.h" 32 33 #include <memory> 34 35 namespace WebCore { 36 37 class ResourceResponse; 38 struct CrossThreadResourceResponseData; 39 40 // Do not use this class directly, use the class ResponseResponse instead 41 class ResourceResponseBase { 42 public: 43 static std::auto_ptr<ResourceResponse> adopt(std::auto_ptr<CrossThreadResourceResponseData>); 44 45 // Gets a copy of the data suitable for passing to another thread. 46 std::auto_ptr<CrossThreadResourceResponseData> copyData() const; 47 isNull()48 bool isNull() const { return m_isNull; } 49 bool isHTTP() const; 50 51 const KURL& url() const; 52 void setURL(const KURL& url); 53 54 const String& mimeType() const; 55 void setMimeType(const String& mimeType); 56 57 long long expectedContentLength() const; 58 void setExpectedContentLength(long long expectedContentLength); 59 60 const String& textEncodingName() const; 61 void setTextEncodingName(const String& name); 62 63 // FIXME should compute this on the fly 64 const String& suggestedFilename() const; 65 void setSuggestedFilename(const String&); 66 67 int httpStatusCode() const; 68 void setHTTPStatusCode(int); 69 70 const String& httpStatusText() const; 71 void setHTTPStatusText(const String&); 72 73 String httpHeaderField(const AtomicString& name) const; 74 void setHTTPHeaderField(const AtomicString& name, const String& value); 75 const HTTPHeaderMap& httpHeaderFields() const; 76 isMultipart()77 bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; } 78 79 bool isAttachment() const; 80 81 // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-odified header. 82 // Leaving it for now but this should go away in favor of generic solution. 83 void setLastModifiedDate(time_t); 84 time_t lastModifiedDate() const; 85 86 // These functions return parsed values of the corresponding response headers. 87 // NaN means that the header was not present or had invalid value. 88 bool cacheControlContainsNoCache() const; 89 bool cacheControlContainsNoStore() const; 90 bool cacheControlContainsMustRevalidate() const; 91 double cacheControlMaxAge() const; 92 double date() const; 93 double age() const; 94 double expires() const; 95 double lastModified() const; 96 97 // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information memoryUsage()98 unsigned memoryUsage() const 99 { 100 // average size, mostly due to URL and Header Map strings 101 return 1280; 102 } 103 104 static bool compare(const ResourceResponse& a, const ResourceResponse& b); 105 106 protected: 107 ResourceResponseBase(); 108 ResourceResponseBase(const KURL& url, const String& mimeType, long long expectedLength, const String& textEncodingName, const String& filename); 109 110 void lazyInit() const; 111 112 // The ResourceResponse subclass may "shadow" this method to lazily initialize platform specific fields platformLazyInit()113 void platformLazyInit() { } 114 115 // The ResourceResponse subclass may "shadow" this method to compare platform specific fields platformCompare(const ResourceResponse &,const ResourceResponse &)116 static bool platformCompare(const ResourceResponse&, const ResourceResponse&) { return true; } 117 118 KURL m_url; 119 String m_mimeType; 120 long long m_expectedContentLength; 121 String m_textEncodingName; 122 String m_suggestedFilename; 123 int m_httpStatusCode; 124 String m_httpStatusText; 125 HTTPHeaderMap m_httpHeaderFields; 126 time_t m_lastModifiedDate; 127 128 bool m_isNull : 1; 129 130 private: 131 void parseCacheControlDirectives() const; 132 133 mutable bool m_haveParsedCacheControlHeader : 1; 134 mutable bool m_haveParsedAgeHeader : 1; 135 mutable bool m_haveParsedDateHeader : 1; 136 mutable bool m_haveParsedExpiresHeader : 1; 137 mutable bool m_haveParsedLastModifiedHeader : 1; 138 139 mutable bool m_cacheControlContainsNoCache : 1; 140 mutable bool m_cacheControlContainsNoStore : 1; 141 mutable bool m_cacheControlContainsMustRevalidate : 1; 142 mutable double m_cacheControlMaxAge; 143 144 mutable double m_age; 145 mutable double m_date; 146 mutable double m_expires; 147 mutable double m_lastModified; 148 }; 149 150 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); } 151 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); } 152 153 struct CrossThreadResourceResponseData { 154 KURL m_url; 155 String m_mimeType; 156 long long m_expectedContentLength; 157 String m_textEncodingName; 158 String m_suggestedFilename; 159 int m_httpStatusCode; 160 String m_httpStatusText; 161 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders; 162 time_t m_lastModifiedDate; 163 }; 164 165 } // namespace WebCore 166 167 #endif // ResourceResponseBase_h 168