Home | History | Annotate | Download | only in src
      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:
     49     WebURLRequestPrivateImpl()
     50     {
     51         m_resourceRequest = &m_resourceRequestAllocation;
     52     }
     53 
     54     WebURLRequestPrivateImpl(const WebURLRequestPrivate* p)
     55         : m_resourceRequestAllocation(*p->m_resourceRequest)
     56     {
     57         m_resourceRequest = &m_resourceRequestAllocation;
     58         m_allowStoredCredentials = p->m_allowStoredCredentials;
     59     }
     60 
     61     virtual void dispose() { delete this; }
     62 
     63     ResourceRequest m_resourceRequestAllocation;
     64 };
     65 
     66 void WebURLRequest::initialize()
     67 {
     68     assign(new WebURLRequestPrivateImpl());
     69 }
     70 
     71 void WebURLRequest::reset()
     72 {
     73     assign(0);
     74 }
     75 
     76 void WebURLRequest::assign(const WebURLRequest& r)
     77 {
     78     if (&r != this)
     79         assign(r.m_private ? new WebURLRequestPrivateImpl(r.m_private) : 0);
     80 }
     81 
     82 bool WebURLRequest::isNull() const
     83 {
     84     return !m_private || m_private->m_resourceRequest->isNull();
     85 }
     86 
     87 WebURL WebURLRequest::url() const
     88 {
     89     return m_private->m_resourceRequest->url();
     90 }
     91 
     92 void WebURLRequest::setURL(const WebURL& url)
     93 {
     94     m_private->m_resourceRequest->setURL(url);
     95 }
     96 
     97 WebURL WebURLRequest::firstPartyForCookies() const
     98 {
     99     return m_private->m_resourceRequest->firstPartyForCookies();
    100 }
    101 
    102 void WebURLRequest::setFirstPartyForCookies(const WebURL& firstPartyForCookies)
    103 {
    104     m_private->m_resourceRequest->setFirstPartyForCookies(firstPartyForCookies);
    105 }
    106 
    107 bool WebURLRequest::allowCookies() const
    108 {
    109     return m_private->m_resourceRequest->allowCookies();
    110 }
    111 
    112 void WebURLRequest::setAllowCookies(bool allowCookies)
    113 {
    114     m_private->m_resourceRequest->setAllowCookies(allowCookies);
    115 }
    116 
    117 bool WebURLRequest::allowStoredCredentials() const
    118 {
    119     return m_private->m_allowStoredCredentials;
    120 }
    121 
    122 void WebURLRequest::setAllowStoredCredentials(bool allowStoredCredentials)
    123 {
    124     m_private->m_allowStoredCredentials = allowStoredCredentials;
    125 }
    126 
    127 WebURLRequest::CachePolicy WebURLRequest::cachePolicy() const
    128 {
    129     return static_cast<WebURLRequest::CachePolicy>(
    130         m_private->m_resourceRequest->cachePolicy());
    131 }
    132 
    133 void WebURLRequest::setCachePolicy(CachePolicy cachePolicy)
    134 {
    135     m_private->m_resourceRequest->setCachePolicy(
    136         static_cast<ResourceRequestCachePolicy>(cachePolicy));
    137 }
    138 
    139 WebString WebURLRequest::httpMethod() const
    140 {
    141     return m_private->m_resourceRequest->httpMethod();
    142 }
    143 
    144 void WebURLRequest::setHTTPMethod(const WebString& httpMethod)
    145 {
    146     m_private->m_resourceRequest->setHTTPMethod(httpMethod);
    147 }
    148 
    149 WebString WebURLRequest::httpHeaderField(const WebString& name) const
    150 {
    151     return m_private->m_resourceRequest->httpHeaderField(name);
    152 }
    153 
    154 void WebURLRequest::setHTTPHeaderField(const WebString& name, const WebString& value)
    155 {
    156     m_private->m_resourceRequest->setHTTPHeaderField(name, value);
    157 }
    158 
    159 void WebURLRequest::addHTTPHeaderField(const WebString& name, const WebString& value)
    160 {
    161     m_private->m_resourceRequest->addHTTPHeaderField(name, value);
    162 }
    163 
    164 void WebURLRequest::clearHTTPHeaderField(const WebString& name)
    165 {
    166     // FIXME: Add a clearHTTPHeaderField method to ResourceRequest.
    167     const HTTPHeaderMap& map = m_private->m_resourceRequest->httpHeaderFields();
    168     const_cast<HTTPHeaderMap*>(&map)->remove(name);
    169 }
    170 
    171 void WebURLRequest::visitHTTPHeaderFields(WebHTTPHeaderVisitor* visitor) const
    172 {
    173     const HTTPHeaderMap& map = m_private->m_resourceRequest->httpHeaderFields();
    174     for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it)
    175         visitor->visitHeader(it->first, it->second);
    176 }
    177 
    178 WebHTTPBody WebURLRequest::httpBody() const
    179 {
    180     return WebHTTPBody(m_private->m_resourceRequest->httpBody());
    181 }
    182 
    183 void WebURLRequest::setHTTPBody(const WebHTTPBody& httpBody)
    184 {
    185     m_private->m_resourceRequest->setHTTPBody(httpBody);
    186 }
    187 
    188 bool WebURLRequest::reportUploadProgress() const
    189 {
    190     return m_private->m_resourceRequest->reportUploadProgress();
    191 }
    192 
    193 void WebURLRequest::setReportUploadProgress(bool reportUploadProgress)
    194 {
    195     m_private->m_resourceRequest->setReportUploadProgress(reportUploadProgress);
    196 }
    197 
    198 bool WebURLRequest::reportLoadTiming() const
    199 {
    200     return m_private->m_resourceRequest->reportLoadTiming();
    201 }
    202 
    203 void WebURLRequest::setReportRawHeaders(bool reportRawHeaders)
    204 {
    205     m_private->m_resourceRequest->setReportRawHeaders(reportRawHeaders);
    206 }
    207 
    208 bool WebURLRequest::reportRawHeaders() const
    209 {
    210     return m_private->m_resourceRequest->reportRawHeaders();
    211 }
    212 
    213 void WebURLRequest::setReportLoadTiming(bool reportLoadTiming)
    214 {
    215     m_private->m_resourceRequest->setReportLoadTiming(reportLoadTiming);
    216 }
    217 
    218 WebURLRequest::TargetType WebURLRequest::targetType() const
    219 {
    220     return static_cast<TargetType>(m_private->m_resourceRequest->targetType());
    221 }
    222 
    223 bool WebURLRequest::hasUserGesture() const
    224 {
    225     return m_private->m_resourceRequest->hasUserGesture();
    226 }
    227 
    228 void WebURLRequest::setHasUserGesture(bool hasUserGesture)
    229 {
    230     m_private->m_resourceRequest->setHasUserGesture(hasUserGesture);
    231 }
    232 
    233 void WebURLRequest::setTargetType(TargetType targetType)
    234 {
    235     m_private->m_resourceRequest->setTargetType(
    236         static_cast<ResourceRequest::TargetType>(targetType));
    237 }
    238 
    239 int WebURLRequest::requestorID() const
    240 {
    241     return m_private->m_resourceRequest->requestorID();
    242 }
    243 
    244 void WebURLRequest::setRequestorID(int requestorID)
    245 {
    246     m_private->m_resourceRequest->setRequestorID(requestorID);
    247 }
    248 
    249 int WebURLRequest::requestorProcessID() const
    250 {
    251     return m_private->m_resourceRequest->requestorProcessID();
    252 }
    253 
    254 void WebURLRequest::setRequestorProcessID(int requestorProcessID)
    255 {
    256     m_private->m_resourceRequest->setRequestorProcessID(requestorProcessID);
    257 }
    258 
    259 int WebURLRequest::appCacheHostID() const
    260 {
    261     return m_private->m_resourceRequest->appCacheHostID();
    262 }
    263 
    264 void WebURLRequest::setAppCacheHostID(int appCacheHostID)
    265 {
    266     m_private->m_resourceRequest->setAppCacheHostID(appCacheHostID);
    267 }
    268 
    269 bool WebURLRequest::downloadToFile() const
    270 {
    271     return m_private->m_resourceRequest->downloadToFile();
    272 }
    273 
    274 void WebURLRequest::setDownloadToFile(bool downloadToFile)
    275 {
    276     m_private->m_resourceRequest->setDownloadToFile(downloadToFile);
    277 }
    278 
    279 ResourceRequest& WebURLRequest::toMutableResourceRequest()
    280 {
    281     ASSERT(m_private);
    282     ASSERT(m_private->m_resourceRequest);
    283 
    284     return *m_private->m_resourceRequest;
    285 }
    286 
    287 const ResourceRequest& WebURLRequest::toResourceRequest() const
    288 {
    289     ASSERT(m_private);
    290     ASSERT(m_private->m_resourceRequest);
    291 
    292     return *m_private->m_resourceRequest;
    293 }
    294 
    295 void WebURLRequest::assign(WebURLRequestPrivate* p)
    296 {
    297     // Subclasses may call this directly so a self-assignment check is needed
    298     // here as well as in the public assign method.
    299     if (m_private == p)
    300         return;
    301     if (m_private)
    302         m_private->dispose();
    303     m_private = p;
    304 }
    305 
    306 } // namespace WebKit
    307