Home | History | Annotate | Download | only in wince
      1 /*
      2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2007-2009 Torch Mobile, Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "MIMETypeRegistry.h"
     29 
     30 #include <wtf/HashMap.h>
     31 #include <windows.h>
     32 #include <winreg.h>
     33 
     34 namespace WebCore {
     35 
     36 static String mimeTypeForExtension(const String& extension)
     37 {
     38     String ext = "." + extension;
     39     WCHAR contentTypeStr[256];
     40     DWORD contentTypeStrLen = sizeof(contentTypeStr);
     41     DWORD valueType;
     42 
     43     HKEY key;
     44     String result;
     45     if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, ext.charactersWithNullTermination(), 0, 0, &key))
     46         return result;
     47 
     48     if (ERROR_SUCCESS == RegQueryValueEx(key, L"Content Type", 0, &valueType, (LPBYTE)contentTypeStr, &contentTypeStrLen) && valueType == REG_SZ)
     49         result = String(contentTypeStr, contentTypeStrLen / sizeof(contentTypeStr[0]) - 1);
     50 
     51     RegCloseKey(key);
     52 
     53     return result;
     54 }
     55 
     56 static HashMap<String, String> mimetypeMap;
     57 
     58 static void initMIMETypeEntensionMap()
     59 {
     60     if (mimetypeMap.isEmpty()) {
     61         //fill with initial values
     62         mimetypeMap.add("txt", "text/plain");
     63         mimetypeMap.add("pdf", "application/pdf");
     64         mimetypeMap.add("ps", "application/postscript");
     65         mimetypeMap.add("html", "text/html");
     66         mimetypeMap.add("htm", "text/html");
     67         mimetypeMap.add("xml", "text/xml");
     68         mimetypeMap.add("xsl", "text/xsl");
     69         mimetypeMap.add("js", "application/x-javascript");
     70         mimetypeMap.add("xhtml", "application/xhtml+xml");
     71         mimetypeMap.add("rss", "application/rss+xml");
     72         mimetypeMap.add("webarchive", "application/x-webarchive");
     73         mimetypeMap.add("svg", "image/svg+xml");
     74         mimetypeMap.add("svgz", "image/svg+xml");
     75         mimetypeMap.add("jpg", "image/jpeg");
     76         mimetypeMap.add("jpeg", "image/jpeg");
     77         mimetypeMap.add("png", "image/png");
     78         mimetypeMap.add("tif", "image/tiff");
     79         mimetypeMap.add("tiff", "image/tiff");
     80         mimetypeMap.add("ico", "image/ico");
     81         mimetypeMap.add("cur", "image/ico");
     82         mimetypeMap.add("bmp", "image/bmp");
     83         mimetypeMap.add("css", "text/css");
     84         // FIXME: Custom font works only when MIME is "text/plain"
     85         mimetypeMap.add("ttf", "text/plain"); // "font/ttf"
     86         mimetypeMap.add("otf", "text/plain"); // "font/otf"
     87 #if ENABLE(WML)
     88         mimetypeMap.add("wml", "text/vnd.wap.wml");
     89 #endif
     90 #if ENABLE(WBXML)
     91         mimetypeMap.add("wbxml", "application/vnd.wap.wmlc");
     92 #endif
     93     }
     94 }
     95 
     96 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
     97 {
     98     if (type.isEmpty())
     99         return String();
    100 
    101     // Avoid conflicts with "ttf" and "otf"
    102     if (equalIgnoringCase(type, "text/plain"))
    103         return "txt";
    104 
    105     initMIMETypeEntensionMap();
    106 
    107     for (HashMap<String, String>::iterator i = mimetypeMap.begin(); i != mimetypeMap.end(); ++i) {
    108         if (equalIgnoringCase(i->second, type))
    109             return i->first;
    110     }
    111 
    112 #if ENABLE(XHTMLMP)
    113     if (equalIgnoringCase("application/vnd.wap.xhtml+xml", type))
    114         return String("xml");
    115 #endif
    116 
    117     return String();
    118 }
    119 
    120 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
    121 {
    122     if (ext.isEmpty())
    123         return String();
    124 
    125     initMIMETypeEntensionMap();
    126 
    127     String result = mimetypeMap.get(ext.lower());
    128     if (result.isEmpty()) {
    129         result = mimeTypeForExtension(ext);
    130         if (!result.isEmpty())
    131             mimetypeMap.add(ext, result);
    132     }
    133     return result.isEmpty() ? "unknown/unknown" : result;
    134 }
    135 
    136 }
    137