Upstream version 5.34.104.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/ResourceLoadPriority.h"
34 #include "platform/weborigin/KURL.h"
35 #include "platform/weborigin/Referrer.h"
36 #include "wtf/OwnPtr.h"
37
38 namespace WebCore {
39
40 enum ResourceRequestCachePolicy {
41     UseProtocolCachePolicy, // normal load
42     ReloadIgnoringCacheData, // reload
43     ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
44     ReturnCacheDataDontLoad  // results of a post - allow stale data and only use cache
45 };
46
47 struct CrossThreadResourceRequestData;
48
49 class PLATFORM_EXPORT ResourceRequest {
50     WTF_MAKE_FAST_ALLOCATED;
51 public:
52     // The type of this ResourceRequest, based on how the resource will be used.
53     enum TargetType {
54         TargetIsMainFrame,
55         TargetIsSubframe,
56         TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
57         TargetIsStyleSheet,
58         TargetIsScript,
59         TargetIsFont,
60         TargetIsImage,
61         TargetIsObject,
62         TargetIsMedia,
63         TargetIsWorker,
64         TargetIsSharedWorker,
65         TargetIsPrefetch,
66         TargetIsFavicon,
67         TargetIsXHR,
68         TargetIsTextTrack,
69         TargetIsPing,
70         TargetIsServiceWorker,
71         TargetIsUnspecified,
72     };
73
74     class ExtraData : public RefCounted<ExtraData> {
75     public:
76         virtual ~ExtraData() { }
77     };
78
79     ResourceRequest()
80     {
81         initialize(KURL(), UseProtocolCachePolicy);
82     }
83
84     ResourceRequest(const String& urlString)
85     {
86         initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
87     }
88
89     ResourceRequest(const KURL& url)
90     {
91         initialize(url, UseProtocolCachePolicy);
92     }
93
94     ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
95     {
96         initialize(url, cachePolicy);
97         setHTTPReferrer(referrer);
98     }
99
100     static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
101
102     // Gets a copy of the data suitable for passing to another thread.
103     PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
104
105     bool isNull() const;
106     bool isEmpty() const;
107
108     const KURL& url() const;
109     void setURL(const KURL& url);
110
111     void removeCredentials();
112
113     ResourceRequestCachePolicy cachePolicy() const;
114     void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
115
116     double timeoutInterval() const; // May return 0 when using platform default.
117     void setTimeoutInterval(double timeoutInterval);
118
119     const KURL& firstPartyForCookies() const;
120     void setFirstPartyForCookies(const KURL& firstPartyForCookies);
121
122     const AtomicString& httpMethod() const;
123     void setHTTPMethod(const AtomicString&);
124
125     const HTTPHeaderMap& httpHeaderFields() const;
126     const AtomicString& httpHeaderField(const AtomicString& name) const;
127     const AtomicString& httpHeaderField(const char* name) const;
128     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
129     void setHTTPHeaderField(const char* name, const AtomicString& value);
130     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
131     void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
132     void clearHTTPHeaderField(const AtomicString& name);
133
134     void clearHTTPAuthorization();
135
136     const AtomicString& httpContentType() const { return httpHeaderField("Content-Type");  }
137     void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
138     void clearHTTPContentType();
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     void clearHTTPAccept();
156
157     FormData* httpBody() const;
158     void setHTTPBody(PassRefPtr<FormData> httpBody);
159
160     bool allowCookies() const;
161     void setAllowCookies(bool allowCookies);
162
163     ResourceLoadPriority priority() const;
164     void setPriority(ResourceLoadPriority);
165
166     bool isConditional() const;
167
168     // Whether the associated ResourceHandleClient needs to be notified of
169     // upload progress made for that resource.
170     bool reportUploadProgress() const { return m_reportUploadProgress; }
171     void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
172
173     // Whether the timing information should be collected for the request.
174     bool reportLoadTiming() const { return m_reportLoadTiming; }
175     void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
176
177     // Whether actual headers being sent/received should be collected and reported for the request.
178     bool reportRawHeaders() const { return m_reportRawHeaders; }
179     void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
180
181     // Allows the request to be matched up with its requestor.
182     int requestorID() const { return m_requestorID; }
183     void setRequestorID(int requestorID) { m_requestorID = requestorID; }
184
185     // The process id of the process from which this request originated. In
186     // the case of out-of-process plugins, this allows to link back the
187     // request to the plugin process (as it is processed through a render
188     // view process).
189     int requestorProcessID() const { return m_requestorProcessID; }
190     void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
191
192     // Allows the request to be matched up with its app cache host.
193     int appCacheHostID() const { return m_appCacheHostID; }
194     void setAppCacheHostID(int id) { m_appCacheHostID = id; }
195
196     // True if request was user initiated.
197     bool hasUserGesture() const { return m_hasUserGesture; }
198     void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
199
200     // True if request should be downloaded to file.
201     bool downloadToFile() const { return m_downloadToFile; }
202     void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
203
204     // Extra data associated with this request.
205     ExtraData* extraData() const { return m_extraData.get(); }
206     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
207
208     // What this request is for.
209     TargetType targetType() const { return m_targetType; }
210     void setTargetType(TargetType type) { m_targetType = type; }
211
212     static double defaultTimeoutInterval(); // May return 0 when using platform default.
213     static void setDefaultTimeoutInterval(double);
214
215     static bool compare(const ResourceRequest&, const ResourceRequest&);
216
217 private:
218     void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
219
220     KURL m_url;
221     ResourceRequestCachePolicy m_cachePolicy;
222     double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
223     KURL m_firstPartyForCookies;
224     AtomicString m_httpMethod;
225     HTTPHeaderMap m_httpHeaderFields;
226     RefPtr<FormData> m_httpBody;
227     bool m_allowCookies : 1;
228     bool m_reportUploadProgress : 1;
229     bool m_reportLoadTiming : 1;
230     bool m_reportRawHeaders : 1;
231     bool m_hasUserGesture : 1;
232     bool m_downloadToFile : 1;
233     ResourceLoadPriority m_priority;
234     int m_requestorID;
235     int m_requestorProcessID;
236     int m_appCacheHostID;
237     RefPtr<ExtraData> m_extraData;
238     TargetType m_targetType;
239     ReferrerPolicy m_referrerPolicy;
240
241     static double s_defaultTimeoutInterval;
242 };
243
244 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
245
246 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
247 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
248
249 struct CrossThreadResourceRequestData {
250     WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
251 public:
252     CrossThreadResourceRequestData() { }
253     KURL m_url;
254
255     ResourceRequestCachePolicy m_cachePolicy;
256     double m_timeoutInterval;
257     KURL m_firstPartyForCookies;
258
259     String m_httpMethod;
260     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
261     RefPtr<FormData> m_httpBody;
262     bool m_allowCookies;
263     bool m_reportUploadProgress;
264     bool m_hasUserGesture;
265     bool m_downloadToFile;
266     ResourceLoadPriority m_priority;
267     int m_requestorID;
268     int m_requestorProcessID;
269     int m_appCacheHostID;
270     ResourceRequest::TargetType m_targetType;
271     ReferrerPolicy m_referrerPolicy;
272 };
273
274 unsigned initializeMaximumHTTPConnectionCountPerHost();
275
276 } // namespace WebCore
277
278 #endif // ResourceRequest_h