Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / network / ResourceResponse.h
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 ResourceResponse_h
28 #define ResourceResponse_h
29
30 #include "platform/PlatformExport.h"
31 #include "platform/blob/BlobData.h"
32 #include "platform/network/HTTPHeaderMap.h"
33 #include "platform/network/HTTPParsers.h"
34 #include "platform/network/ResourceLoadInfo.h"
35 #include "platform/network/ResourceLoadTiming.h"
36 #include "platform/weborigin/KURL.h"
37 #include "public/platform/WebServiceWorkerResponseType.h"
38 #include "wtf/PassOwnPtr.h"
39 #include "wtf/RefPtr.h"
40 #include "wtf/text/CString.h"
41
42 namespace blink {
43
44 struct CrossThreadResourceResponseData;
45
46 class PLATFORM_EXPORT ResourceResponse {
47     WTF_MAKE_FAST_ALLOCATED;
48 public:
49     enum HTTPVersion { Unknown, HTTP_0_9, HTTP_1_0, HTTP_1_1 };
50
51     class ExtraData : public RefCounted<ExtraData> {
52     public:
53         virtual ~ExtraData() { }
54     };
55
56     static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
57
58     // Gets a copy of the data suitable for passing to another thread.
59     PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
60
61     ResourceResponse();
62     ResourceResponse(const KURL&, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename);
63
64     bool isNull() const { return m_isNull; }
65     bool isHTTP() const;
66
67     const KURL& url() const;
68     void setURL(const KURL&);
69
70     const AtomicString& mimeType() const;
71     void setMimeType(const AtomicString&);
72
73     long long expectedContentLength() const;
74     void setExpectedContentLength(long long);
75
76     const AtomicString& textEncodingName() const;
77     void setTextEncodingName(const AtomicString&);
78
79     // FIXME: Should compute this on the fly.
80     // 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.
81     const String& suggestedFilename() const;
82     void setSuggestedFilename(const String&);
83
84     int httpStatusCode() const;
85     void setHTTPStatusCode(int);
86
87     const AtomicString& httpStatusText() const;
88     void setHTTPStatusText(const AtomicString&);
89
90     const AtomicString& httpHeaderField(const AtomicString& name) const;
91     const AtomicString& httpHeaderField(const char* name) const;
92     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
93     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
94     void clearHTTPHeaderField(const AtomicString& name);
95     const HTTPHeaderMap& httpHeaderFields() const;
96
97     bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
98
99     bool isAttachment() const;
100
101     // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
102     // Leaving it for now but this should go away in favor of generic solution.
103     void setLastModifiedDate(time_t);
104     time_t lastModifiedDate() const;
105
106     // These functions return parsed values of the corresponding response headers.
107     // NaN means that the header was not present or had invalid value.
108     bool cacheControlContainsNoCache();
109     bool cacheControlContainsNoStore();
110     bool cacheControlContainsMustRevalidate();
111     bool hasCacheValidatorFields() const;
112     double cacheControlMaxAge();
113     double date() const;
114     double age() const;
115     double expires() const;
116     double lastModified() const;
117
118     unsigned connectionID() const;
119     void setConnectionID(unsigned);
120
121     bool connectionReused() const;
122     void setConnectionReused(bool);
123
124     bool wasCached() const;
125     void setWasCached(bool);
126
127     ResourceLoadTiming* resourceLoadTiming() const;
128     void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
129
130     PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
131     void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
132
133     HTTPVersion httpVersion() const { return m_httpVersion; }
134     void setHTTPVersion(HTTPVersion version) { m_httpVersion = version; }
135
136     const CString& getSecurityInfo() const { return m_securityInfo; }
137     void setSecurityInfo(const CString& securityInfo) { m_securityInfo = securityInfo; }
138
139     long long appCacheID() const { return m_appCacheID; }
140     void setAppCacheID(long long id) { m_appCacheID = id; }
141
142     const KURL& appCacheManifestURL() const { return m_appCacheManifestURL; }
143     void setAppCacheManifestURL(const KURL& url) { m_appCacheManifestURL = url; }
144
145     bool wasFetchedViaSPDY() const { return m_wasFetchedViaSPDY; }
146     void setWasFetchedViaSPDY(bool value) { m_wasFetchedViaSPDY = value; }
147
148     bool wasNpnNegotiated() const { return m_wasNpnNegotiated; }
149     void setWasNpnNegotiated(bool value) { m_wasNpnNegotiated = value; }
150
151     bool wasAlternateProtocolAvailable() const
152     {
153       return m_wasAlternateProtocolAvailable;
154     }
155     void setWasAlternateProtocolAvailable(bool value)
156     {
157       m_wasAlternateProtocolAvailable = value;
158     }
159
160     bool wasFetchedViaProxy() const { return m_wasFetchedViaProxy; }
161     void setWasFetchedViaProxy(bool value) { m_wasFetchedViaProxy = value; }
162
163     bool wasFetchedViaServiceWorker() const { return m_wasFetchedViaServiceWorker; }
164     void setWasFetchedViaServiceWorker(bool value) { m_wasFetchedViaServiceWorker = value; }
165
166     bool wasFallbackRequiredByServiceWorker() const { return m_wasFallbackRequiredByServiceWorker; }
167     void setWasFallbackRequiredByServiceWorker(bool value) { m_wasFallbackRequiredByServiceWorker = value; }
168
169     WebServiceWorkerResponseType serviceWorkerResponseType() const { return m_serviceWorkerResponseType; }
170     void setServiceWorkerResponseType(WebServiceWorkerResponseType value) { m_serviceWorkerResponseType = value; }
171
172     bool isMultipartPayload() const { return m_isMultipartPayload; }
173     void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
174
175     double responseTime() const { return m_responseTime; }
176     void setResponseTime(double responseTime) { m_responseTime = responseTime; }
177
178     const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
179     void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
180
181     unsigned short remotePort() const { return m_remotePort; }
182     void setRemotePort(unsigned short value) { m_remotePort = value; }
183
184     const String& downloadedFilePath() const { return m_downloadedFilePath; }
185     void setDownloadedFilePath(const String&);
186
187     // Extra data associated with this response.
188     ExtraData* extraData() const { return m_extraData.get(); }
189     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
190
191     // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
192     unsigned memoryUsage() const
193     {
194         // average size, mostly due to URL and Header Map strings
195         return 1280;
196     }
197
198     // This method doesn't compare the all members.
199     static bool compare(const ResourceResponse&, const ResourceResponse&);
200
201 private:
202     void updateHeaderParsedState(const AtomicString& name);
203
204     KURL m_url;
205     AtomicString m_mimeType;
206     long long m_expectedContentLength;
207     AtomicString m_textEncodingName;
208     String m_suggestedFilename;
209     int m_httpStatusCode;
210     AtomicString m_httpStatusText;
211     HTTPHeaderMap m_httpHeaderFields;
212     time_t m_lastModifiedDate;
213     bool m_wasCached : 1;
214     unsigned m_connectionID;
215     bool m_connectionReused : 1;
216     RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
217     RefPtr<ResourceLoadInfo> m_resourceLoadInfo;
218
219     bool m_isNull : 1;
220
221     CacheControlHeader m_cacheControlHeader;
222
223     mutable bool m_haveParsedAgeHeader : 1;
224     mutable bool m_haveParsedDateHeader : 1;
225     mutable bool m_haveParsedExpiresHeader : 1;
226     mutable bool m_haveParsedLastModifiedHeader : 1;
227
228     mutable double m_age;
229     mutable double m_date;
230     mutable double m_expires;
231     mutable double m_lastModified;
232
233     // An opaque value that contains some information regarding the security of
234     // the connection for this request, such as SSL connection info (empty
235     // string if not over HTTPS).
236     CString m_securityInfo;
237
238     // HTTP version used in the response, if known.
239     HTTPVersion m_httpVersion;
240
241     // The id of the appcache this response was retrieved from, or zero if
242     // the response was not retrieved from an appcache.
243     long long m_appCacheID;
244
245     // The manifest url of the appcache this response was retrieved from, if any.
246     // Note: only valid for main resource responses.
247     KURL m_appCacheManifestURL;
248
249     // Set to true if this is part of a multipart response.
250     bool m_isMultipartPayload;
251
252     // Was the resource fetched over SPDY.  See http://dev.chromium.org/spdy
253     bool m_wasFetchedViaSPDY;
254
255     // Was the resource fetched over a channel which used TLS/Next-Protocol-Negotiation (also SPDY related).
256     bool m_wasNpnNegotiated;
257
258     // Was the resource fetched over a channel which specified "Alternate-Protocol"
259     // (e.g.: Alternate-Protocol: 443:npn-spdy/1).
260     bool m_wasAlternateProtocolAvailable;
261
262     // Was the resource fetched over an explicit proxy (HTTP, SOCKS, etc).
263     bool m_wasFetchedViaProxy;
264
265     // Was the resource fetched over a ServiceWorker.
266     bool m_wasFetchedViaServiceWorker;
267
268     // Was the fallback request with skip service worker flag required.
269     bool m_wasFallbackRequiredByServiceWorker;
270
271     // The type of the response which was fetched by the ServiceWorker.
272     WebServiceWorkerResponseType m_serviceWorkerResponseType;
273
274     // The time at which the response headers were received.  For cached
275     // responses, this time could be "far" in the past.
276     double m_responseTime;
277
278     // Remote IP address of the socket which fetched this resource.
279     AtomicString m_remoteIPAddress;
280
281     // Remote port number of the socket which fetched this resource.
282     unsigned short m_remotePort;
283
284     // The downloaded file path if the load streamed to a file.
285     String m_downloadedFilePath;
286
287     // The handle to the downloaded file to ensure the underlying file will not
288     // be deleted.
289     RefPtr<BlobDataHandle> m_downloadedFileHandle;
290
291     // ExtraData associated with the response.
292     RefPtr<ExtraData> m_extraData;
293 };
294
295 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
296 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
297
298 struct CrossThreadResourceResponseData {
299     WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
300 public:
301     CrossThreadResourceResponseData() { }
302     KURL m_url;
303     String m_mimeType;
304     long long m_expectedContentLength;
305     String m_textEncodingName;
306     String m_suggestedFilename;
307     int m_httpStatusCode;
308     String m_httpStatusText;
309     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
310     time_t m_lastModifiedDate;
311     RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
312     CString m_securityInfo;
313     ResourceResponse::HTTPVersion m_httpVersion;
314     long long m_appCacheID;
315     KURL m_appCacheManifestURL;
316     bool m_isMultipartPayload;
317     bool m_wasFetchedViaSPDY;
318     bool m_wasNpnNegotiated;
319     bool m_wasAlternateProtocolAvailable;
320     bool m_wasFetchedViaProxy;
321     bool m_wasFetchedViaServiceWorker;
322     bool m_wasFallbackRequiredByServiceWorker;
323     WebServiceWorkerResponseType m_serviceWorkerResponseType;
324     double m_responseTime;
325     String m_remoteIPAddress;
326     unsigned short m_remotePort;
327     String m_downloadedFilePath;
328     RefPtr<BlobDataHandle> m_downloadedFileHandle;
329 };
330
331 } // namespace blink
332
333 #endif // ResourceResponse_h