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: 52 static PassRefPtr<ExtraDataContainer> create(WebURLResponse::ExtraData* extraData) { return adoptRef(new ExtraDataContainer(extraData)); } 53 54 virtual ~ExtraDataContainer() { } 55 56 WebURLResponse::ExtraData* extraData() const { return m_extraData.get(); } 57 58 private: 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: 73 WebURLResponsePrivateImpl() 74 { 75 m_resourceResponse = &m_resourceResponseAllocation; 76 } 77 78 WebURLResponsePrivateImpl(const WebURLResponsePrivate* p) 79 : m_resourceResponseAllocation(*p->m_resourceResponse) 80 { 81 m_resourceResponse = &m_resourceResponseAllocation; 82 } 83 84 virtual void dispose() { delete this; } 85 86 private: 87 virtual ~WebURLResponsePrivateImpl() { } 88 89 ResourceResponse m_resourceResponseAllocation; 90 }; 91 92 void WebURLResponse::initialize() 93 { 94 assign(new WebURLResponsePrivateImpl()); 95 } 96 97 void WebURLResponse::reset() 98 { 99 assign(0); 100 } 101 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 108 bool WebURLResponse::isNull() const 109 { 110 return !m_private || m_private->m_resourceResponse->isNull(); 111 } 112 113 WebURL WebURLResponse::url() const 114 { 115 return m_private->m_resourceResponse->url(); 116 } 117 118 void WebURLResponse::setURL(const WebURL& url) 119 { 120 m_private->m_resourceResponse->setURL(url); 121 } 122 123 unsigned WebURLResponse::connectionID() const 124 { 125 return m_private->m_resourceResponse->connectionID(); 126 } 127 128 void WebURLResponse::setConnectionID(unsigned connectionID) 129 { 130 m_private->m_resourceResponse->setConnectionID(connectionID); 131 } 132 133 bool WebURLResponse::connectionReused() const 134 { 135 return m_private->m_resourceResponse->connectionReused(); 136 } 137 138 void WebURLResponse::setConnectionReused(bool connectionReused) 139 { 140 m_private->m_resourceResponse->setConnectionReused(connectionReused); 141 } 142 143 WebURLLoadTiming WebURLResponse::loadTiming() 144 { 145 return WebURLLoadTiming(m_private->m_resourceResponse->resourceLoadTiming()); 146 } 147 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 154 WebHTTPLoadInfo WebURLResponse::httpLoadInfo() 155 { 156 return WebHTTPLoadInfo(m_private->m_resourceResponse->resourceLoadInfo()); 157 } 158 159 void WebURLResponse::setHTTPLoadInfo(const WebHTTPLoadInfo& value) 160 { 161 m_private->m_resourceResponse->setResourceLoadInfo(value); 162 } 163 164 double WebURLResponse::responseTime() const 165 { 166 return m_private->m_resourceResponse->responseTime(); 167 } 168 169 void WebURLResponse::setResponseTime(double responseTime) 170 { 171 m_private->m_resourceResponse->setResponseTime(responseTime); 172 } 173 174 WebString WebURLResponse::mimeType() const 175 { 176 return m_private->m_resourceResponse->mimeType(); 177 } 178 179 void WebURLResponse::setMIMEType(const WebString& mimeType) 180 { 181 m_private->m_resourceResponse->setMimeType(mimeType); 182 } 183 184 long long WebURLResponse::expectedContentLength() const 185 { 186 return m_private->m_resourceResponse->expectedContentLength(); 187 } 188 189 void WebURLResponse::setExpectedContentLength(long long expectedContentLength) 190 { 191 m_private->m_resourceResponse->setExpectedContentLength(expectedContentLength); 192 } 193 194 WebString WebURLResponse::textEncodingName() const 195 { 196 return m_private->m_resourceResponse->textEncodingName(); 197 } 198 199 void WebURLResponse::setTextEncodingName(const WebString& textEncodingName) 200 { 201 m_private->m_resourceResponse->setTextEncodingName(textEncodingName); 202 } 203 204 WebString WebURLResponse::suggestedFileName() const 205 { 206 return m_private->m_resourceResponse->suggestedFilename(); 207 } 208 209 void WebURLResponse::setSuggestedFileName(const WebString& suggestedFileName) 210 { 211 m_private->m_resourceResponse->setSuggestedFilename(suggestedFileName); 212 } 213 214 WebURLResponse::HTTPVersion WebURLResponse::httpVersion() const 215 { 216 return static_cast<HTTPVersion>(m_private->m_resourceResponse->httpVersion()); 217 } 218 219 void WebURLResponse::setHTTPVersion(HTTPVersion version) 220 { 221 m_private->m_resourceResponse->setHTTPVersion(static_cast<ResourceResponse::HTTPVersion>(version)); 222 } 223 224 int WebURLResponse::httpStatusCode() const 225 { 226 return m_private->m_resourceResponse->httpStatusCode(); 227 } 228 229 void WebURLResponse::setHTTPStatusCode(int httpStatusCode) 230 { 231 m_private->m_resourceResponse->setHTTPStatusCode(httpStatusCode); 232 } 233 234 WebString WebURLResponse::httpStatusText() const 235 { 236 return m_private->m_resourceResponse->httpStatusText(); 237 } 238 239 void WebURLResponse::setHTTPStatusText(const WebString& httpStatusText) 240 { 241 m_private->m_resourceResponse->setHTTPStatusText(httpStatusText); 242 } 243 244 WebString WebURLResponse::httpHeaderField(const WebString& name) const 245 { 246 return m_private->m_resourceResponse->httpHeaderField(name); 247 } 248 249 void WebURLResponse::setHTTPHeaderField(const WebString& name, const WebString& value) 250 { 251 m_private->m_resourceResponse->setHTTPHeaderField(name, value); 252 } 253 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 262 void WebURLResponse::clearHTTPHeaderField(const WebString& name) 263 { 264 m_private->m_resourceResponse->clearHTTPHeaderField(name); 265 } 266 267 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 274 double WebURLResponse::lastModifiedDate() const 275 { 276 return static_cast<double>(m_private->m_resourceResponse->lastModifiedDate()); 277 } 278 279 void WebURLResponse::setLastModifiedDate(double lastModifiedDate) 280 { 281 m_private->m_resourceResponse->setLastModifiedDate(static_cast<time_t>(lastModifiedDate)); 282 } 283 284 long long WebURLResponse::appCacheID() const 285 { 286 return m_private->m_resourceResponse->appCacheID(); 287 } 288 289 void WebURLResponse::setAppCacheID(long long appCacheID) 290 { 291 m_private->m_resourceResponse->setAppCacheID(appCacheID); 292 } 293 294 WebURL WebURLResponse::appCacheManifestURL() const 295 { 296 return m_private->m_resourceResponse->appCacheManifestURL(); 297 } 298 299 void WebURLResponse::setAppCacheManifestURL(const WebURL& url) 300 { 301 m_private->m_resourceResponse->setAppCacheManifestURL(url); 302 } 303 304 WebCString WebURLResponse::securityInfo() const 305 { 306 // FIXME: getSecurityInfo is misnamed. 307 return m_private->m_resourceResponse->getSecurityInfo(); 308 } 309 310 void WebURLResponse::setSecurityInfo(const WebCString& securityInfo) 311 { 312 m_private->m_resourceResponse->setSecurityInfo(securityInfo); 313 } 314 315 ResourceResponse& WebURLResponse::toMutableResourceResponse() 316 { 317 ASSERT(m_private); 318 ASSERT(m_private->m_resourceResponse); 319 320 return *m_private->m_resourceResponse; 321 } 322 323 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 331 bool WebURLResponse::wasCached() const 332 { 333 return m_private->m_resourceResponse->wasCached(); 334 } 335 336 void WebURLResponse::setWasCached(bool value) 337 { 338 m_private->m_resourceResponse->setWasCached(value); 339 } 340 341 bool WebURLResponse::wasFetchedViaSPDY() const 342 { 343 return m_private->m_resourceResponse->wasFetchedViaSPDY(); 344 } 345 346 void WebURLResponse::setWasFetchedViaSPDY(bool value) 347 { 348 m_private->m_resourceResponse->setWasFetchedViaSPDY(value); 349 } 350 351 bool WebURLResponse::wasNpnNegotiated() const 352 { 353 return m_private->m_resourceResponse->wasNpnNegotiated(); 354 } 355 356 void WebURLResponse::setWasNpnNegotiated(bool value) 357 { 358 m_private->m_resourceResponse->setWasNpnNegotiated(value); 359 } 360 361 bool WebURLResponse::wasAlternateProtocolAvailable() const 362 { 363 return m_private->m_resourceResponse->wasAlternateProtocolAvailable(); 364 } 365 366 void WebURLResponse::setWasAlternateProtocolAvailable(bool value) 367 { 368 m_private->m_resourceResponse->setWasAlternateProtocolAvailable(value); 369 } 370 371 bool WebURLResponse::wasFetchedViaProxy() const 372 { 373 return m_private->m_resourceResponse->wasFetchedViaProxy(); 374 } 375 376 void WebURLResponse::setWasFetchedViaProxy(bool value) 377 { 378 m_private->m_resourceResponse->setWasFetchedViaProxy(value); 379 } 380 381 bool WebURLResponse::isMultipartPayload() const 382 { 383 return m_private->m_resourceResponse->isMultipartPayload(); 384 } 385 386 void WebURLResponse::setIsMultipartPayload(bool value) 387 { 388 m_private->m_resourceResponse->setIsMultipartPayload(value); 389 } 390 391 WebString WebURLResponse::downloadFilePath() const 392 { 393 return m_private->m_resourceResponse->downloadedFilePath(); 394 } 395 396 void WebURLResponse::setDownloadFilePath(const WebString& downloadFilePath) 397 { 398 m_private->m_resourceResponse->setDownloadedFilePath(downloadFilePath); 399 } 400 401 WebString WebURLResponse::remoteIPAddress() const 402 { 403 return m_private->m_resourceResponse->remoteIPAddress(); 404 } 405 406 void WebURLResponse::setRemoteIPAddress(const WebString& remoteIPAddress) 407 { 408 m_private->m_resourceResponse->setRemoteIPAddress(remoteIPAddress); 409 } 410 411 unsigned short WebURLResponse::remotePort() const 412 { 413 return m_private->m_resourceResponse->remotePort(); 414 } 415 416 void WebURLResponse::setRemotePort(unsigned short remotePort) 417 { 418 m_private->m_resourceResponse->setRemotePort(remotePort); 419 } 420 421 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 429 void WebURLResponse::setExtraData(WebURLResponse::ExtraData* extraData) 430 { 431 m_private->m_resourceResponse->setExtraData(ExtraDataContainer::create(extraData)); 432 } 433 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