2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #import "NetscapePluginModule.h"
29 #import <WebCore/WebCoreNSStringExtras.h>
30 #import <wtf/HashSet.h>
32 using namespace WebCore;
36 static bool getPluginArchitecture(CFBundleRef bundle, cpu_type_t& pluginArchitecture)
38 RetainPtr<CFArrayRef> pluginArchitecturesArray(AdoptCF, CFBundleCopyExecutableArchitectures(bundle));
39 if (!pluginArchitecturesArray)
42 // Turn the array into a set.
43 HashSet<unsigned> architectures;
44 for (CFIndex i = 0, numPluginArchitectures = CFArrayGetCount(pluginArchitecturesArray.get()); i < numPluginArchitectures; ++i) {
45 CFNumberRef number = static_cast<CFNumberRef>(CFArrayGetValueAtIndex(pluginArchitecturesArray.get(), i));
48 if (!CFNumberGetValue(number, kCFNumberSInt32Type, &architecture))
50 architectures.add(architecture);
54 // We only support 64-bit Intel plug-ins on 64-bit Intel.
55 if (architectures.contains(kCFBundleExecutableArchitectureX86_64)) {
56 pluginArchitecture = CPU_TYPE_X86_64;
60 // We also support 32-bit Intel plug-ins on 64-bit Intel.
61 if (architectures.contains(kCFBundleExecutableArchitectureI386)) {
62 pluginArchitecture = CPU_TYPE_X86;
65 #elif defined(__i386__)
66 // We only support 32-bit Intel plug-ins on 32-bit Intel.
67 if (architectures.contains(kCFBundleExecutableArchitectureI386)) {
68 pluginArchitecture = CPU_TYPE_X86;
71 #elif defined(__ppc64__)
72 // We only support 64-bit PPC plug-ins on 64-bit PPC.
73 if (architectures.contains(kCFBundleExecutableArchitecturePPC64)) {
74 pluginArchitecture = CPU_TYPE_POWERPC64;
77 #elif defined(__ppc__)
78 // We only support 32-bit PPC plug-ins on 32-bit PPC.
79 if (architectures.contains(kCFBundleExecutableArchitecturePPC)) {
80 pluginArchitecture = CPU_TYPE_POWERPC;
84 #error "Unhandled architecture"
90 static bool getPluginInfoFromPropertyLists(CFBundleRef bundle, PluginInfo& pluginInfo)
92 // FIXME: Handle WebPluginMIMETypesFilenameKey.
94 CFDictionaryRef mimeTypes = static_cast<CFDictionaryRef>(CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("WebPluginMIMETypes")));
95 if (!mimeTypes || CFGetTypeID(mimeTypes) != CFDictionaryGetTypeID())
98 // Get the plug-in name.
99 CFStringRef pluginName = static_cast<CFStringRef>(CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("WebPluginName")));
100 if (pluginName && CFGetTypeID(pluginName) == CFStringGetTypeID())
101 pluginInfo.name = pluginName;
103 // Get the plug-in description.
104 CFStringRef pluginDescription = static_cast<CFStringRef>(CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("WebPluginDescription")));
105 if (pluginDescription && CFGetTypeID(pluginDescription) == CFStringGetTypeID())
106 pluginInfo.desc = pluginDescription;
108 // Get the MIME type mapping dictionary.
109 CFIndex numMimeTypes = CFDictionaryGetCount(mimeTypes);
110 Vector<CFStringRef> mimeTypesVector(numMimeTypes);
111 Vector<CFDictionaryRef> mimeTypeInfoVector(numMimeTypes);
112 CFDictionaryGetKeysAndValues(mimeTypes, reinterpret_cast<const void**>(mimeTypesVector.data()), reinterpret_cast<const void**>(mimeTypeInfoVector.data()));
114 for (CFIndex i = 0; i < numMimeTypes; ++i) {
115 MimeClassInfo mimeClassInfo;
117 // If this MIME type is invalid, ignore it.
118 CFStringRef mimeType = mimeTypesVector[i];
119 if (!mimeType || CFGetTypeID(mimeType) != CFStringGetTypeID() || CFStringGetLength(mimeType) == 0)
122 // If this MIME type doesn't have a valid info dictionary, ignore it.
123 CFDictionaryRef mimeTypeInfo = mimeTypeInfoVector[i];
124 if (!mimeTypeInfo || CFGetTypeID(mimeTypeInfo) != CFDictionaryGetTypeID())
127 // Get the MIME type description.
128 CFStringRef mimeTypeDescription = static_cast<CFStringRef>(CFDictionaryGetValue(mimeTypeInfo, CFSTR("WebPluginTypeDescription")));
129 if (mimeTypeDescription && CFGetTypeID(mimeTypeDescription) != CFStringGetTypeID())
130 mimeTypeDescription = 0;
132 mimeClassInfo.type = String(mimeType).lower();
133 mimeClassInfo.desc = mimeTypeDescription;
135 // Now get the extensions for this MIME type.
136 CFIndex numExtensions = 0;
137 CFArrayRef extensionsArray = static_cast<CFArrayRef>(CFDictionaryGetValue(mimeTypeInfo, CFSTR("WebPluginExtensions")));
138 if (extensionsArray && CFGetTypeID(extensionsArray) == CFArrayGetTypeID())
139 numExtensions = CFArrayGetCount(extensionsArray);
141 for (CFIndex i = 0; i < numExtensions; ++i) {
142 CFStringRef extension = static_cast<CFStringRef>(CFArrayGetValueAtIndex(extensionsArray, i));
143 if (!extension || CFGetTypeID(extension) != CFStringGetTypeID())
146 mimeClassInfo.extensions.append(String(extension).lower());
149 // Add this MIME type.
150 pluginInfo.mimes.append(mimeClassInfo);
158 explicit ResourceMap(CFBundleRef bundle)
160 , m_currentResourceFile(CurResFile())
161 , m_bundleResourceMap(CFBundleOpenBundleResourceMap(m_bundle))
163 UseResFile(m_bundleResourceMap);
168 // Close the resource map.
169 CFBundleCloseBundleResourceMap(m_bundle, m_bundleResourceMap);
171 // And restore the old resource.
172 UseResFile(m_currentResourceFile);
175 bool isValid() const { return m_bundleResourceMap != -1; }
178 CFBundleRef m_bundle;
179 ResFileRefNum m_currentResourceFile;
180 ResFileRefNum m_bundleResourceMap;
183 static bool getStringListResource(ResID resourceID, Vector<String>& stringList) {
184 Handle stringListHandle = Get1Resource('STR#', resourceID);
185 if (!stringListHandle || !*stringListHandle)
188 // Get the string list size.
189 Size stringListSize = GetHandleSize(stringListHandle);
190 if (stringListSize < static_cast<Size>(sizeof(UInt16)))
193 CFStringEncoding stringEncoding = stringEncodingForResource(stringListHandle);
195 unsigned char* ptr = reinterpret_cast<unsigned char*>(*stringListHandle);
196 unsigned char* end = ptr + stringListSize;
198 // Get the number of strings in the string list.
199 UInt16 numStrings = *reinterpret_cast<UInt16*>(ptr);
200 ptr += sizeof(UInt16);
202 for (UInt16 i = 0; i < numStrings; ++i) {
203 // We're past the end of the string, bail.
207 // Get the string length.
208 unsigned char stringLength = *ptr++;
210 RetainPtr<CFStringRef> cfString(AdoptCF, CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, ptr, stringLength, stringEncoding, false, kCFAllocatorNull));
214 stringList.append(cfString.get());
224 static const ResID PluginNameOrDescriptionStringNumber = 126;
225 static const ResID MIMEDescriptionStringNumber = 127;
226 static const ResID MIMEListStringStringNumber = 128;
228 static bool getPluginInfoFromCarbonResources(CFBundleRef bundle, PluginInfo& pluginInfo)
230 ResourceMap resourceMap(bundle);
231 if (!resourceMap.isValid())
234 // Get the description and name string list.
235 Vector<String> descriptionAndName;
236 if (!getStringListResource(PluginNameOrDescriptionStringNumber, descriptionAndName))
239 // Get the MIME types and extensions string list. This list needs to be a multiple of two.
240 Vector<String> mimeTypesAndExtensions;
241 if (!getStringListResource(MIMEListStringStringNumber, mimeTypesAndExtensions))
244 if (mimeTypesAndExtensions.size() % 2)
247 size_t numMimeTypes = mimeTypesAndExtensions.size() / 2;
249 // Now get the MIME type descriptions string list. This string list needs to be the same length as the number of MIME types.
250 Vector<String> mimeTypeDescriptions;
251 if (!getStringListResource(MIMEDescriptionStringNumber, mimeTypeDescriptions))
254 if (mimeTypeDescriptions.size() != numMimeTypes)
257 // Add all MIME types.
258 for (size_t i = 0; i < mimeTypesAndExtensions.size() / 2; ++i) {
259 MimeClassInfo mimeClassInfo;
261 const String& mimeType = mimeTypesAndExtensions[i * 2];
262 const String& description = mimeTypeDescriptions[i];
264 mimeClassInfo.type = mimeType.lower();
265 mimeClassInfo.desc = description;
267 Vector<String> extensions;
268 mimeTypesAndExtensions[i * 2 + 1].split(',', extensions);
270 for (size_t i = 0; i < extensions.size(); ++i)
271 mimeClassInfo.extensions.append(extensions[i].lower());
273 pluginInfo.mimes.append(mimeClassInfo);
276 // Set the description and name if they exist.
277 if (descriptionAndName.size() > 0)
278 pluginInfo.desc = descriptionAndName[0];
279 if (descriptionAndName.size() > 1)
280 pluginInfo.name = descriptionAndName[1];
285 bool NetscapePluginModule::getPluginInfo(const String& pluginPath, PluginInfoStore::Plugin& plugin)
287 RetainPtr<CFStringRef> bundlePath(AdoptCF, pluginPath.createCFString());
288 RetainPtr<CFURLRef> bundleURL(AdoptCF, CFURLCreateWithFileSystemPath(kCFAllocatorDefault, bundlePath.get(), kCFURLPOSIXPathStyle, false));
290 // Try to initialize the bundle.
291 RetainPtr<CFBundleRef> bundle(AdoptCF, CFBundleCreate(kCFAllocatorDefault, bundleURL.get()));
295 // Check if this bundle is an NPAPI plug-in.
296 UInt32 packageType = 0;
297 CFBundleGetPackageInfo(bundle.get(), &packageType, 0);
298 if (packageType != FOUR_CHAR_CODE('BRPL'))
301 // Check that the architecture is valid.
302 cpu_type_t pluginArchitecture = 0;
303 if (!getPluginArchitecture(bundle.get(), pluginArchitecture))
306 // Check that there's valid info for this plug-in.
307 if (!getPluginInfoFromPropertyLists(bundle.get(), plugin.info) &&
308 !getPluginInfoFromCarbonResources(bundle.get(), plugin.info))
311 plugin.path = pluginPath;
312 plugin.pluginArchitecture = pluginArchitecture;
313 plugin.bundleIdentifier = CFBundleGetIdentifier(bundle.get());
314 plugin.versionNumber = CFBundleGetVersionNumber(bundle.get());
316 RetainPtr<CFStringRef> filename(AdoptCF, CFURLCopyLastPathComponent(bundleURL.get()));
317 plugin.info.file = filename.get();
319 if (plugin.info.name.isNull())
320 plugin.info.name = plugin.info.file;
321 if (plugin.info.desc.isNull())
322 plugin.info.desc = plugin.info.file;
327 void NetscapePluginModule::determineQuirks()
329 PluginInfoStore::Plugin plugin;
330 if (!getPluginInfo(m_pluginPath, plugin))
333 if (plugin.bundleIdentifier == "com.macromedia.Flash Player.plugin") {
334 // Flash requires that the return value of getprogname() be "WebKitPluginHost".
335 m_pluginQuirks.add(PluginQuirks::PrognameShouldBeWebKitPluginHost);
339 } // namespace WebKit