[BlackBerry] Set correct ResourceRequest target type.
[WebKit-https.git] / Source / WebCore / platform / network / blackberry / ResourceRequestBlackBerry.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2011 Research In Motion Limited. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "ResourceRequest.h"
21
22 #include "BlobRegistryImpl.h"
23 #include <BlackBerryPlatformClient.h>
24 #include <network/NetworkRequest.h>
25 #include <wtf/HashMap.h>
26 #include <wtf/text/CString.h>
27
28 using BlackBerry::Platform::NetworkRequest;
29
30 namespace WebCore {
31
32 unsigned initializeMaximumHTTPConnectionCountPerHost()
33 {
34     return 6;
35 }
36
37 static inline NetworkRequest::CachePolicy platformCachePolicyForRequest(const ResourceRequest& request)
38 {
39     switch (request.cachePolicy()) {
40     case WebCore::UseProtocolCachePolicy:
41         return NetworkRequest::UseProtocolCachePolicy;
42     case WebCore::ReloadIgnoringCacheData:
43         return NetworkRequest::ReloadIgnoringCacheData;
44     case WebCore::ReturnCacheDataElseLoad:
45         return NetworkRequest::ReturnCacheDataElseLoad;
46     case WebCore::ReturnCacheDataDontLoad:
47         return NetworkRequest::ReturnCacheDataDontLoad;
48     default:
49         ASSERT_NOT_REACHED();
50         return NetworkRequest::UseProtocolCachePolicy;
51     }
52 }
53
54 static inline NetworkRequest::TargetType platformTargetTypeForRequest(const ResourceRequest& request)
55 {
56     if (request.isXMLHTTPRequest())
57         return NetworkRequest::TargetIsXMLHTTPRequest;
58
59     switch (request.targetType()) {
60     case ResourceRequest::TargetIsMainFrame:
61         return NetworkRequest::TargetIsMainFrame;
62     case ResourceRequest::TargetIsSubframe:
63         return NetworkRequest::TargetIsSubframe;
64     case ResourceRequest::TargetIsSubresource:
65         return NetworkRequest::TargetIsSubresource;
66     case ResourceRequest::TargetIsStyleSheet:
67         return NetworkRequest::TargetIsStyleSheet;
68     case ResourceRequest::TargetIsScript:
69         return NetworkRequest::TargetIsScript;
70     case ResourceRequest::TargetIsFontResource:
71         return NetworkRequest::TargetIsFontResource;
72     case ResourceRequest::TargetIsImage:
73         return NetworkRequest::TargetIsImage;
74     case ResourceRequest::TargetIsObject:
75         return NetworkRequest::TargetIsObject;
76     case ResourceRequest::TargetIsMedia:
77         return NetworkRequest::TargetIsMedia;
78     case ResourceRequest::TargetIsWorker:
79         return NetworkRequest::TargetIsWorker;
80     case ResourceRequest::TargetIsSharedWorker:
81         return NetworkRequest::TargetIsSharedWorker;
82     default:
83         ASSERT_NOT_REACHED();
84         return NetworkRequest::TargetIsUnknown;
85     }
86 }
87
88 typedef HashMap<String, ResourceRequest::TargetType> MimeTypeResourceRequestTypeMap;
89
90 static const MimeTypeResourceRequestTypeMap& mimeTypeRequestTypeMap()
91 {
92     static MimeTypeResourceRequestTypeMap* map = 0;
93     if (!map) {
94         map = new MimeTypeResourceRequestTypeMap;
95
96         if (map) {
97             // The list here should match extensionMap[] in MIMETypeRegistryBlackBerry.cpp
98             map->add(String("text/css"), ResourceRequest::TargetIsStyleSheet);
99             map->add(String("application/x-javascript"), ResourceRequest::TargetIsScript);
100             map->add(String("image/bmp"), ResourceRequest::TargetIsImage);
101             map->add(String("image/gif"), ResourceRequest::TargetIsImage);
102             map->add(String("image/x-icon"), ResourceRequest::TargetIsImage);
103             map->add(String("image/jpeg"), ResourceRequest::TargetIsImage);
104             map->add(String("image/png"), ResourceRequest::TargetIsImage);
105             map->add(String("image/x-portable-bitmap"), ResourceRequest::TargetIsImage);
106             map->add(String("image/x-portable-graymap"), ResourceRequest::TargetIsImage);
107             map->add(String("image/x-portable-pixmap"), ResourceRequest::TargetIsImage);
108             map->add(String("image/svg+xml"), ResourceRequest::TargetIsImage);
109             map->add(String("image/tiff"), ResourceRequest::TargetIsImage);
110             map->add(String("image/x-xbitmap"), ResourceRequest::TargetIsImage);
111             map->add(String("image/x-xpm"), ResourceRequest::TargetIsImage);
112         }
113     }
114
115     return *map;
116 }
117
118 ResourceRequest::TargetType ResourceRequest::targetTypeFromMimeType(const String& mimeType)
119 {
120     const MimeTypeResourceRequestTypeMap& map = mimeTypeRequestTypeMap();
121
122     MimeTypeResourceRequestTypeMap::const_iterator iter = map.find(mimeType);
123     if (iter == map.end())
124         return ResourceRequest::TargetIsUnspecified;
125
126     return iter->second;
127 }
128
129 void ResourceRequest::initializePlatformRequest(NetworkRequest& platformRequest, bool isInitial) const
130 {
131     // If this is the initial load, skip the request body and headers.
132     if (isInitial)
133         platformRequest.setRequestInitial(timeoutInterval());
134     else {
135         platformRequest.setRequestUrl(url().string().utf8().data(),
136                 httpMethod().latin1().data(),
137                 platformCachePolicyForRequest(*this),
138                 platformTargetTypeForRequest(*this),
139                 timeoutInterval());
140
141         platformRequest.setConditional(isConditional());
142
143         if (httpBody() && !httpBody()->isEmpty()) {
144             const Vector<FormDataElement>& elements = httpBody()->elements();
145             // Use setData for simple forms because it is slightly more efficient.
146             if (elements.size() == 1 && elements[0].m_type == FormDataElement::data)
147                 platformRequest.setData(elements[0].m_data.data(), elements[0].m_data.size());
148             else {
149                 for (unsigned i = 0; i < elements.size(); ++i) {
150                     const FormDataElement& element = elements[i];
151                     if (element.m_type == FormDataElement::data)
152                         platformRequest.addMultipartData(element.m_data.data(), element.m_data.size());
153                     else if (element.m_type == FormDataElement::encodedFile)
154                         platformRequest.addMultipartFilename(element.m_filename.characters(), element.m_filename.length());
155 #if ENABLE(BLOB)
156                     else if (element.m_type == FormDataElement::encodedBlob) {
157                         RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_blobURL));
158                         if (blobData) {
159                             for (size_t j = 0; j < blobData->items().size(); ++j) {
160                                 const BlobDataItem& blobItem = blobData->items()[j];
161                                 if (blobItem.type == BlobDataItem::Data)
162                                     platformRequest.addMultipartData(blobItem.data->data() + static_cast<int>(blobItem.offset), static_cast<int>(blobItem.length));
163                                 else {
164                                     ASSERT(blobItem.type == BlobDataItem::File);
165                                     platformRequest.addMultipartFilename(blobItem.path.characters(), blobItem.path.length(), blobItem.offset, blobItem.length, blobItem.expectedModificationTime);
166                                 }
167                             }
168                         }
169                     }
170 #endif
171                     else
172                         ASSERT_NOT_REACHED(); // unknown type
173                 }
174             }
175         }
176
177         for (HTTPHeaderMap::const_iterator it = httpHeaderFields().begin(); it != httpHeaderFields().end(); ++it) {
178             String key = it->first;
179             String value = it->second;
180             if (!key.isEmpty() && !value.isEmpty())
181                 platformRequest.addHeader(key.latin1().data(), value.latin1().data());
182         }
183
184         // Locale has the form "en-US". Construct accept language like "en-US, en;q=0.8".
185         std::string locale = BlackBerry::Platform::Client::get()->getLocale();
186         // POSIX locale has '_' instead of '-'.
187         // Replace to conform to HTTP spec.
188         size_t underscore = locale.find('_');
189         if (underscore != std::string::npos)
190             locale.replace(underscore, 1, "-");
191         std::string acceptLanguage = locale + ", " + locale.substr(0, 2) + ";q=0.8";
192         platformRequest.addHeader("Accept-Language", acceptLanguage.c_str());
193     }
194 }
195
196 PassOwnPtr<CrossThreadResourceRequestData> ResourceRequest::doPlatformCopyData(PassOwnPtr<CrossThreadResourceRequestData> data) const
197 {
198     data->m_token = m_token;
199     data->m_anchorText = m_anchorText;
200     data->m_isXMLHTTPRequest = m_isXMLHTTPRequest;
201     data->m_mustHandleInternally = m_mustHandleInternally;
202     return data;
203 }
204
205 void ResourceRequest::doPlatformAdopt(PassOwnPtr<CrossThreadResourceRequestData> data)
206 {
207     m_token = data->m_token;
208     m_anchorText = data->m_anchorText;
209     m_isXMLHTTPRequest = data->m_isXMLHTTPRequest;
210     m_mustHandleInternally = data->m_mustHandleInternally;
211     m_forceDownload = data->m_forceDownload;
212 }
213
214 } // namespace WebCore