Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / network / ResourceRequest.h
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
4  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef ResourceRequest_h
29 #define ResourceRequest_h
30
31 #include "platform/network/FormData.h"
32 #include "platform/network/HTTPHeaderMap.h"
33 #include "platform/network/HTTPParsers.h"
34 #include "platform/network/ResourceLoadPriority.h"
35 #include "platform/weborigin/KURL.h"
36 #include "platform/weborigin/Referrer.h"
37 #include "wtf/OwnPtr.h"
38
39 namespace WebCore {
40
41 enum ResourceRequestCachePolicy {
42     UseProtocolCachePolicy, // normal load
43     ReloadIgnoringCacheData, // reload
44     ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
45     ReturnCacheDataDontLoad  // results of a post - allow stale data and only use cache
46 };
47
48 struct CrossThreadResourceRequestData;
49
50 class PLATFORM_EXPORT ResourceRequest {
51     WTF_MAKE_FAST_ALLOCATED;
52 public:
53     // The type of this ResourceRequest, based on how the resource will be used.
54     enum TargetType {
55         TargetIsMainFrame,
56         TargetIsSubframe,
57         TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
58         TargetIsStyleSheet,
59         TargetIsScript,
60         TargetIsFont,
61         TargetIsImage,
62         TargetIsObject,
63         TargetIsMedia,
64         TargetIsWorker,
65         TargetIsSharedWorker,
66         TargetIsPrefetch,
67         TargetIsFavicon,
68         TargetIsXHR,
69         TargetIsTextTrack,
70         TargetIsPing,
71         TargetIsServiceWorker,
72         TargetIsUnspecified,
73     };
74
75     class ExtraData : public RefCounted<ExtraData> {
76     public:
77         virtual ~ExtraData() { }
78     };
79
80     ResourceRequest()
81     {
82         initialize(KURL(), UseProtocolCachePolicy);
83     }
84
85     ResourceRequest(const String& urlString)
86     {
87         initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
88     }
89
90     ResourceRequest(const KURL& url)
91     {
92         initialize(url, UseProtocolCachePolicy);
93     }
94
95     ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
96     {
97         initialize(url, cachePolicy);
98         setHTTPReferrer(referrer);
99     }
100
101     static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
102
103     // Gets a copy of the data suitable for passing to another thread.
104     PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
105
106     bool isNull() const;
107     bool isEmpty() const;
108
109     const KURL& url() const;
110     void setURL(const KURL& url);
111
112     void removeCredentials();
113
114     ResourceRequestCachePolicy cachePolicy() const;
115     void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
116
117     double timeoutInterval() const; // May return 0 when using platform default.
118     void setTimeoutInterval(double timeoutInterval);
119
120     const KURL& firstPartyForCookies() const;
121     void setFirstPartyForCookies(const KURL& firstPartyForCookies);
122
123     const AtomicString& httpMethod() const;
124     void setHTTPMethod(const AtomicString&);
125
126     const HTTPHeaderMap& httpHeaderFields() const;
127     const AtomicString& httpHeaderField(const AtomicString& name) const;
128     const AtomicString& httpHeaderField(const char* name) const;
129     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
130     void setHTTPHeaderField(const char* name, const AtomicString& value);
131     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
132     void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
133     void clearHTTPHeaderField(const AtomicString& name);
134
135     void clearHTTPAuthorization();
136
137     const AtomicString& httpContentType() const { return httpHeaderField("Content-Type");  }
138     void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
139
140     const AtomicString& httpReferrer() const { return httpHeaderField("Referer"); }
141     ReferrerPolicy referrerPolicy() const { return m_referrerPolicy; }
142     void setHTTPReferrer(const Referrer& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer.referrer); m_referrerPolicy = httpReferrer.referrerPolicy; }
143     void clearHTTPReferrer();
144
145     const AtomicString& httpOrigin() const { return httpHeaderField("Origin"); }
146     void setHTTPOrigin(const AtomicString& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
147     void clearHTTPOrigin();
148
149     const AtomicString& httpUserAgent() const { return httpHeaderField("User-Agent"); }
150     void setHTTPUserAgent(const AtomicString& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
151     void clearHTTPUserAgent();
152
153     const AtomicString& httpAccept() const { return httpHeaderField("Accept"); }
154     void setHTTPAccept(const AtomicString& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
155
156     FormData* httpBody() const;
157     void setHTTPBody(PassRefPtr<FormData> httpBody);
158
159     bool allowStoredCredentials() const;
160     void setAllowStoredCredentials(bool allowCredentials);
161
162     ResourceLoadPriority priority() const;
163     void setPriority(ResourceLoadPriority, int intraPriorityValue = 0);
164
165     bool isConditional() const;
166
167     // Whether the associated ResourceHandleClient needs to be notified of
168     // upload progress made for that resource.
169     bool reportUploadProgress() const { return m_reportUploadProgress; }
170     void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
171
172     // Whether the timing information should be collected for the request.
173     bool reportLoadTiming() const { return m_reportLoadTiming; }
174     void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
175
176     // Whether actual headers being sent/received should be collected and reported for the request.
177     bool reportRawHeaders() const { return m_reportRawHeaders; }
178     void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
179
180     // Allows the request to be matched up with its requestor.
181     int requestorID() const { return m_requestorID; }
182     void setRequestorID(int requestorID) { m_requestorID = requestorID; }
183
184     // The process id of the process from which this request originated. In
185     // the case of out-of-process plugins, this allows to link back the
186     // request to the plugin process (as it is processed through a render
187     // view process).
188     int requestorProcessID() const { return m_requestorProcessID; }
189     void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
190
191     // Allows the request to be matched up with its app cache host.
192     int appCacheHostID() const { return m_appCacheHostID; }
193     void setAppCacheHostID(int id) { m_appCacheHostID = id; }
194
195     // True if request was user initiated.
196     bool hasUserGesture() const { return m_hasUserGesture; }
197     void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
198
199     // True if request should be downloaded to file.
200     bool downloadToFile() const { return m_downloadToFile; }
201     void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
202
203     // Extra data associated with this request.
204     ExtraData* extraData() const { return m_extraData.get(); }
205     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
206
207     // What this request is for.
208     TargetType targetType() const { return m_targetType; }
209     void setTargetType(TargetType type) { m_targetType = type; }
210
211     bool cacheControlContainsNoCache();
212     bool cacheControlContainsNoStore();
213     bool hasCacheValidatorFields();
214
215     static double defaultTimeoutInterval(); // May return 0 when using platform default.
216     static void setDefaultTimeoutInterval(double);
217
218     static bool compare(const ResourceRequest&, const ResourceRequest&);
219
220 private:
221     void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
222
223     KURL m_url;
224     ResourceRequestCachePolicy m_cachePolicy;
225     double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
226     KURL m_firstPartyForCookies;
227     AtomicString m_httpMethod;
228     HTTPHeaderMap m_httpHeaderFields;
229     RefPtr<FormData> m_httpBody;
230     bool m_allowStoredCredentials : 1;
231     bool m_reportUploadProgress : 1;
232     bool m_reportLoadTiming : 1;
233     bool m_reportRawHeaders : 1;
234     bool m_hasUserGesture : 1;
235     bool m_downloadToFile : 1;
236     ResourceLoadPriority m_priority;
237     int m_intraPriorityValue;
238     int m_requestorID;
239     int m_requestorProcessID;
240     int m_appCacheHostID;
241     RefPtr<ExtraData> m_extraData;
242     TargetType m_targetType;
243     ReferrerPolicy m_referrerPolicy;
244     CacheControlHeader m_cacheControlHeader;
245
246     static double s_defaultTimeoutInterval;
247 };
248
249 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
250
251 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
252 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
253
254 struct CrossThreadResourceRequestData {
255     WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
256 public:
257     CrossThreadResourceRequestData() { }
258     KURL m_url;
259
260     ResourceRequestCachePolicy m_cachePolicy;
261     double m_timeoutInterval;
262     KURL m_firstPartyForCookies;
263
264     String m_httpMethod;
265     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
266     RefPtr<FormData> m_httpBody;
267     bool m_allowStoredCredentials;
268     bool m_reportUploadProgress;
269     bool m_hasUserGesture;
270     bool m_downloadToFile;
271     ResourceLoadPriority m_priority;
272     int m_intraPriorityValue;
273     int m_requestorID;
274     int m_requestorProcessID;
275     int m_appCacheHostID;
276     ResourceRequest::TargetType m_targetType;
277     ReferrerPolicy m_referrerPolicy;
278 };
279
280 unsigned initializeMaximumHTTPConnectionCountPerHost();
281
282 } // namespace WebCore
283
284 #endif // ResourceRequest_h