| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "net/base/mime_util.h" | 7 #include "net/base/mime_util.h" |
| 8 #include "net/base/platform_mime_util.h" | 8 #include "net/base/platform_mime_util.h" |
| 9 | 9 |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/singleton.h" | 12 #include "base/singleton.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 | 14 |
| 15 using std::string; | 15 using std::string; |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 // Singleton utility class for mime types. | 19 // Singleton utility class for mime types. |
| 20 class MimeUtil : public PlatformMimeUtil { | 20 class MimeUtil : public PlatformMimeUtil { |
| 21 public: | 21 public: |
| 22 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, | 22 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, |
| 23 std::string* mime_type) const; | 23 std::string* mime_type) const; |
| 24 | 24 |
| 25 bool GetMimeTypeFromFile(const FilePath& file_path, | 25 bool GetMimeTypeFromFile(const FilePath& file_path, |
| 26 std::string* mime_type) const; | 26 std::string* mime_type) const; |
| 27 | 27 |
| 28 bool IsSupportedImageMimeType(const char* mime_type) const; | 28 bool IsSupportedImageMimeType(const char* mime_type) const; |
| 29 bool IsSupportedMediaMimeType(const char* mime_type) const; |
| 29 bool IsSupportedNonImageMimeType(const char* mime_type) const; | 30 bool IsSupportedNonImageMimeType(const char* mime_type) const; |
| 30 bool IsSupportedJavascriptMimeType(const char* mime_type) const; | 31 bool IsSupportedJavascriptMimeType(const char* mime_type) const; |
| 31 | 32 |
| 32 bool IsViewSourceMimeType(const char* mime_type) const; | 33 bool IsViewSourceMimeType(const char* mime_type) const; |
| 33 | 34 |
| 34 bool IsSupportedMimeType(const std::string& mime_type) const; | 35 bool IsSupportedMimeType(const std::string& mime_type) const; |
| 35 | 36 |
| 36 bool MatchesMimeType(const std::string &mime_type_pattern, | 37 bool MatchesMimeType(const std::string &mime_type_pattern, |
| 37 const std::string &mime_type) const; | 38 const std::string &mime_type) const; |
| 38 | 39 |
| 39 private: | 40 private: |
| 40 friend struct DefaultSingletonTraits<MimeUtil>; | 41 friend struct DefaultSingletonTraits<MimeUtil>; |
| 41 MimeUtil() { | 42 MimeUtil() { |
| 42 InitializeMimeTypeMaps(); | 43 InitializeMimeTypeMaps(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 // For faster lookup, keep hash sets. | 46 // For faster lookup, keep hash sets. |
| 46 void InitializeMimeTypeMaps(); | 47 void InitializeMimeTypeMaps(); |
| 47 | 48 |
| 48 typedef base::hash_set<std::string> MimeMappings; | 49 typedef base::hash_set<std::string> MimeMappings; |
| 49 MimeMappings image_map_; | 50 MimeMappings image_map_; |
| 51 MimeMappings media_map_; |
| 50 MimeMappings non_image_map_; | 52 MimeMappings non_image_map_; |
| 51 MimeMappings javascript_map_; | 53 MimeMappings javascript_map_; |
| 52 MimeMappings view_source_map_; | 54 MimeMappings view_source_map_; |
| 53 }; // class MimeUtil | 55 }; // class MimeUtil |
| 54 | 56 |
| 55 struct MimeInfo { | 57 struct MimeInfo { |
| 56 const char* mime_type; | 58 const char* mime_type; |
| 57 const char* extensions; // comma separated list | 59 const char* extensions; // comma separated list |
| 58 }; | 60 }; |
| 59 | 61 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 "image/jpeg", | 161 "image/jpeg", |
| 160 "image/pjpeg", | 162 "image/pjpeg", |
| 161 "image/jpg", | 163 "image/jpg", |
| 162 "image/png", | 164 "image/png", |
| 163 "image/gif", | 165 "image/gif", |
| 164 "image/bmp", | 166 "image/bmp", |
| 165 "image/x-icon", // ico | 167 "image/x-icon", // ico |
| 166 "image/x-xbitmap" // xbm | 168 "image/x-xbitmap" // xbm |
| 167 }; | 169 }; |
| 168 | 170 |
| 171 // TODO(hclam): Integrate this list with |secondary_mappings| above. |
| 172 static const char* const supported_media_types[] = { |
| 173 // Ogg. |
| 174 "audio/ogg", |
| 175 "video/ogg", |
| 176 |
| 177 // MPEG-4. |
| 178 "application/mp4", |
| 179 "audio/mp4", |
| 180 "audio/x-m4a", |
| 181 "video/mp4", |
| 182 "video/x-m4v", |
| 183 |
| 184 // MP3. |
| 185 // TODO(hclam): may add "audio/mpeg" and "audio/x-mp3". |
| 186 "audio/mp3", |
| 187 |
| 188 // AAC. |
| 189 "audio/aac", |
| 190 "audio/x-aac" |
| 191 }; |
| 192 |
| 169 // Note: does not include javascript types list (see supported_javascript_types) | 193 // Note: does not include javascript types list (see supported_javascript_types) |
| 170 static const char* const supported_non_image_types[] = { | 194 static const char* const supported_non_image_types[] = { |
| 171 "text/html", | 195 "text/html", |
| 172 "text/xml", | 196 "text/xml", |
| 173 "text/xsl", | 197 "text/xsl", |
| 174 "text/plain", | 198 "text/plain", |
| 175 "text/", | 199 "text/", |
| 176 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type | 200 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type |
| 177 "application/xml", | 201 "application/xml", |
| 178 "application/xhtml+xml", | 202 "application/xhtml+xml", |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 "application/xml", | 234 "application/xml", |
| 211 "application/rss+xml", | 235 "application/rss+xml", |
| 212 "application/atom+xml", | 236 "application/atom+xml", |
| 213 "image/svg+xml" | 237 "image/svg+xml" |
| 214 }; | 238 }; |
| 215 | 239 |
| 216 void MimeUtil::InitializeMimeTypeMaps() { | 240 void MimeUtil::InitializeMimeTypeMaps() { |
| 217 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 241 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
| 218 image_map_.insert(supported_image_types[i]); | 242 image_map_.insert(supported_image_types[i]); |
| 219 | 243 |
| 220 // Initialize the supported non-image types | 244 // Initialize the supported non-image types. |
| 221 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 245 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
| 222 non_image_map_.insert(supported_non_image_types[i]); | 246 non_image_map_.insert(supported_non_image_types[i]); |
| 223 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | 247 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) |
| 224 non_image_map_.insert(supported_javascript_types[i]); | 248 non_image_map_.insert(supported_javascript_types[i]); |
| 249 for (size_t i = 0; i < arraysize(supported_media_types); ++i) |
| 250 non_image_map_.insert(supported_media_types[i]); |
| 251 |
| 252 // Initialize the supported media types. |
| 253 for (size_t i = 0; i < arraysize(supported_media_types); ++i) |
| 254 media_map_.insert(supported_media_types[i]); |
| 225 | 255 |
| 226 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | 256 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) |
| 227 javascript_map_.insert(supported_javascript_types[i]); | 257 javascript_map_.insert(supported_javascript_types[i]); |
| 228 | 258 |
| 229 for (size_t i = 0; i < arraysize(view_source_types); ++i) | 259 for (size_t i = 0; i < arraysize(view_source_types); ++i) |
| 230 view_source_map_.insert(view_source_types[i]); | 260 view_source_map_.insert(view_source_types[i]); |
| 231 } | 261 } |
| 232 | 262 |
| 233 bool MimeUtil::IsSupportedImageMimeType(const char* mime_type) const { | 263 bool MimeUtil::IsSupportedImageMimeType(const char* mime_type) const { |
| 234 return image_map_.find(mime_type) != image_map_.end(); | 264 return image_map_.find(mime_type) != image_map_.end(); |
| 235 } | 265 } |
| 236 | 266 |
| 267 bool MimeUtil::IsSupportedMediaMimeType(const char* mime_type) const { |
| 268 return media_map_.find(mime_type) != media_map_.end(); |
| 269 } |
| 270 |
| 237 bool MimeUtil::IsSupportedNonImageMimeType(const char* mime_type) const { | 271 bool MimeUtil::IsSupportedNonImageMimeType(const char* mime_type) const { |
| 238 return non_image_map_.find(mime_type) != non_image_map_.end(); | 272 return non_image_map_.find(mime_type) != non_image_map_.end(); |
| 239 } | 273 } |
| 240 | 274 |
| 241 bool MimeUtil::IsSupportedJavascriptMimeType(const char* mime_type) const { | 275 bool MimeUtil::IsSupportedJavascriptMimeType(const char* mime_type) const { |
| 242 return javascript_map_.find(mime_type) != javascript_map_.end(); | 276 return javascript_map_.find(mime_type) != javascript_map_.end(); |
| 243 } | 277 } |
| 244 | 278 |
| 245 bool MimeUtil::IsViewSourceMimeType(const char* mime_type) const { | 279 bool MimeUtil::IsViewSourceMimeType(const char* mime_type) const { |
| 246 return view_source_map_.find(mime_type) != view_source_map_.end(); | 280 return view_source_map_.find(mime_type) != view_source_map_.end(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 343 |
| 310 bool GetPreferredExtensionForMimeType(const std::string& mime_type, | 344 bool GetPreferredExtensionForMimeType(const std::string& mime_type, |
| 311 FilePath::StringType* extension) { | 345 FilePath::StringType* extension) { |
| 312 return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension); | 346 return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension); |
| 313 } | 347 } |
| 314 | 348 |
| 315 bool IsSupportedImageMimeType(const char* mime_type) { | 349 bool IsSupportedImageMimeType(const char* mime_type) { |
| 316 return GetMimeUtil()->IsSupportedImageMimeType(mime_type); | 350 return GetMimeUtil()->IsSupportedImageMimeType(mime_type); |
| 317 } | 351 } |
| 318 | 352 |
| 353 bool IsSupportedMediaMimeType(const char* mime_type) { |
| 354 return GetMimeUtil()->IsSupportedMediaMimeType(mime_type); |
| 355 } |
| 356 |
| 319 bool IsSupportedNonImageMimeType(const char* mime_type) { | 357 bool IsSupportedNonImageMimeType(const char* mime_type) { |
| 320 return GetMimeUtil()->IsSupportedNonImageMimeType(mime_type); | 358 return GetMimeUtil()->IsSupportedNonImageMimeType(mime_type); |
| 321 } | 359 } |
| 322 | 360 |
| 323 bool IsSupportedJavascriptMimeType(const char* mime_type) { | 361 bool IsSupportedJavascriptMimeType(const char* mime_type) { |
| 324 return GetMimeUtil()->IsSupportedJavascriptMimeType(mime_type); | 362 return GetMimeUtil()->IsSupportedJavascriptMimeType(mime_type); |
| 325 } | 363 } |
| 326 | 364 |
| 327 bool IsViewSourceMimeType(const char* mime_type) { | 365 bool IsViewSourceMimeType(const char* mime_type) { |
| 328 return GetMimeUtil()->IsViewSourceMimeType(mime_type); | 366 return GetMimeUtil()->IsViewSourceMimeType(mime_type); |
| 329 } | 367 } |
| 330 | 368 |
| 331 bool IsSupportedMimeType(const std::string& mime_type) { | 369 bool IsSupportedMimeType(const std::string& mime_type) { |
| 332 return GetMimeUtil()->IsSupportedMimeType(mime_type); | 370 return GetMimeUtil()->IsSupportedMimeType(mime_type); |
| 333 } | 371 } |
| 334 | 372 |
| 335 bool MatchesMimeType(const std::string &mime_type_pattern, | 373 bool MatchesMimeType(const std::string &mime_type_pattern, |
| 336 const std::string &mime_type) { | 374 const std::string &mime_type) { |
| 337 return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); | 375 return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); |
| 338 } | 376 } |
| 339 | 377 |
| 340 } // namespace net | 378 } // namespace net |
| OLD | NEW |