2 * Copyright (C) 2005 Apple Computer, 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
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 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import <WebKit/WebBasePluginPackage.h>
31 #import <JavaScriptCore/Assertions.h>
32 #import <WebKit/WebKitNSStringExtras.h>
33 #import <WebKit/WebNetscapePluginPackage.h>
34 #import <WebKit/WebNSObjectExtras.h>
35 #import <WebKit/WebPluginPackage.h>
36 #import <WebCore/WebCoreObjCExtras.h>
38 #import <WebKitSystemInterface.h>
40 #import "WebKitLogging.h"
41 #import "WebTypesInternal.h"
43 #import <mach-o/arch.h>
44 #import <mach-o/loader.h>
46 #define JavaCocoaPluginIdentifier @"com.apple.JavaPluginCocoa"
47 #define JavaCarbonPluginIdentifier @"com.apple.JavaAppletPlugin"
48 #define JavaCFMPluginFilename @"Java Applet Plugin Enabler"
50 #define QuickTimeCarbonPluginIdentifier @"com.apple.QuickTime Plugin.plugin"
51 #define QuickTimeCocoaPluginIdentifier @"com.apple.quicktime.webplugin"
53 @interface NSArray (WebPluginExtensions)
54 - (NSArray *)_web_lowercaseStrings;
57 @implementation WebBasePluginPackage
59 #ifndef BUILDING_ON_TIGER
62 WebCoreObjCFinalizeOnMainThread(self);
66 + (WebBasePluginPackage *)pluginWithPath:(NSString *)pluginPath
69 WebBasePluginPackage *pluginPackage = [[WebPluginPackage alloc] initWithPath:pluginPath];
75 pluginPackage = [[WebNetscapePluginPackage alloc] initWithPath:pluginPath];
79 return [pluginPackage autorelease];
82 + (NSString *)preferredLocalizationName
84 return WebCFAutorelease(WKCopyCFLocalizationPreferredName(NULL));
87 - (NSString *)pathByResolvingSymlinksAndAliasesInPath:(NSString *)thePath
89 NSString *newPath = [thePath stringByResolvingSymlinksInPath];
94 err = FSPathMakeRef((const UInt8 *)[thePath fileSystemRepresentation], &fref, NULL);
98 Boolean targetIsFolder;
100 err = FSResolveAliasFileWithMountFlags(&fref, TRUE, &targetIsFolder, &wasAliased, kResolveAliasFileNoUI);
105 CFURLRef URL = CFURLCreateFromFSRef(kCFAllocatorDefault, &fref);
106 newPath = [(NSURL *)URL path];
113 - (id)initWithPath:(NSString *)pluginPath
115 if (!(self = [super init]))
118 path = [[self pathByResolvingSymlinksAndAliasesInPath:pluginPath] retain];
119 bundle = [[NSBundle alloc] initWithPath:path];
121 // 32-bit PowerPC is the only platform where non-bundled CFM plugins are supported
127 cfBundle = CFBundleCreate(NULL, (CFURLRef)[NSURL fileURLWithPath:path]);
128 extensionToMIME = [[NSMutableDictionary alloc] init];
133 - (BOOL)getPluginInfoFromBundleAndMIMEDictionary:(NSDictionary *)MIMETypes
139 MIMETypes = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesKey];
144 NSMutableDictionary *MIMEToExtensionsDictionary = [NSMutableDictionary dictionary];
145 NSMutableDictionary *MIMEToDescriptionDictionary = [NSMutableDictionary dictionary];
146 NSEnumerator *keyEnumerator = [MIMETypes keyEnumerator];
147 NSDictionary *MIMEDictionary;
148 NSString *MIME, *description;
151 while ((MIME = [keyEnumerator nextObject]) != nil) {
152 MIMEDictionary = [MIMETypes objectForKey:MIME];
154 // FIXME: Consider storing disabled MIME types.
155 NSNumber *isEnabled = [MIMEDictionary objectForKey:WebPluginTypeEnabledKey];
156 if (isEnabled && [isEnabled boolValue] == NO)
159 extensions = [[MIMEDictionary objectForKey:WebPluginExtensionsKey] _web_lowercaseStrings];
160 if ([extensions count] == 0)
161 extensions = [NSArray arrayWithObject:@""];
163 MIME = [MIME lowercaseString];
165 [MIMEToExtensionsDictionary setObject:extensions forKey:MIME];
167 description = [MIMEDictionary objectForKey:WebPluginTypeDescriptionKey];
171 [MIMEToDescriptionDictionary setObject:description forKey:MIME];
174 [self setMIMEToExtensionsDictionary:MIMEToExtensionsDictionary];
175 [self setMIMEToDescriptionDictionary:MIMEToDescriptionDictionary];
177 NSString *filename = [self filename];
179 NSString *theName = [bundle objectForInfoDictionaryKey:WebPluginNameKey];
182 [self setName:theName];
184 description = [bundle objectForInfoDictionaryKey:WebPluginDescriptionKey];
186 description = filename;
187 [self setPluginDescription:description];
192 - (NSDictionary *)pListForPath:(NSString *)pListPath createFile:(BOOL)createFile
194 if (createFile && [self load] && BP_CreatePluginMIMETypesPreferences)
195 BP_CreatePluginMIMETypesPreferences();
197 NSDictionary *pList = nil;
198 NSData *data = [NSData dataWithContentsOfFile:pListPath];
200 pList = [NSPropertyListSerialization propertyListFromData:data
201 mutabilityOption:NSPropertyListImmutable
203 errorDescription:nil];
209 - (BOOL)getPluginInfoFromPLists
214 NSDictionary *MIMETypes = nil;
215 NSString *pListFilename = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesFilenameKey];
217 // Check if the MIME types are claimed in a plist in the user's preferences directory.
219 NSString *pListPath = [NSString stringWithFormat:@"%@/Library/Preferences/%@", NSHomeDirectory(), pListFilename];
220 NSDictionary *pList = [self pListForPath:pListPath createFile:NO];
222 // If the plist isn't localized, have the plug-in recreate it in the preferred language.
223 NSString *localizationName = [pList objectForKey:WebPluginLocalizationNameKey];
224 if (![localizationName isEqualToString:[[self class] preferredLocalizationName]])
225 pList = [self pListForPath:pListPath createFile:YES];
226 MIMETypes = [pList objectForKey:WebPluginMIMETypesKey];
228 // Plist doesn't exist, ask the plug-in to create it.
229 MIMETypes = [[self pListForPath:pListPath createFile:YES] objectForKey:WebPluginMIMETypesKey];
232 // Pass the MIME dictionary to the superclass to parse it.
233 return [self getPluginInfoFromBundleAndMIMEDictionary:MIMETypes];
238 if (bundle && !BP_CreatePluginMIMETypesPreferences)
239 BP_CreatePluginMIMETypesPreferences = (BP_CreatePluginMIMETypesPreferencesFuncPtr)CFBundleGetFunctionPointerForName(cfBundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
246 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
247 [pluginDatabases release];
251 [pluginDescription release];
253 [MIMEToDescription release];
254 [MIMEToExtensions release];
255 [extensionToMIME release];
266 ASSERT_MAIN_THREAD();
267 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
268 [pluginDatabases release];
286 - (NSString *)filename
288 return [path lastPathComponent];
291 - (NSString *)pluginDescription
293 return pluginDescription;
296 - (NSEnumerator *)extensionEnumerator
298 return [extensionToMIME keyEnumerator];
301 - (NSEnumerator *)MIMETypeEnumerator
303 return [MIMEToExtensions keyEnumerator];
306 - (NSString *)descriptionForMIMEType:(NSString *)MIMEType
308 return [MIMEToDescription objectForKey:MIMEType];
311 - (NSString *)MIMETypeForExtension:(NSString *)extension
313 return [extensionToMIME objectForKey:extension];
316 - (NSArray *)extensionsForMIMEType:(NSString *)MIMEType
318 return [MIMEToExtensions objectForKey:MIMEType];
326 - (void)setName:(NSString *)theName
329 name = [theName retain];
332 - (void)setPath:(NSString *)thePath
335 path = [thePath retain];
338 - (void)setPluginDescription:(NSString *)description
340 [pluginDescription release];
341 pluginDescription = [description retain];
344 - (void)setMIMEToDescriptionDictionary:(NSDictionary *)MIMEToDescriptionDictionary
346 [MIMEToDescription release];
347 MIMEToDescription = [MIMEToDescriptionDictionary retain];
350 - (void)setMIMEToExtensionsDictionary:(NSDictionary *)MIMEToExtensionsDictionary
352 [MIMEToExtensions release];
353 MIMEToExtensions = [MIMEToExtensionsDictionary retain];
355 // Reverse the mapping
356 [extensionToMIME removeAllObjects];
358 NSEnumerator *MIMEEnumerator = [MIMEToExtensions keyEnumerator], *extensionEnumerator;
359 NSString *MIME, *extension;
362 while ((MIME = [MIMEEnumerator nextObject]) != nil) {
363 extensions = [MIMEToExtensions objectForKey:MIME];
364 extensionEnumerator = [extensions objectEnumerator];
366 while ((extension = [extensionEnumerator nextObject]) != nil) {
367 if (![extension isEqualToString:@""])
368 [extensionToMIME setObject:MIME forKey:extension];
373 - (NSString *)description
375 return [NSString stringWithFormat:@"name: %@\npath: %@\nmimeTypes:\n%@\npluginDescription:%@",
376 name, path, [MIMEToExtensions description], [MIMEToDescription description], pluginDescription];
379 - (BOOL)isQuickTimePlugIn
381 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
382 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCarbonPluginIdentifier] ||
383 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCocoaPluginIdentifier];
388 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
389 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCocoaPluginIdentifier] ||
390 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCarbonPluginIdentifier] ||
391 [[path lastPathComponent] _webkit_isCaseInsensitiveEqualToString:JavaCFMPluginFilename];
394 - (BOOL)isNativeLibraryData:(NSData *)data
396 // If we have a 32-bit thin Mach-O file, see if we have an i386 binary. If not, don't load it.
397 // This is designed to be the safest possible test for now. We'll only reject files that we
398 // can easily tell are wrong.
399 if ([data length] >= sizeof(struct mach_header)) {
400 const NXArchInfo *localArch = NXGetLocalArchInfo();
401 if (localArch != NULL) {
402 struct mach_header *header = (struct mach_header *)[data bytes];
403 if (header->magic == MH_MAGIC)
404 return (header->cputype == localArch->cputype);
405 if (header->magic == MH_CIGAM)
406 return ((cpu_type_t) OSSwapInt32(header->cputype) == localArch->cputype);
412 - (UInt32)versionNumber
414 // CFBundleGetVersionNumber doesn't work with all possible versioning schemes, but we think for now it's good enough for us.
415 return CFBundleGetVersionNumber(cfBundle);
418 - (void)wasAddedToPluginDatabase:(WebPluginDatabase *)database
420 if (!pluginDatabases)
421 pluginDatabases = [[NSMutableSet alloc] init];
423 ASSERT(![pluginDatabases containsObject:database]);
424 [pluginDatabases addObject:database];
427 - (void)wasRemovedFromPluginDatabase:(WebPluginDatabase *)database
429 ASSERT(pluginDatabases);
430 ASSERT([pluginDatabases containsObject:database]);
432 [pluginDatabases removeObject:database];
437 @implementation NSArray (WebPluginExtensions)
439 - (NSArray *)_web_lowercaseStrings
441 NSMutableArray *lowercaseStrings = [NSMutableArray arrayWithCapacity:[self count]];
442 NSEnumerator *strings = [self objectEnumerator];
445 while ((string = [strings nextObject]) != nil) {
446 if ([string isKindOfClass:[NSString class]])
447 [lowercaseStrings addObject:[string lowercaseString]];
450 return lowercaseStrings;