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];
106 // 32-bit PowerPC is the only platform where non-bundled CFM plugins are supported
112 cfBundle = CFBundleCreate(NULL, (CFURLRef)[NSURL fileURLWithPath:path]);
113 extensionToMIME = [[NSMutableDictionary alloc] init];
118 - (BOOL)getPluginInfoFromBundleAndMIMEDictionary:(NSDictionary *)MIMETypes
124 MIMETypes = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesKey];
129 NSMutableDictionary *MIMEToExtensionsDictionary = [NSMutableDictionary dictionary];
130 NSMutableDictionary *MIMEToDescriptionDictionary = [NSMutableDictionary dictionary];
131 NSEnumerator *keyEnumerator = [MIMETypes keyEnumerator];
132 NSDictionary *MIMEDictionary;
133 NSString *MIME, *description;
136 while ((MIME = [keyEnumerator nextObject]) != nil) {
137 MIMEDictionary = [MIMETypes objectForKey:MIME];
139 // FIXME: Consider storing disabled MIME types.
140 NSNumber *isEnabled = [MIMEDictionary objectForKey:WebPluginTypeEnabledKey];
141 if (isEnabled && [isEnabled boolValue] == NO)
144 extensions = [[MIMEDictionary objectForKey:WebPluginExtensionsKey] _web_lowercaseStrings];
145 if ([extensions count] == 0)
146 extensions = [NSArray arrayWithObject:@""];
148 MIME = [MIME lowercaseString];
150 [MIMEToExtensionsDictionary setObject:extensions forKey:MIME];
152 description = [MIMEDictionary objectForKey:WebPluginTypeDescriptionKey];
156 [MIMEToDescriptionDictionary setObject:description forKey:MIME];
159 [self setMIMEToExtensionsDictionary:MIMEToExtensionsDictionary];
160 [self setMIMEToDescriptionDictionary:MIMEToDescriptionDictionary];
162 NSString *filename = [self filename];
164 NSString *theName = [bundle objectForInfoDictionaryKey:WebPluginNameKey];
167 [self setName:theName];
169 description = [bundle objectForInfoDictionaryKey:WebPluginDescriptionKey];
171 description = filename;
172 [self setPluginDescription:description];
177 - (NSDictionary *)pListForPath:(NSString *)pListPath createFile:(BOOL)createFile
179 if (createFile && [self load] && BP_CreatePluginMIMETypesPreferences)
180 BP_CreatePluginMIMETypesPreferences();
182 NSDictionary *pList = nil;
183 NSData *data = [NSData dataWithContentsOfFile:pListPath];
185 pList = [NSPropertyListSerialization propertyListFromData:data
186 mutabilityOption:NSPropertyListImmutable
188 errorDescription:nil];
194 - (BOOL)getPluginInfoFromPLists
199 NSDictionary *MIMETypes = nil;
200 NSString *pListFilename = [bundle objectForInfoDictionaryKey:WebPluginMIMETypesFilenameKey];
202 // Check if the MIME types are claimed in a plist in the user's preferences directory.
204 NSString *pListPath = [NSString stringWithFormat:@"%@/Library/Preferences/%@", NSHomeDirectory(), pListFilename];
205 NSDictionary *pList = [self pListForPath:pListPath createFile:NO];
207 // If the plist isn't localized, have the plug-in recreate it in the preferred language.
208 NSString *localizationName = [pList objectForKey:WebPluginLocalizationNameKey];
209 if (![localizationName isEqualToString:[[self class] preferredLocalizationName]])
210 pList = [self pListForPath:pListPath createFile:YES];
211 MIMETypes = [pList objectForKey:WebPluginMIMETypesKey];
213 // Plist doesn't exist, ask the plug-in to create it.
214 MIMETypes = [[self pListForPath:pListPath createFile:YES] objectForKey:WebPluginMIMETypesKey];
217 // Pass the MIME dictionary to the superclass to parse it.
218 return [self getPluginInfoFromBundleAndMIMEDictionary:MIMETypes];
223 if (bundle && !BP_CreatePluginMIMETypesPreferences)
224 BP_CreatePluginMIMETypesPreferences = (BP_CreatePluginMIMETypesPreferencesFuncPtr)CFBundleGetFunctionPointerForName(cfBundle, CFSTR("BP_CreatePluginMIMETypesPreferences"));
231 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
232 [pluginDatabases release];
236 [pluginDescription release];
238 [MIMEToDescription release];
239 [MIMEToExtensions release];
240 [extensionToMIME release];
251 ASSERT(!pluginDatabases || [pluginDatabases count] == 0);
252 [pluginDatabases release];
270 - (NSString *)filename
272 return [path lastPathComponent];
275 - (NSString *)pluginDescription
277 return pluginDescription;
280 - (NSEnumerator *)extensionEnumerator
282 return [extensionToMIME keyEnumerator];
285 - (NSEnumerator *)MIMETypeEnumerator
287 return [MIMEToExtensions keyEnumerator];
290 - (NSString *)descriptionForMIMEType:(NSString *)MIMEType
292 return [MIMEToDescription objectForKey:MIMEType];
295 - (NSString *)MIMETypeForExtension:(NSString *)extension
297 return [extensionToMIME objectForKey:extension];
300 - (NSArray *)extensionsForMIMEType:(NSString *)MIMEType
302 return [MIMEToExtensions objectForKey:MIMEType];
310 - (void)setName:(NSString *)theName
313 name = [theName retain];
316 - (void)setPath:(NSString *)thePath
319 path = [thePath retain];
322 - (void)setPluginDescription:(NSString *)description
324 [pluginDescription release];
325 pluginDescription = [description retain];
328 - (void)setMIMEToDescriptionDictionary:(NSDictionary *)MIMEToDescriptionDictionary
330 [MIMEToDescription release];
331 MIMEToDescription = [MIMEToDescriptionDictionary retain];
334 - (void)setMIMEToExtensionsDictionary:(NSDictionary *)MIMEToExtensionsDictionary
336 [MIMEToExtensions release];
337 MIMEToExtensions = [MIMEToExtensionsDictionary retain];
339 // Reverse the mapping
340 [extensionToMIME removeAllObjects];
342 NSEnumerator *MIMEEnumerator = [MIMEToExtensions keyEnumerator], *extensionEnumerator;
343 NSString *MIME, *extension;
346 while ((MIME = [MIMEEnumerator nextObject]) != nil) {
347 extensions = [MIMEToExtensions objectForKey:MIME];
348 extensionEnumerator = [extensions objectEnumerator];
350 while ((extension = [extensionEnumerator nextObject]) != nil) {
351 if (![extension isEqualToString:@""])
352 [extensionToMIME setObject:MIME forKey:extension];
357 - (NSString *)description
359 return [NSString stringWithFormat:@"name: %@\npath: %@\nmimeTypes:\n%@\npluginDescription:%@",
360 name, path, [MIMEToExtensions description], [MIMEToDescription description], pluginDescription];
363 - (BOOL)isQuickTimePlugIn
365 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
366 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCarbonPluginIdentifier] ||
367 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:QuickTimeCocoaPluginIdentifier];
372 NSString *bundleIdentifier = [[self bundle] bundleIdentifier];
373 return [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCocoaPluginIdentifier] ||
374 [bundleIdentifier _webkit_isCaseInsensitiveEqualToString:JavaCarbonPluginIdentifier] ||
375 [[path lastPathComponent] _webkit_isCaseInsensitiveEqualToString:JavaCFMPluginFilename];
378 - (BOOL)isNativeLibraryData:(NSData *)data
380 // If we have a 32-bit thin Mach-O file, see if we have an i386 binary. If not, don't load it.
381 // This is designed to be the safest possible test for now. We'll only reject files that we
382 // can easily tell are wrong.
383 if ([data length] >= sizeof(struct mach_header)) {
384 const NXArchInfo *localArch = NXGetLocalArchInfo();
385 if (localArch != NULL) {
386 struct mach_header *header = (struct mach_header *)[data bytes];
387 if (header->magic == MH_MAGIC)
388 return (header->cputype == localArch->cputype);
389 if (header->magic == MH_CIGAM)
390 return ((cpu_type_t) OSSwapInt32(header->cputype) == localArch->cputype);
396 - (void)wasAddedToPluginDatabase:(WebPluginDatabase *)database
398 if (!pluginDatabases)
399 pluginDatabases = [[NSMutableSet alloc] init];
401 ASSERT(![pluginDatabases containsObject:database]);
402 [pluginDatabases addObject:database];
405 - (void)wasRemovedFromPluginDatabase:(WebPluginDatabase *)database
407 ASSERT(pluginDatabases);
408 ASSERT([pluginDatabases containsObject:database]);
410 [pluginDatabases removeObject:database];
415 @implementation NSArray (WebPluginExtensions)
417 - (NSArray *)_web_lowercaseStrings
419 NSMutableArray *lowercaseStrings = [NSMutableArray arrayWithCapacity:[self count]];
420 NSEnumerator *strings = [self objectEnumerator];
423 while ((string = [strings nextObject]) != nil) {
424 if ([string isKindOfClass:[NSString class]])
425 [lowercaseStrings addObject:[string lowercaseString]];
428 return lowercaseStrings;