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 "public/platform/WebURLResponse.h"
33
34 #include "platform/exported/WebURLResponsePrivate.h"
35 #include "platform/network/ResourceLoadTiming.h"
36 #include "platform/network/ResourceResponse.h"
37 #include "public/platform/WebHTTPHeaderVisitor.h"
38 #include "public/platform/WebHTTPLoadInfo.h"
39 #include "public/platform/WebString.h"
40 #include "public/platform/WebURL.h"
41 #include "public/platform/WebURLLoadTiming.h"
42 #include "wtf/RefPtr.h"
43
44 using namespace WebCore;
45
46 namespace blink {
47
48 namespace {
49
50 class ExtraDataContainer : public ResourceResponse::ExtraData {
51 public:
create(WebURLResponse::ExtraData * extraData)52 static PassRefPtr<ExtraDataContainer> create(WebURLResponse::ExtraData* extraData) { return adoptRef(new ExtraDataContainer(extraData)); }
53
~ExtraDataContainer()54 virtual ~ExtraDataContainer() { }
55
extraData() const56 WebURLResponse::ExtraData* extraData() const { return m_extraData.get(); }
57
58 private:
ExtraDataContainer(WebURLResponse::ExtraData * extraData)59 explicit ExtraDataContainer(WebURLResponse::ExtraData* extraData)
60 : m_extraData(adoptPtr(extraData))
61 {
62 }
63
64 OwnPtr<WebURLResponse::ExtraData> m_extraData;
65 };
66
67 } // namespace
68
69 // The standard implementation of WebURLResponsePrivate, which maintains
70 // ownership of a ResourceResponse instance.
71 class WebURLResponsePrivateImpl : public WebURLResponsePrivate {
72 public:
WebURLResponsePrivateImpl()73 WebURLResponsePrivateImpl()
74 {
75 m_resourceResponse = &m_resourceResponseAllocation;
76 }
77
WebURLResponsePrivateImpl(const WebURLResponsePrivate * p)78 WebURLResponsePrivateImpl(const WebURLResponsePrivate* p)
79 : m_resourceResponseAllocation(*p->m_resourceResponse)
80 {
81 m_resourceResponse = &m_resourceResponseAllocation;
82 }
83
dispose()84 virtual void dispose() { delete this; }
85
86 private:
~WebURLResponsePrivateImpl()87 virtual ~WebURLResponsePrivateImpl() { }
88
89 ResourceResponse m_resourceResponseAllocation;
90 };
91
initialize()92 void WebURLResponse::initialize()
93 {
94 assign(new WebURLResponsePrivateImpl());
95 }
96
reset()97 void WebURLResponse::reset()
98 {
99 assign(0);
100 }
101
assign(const WebURLResponse & r)102 void WebURLResponse::assign(const WebURLResponse& r)
103 {
104 if (&r != this)
105 assign(r.m_private ? new WebURLResponsePrivateImpl(r.m_private) : 0);
106 }
107
isNull() const108 bool WebURLResponse::isNull() const
109 {
110 return !m_private || m_private->m_resourceResponse->isNull();
111 }
112
url() const113 WebURL WebURLResponse::url() const
114 {
115 return m_private->m_resourceResponse->url();
116 }
117
setURL(const WebURL & url)118 void WebURLResponse::setURL(const WebURL& url)
119 {
120 m_private->m_resourceResponse->setURL(url);
121 }
122
connectionID() const123 unsigned WebURLResponse::connectionID() const
124 {
125 return m_private->m_resourceResponse->connectionID();
126 }
127
setConnectionID(unsigned connectionID)128 void WebURLResponse::setConnectionID(unsigned connectionID)
129 {
130 m_private->m_resourceResponse->setConnectionID(connectionID);
131 }
132
connectionReused() const133 bool WebURLResponse::connectionReused() const
134 {
135 return m_private->m_resourceResponse->connectionReused();
136 }
137
setConnectionReused(bool connectionReused)138 void WebURLResponse::setConnectionReused(bool connectionReused)
139 {
140 m_private->m_resourceResponse->setConnectionReused(connectionReused);
141 }
142
loadTiming()143 WebURLLoadTiming WebURLResponse::loadTiming()
144 {
145 return WebURLLoadTiming(m_private->m_resourceResponse->resourceLoadTiming());
146 }
147
setLoadTiming(const WebURLLoadTiming & timing)148 void WebURLResponse::setLoadTiming(const WebURLLoadTiming& timing)
149 {
150 RefPtr<ResourceLoadTiming> loadTiming = PassRefPtr<ResourceLoadTiming>(timing);
151 m_private->m_resourceResponse->setResourceLoadTiming(loadTiming.release());
152 }
153
httpLoadInfo()154 WebHTTPLoadInfo WebURLResponse::httpLoadInfo()
155 {
156 return WebHTTPLoadInfo(m_private->m_resourceResponse->resourceLoadInfo());
157 }
158
setHTTPLoadInfo(const WebHTTPLoadInfo & value)159 void WebURLResponse::setHTTPLoadInfo(const WebHTTPLoadInfo& value)
160 {
161 m_private->m_resourceResponse->setResourceLoadInfo(value);
162 }
163
responseTime() const164 double WebURLResponse::responseTime() const
165 {
166 return m_private->m_resourceResponse->responseTime();
167 }
168
setResponseTime(double responseTime)169 void WebURLResponse::setResponseTime(double responseTime)
170 {
171 m_private->m_resourceResponse->setResponseTime(responseTime);
172 }
173
mimeType() const174 WebString WebURLResponse::mimeType() const
175 {
176 return m_private->m_resourceResponse->mimeType();
177 }
178
setMIMEType(const WebString & mimeType)179 void WebURLResponse::setMIMEType(const WebString& mimeType)
180 {
181 m_private->m_resourceResponse->setMimeType(mimeType);
182 }
183
expectedContentLength() const184 long long WebURLResponse::expectedContentLength() const
185 {
186 return m_private->m_resourceResponse->expectedContentLength();
187 }
188
setExpectedContentLength(long long expectedContentLength)189 void WebURLResponse::setExpectedContentLength(long long expectedContentLength)
190 {
191 m_private->m_resourceResponse->setExpectedContentLength(expectedContentLength);
192 }
193
textEncodingName() const194 WebString WebURLResponse::textEncodingName() const
195 {
196 return m_private->m_resourceResponse->textEncodingName();
197 }
198
setTextEncodingName(const WebString & textEncodingName)199 void WebURLResponse::setTextEncodingName(const WebString& textEncodingName)
200 {
201 m_private->m_resourceResponse->setTextEncodingName(textEncodingName);
202 }
203
suggestedFileName() const204 WebString WebURLResponse::suggestedFileName() const
205 {
206 return m_private->m_resourceResponse->suggestedFilename();
207 }
208
setSuggestedFileName(const WebString & suggestedFileName)209 void WebURLResponse::setSuggestedFileName(const WebString& suggestedFileName)
210 {
211 m_private->m_resourceResponse->setSuggestedFilename(suggestedFileName);
212 }
213
httpVersion() const214 WebURLResponse::HTTPVersion WebURLResponse::httpVersion() const
215 {
216 return static_cast<HTTPVersion>(m_private->m_resourceResponse->httpVersion());
217 }
218
setHTTPVersion(HTTPVersion version)219 void WebURLResponse::setHTTPVersion(HTTPVersion version)
220 {
221 m_private->m_resourceResponse->setHTTPVersion(static_cast<ResourceResponse::HTTPVersion>(version));
222 }
223
httpStatusCode() const224 int WebURLResponse::httpStatusCode() const
225 {
226 return m_private->m_resourceResponse->httpStatusCode();
227 }
228
setHTTPStatusCode(int httpStatusCode)229 void WebURLResponse::setHTTPStatusCode(int httpStatusCode)
230 {
231 m_private->m_resourceResponse->setHTTPStatusCode(httpStatusCode);
232 }
233
httpStatusText() const234 WebString WebURLResponse::httpStatusText() const
235 {
236 return m_private->m_resourceResponse->httpStatusText();
237 }
238
setHTTPStatusText(const WebString & httpStatusText)239 void WebURLResponse::setHTTPStatusText(const WebString& httpStatusText)
240 {
241 m_private->m_resourceResponse->setHTTPStatusText(httpStatusText);
242 }
243
httpHeaderField(const WebString & name) const244 WebString WebURLResponse::httpHeaderField(const WebString& name) const
245 {
246 return m_private->m_resourceResponse->httpHeaderField(name);
247 }
248
setHTTPHeaderField(const WebString & name,const WebString & value)249 void WebURLResponse::setHTTPHeaderField(const WebString& name, const WebString& value)
250 {
251 m_private->m_resourceResponse->setHTTPHeaderField(name, value);
252 }
253
addHTTPHeaderField(const WebString & name,const WebString & value)254 void WebURLResponse::addHTTPHeaderField(const WebString& name, const WebString& value)
255 {
256 if (name.isNull() || value.isNull())
257 return;
258
259 m_private->m_resourceResponse->addHTTPHeaderField(name, value);
260 }
261
clearHTTPHeaderField(const WebString & name)262 void WebURLResponse::clearHTTPHeaderField(const WebString& name)
263 {
264 m_private->m_resourceResponse->clearHTTPHeaderField(name);
265 }
266
visitHTTPHeaderFields(WebHTTPHeaderVisitor * visitor) const267 void WebURLResponse::visitHTTPHeaderFields(WebHTTPHeaderVisitor* visitor) const
268 {
269 const HTTPHeaderMap& map = m_private->m_resourceResponse->httpHeaderFields();
270 for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it)
271 visitor->visitHeader(it->key, it->value);
272 }
273
lastModifiedDate() const274 double WebURLResponse::lastModifiedDate() const
275 {
276 return static_cast<double>(m_private->m_resourceResponse->lastModifiedDate());
277 }
278
setLastModifiedDate(double lastModifiedDate)279 void WebURLResponse::setLastModifiedDate(double lastModifiedDate)
280 {
281 m_private->m_resourceResponse->setLastModifiedDate(static_cast<time_t>(lastModifiedDate));
282 }
283
appCacheID() const284 long long WebURLResponse::appCacheID() const
285 {
286 return m_private->m_resourceResponse->appCacheID();
287 }
288
setAppCacheID(long long appCacheID)289 void WebURLResponse::setAppCacheID(long long appCacheID)
290 {
291 m_private->m_resourceResponse->setAppCacheID(appCacheID);
292 }
293
appCacheManifestURL() const294 WebURL WebURLResponse::appCacheManifestURL() const
295 {
296 return m_private->m_resourceResponse->appCacheManifestURL();
297 }
298
setAppCacheManifestURL(const WebURL & url)299 void WebURLResponse::setAppCacheManifestURL(const WebURL& url)
300 {
301 m_private->m_resourceResponse->setAppCacheManifestURL(url);
302 }
303
securityInfo() const304 WebCString WebURLResponse::securityInfo() const
305 {
306 // FIXME: getSecurityInfo is misnamed.
307 return m_private->m_resourceResponse->getSecurityInfo();
308 }
309
setSecurityInfo(const WebCString & securityInfo)310 void WebURLResponse::setSecurityInfo(const WebCString& securityInfo)
311 {
312 m_private->m_resourceResponse->setSecurityInfo(securityInfo);
313 }
314
toMutableResourceResponse()315 ResourceResponse& WebURLResponse::toMutableResourceResponse()
316 {
317 ASSERT(m_private);
318 ASSERT(m_private->m_resourceResponse);
319
320 return *m_private->m_resourceResponse;
321 }
322
toResourceResponse() const323 const ResourceResponse& WebURLResponse::toResourceResponse() const
324 {
325 ASSERT(m_private);
326 ASSERT(m_private->m_resourceResponse);
327
328 return *m_private->m_resourceResponse;
329 }
330
wasCached() const331 bool WebURLResponse::wasCached() const
332 {
333 return m_private->m_resourceResponse->wasCached();
334 }
335
setWasCached(bool value)336 void WebURLResponse::setWasCached(bool value)
337 {
338 m_private->m_resourceResponse->setWasCached(value);
339 }
340
wasFetchedViaSPDY() const341 bool WebURLResponse::wasFetchedViaSPDY() const
342 {
343 return m_private->m_resourceResponse->wasFetchedViaSPDY();
344 }
345
setWasFetchedViaSPDY(bool value)346 void WebURLResponse::setWasFetchedViaSPDY(bool value)
347 {
348 m_private->m_resourceResponse->setWasFetchedViaSPDY(value);
349 }
350
wasNpnNegotiated() const351 bool WebURLResponse::wasNpnNegotiated() const
352 {
353 return m_private->m_resourceResponse->wasNpnNegotiated();
354 }
355
setWasNpnNegotiated(bool value)356 void WebURLResponse::setWasNpnNegotiated(bool value)
357 {
358 m_private->m_resourceResponse->setWasNpnNegotiated(value);
359 }
360
wasAlternateProtocolAvailable() const361 bool WebURLResponse::wasAlternateProtocolAvailable() const
362 {
363 return m_private->m_resourceResponse->wasAlternateProtocolAvailable();
364 }
365
setWasAlternateProtocolAvailable(bool value)366 void WebURLResponse::setWasAlternateProtocolAvailable(bool value)
367 {
368 m_private->m_resourceResponse->setWasAlternateProtocolAvailable(value);
369 }
370
wasFetchedViaProxy() const371 bool WebURLResponse::wasFetchedViaProxy() const
372 {
373 return m_private->m_resourceResponse->wasFetchedViaProxy();
374 }
375
setWasFetchedViaProxy(bool value)376 void WebURLResponse::setWasFetchedViaProxy(bool value)
377 {
378 m_private->m_resourceResponse->setWasFetchedViaProxy(value);
379 }
380
isMultipartPayload() const381 bool WebURLResponse::isMultipartPayload() const
382 {
383 return m_private->m_resourceResponse->isMultipartPayload();
384 }
385
setIsMultipartPayload(bool value)386 void WebURLResponse::setIsMultipartPayload(bool value)
387 {
388 m_private->m_resourceResponse->setIsMultipartPayload(value);
389 }
390
downloadFilePath() const391 WebString WebURLResponse::downloadFilePath() const
392 {
393 return m_private->m_resourceResponse->downloadedFilePath();
394 }
395
setDownloadFilePath(const WebString & downloadFilePath)396 void WebURLResponse::setDownloadFilePath(const WebString& downloadFilePath)
397 {
398 m_private->m_resourceResponse->setDownloadedFilePath(downloadFilePath);
399 }
400
remoteIPAddress() const401 WebString WebURLResponse::remoteIPAddress() const
402 {
403 return m_private->m_resourceResponse->remoteIPAddress();
404 }
405
setRemoteIPAddress(const WebString & remoteIPAddress)406 void WebURLResponse::setRemoteIPAddress(const WebString& remoteIPAddress)
407 {
408 m_private->m_resourceResponse->setRemoteIPAddress(remoteIPAddress);
409 }
410
remotePort() const411 unsigned short WebURLResponse::remotePort() const
412 {
413 return m_private->m_resourceResponse->remotePort();
414 }
415
setRemotePort(unsigned short remotePort)416 void WebURLResponse::setRemotePort(unsigned short remotePort)
417 {
418 m_private->m_resourceResponse->setRemotePort(remotePort);
419 }
420
extraData() const421 WebURLResponse::ExtraData* WebURLResponse::extraData() const
422 {
423 RefPtr<ResourceResponse::ExtraData> data = m_private->m_resourceResponse->extraData();
424 if (!data)
425 return 0;
426 return static_cast<ExtraDataContainer*>(data.get())->extraData();
427 }
428
setExtraData(WebURLResponse::ExtraData * extraData)429 void WebURLResponse::setExtraData(WebURLResponse::ExtraData* extraData)
430 {
431 m_private->m_resourceResponse->setExtraData(ExtraDataContainer::create(extraData));
432 }
433
assign(WebURLResponsePrivate * p)434 void WebURLResponse::assign(WebURLResponsePrivate* p)
435 {
436 // Subclasses may call this directly so a self-assignment check is needed
437 // here as well as in the public assign method.
438 if (m_private == p)
439 return;
440 if (m_private)
441 m_private->dispose();
442 m_private = p;
443 }
444
445 } // namespace blink
446