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 "WebURLResponse.h"
33
34 #include "ResourceResponse.h"
35
36 #include "WebHTTPHeaderVisitor.h"
37 #include "WebString.h"
38 #include "WebURL.h"
39 #include "WebURLResponsePrivate.h"
40
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 // The standard implementation of WebURLResponsePrivate, which maintains
46 // ownership of a ResourceResponse instance.
47 class WebURLResponsePrivateImpl : public WebURLResponsePrivate {
48 public:
WebURLResponsePrivateImpl()49 WebURLResponsePrivateImpl()
50 {
51 m_resourceResponse = &m_resourceResponseAllocation;
52 }
53
WebURLResponsePrivateImpl(const WebURLResponsePrivate * p)54 WebURLResponsePrivateImpl(const WebURLResponsePrivate* p)
55 : m_resourceResponseAllocation(*p->m_resourceResponse)
56 {
57 m_resourceResponse = &m_resourceResponseAllocation;
58 }
59
dispose()60 virtual void dispose() { delete this; }
61
62 ResourceResponse m_resourceResponseAllocation;
63 };
64
initialize()65 void WebURLResponse::initialize()
66 {
67 assign(new WebURLResponsePrivateImpl());
68 }
69
reset()70 void WebURLResponse::reset()
71 {
72 assign(0);
73 }
74
assign(const WebURLResponse & r)75 void WebURLResponse::assign(const WebURLResponse& r)
76 {
77 if (&r != this)
78 assign(r.m_private ? new WebURLResponsePrivateImpl(r.m_private) : 0);
79 }
80
isNull() const81 bool WebURLResponse::isNull() const
82 {
83 return !m_private || m_private->m_resourceResponse->isNull();
84 }
85
url() const86 WebURL WebURLResponse::url() const
87 {
88 return m_private->m_resourceResponse->url();
89 }
90
setURL(const WebURL & url)91 void WebURLResponse::setURL(const WebURL& url)
92 {
93 m_private->m_resourceResponse->setURL(url);
94 }
95
mimeType() const96 WebString WebURLResponse::mimeType() const
97 {
98 return m_private->m_resourceResponse->mimeType();
99 }
100
setMIMEType(const WebString & mimeType)101 void WebURLResponse::setMIMEType(const WebString& mimeType)
102 {
103 m_private->m_resourceResponse->setMimeType(mimeType);
104 }
105
expectedContentLength() const106 long long WebURLResponse::expectedContentLength() const
107 {
108 return m_private->m_resourceResponse->expectedContentLength();
109 }
110
setExpectedContentLength(long long expectedContentLength)111 void WebURLResponse::setExpectedContentLength(long long expectedContentLength)
112 {
113 m_private->m_resourceResponse->setExpectedContentLength(expectedContentLength);
114 }
115
textEncodingName() const116 WebString WebURLResponse::textEncodingName() const
117 {
118 return m_private->m_resourceResponse->textEncodingName();
119 }
120
setTextEncodingName(const WebString & textEncodingName)121 void WebURLResponse::setTextEncodingName(const WebString& textEncodingName)
122 {
123 m_private->m_resourceResponse->setTextEncodingName(textEncodingName);
124 }
125
suggestedFileName() const126 WebString WebURLResponse::suggestedFileName() const
127 {
128 return m_private->m_resourceResponse->suggestedFilename();
129 }
130
setSuggestedFileName(const WebString & suggestedFileName)131 void WebURLResponse::setSuggestedFileName(const WebString& suggestedFileName)
132 {
133 m_private->m_resourceResponse->setSuggestedFilename(suggestedFileName);
134 }
135
httpStatusCode() const136 int WebURLResponse::httpStatusCode() const
137 {
138 return m_private->m_resourceResponse->httpStatusCode();
139 }
140
setHTTPStatusCode(int httpStatusCode)141 void WebURLResponse::setHTTPStatusCode(int httpStatusCode)
142 {
143 m_private->m_resourceResponse->setHTTPStatusCode(httpStatusCode);
144 }
145
httpStatusText() const146 WebString WebURLResponse::httpStatusText() const
147 {
148 return m_private->m_resourceResponse->httpStatusText();
149 }
150
setHTTPStatusText(const WebString & httpStatusText)151 void WebURLResponse::setHTTPStatusText(const WebString& httpStatusText)
152 {
153 m_private->m_resourceResponse->setHTTPStatusText(httpStatusText);
154 }
155
httpHeaderField(const WebString & name) const156 WebString WebURLResponse::httpHeaderField(const WebString& name) const
157 {
158 return m_private->m_resourceResponse->httpHeaderField(name);
159 }
160
setHTTPHeaderField(const WebString & name,const WebString & value)161 void WebURLResponse::setHTTPHeaderField(const WebString& name, const WebString& value)
162 {
163 m_private->m_resourceResponse->setHTTPHeaderField(name, value);
164 }
165
addHTTPHeaderField(const WebString & name,const WebString & value)166 void WebURLResponse::addHTTPHeaderField(const WebString& name, const WebString& value)
167 {
168 // FIXME: Add an addHTTPHeaderField method to ResourceResponse.
169 const HTTPHeaderMap& map = m_private->m_resourceResponse->httpHeaderFields();
170 String valueStr(value);
171 pair<HTTPHeaderMap::iterator, bool> result =
172 const_cast<HTTPHeaderMap*>(&map)->add(name, valueStr);
173 if (!result.second)
174 result.first->second += ", " + valueStr;
175 }
176
clearHTTPHeaderField(const WebString & name)177 void WebURLResponse::clearHTTPHeaderField(const WebString& name)
178 {
179 // FIXME: Add a clearHTTPHeaderField method to ResourceResponse.
180 const HTTPHeaderMap& map = m_private->m_resourceResponse->httpHeaderFields();
181 const_cast<HTTPHeaderMap*>(&map)->remove(name);
182 }
183
visitHTTPHeaderFields(WebHTTPHeaderVisitor * visitor) const184 void WebURLResponse::visitHTTPHeaderFields(WebHTTPHeaderVisitor* visitor) const
185 {
186 const HTTPHeaderMap& map = m_private->m_resourceResponse->httpHeaderFields();
187 for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it)
188 visitor->visitHeader(it->first, it->second);
189 }
190
lastModifiedDate() const191 double WebURLResponse::lastModifiedDate() const
192 {
193 return static_cast<double>(m_private->m_resourceResponse->lastModifiedDate());
194 }
195
setLastModifiedDate(double lastModifiedDate)196 void WebURLResponse::setLastModifiedDate(double lastModifiedDate)
197 {
198 m_private->m_resourceResponse->setLastModifiedDate(static_cast<time_t>(lastModifiedDate));
199 }
200
isContentFiltered() const201 bool WebURLResponse::isContentFiltered() const
202 {
203 return m_private->m_resourceResponse->isContentFiltered();
204 }
205
setIsContentFiltered(bool isContentFiltered)206 void WebURLResponse::setIsContentFiltered(bool isContentFiltered)
207 {
208 m_private->m_resourceResponse->setIsContentFiltered(isContentFiltered);
209 }
210
appCacheID() const211 long long WebURLResponse::appCacheID() const
212 {
213 return m_private->m_resourceResponse->appCacheID();
214 }
215
setAppCacheID(long long appCacheID)216 void WebURLResponse::setAppCacheID(long long appCacheID)
217 {
218 m_private->m_resourceResponse->setAppCacheID(appCacheID);
219 }
220
appCacheManifestURL() const221 WebURL WebURLResponse::appCacheManifestURL() const
222 {
223 return m_private->m_resourceResponse->appCacheManifestURL();
224 }
225
setAppCacheManifestURL(const WebURL & url)226 void WebURLResponse::setAppCacheManifestURL(const WebURL& url)
227 {
228 m_private->m_resourceResponse->setAppCacheManifestURL(url);
229 }
230
securityInfo() const231 WebCString WebURLResponse::securityInfo() const
232 {
233 // FIXME: getSecurityInfo is misnamed.
234 return m_private->m_resourceResponse->getSecurityInfo();
235 }
236
setSecurityInfo(const WebCString & securityInfo)237 void WebURLResponse::setSecurityInfo(const WebCString& securityInfo)
238 {
239 m_private->m_resourceResponse->setSecurityInfo(securityInfo);
240 }
241
toMutableResourceResponse()242 ResourceResponse& WebURLResponse::toMutableResourceResponse()
243 {
244 ASSERT(m_private);
245 ASSERT(m_private->m_resourceResponse);
246
247 return *m_private->m_resourceResponse;
248 }
249
toResourceResponse() const250 const ResourceResponse& WebURLResponse::toResourceResponse() const
251 {
252 ASSERT(m_private);
253 ASSERT(m_private->m_resourceResponse);
254
255 return *m_private->m_resourceResponse;
256 }
257
wasFetchedViaSPDY() const258 bool WebURLResponse::wasFetchedViaSPDY() const
259 {
260 return m_private->m_resourceResponse->wasFetchedViaSPDY();
261 }
262
setWasFetchedViaSPDY(bool value)263 void WebURLResponse::setWasFetchedViaSPDY(bool value)
264 {
265 m_private->m_resourceResponse->setWasFetchedViaSPDY(value);
266 }
267
assign(WebURLResponsePrivate * p)268 void WebURLResponse::assign(WebURLResponsePrivate* p)
269 {
270 // Subclasses may call this directly so a self-assignment check is needed
271 // here as well as in the public assign method.
272 if (m_private == p)
273 return;
274 if (m_private)
275 m_private->dispose();
276 m_private = p;
277 }
278
279 } // namespace WebKit
280