OSDN Git Service

Merge changes I78ff6a85,Ic85c6405,Ibf903baa,I3a0459db,I35140385,I54790419,I6bfe5d24...
[android-x86/external-webkit.git] / Source / WebKit2 / Shared / Plugins / Netscape / mac / NetscapePluginModuleMac.mm
1 /*
2  * Copyright (C) 2010 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. 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.
24  */
25
26 #import "config.h"
27 #import "NetscapePluginModule.h"
28
29 #import <WebCore/WebCoreNSStringExtras.h>
30 #import <wtf/HashSet.h>
31
32 using namespace WebCore;
33
34 namespace WebKit {
35
36 static bool getPluginArchitecture(CFBundleRef bundle, cpu_type_t& pluginArchitecture)
37 {
38     RetainPtr<CFArrayRef> pluginArchitecturesArray(AdoptCF, CFBundleCopyExecutableArchitectures(bundle));
39     if (!pluginArchitecturesArray)
40         return false;
41
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));
46         
47         SInt32 architecture;
48         if (!CFNumberGetValue(number, kCFNumberSInt32Type, &architecture))
49             continue;
50         architectures.add(architecture);
51     }
52     
53 #ifdef __x86_64__
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;
57         return true;
58     }
59
60     // We also support 32-bit Intel plug-ins on 64-bit Intel.
61     if (architectures.contains(kCFBundleExecutableArchitectureI386)) {
62         pluginArchitecture = CPU_TYPE_X86;
63         return true;
64     }
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;
69         return true;
70     }
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;
75         return true;
76     }
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;
81         return true;
82     }
83 #else
84 #error "Unhandled architecture"
85 #endif
86
87     return false;
88 }
89
90 static bool getPluginInfoFromPropertyLists(CFBundleRef bundle, PluginInfo& pluginInfo)
91 {
92     // FIXME: Handle WebPluginMIMETypesFilenameKey.
93     
94     CFDictionaryRef mimeTypes = static_cast<CFDictionaryRef>(CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("WebPluginMIMETypes")));
95     if (!mimeTypes || CFGetTypeID(mimeTypes) != CFDictionaryGetTypeID())
96         return false;
97
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;
102     
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;
107     
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()));
113     
114     for (CFIndex i = 0; i < numMimeTypes; ++i) {
115         MimeClassInfo mimeClassInfo;
116         
117         // If this MIME type is invalid, ignore it.
118         CFStringRef mimeType = mimeTypesVector[i];
119         if (!mimeType || CFGetTypeID(mimeType) != CFStringGetTypeID() || CFStringGetLength(mimeType) == 0)
120             continue;
121
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())
125             continue;
126
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;
131
132         mimeClassInfo.type = String(mimeType).lower();
133         mimeClassInfo.desc = mimeTypeDescription;
134
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);
140
141         for (CFIndex i = 0; i < numExtensions; ++i) {
142             CFStringRef extension = static_cast<CFStringRef>(CFArrayGetValueAtIndex(extensionsArray, i));
143             if (!extension || CFGetTypeID(extension) != CFStringGetTypeID())
144                 continue;
145             
146             mimeClassInfo.extensions.append(String(extension).lower());
147         }
148
149         // Add this MIME type.
150         pluginInfo.mimes.append(mimeClassInfo);
151     }
152
153     return true;    
154 }
155
156 class ResourceMap {
157 public:
158     explicit ResourceMap(CFBundleRef bundle)
159         : m_bundle(bundle)
160         , m_currentResourceFile(CurResFile())
161         , m_bundleResourceMap(CFBundleOpenBundleResourceMap(m_bundle))
162     {
163         UseResFile(m_bundleResourceMap);
164     }
165
166     ~ResourceMap()
167     {
168         // Close the resource map.
169         CFBundleCloseBundleResourceMap(m_bundle, m_bundleResourceMap);
170         
171         // And restore the old resource.
172         UseResFile(m_currentResourceFile);
173     }
174
175     bool isValid() const { return m_bundleResourceMap != -1; }
176
177 private:
178     CFBundleRef m_bundle;
179     ResFileRefNum m_currentResourceFile;
180     ResFileRefNum m_bundleResourceMap;
181 };
182
183 static bool getStringListResource(ResID resourceID, Vector<String>& stringList) {
184     Handle stringListHandle = Get1Resource('STR#', resourceID);
185     if (!stringListHandle || !*stringListHandle)
186         return false;
187
188     // Get the string list size.
189     Size stringListSize = GetHandleSize(stringListHandle);
190     if (stringListSize < static_cast<Size>(sizeof(UInt16)))
191         return false;
192
193     CFStringEncoding stringEncoding = stringEncodingForResource(stringListHandle);
194
195     unsigned char* ptr = reinterpret_cast<unsigned char*>(*stringListHandle);
196     unsigned char* end = ptr + stringListSize;
197     
198     // Get the number of strings in the string list.
199     UInt16 numStrings = *reinterpret_cast<UInt16*>(ptr);
200     ptr += sizeof(UInt16);
201
202     for (UInt16 i = 0; i < numStrings; ++i) {
203         // We're past the end of the string, bail.
204         if (ptr >= end)
205             return false;
206
207         // Get the string length.
208         unsigned char stringLength = *ptr++;
209
210         RetainPtr<CFStringRef> cfString(AdoptCF, CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, ptr, stringLength, stringEncoding, false, kCFAllocatorNull));
211         if (!cfString.get())
212             return false;
213
214         stringList.append(cfString.get());
215         ptr += stringLength;
216     }
217
218     if (ptr != end)
219         return false;
220
221     return true;
222 }
223
224 static const ResID PluginNameOrDescriptionStringNumber = 126;
225 static const ResID MIMEDescriptionStringNumber = 127;
226 static const ResID MIMEListStringStringNumber = 128;
227
228 static bool getPluginInfoFromCarbonResources(CFBundleRef bundle, PluginInfo& pluginInfo)
229 {
230     ResourceMap resourceMap(bundle);
231     if (!resourceMap.isValid())
232         return false;
233
234     // Get the description and name string list.
235     Vector<String> descriptionAndName;
236     if (!getStringListResource(PluginNameOrDescriptionStringNumber, descriptionAndName))
237         return false;
238
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))
242         return false;
243
244     if (mimeTypesAndExtensions.size() % 2)
245         return false;
246
247     size_t numMimeTypes = mimeTypesAndExtensions.size() / 2;
248     
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))
252         return false;
253
254     if (mimeTypeDescriptions.size() != numMimeTypes)
255         return false;
256
257     // Add all MIME types.
258     for (size_t i = 0; i < mimeTypesAndExtensions.size() / 2; ++i) {
259         MimeClassInfo mimeClassInfo;
260         
261         const String& mimeType = mimeTypesAndExtensions[i * 2];
262         const String& description = mimeTypeDescriptions[i];
263         
264         mimeClassInfo.type = mimeType.lower();
265         mimeClassInfo.desc = description;
266         
267         Vector<String> extensions;
268         mimeTypesAndExtensions[i * 2 + 1].split(',', extensions);
269         
270         for (size_t i = 0; i < extensions.size(); ++i)
271             mimeClassInfo.extensions.append(extensions[i].lower());
272
273         pluginInfo.mimes.append(mimeClassInfo);
274     }
275
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];
281
282     return true;
283 }
284
285 bool NetscapePluginModule::getPluginInfo(const String& pluginPath, PluginInfoStore::Plugin& plugin)
286 {
287     RetainPtr<CFStringRef> bundlePath(AdoptCF, pluginPath.createCFString());
288     RetainPtr<CFURLRef> bundleURL(AdoptCF, CFURLCreateWithFileSystemPath(kCFAllocatorDefault, bundlePath.get(), kCFURLPOSIXPathStyle, false));
289     
290     // Try to initialize the bundle.
291     RetainPtr<CFBundleRef> bundle(AdoptCF, CFBundleCreate(kCFAllocatorDefault, bundleURL.get()));
292     if (!bundle)
293         return false;
294     
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'))
299         return false;
300     
301     // Check that the architecture is valid.
302     cpu_type_t pluginArchitecture = 0;
303     if (!getPluginArchitecture(bundle.get(), pluginArchitecture))
304         return false;
305     
306     // Check that there's valid info for this plug-in.
307     if (!getPluginInfoFromPropertyLists(bundle.get(), plugin.info) &&
308         !getPluginInfoFromCarbonResources(bundle.get(), plugin.info))
309         return false;
310     
311     plugin.path = pluginPath;
312     plugin.pluginArchitecture = pluginArchitecture;
313     plugin.bundleIdentifier = CFBundleGetIdentifier(bundle.get());
314     plugin.versionNumber = CFBundleGetVersionNumber(bundle.get());
315     
316     RetainPtr<CFStringRef> filename(AdoptCF, CFURLCopyLastPathComponent(bundleURL.get()));
317     plugin.info.file = filename.get();
318     
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;
323     
324     return true;
325 }
326
327 void NetscapePluginModule::determineQuirks()
328 {
329     PluginInfoStore::Plugin plugin;
330     if (!getPluginInfo(m_pluginPath, plugin))
331         return;
332
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);
336     }
337 }
338
339 } // namespace WebKit