9f9c5f42c992fbfd9b47e3893897ef034e17f438
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / UTIRegistry.cpp
1 /*
2  * Copyright (C) 2017-2018 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "UTIRegistry.h"
28
29 #if USE(CG)
30
31 #include "ImageSourceCG.h"
32 #include "MIMETypeRegistry.h"
33
34 #include <wtf/HashSet.h>
35 #include <wtf/NeverDestroyed.h>
36 #include <ImageIO/ImageIO.h>
37
38 namespace WebCore {
39
40 const HashSet<String>& defaultSupportedImageTypes()
41 {
42     static const auto defaultSupportedImageTypes = makeNeverDestroyed([] {
43         const String defaultSupportedImageTypes[] = {
44             "com.compuserve.gif"_s,
45             "com.microsoft.bmp"_s,
46             "com.microsoft.cur"_s,
47             "com.microsoft.ico"_s,
48             "public.jpeg"_s,
49             "public.png"_s,
50             "public.tiff"_s,
51 #if !PLATFORM(WIN)
52             "public.jpeg-2000"_s,
53             "public.mpo-image"_s,
54 #endif
55 #if HAVE(WEBP)
56             "public.webp"_s,
57             "com.google.webp"_s,
58 #endif
59         };
60
61         RetainPtr<CFArrayRef> systemImageTypes = adoptCF(CGImageSourceCopyTypeIdentifiers());
62         CFIndex count = CFArrayGetCount(systemImageTypes.get());
63
64         HashSet<String> defaultCGSupportedImageTypes;
65         for (auto& imageType : defaultSupportedImageTypes) {
66             RetainPtr<CFStringRef> string = imageType.createCFString();
67             if (CFArrayContainsValue(systemImageTypes.get(), CFRangeMake(0, count), string.get()))
68                 defaultCGSupportedImageTypes.add(imageType);
69         }
70         return defaultCGSupportedImageTypes;
71     }());
72
73     return defaultSupportedImageTypes;
74 }
75
76 HashSet<String>& additionalSupportedImageTypes()
77 {
78     static NeverDestroyed<HashSet<String>> additionalSupportedImageTypes;
79     return additionalSupportedImageTypes;
80 }
81
82 void setAdditionalSupportedImageTypes(const Vector<String>& imageTypes)
83 {
84     MIMETypeRegistry::additionalSupportedImageMIMETypes().clear();
85     for (const auto& imageType : imageTypes) {
86         additionalSupportedImageTypes().add(imageType);
87         auto mimeType = MIMETypeForImageType(imageType);
88         if (!mimeType.isEmpty())
89             MIMETypeRegistry::additionalSupportedImageMIMETypes().add(mimeType);
90     }
91 }
92
93 void setAdditionalSupportedImageTypesForTesting(const String& imageTypes)
94 {
95     setAdditionalSupportedImageTypes(imageTypes.split(';'));
96 }
97
98 bool isSupportedImageType(const String& imageType)
99 {
100     if (imageType.isEmpty())
101         return false;
102     return defaultSupportedImageTypes().contains(imageType) || additionalSupportedImageTypes().contains(imageType);
103 }
104
105 }
106
107 #endif