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>
37 #import <WebKitSystemInterface.h>
39 #import "WebTypesInternal.h"
41 #import <mach-o/arch.h>
42 #import <mach-o/loader.h>
44 #define JavaCocoaPluginIdentifier @"com.apple.JavaPluginCocoa"
45 #define JavaCarbonPluginIdentifier @"com.apple.JavaAppletPlugin"
46 #define JavaCFMPluginFilename @"Java Applet Plugin Enabler"
48 #define QuickTimeCarbonPluginIdentifier @"com.apple.QuickTime Plugin.plugin"
49 #define QuickTimeCocoaPluginIdentifier @"com.apple.quicktime.webplugin"
51 @interface NSArray (WebPluginExtensions)
52 - (NSArray *)_web_lowercaseStrings;
55 @implementation WebBasePluginPackage
57 + (WebBasePluginPackage *)pluginWithPath:(NSString *)pluginPath
59 WebBasePluginPackage *pluginPackage = [[WebPluginPackage alloc] initWithPath:pluginPath];
62 pluginPackage = [[WebNetscapePluginPackage alloc] initWithPath:pluginPath];
64 return [pluginPackage autorelease];
67 + (NSString *)preferredLocalizationName
69 return WebCFAutorelease(WKCopyCFLocalizationPreferredName(NULL));
72 - (NSString *)pathByResolvingSymlinksAndAliasesInPath:(NSString *)thePath
74 NSString *newPath = [thePath stringByResolvingSymlinksInPath];
79 err = FSPathMakeRef((const UInt8 *)[thePath fileSystemRepresentation], &fref, NULL);
83 Boolean targetIsFolder;
85 err = FSResolveAliasFileWithMountFlags(&fref, TRUE, &targetIsFolder, &wasAliased, kResolveAliasFileNoUI);
90 CFURLRef URL = CFURLCreateFromFSRef(kCFAllocatorDefault, &fref);
91 newPath = [(NSURL *)URL path];
98 - (id)initWithPath:(NSString *)pluginPath
100 if (!(self = [super init]))
103 path = [[self pathByResolvingSymlinksAndAliasesInPath:pluginPath] retain];
104 bundle = [[NSBundle alloc] initWithPath:path];
109 cfBundle = CFBundleCreate(NULL, (CFURLRef)[NSURL fileURLWithPath:path]);
110 extensionToMIME = [[NSMutableDictionary alloc] init];
115 - (BOOL)getPluginInfoFromBundleAndMIMEDictionary:(NSDictionary *)MIMETypes
121 MIMETypes = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesKey];
126 NSMutableDictionary *MIMEToExtensionsDictionary = [NSMutableDictionary dictionary];
127 NSMutableDictionary *MIMEToDescriptionDictionary = [NSMutableDictionary dictionary];
128 NSEnumerator *keyEnumerator = [MIMETypes keyEnumerator];
129 NSDictionary *MIMEDictionary;
130 NSString *MIME, *description;
133 while ((MIME = [keyEnumerator nextObject]) != nil) {
134 MIMEDictionary = [MIMETypes objectForKey:MIME];
136 // FIXME: Consider storing disabled MIME types.
137 NSNumber *isEnabled = [MIMEDictionary objectForKey:WebPluginTypeEnabledKey];
138 if (isEnabled && [isEnabled boolValue] == NO)
141 extensions = [[MIMEDictionary objectForKey:WebPluginExtensionsKey] _web_lowercaseStrings];
142 if ([extensions count] == 0)
143 extensions = [NSArray arrayWithObject:@""];
145 MIME = [MIME lowercaseString];
147 [MIMEToExtensionsDictionary setObject:extensions forKey:MIME];
149 description = [MIMEDictionary objectForKey:WebPluginTypeDescriptionKey];
153 [MIMEToDescriptionDictionary setObject:description forKey:MIME];
156 [self setMIMEToExtensionsDictionary:MIMEToExtensionsDictionary];
157 [self setMIMEToDescriptionDictionary:MIMEToDescriptionDictionary];
159 NSString *filename = [self filename];
161 NSString *theName = [bundle objectForInfoDictionaryKey:WebPluginNameKey];
164 [self setName:theName];
166 description = [bundle objectForInfoDictionaryKey:WebPluginDescriptionKey];
168 description = filename;
169 [self setPluginDescription:description];
174 - (NSDictionary *)pListForPath:(NSString *)pListPath createFile:(BOOL)createFile
176 if (createFile && [self load] && BP_CreatePluginMIMETypesPreferences)
177 BP_CreatePluginMIMETypesPreferences();
179 NSDictionary *pList = nil;
180 NSData *data = [NSData dataWithContentsOfFile:pListPath];
182 pList = [NSPropertyListSerialization propertyListFromData:data
183 mutabilityOption:NSPropertyListImmutable
185 errorDescription:nil];
191 - (BOOL)getPluginInfoFromPLists
196 NSDictionary *MIMETypes = nil;
197 NSString *pListFilename = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesFilenameKey];
199 // Check if the MIME types are claimed in a plist in the user's preferences directory.
201 NSString *pListPath = [NSString stringWithFormat:@"%@/Library/Preferences/%@", NSHomeDirectory(), pListFilename];
202 NSDictionary *pList = [self pListForPath:pListPath createFile:NO];
204 // If the plist isn't localized, have the plug-in recreate it in the preferred language.
205 NSString *localizationName = [pList objectForKey:WebPluginLocalizationNameKey];
206 if (![localizationName isEqualToString:[[self class] preferredLocalizationName]])
207 pList = [self pListForPath:pListPath createFile:YES];
208 MIMETypes = [pList objectForKey:WebPluginMIMETypesKey];
210 // Plist doesn't exist, ask the plug-in to create it.
211 MIMETypes = [[self pListForPath:pListPath createFile:YES] objectForKey:WebPluginMIMETypesKey];
214 // Pass the MIME dictionary to the superclass to parse it.
215 return [self getPluginInfoFromBundleAndMIMEDictionary:MIMETypes];
220 if (bundle && !BP_CreatePluginMIMETypesPreferences)
221 BP_CreatePluginMIMETypesPreferences = (BP_CreatePluginMIMETypesPreferencesFuncPtr)CFBundleGetFunctionPointerForName(cfBundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
228 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
229 [pluginDatabases release];
233 [pluginDescription release];
235 [MIMEToDescription release];
236 [MIMEToExtensions release];
237 [extensionToMIME release];
248 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
249 [pluginDatabases release];
267 - (NSString *)filename
269 return [path lastPathComponent];
272 - (NSString *)pluginDescription
274 return pluginDescription;
277 - (NSEnumerator *)extensionEnumerator
279 return [extensionToMIME keyEnumerator];
282 - (NSEnumerator *)MIMETypeEnumerator
284 return [MIMEToExtensions keyEnumerator];
287 - (NSString *)descriptionForMIMEType:(NSString *)MIMEType
289 return [MIMEToDescription objectForKey:MIMEType];
292 - (NSString *)MIMETypeForExtension:(NSString *)extension
294 return [extensionToMIME objectForKey:extension];
297 - (NSArray *)extensionsForMIMEType:(NSString *)MIMEType
299 return [MIMEToExtensions objectForKey:MIMEType];
307 - (void)setName:(NSString *)theName
310 name = [theName retain];
313 - (void)setPath:(NSString *)thePath
316 path = [thePath retain];
319 - (void)setPluginDescription:(NSString *)description
321 [pluginDescription release];
322 pluginDescription = [description retain];
325 - (void)setMIMEToDescriptionDictionary:(NSDictionary *)MIMEToDescriptionDictionary
327 [MIMEToDescription release];
328 MIMEToDescription = [MIMEToDescriptionDictionary retain];
331 - (void)setMIMEToExtensionsDictionary:(NSDictionary *)MIMEToExtensionsDictionary
333 [MIMEToExtensions release];
334 MIMEToExtensions = [MIMEToExtensionsDictionary retain];
336 // Reverse the mapping
337 [extensionToMIME removeAllObjects];
339 NSEnumerator *MIMEEnumerator = [MIMEToExtensions keyEnumerator], *extensionEnumerator;
340 NSString *MIME, *extension;
343 while ((MIME = [MIMEEnumerator nextObject]) != nil) {
344 extensions = [MIMEToExtensions objectForKey:MIME];
345 extensionEnumerator = [extensions objectEnumerator];
347 while ((extension = [extensionEnumerator nextObject]) != nil) {
348 if (![extension isEqualToString:@""])
349 [extensionToMIME setObject:MIME forKey:extension];
354 - (NSString *)description
356 return [NSString stringWithFormat:@"name: %@\npath: %@\nmimeTypes:\n%@\npluginDescription:%@",
357 name, path, [MIMEToExtensions description], [MIMEToDescription description], pluginDescription];
360 - (BOOL)isQuickTimePlugIn
362 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
363 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCarbonPluginIdentifier] ||
364 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCocoaPluginIdentifier];
369 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
370 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCocoaPluginIdentifier] ||
371 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCarbonPluginIdentifier] ||
372 [[path lastPathComponent] _webkit_isCaseInsensitiveEqualToString:JavaCFMPluginFilename];
375 - (BOOL)isNativeLibraryData:(NSData *)data
377 // If we have a 32-bit thin Mach-O file, see if we have an i386 binary. If not, don't load it.
378 // This is designed to be the safest possible test for now. We'll only reject files that we
379 // can easily tell are wrong.
380 if ([data length] >= sizeof(struct mach_header)) {
381 const NXArchInfo *localArch = NXGetLocalArchInfo();
382 if (localArch != NULL) {
383 struct mach_header *header = (struct mach_header *)[data bytes];
384 if (header->magic == MH_MAGIC)
385 return (header->cputype == localArch->cputype);
386 if (header->magic == MH_CIGAM)
387 return ((cpu_type_t) OSSwapInt32(header->cputype) == localArch->cputype);
393 - (void)wasAddedToPluginDatabase:(WebPluginDatabase *)database
395 if (!pluginDatabases)
396 pluginDatabases = [[NSMutableSet alloc] init];
398 ASSERT(![pluginDatabases containsObject:database]);
399 [pluginDatabases addObject:database];
402 - (void)wasRemovedFromPluginDatabase:(WebPluginDatabase *)database
404 ASSERT(pluginDatabases);
405 ASSERT([pluginDatabases containsObject:database]);
407 [pluginDatabases removeObject:database];
412 @implementation NSArray (WebPluginExtensions)
414 - (NSArray *)_web_lowercaseStrings
416 NSMutableArray *lowercaseStrings = [NSMutableArray arrayWithCapacity:[self count]];
417 NSEnumerator *strings = [self objectEnumerator];
420 while ((string = [strings nextObject]) != nil) {
421 if ([string isKindOfClass:[NSString class]])
422 [lowercaseStrings addObject:[string lowercaseString]];
425 return lowercaseStrings;