• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/WebURLRequest.h"
33 
34 #include "platform/exported/WebURLRequestPrivate.h"
35 #include "platform/network/ResourceRequest.h"
36 #include "public/platform/WebHTTPBody.h"
37 #include "public/platform/WebHTTPHeaderVisitor.h"
38 #include "public/platform/WebURL.h"
39 
40 using namespace WebCore;
41 
42 namespace blink {
43 
44 namespace {
45 
46 class ExtraDataContainer : public ResourceRequest::ExtraData {
47 public:
create(WebURLRequest::ExtraData * extraData)48     static PassRefPtr<ExtraDataContainer> create(WebURLRequest::ExtraData* extraData) { return adoptRef(new ExtraDataContainer(extraData)); }
49 
~ExtraDataContainer()50     virtual ~ExtraDataContainer() { }
51 
extraData() const52     WebURLRequest::ExtraData* extraData() const { return m_extraData.get(); }
53 
54 private:
ExtraDataContainer(WebURLRequest::ExtraData * extraData)55     explicit ExtraDataContainer(WebURLRequest::ExtraData* extraData)
56         : m_extraData(adoptPtr(extraData))
57     {
58     }
59 
60     OwnPtr<WebURLRequest::ExtraData> m_extraData;
61 };
62 
63 } // namespace
64 
65 // The standard implementation of WebURLRequestPrivate, which maintains
66 // ownership of a ResourceRequest instance.
67 class WebURLRequestPrivateImpl : public WebURLRequestPrivate {
68 public:
WebURLRequestPrivateImpl()69     WebURLRequestPrivateImpl()
70     {
71         m_resourceRequest = &m_resourceRequestAllocation;
72     }
73 
WebURLRequestPrivateImpl(const WebURLRequestPrivate * p)74     WebURLRequestPrivateImpl(const WebURLRequestPrivate* p)
75         : m_resourceRequestAllocation(*p->m_resourceRequest)
76     {
77         m_resourceRequest = &m_resourceRequestAllocation;
78         m_allowStoredCredentials = p->m_allowStoredCredentials;
79     }
80 
dispose()81     virtual void dispose() { delete this; }
82 
83 private:
~WebURLRequestPrivateImpl()84     virtual ~WebURLRequestPrivateImpl() { }
85 
86     ResourceRequest m_resourceRequestAllocation;
87 };
88 
initialize()89 void WebURLRequest::initialize()
90 {
91     assign(new WebURLRequestPrivateImpl());
92 }
93 
reset()94 void WebURLRequest::reset()
95 {
96     assign(0);
97 }
98 
assign(const WebURLRequest & r)99 void WebURLRequest::assign(const WebURLRequest& r)
100 {
101     if (&r != this)
102         assign(r.m_private ? new WebURLRequestPrivateImpl(r.m_private) : 0);
103 }
104 
isNull() const105 bool WebURLRequest::isNull() const
106 {
107     return !m_private || m_private->m_resourceRequest->isNull();
108 }
109 
url() const110 WebURL WebURLRequest::url() const
111 {
112     return m_private->m_resourceRequest->url();
113 }
114 
setURL(const WebURL & url)115 void WebURLRequest::setURL(const WebURL& url)
116 {
117     m_private->m_resourceRequest->setURL(url);
118 }
119 
firstPartyForCookies() const120 WebURL WebURLRequest::firstPartyForCookies() const
121 {
122     return m_private->m_resourceRequest->firstPartyForCookies();
123 }
124 
setFirstPartyForCookies(const WebURL & firstPartyForCookies)125 void WebURLRequest::setFirstPartyForCookies(const WebURL& firstPartyForCookies)
126 {
127     m_private->m_resourceRequest->setFirstPartyForCookies(firstPartyForCookies);
128 }
129 
allowCookies() const130 bool WebURLRequest::allowCookies() const
131 {
132     return m_private->m_resourceRequest->allowCookies();
133 }
134 
setAllowCookies(bool allowCookies)135 void WebURLRequest::setAllowCookies(bool allowCookies)
136 {
137     m_private->m_resourceRequest->setAllowCookies(allowCookies);
138 }
139 
allowStoredCredentials() const140 bool WebURLRequest::allowStoredCredentials() const
141 {
142     return m_private->m_allowStoredCredentials;
143 }
144 
setAllowStoredCredentials(bool allowStoredCredentials)145 void WebURLRequest::setAllowStoredCredentials(bool allowStoredCredentials)
146 {
147     m_private->m_allowStoredCredentials = allowStoredCredentials;
148 }
149 
cachePolicy() const150 WebURLRequest::CachePolicy WebURLRequest::cachePolicy() const
151 {
152     return static_cast<WebURLRequest::CachePolicy>(
153         m_private->m_resourceRequest->cachePolicy());
154 }
155 
setCachePolicy(CachePolicy cachePolicy)156 void WebURLRequest::setCachePolicy(CachePolicy cachePolicy)
157 {
158     m_private->m_resourceRequest->setCachePolicy(
159         static_cast<ResourceRequestCachePolicy>(cachePolicy));
160 }
161 
httpMethod() const162 WebString WebURLRequest::httpMethod() const
163 {
164     return m_private->m_resourceRequest->httpMethod();
165 }
166 
setHTTPMethod(const WebString & httpMethod)167 void WebURLRequest::setHTTPMethod(const WebString& httpMethod)
168 {
169     m_private->m_resourceRequest->setHTTPMethod(httpMethod);
170 }
171 
httpHeaderField(const WebString & name) const172 WebString WebURLRequest::httpHeaderField(const WebString& name) const
173 {
174     return m_private->m_resourceRequest->httpHeaderField(name);
175 }
176 
setHTTPHeaderField(const WebString & name,const WebString & value)177 void WebURLRequest::setHTTPHeaderField(const WebString& name, const WebString& value)
178 {
179     m_private->m_resourceRequest->setHTTPHeaderField(name, value);
180 }
181 
addHTTPHeaderField(const WebString & name,const WebString & value)182 void WebURLRequest::addHTTPHeaderField(const WebString& name, const WebString& value)
183 {
184     m_private->m_resourceRequest->addHTTPHeaderField(name, value);
185 }
186 
clearHTTPHeaderField(const WebString & name)187 void WebURLRequest::clearHTTPHeaderField(const WebString& name)
188 {
189     m_private->m_resourceRequest->clearHTTPHeaderField(name);
190 }
191 
visitHTTPHeaderFields(WebHTTPHeaderVisitor * visitor) const192 void WebURLRequest::visitHTTPHeaderFields(WebHTTPHeaderVisitor* visitor) const
193 {
194     const HTTPHeaderMap& map = m_private->m_resourceRequest->httpHeaderFields();
195     for (HTTPHeaderMap::const_iterator it = map.begin(); it != map.end(); ++it)
196         visitor->visitHeader(it->key, it->value);
197 }
198 
httpBody() const199 WebHTTPBody WebURLRequest::httpBody() const
200 {
201     return WebHTTPBody(m_private->m_resourceRequest->httpBody());
202 }
203 
setHTTPBody(const WebHTTPBody & httpBody)204 void WebURLRequest::setHTTPBody(const WebHTTPBody& httpBody)
205 {
206     m_private->m_resourceRequest->setHTTPBody(httpBody);
207 }
208 
reportUploadProgress() const209 bool WebURLRequest::reportUploadProgress() const
210 {
211     return m_private->m_resourceRequest->reportUploadProgress();
212 }
213 
setReportUploadProgress(bool reportUploadProgress)214 void WebURLRequest::setReportUploadProgress(bool reportUploadProgress)
215 {
216     m_private->m_resourceRequest->setReportUploadProgress(reportUploadProgress);
217 }
218 
reportLoadTiming() const219 bool WebURLRequest::reportLoadTiming() const
220 {
221     return m_private->m_resourceRequest->reportLoadTiming();
222 }
223 
setReportRawHeaders(bool reportRawHeaders)224 void WebURLRequest::setReportRawHeaders(bool reportRawHeaders)
225 {
226     m_private->m_resourceRequest->setReportRawHeaders(reportRawHeaders);
227 }
228 
reportRawHeaders() const229 bool WebURLRequest::reportRawHeaders() const
230 {
231     return m_private->m_resourceRequest->reportRawHeaders();
232 }
233 
setReportLoadTiming(bool reportLoadTiming)234 void WebURLRequest::setReportLoadTiming(bool reportLoadTiming)
235 {
236     m_private->m_resourceRequest->setReportLoadTiming(reportLoadTiming);
237 }
238 
targetType() const239 WebURLRequest::TargetType WebURLRequest::targetType() const
240 {
241     // FIXME: Temporary special case until downstream chromium.org knows of the new TargetTypes.
242     TargetType targetType = static_cast<TargetType>(m_private->m_resourceRequest->targetType());
243     if (targetType == TargetIsTextTrack || targetType == TargetIsUnspecified)
244         return TargetIsSubresource;
245     return targetType;
246 }
247 
hasUserGesture() const248 bool WebURLRequest::hasUserGesture() const
249 {
250     return m_private->m_resourceRequest->hasUserGesture();
251 }
252 
setHasUserGesture(bool hasUserGesture)253 void WebURLRequest::setHasUserGesture(bool hasUserGesture)
254 {
255     m_private->m_resourceRequest->setHasUserGesture(hasUserGesture);
256 }
257 
setTargetType(TargetType targetType)258 void WebURLRequest::setTargetType(TargetType targetType)
259 {
260     m_private->m_resourceRequest->setTargetType(
261         static_cast<ResourceRequest::TargetType>(targetType));
262 }
263 
requestorID() const264 int WebURLRequest::requestorID() const
265 {
266     return m_private->m_resourceRequest->requestorID();
267 }
268 
setRequestorID(int requestorID)269 void WebURLRequest::setRequestorID(int requestorID)
270 {
271     m_private->m_resourceRequest->setRequestorID(requestorID);
272 }
273 
requestorProcessID() const274 int WebURLRequest::requestorProcessID() const
275 {
276     return m_private->m_resourceRequest->requestorProcessID();
277 }
278 
setRequestorProcessID(int requestorProcessID)279 void WebURLRequest::setRequestorProcessID(int requestorProcessID)
280 {
281     m_private->m_resourceRequest->setRequestorProcessID(requestorProcessID);
282 }
283 
appCacheHostID() const284 int WebURLRequest::appCacheHostID() const
285 {
286     return m_private->m_resourceRequest->appCacheHostID();
287 }
288 
setAppCacheHostID(int appCacheHostID)289 void WebURLRequest::setAppCacheHostID(int appCacheHostID)
290 {
291     m_private->m_resourceRequest->setAppCacheHostID(appCacheHostID);
292 }
293 
downloadToFile() const294 bool WebURLRequest::downloadToFile() const
295 {
296     return m_private->m_resourceRequest->downloadToFile();
297 }
298 
setDownloadToFile(bool downloadToFile)299 void WebURLRequest::setDownloadToFile(bool downloadToFile)
300 {
301     m_private->m_resourceRequest->setDownloadToFile(downloadToFile);
302 }
303 
extraData() const304 WebURLRequest::ExtraData* WebURLRequest::extraData() const
305 {
306     RefPtr<ResourceRequest::ExtraData> data = m_private->m_resourceRequest->extraData();
307     if (!data)
308         return 0;
309     return static_cast<ExtraDataContainer*>(data.get())->extraData();
310 }
311 
setExtraData(WebURLRequest::ExtraData * extraData)312 void WebURLRequest::setExtraData(WebURLRequest::ExtraData* extraData)
313 {
314     m_private->m_resourceRequest->setExtraData(ExtraDataContainer::create(extraData));
315 }
316 
toMutableResourceRequest()317 ResourceRequest& WebURLRequest::toMutableResourceRequest()
318 {
319     ASSERT(m_private);
320     ASSERT(m_private->m_resourceRequest);
321 
322     return *m_private->m_resourceRequest;
323 }
324 
priority() const325 WebURLRequest::Priority WebURLRequest::priority() const
326 {
327     return static_cast<WebURLRequest::Priority>(
328         m_private->m_resourceRequest->priority());
329 }
330 
toResourceRequest() const331 const ResourceRequest& WebURLRequest::toResourceRequest() const
332 {
333     ASSERT(m_private);
334     ASSERT(m_private->m_resourceRequest);
335 
336     return *m_private->m_resourceRequest;
337 }
338 
assign(WebURLRequestPrivate * p)339 void WebURLRequest::assign(WebURLRequestPrivate* p)
340 {
341     // Subclasses may call this directly so a self-assignment check is needed
342     // here as well as in the public assign method.
343     if (m_private == p)
344         return;
345     if (m_private)
346         m_private->dispose();
347     m_private = p;
348 }
349 
350 } // namespace blink
351