1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebURLRequest.h"
33
34 #include "ResourceRequest.h"
35
36 #include "WebHTTPBody.h"
37 #include "WebHTTPHeaderVisitor.h"
38 #include "WebURL.h"
39 #include "WebURLRequestPrivate.h"
40
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 // The standard implementation of WebURLRequestPrivate, which maintains
46 // ownership of a ResourceRequest instance.
47 class WebURLRequestPrivateImpl : public WebURLRequestPrivate {
48 public:
WebURLRequestPrivateImpl()49 WebURLRequestPrivateImpl()
50 {
51 m_resourceRequest = &m_resourceRequestAllocation;
52 }
53
WebURLRequestPrivateImpl(const WebURLRequestPrivate * p)54 WebURLRequestPrivateImpl(const WebURLRequestPrivate* p)
55 : m_resourceRequestAllocation(*p->m_resourceRequest)
56 {
57 m_resourceRequest = &m_resourceRequestAllocation;
58 }
59
dispose()60 virtual void dispose() { delete this; }
61
62 ResourceRequest m_resourceRequestAllocation;
63 };
64
initialize()65 void WebURLRequest::initialize()
66 {
67 assign(new WebURLRequestPrivateImpl());
68 }
69
reset()70 void WebURLRequest::reset()
71 {
72 assign(0);
73 }
74
assign(const WebURLRequest & r)75 void WebURLRequest::assign(const WebURLRequest& r)
76 {
77 if (&r != this)
78 assign(r.m_private ? new WebURLRequestPrivateImpl(r.m_private) : 0);
79 }
80
isNull() const81 bool WebURLRequest::isNull() const
82 {
83 return !m_private || m_private->m_resourceRequest->isNull();
84 }
85
url() const86 WebURL WebURLRequest::url() const
87 {
88 return m_private->m_resourceRequest->url();
89 }
90
setURL(const WebURL & url)91 void WebURLRequest::setURL(const WebURL& url)
92 {
93 m_private->m_resourceRequest->setURL(url);
94 }
95
firstPartyForCookies() const96 WebURL WebURLRequest::firstPartyForCookies() const
97 {
98 return m_private->m_resourceRequest->firstPartyForCookies();
99 }
100
setFirstPartyForCookies(const WebURL & firstPartyForCookies)101 void WebURLRequest::setFirstPartyForCookies(const WebURL& firstPartyForCookies)
102 {
103 m_private->m_resourceRequest->setFirstPartyForCookies(firstPartyForCookies);
104 }
105
allowCookies() const106 bool WebURLRequest::allowCookies() const
107 {
108 return m_private->m_resourceRequest->allowCookies();
109 }
110
setAllowCookies(bool allowCookies)111 void WebURLRequest::setAllowCookies(bool allowCookies)
112 {
113 m_private->m_resourceRequest->setAllowCookies(allowCookies);
114 }
115
allowStoredCredentials() const116 bool WebURLRequest::allowStoredCredentials() const
117 {
118 return m_private->m_allowStoredCredentials;
119 }
120
setAllowStoredCredentials(bool allowStoredCredentials)121 void WebURLRequest::setAllowStoredCredentials(bool allowStoredCredentials)
122 {
123 m_private->m_allowStoredCredentials = allowStoredCredentials;
124 }
125
cachePolicy() const126 WebURLRequest::CachePolicy WebURLRequest::cachePolicy() const
127 {
128 return static_cast<WebURLRequest::CachePolicy>(
129 m_private->m_resourceRequest->cachePolicy());
130 }
131
setCachePolicy(CachePolicy cachePolicy)132 void WebURLRequest::setCachePolicy(CachePolicy cachePolicy)
133 {
134 m_private->m_resourceRequest->setCachePolicy(
135 static_cast<ResourceRequestCachePolicy>(cachePolicy));
136 }
137
httpMethod() const138 WebString WebURLRequest::httpMethod() const
139 {
140 return m_private->m_resourceRequest->httpMethod();
141 }
142
setHTTPMethod(const WebString & httpMethod)143 void WebURLRequest::setHTTPMethod(const WebString& httpMethod)
144 {
145 m_private->m_resourceRequest->setHTTPMethod(httpMethod);
146 }
147
httpHeaderField(const WebString & name) const148 WebString WebURLRequest::httpHeaderField(const WebString& name) const
149 {
150 return m_private->m_resourceRequest->httpHeaderField(name);
151 }
152
setHTTPHeaderField(const WebString & name,const WebString & value)153 void WebURLRequest::setHTTPHeaderField(const WebString& name, const WebString& value)
154 {
155 m_private->m_resourceRequest->setHTTPHeaderField(name, value);
156 }
157
addHTTPHeaderField(const WebString & name,const WebString & value)158 void WebURLRequest::addHTTPHeaderField(const WebString& name, const WebString& value)
159 {
160 m_private->m_resourceRequest->addHTTPHeaderField(name, value);
161 }
162
clearHTTPHeaderField(const WebString & name)163 void WebURLRequest::clearHTTPHeaderField(const WebString& name)
164 {
165 // FIXME: Add a clearHTTPHeaderField method to ResourceRequest.
166 const HTTPHeaderMap& map = m_private->m_resourceRequest->httpHeaderFields();
167 const_cast<HTTPHeaderMap*>(&map)->remove(name);
168 }
169
visitHTTPHeaderFields(WebHTTPHeaderVisitor * visitor) const170 void WebURLRequest::visitHTTPHeaderFields(WebHTTPHeaderVisitor* visitor) const
171 {
172 const HTTPHeaderMap& map = m_private->m_resourceRequest->httpHeaderFields();
173 for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it)
174 visitor->visitHeader(it->first, it->second);
175 }
176
httpBody() const177 WebHTTPBody WebURLRequest::httpBody() const
178 {
179 return WebHTTPBody(m_private->m_resourceRequest->httpBody());
180 }
181
setHTTPBody(const WebHTTPBody & httpBody)182 void WebURLRequest::setHTTPBody(const WebHTTPBody& httpBody)
183 {
184 m_private->m_resourceRequest->setHTTPBody(httpBody);
185 }
186
reportUploadProgress() const187 bool WebURLRequest::reportUploadProgress() const
188 {
189 return m_private->m_resourceRequest->reportUploadProgress();
190 }
191
setReportUploadProgress(bool reportUploadProgress)192 void WebURLRequest::setReportUploadProgress(bool reportUploadProgress)
193 {
194 m_private->m_resourceRequest->setReportUploadProgress(reportUploadProgress);
195 }
196
targetType() const197 WebURLRequest::TargetType WebURLRequest::targetType() const
198 {
199 return static_cast<TargetType>(m_private->m_resourceRequest->targetType());
200 }
201
setTargetType(TargetType targetType)202 void WebURLRequest::setTargetType(TargetType targetType)
203 {
204 m_private->m_resourceRequest->setTargetType(
205 static_cast<ResourceRequest::TargetType>(targetType));
206 }
207
requestorID() const208 int WebURLRequest::requestorID() const
209 {
210 return m_private->m_resourceRequest->requestorID();
211 }
212
setRequestorID(int requestorID)213 void WebURLRequest::setRequestorID(int requestorID)
214 {
215 m_private->m_resourceRequest->setRequestorID(requestorID);
216 }
217
requestorProcessID() const218 int WebURLRequest::requestorProcessID() const
219 {
220 return m_private->m_resourceRequest->requestorProcessID();
221 }
222
setRequestorProcessID(int requestorProcessID)223 void WebURLRequest::setRequestorProcessID(int requestorProcessID)
224 {
225 m_private->m_resourceRequest->setRequestorProcessID(requestorProcessID);
226 }
227
appCacheHostID() const228 int WebURLRequest::appCacheHostID() const
229 {
230 return m_private->m_resourceRequest->appCacheHostID();
231 }
232
setAppCacheHostID(int appCacheHostID)233 void WebURLRequest::setAppCacheHostID(int appCacheHostID)
234 {
235 m_private->m_resourceRequest->setAppCacheHostID(appCacheHostID);
236 }
237
toMutableResourceRequest()238 ResourceRequest& WebURLRequest::toMutableResourceRequest()
239 {
240 ASSERT(m_private);
241 ASSERT(m_private->m_resourceRequest);
242
243 return *m_private->m_resourceRequest;
244 }
245
toResourceRequest() const246 const ResourceRequest& WebURLRequest::toResourceRequest() const
247 {
248 ASSERT(m_private);
249 ASSERT(m_private->m_resourceRequest);
250
251 return *m_private->m_resourceRequest;
252 }
253
assign(WebURLRequestPrivate * p)254 void WebURLRequest::assign(WebURLRequestPrivate* p)
255 {
256 // Subclasses may call this directly so a self-assignment check is needed
257 // here as well as in the public assign method.
258 if (m_private == p)
259 return;
260 if (m_private)
261 m_private->dispose();
262 m_private = p;
263 }
264
265 } // namespace WebKit
266