1/* 2 * Copyright (C) 2005 Apple Computer, 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 * 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. 16 * 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. 27 */ 28 29#import <WebKit/WebKitErrors.h> 30 31#import <WebKit/WebKitErrorsPrivate.h> 32#import <WebKit/WebLocalizableStrings.h> 33#import <WebKit/WebNSURLExtras.h> 34 35#import <pthread.h> 36 37NSString *WebKitErrorDomain = @"WebKitErrorDomain"; 38 39NSString * const WebKitErrorMIMETypeKey = @"WebKitErrorMIMETypeKey"; 40NSString * const WebKitErrorPlugInNameKey = @"WebKitErrorPlugInNameKey"; 41NSString * const WebKitErrorPlugInPageURLStringKey = @"WebKitErrorPlugInPageURLStringKey"; 42 43// Policy errors 44#define WebKitErrorDescriptionCannotShowMIMEType UI_STRING("Content with specified MIME type can’t be shown", "WebKitErrorCannotShowMIMEType description") 45#define WebKitErrorDescriptionCannotShowURL UI_STRING("The URL can’t be shown", "WebKitErrorCannotShowURL description") 46#define WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange UI_STRING("Frame load interrupted", "WebKitErrorFrameLoadInterruptedByPolicyChange description") 47#define WebKitErrorDescriptionCannotUseRestrictedPort UI_STRING("Not allowed to use restricted network port", "WebKitErrorCannotUseRestrictedPort description") 48 49// Plug-in and java errors 50#define WebKitErrorDescriptionCannotFindPlugin UI_STRING("The plug-in can’t be found", "WebKitErrorCannotFindPlugin description") 51#define WebKitErrorDescriptionCannotLoadPlugin UI_STRING("The plug-in can’t be loaded", "WebKitErrorCannotLoadPlugin description") 52#define WebKitErrorDescriptionJavaUnavailable UI_STRING("Java is unavailable", "WebKitErrorJavaUnavailable description") 53#define WebKitErrorDescriptionPlugInCancelledConnection UI_STRING("Plug-in cancelled", "WebKitErrorPlugInCancelledConnection description") 54#define WebKitErrorDescriptionPlugInWillHandleLoad UI_STRING("Plug-in handled load", "WebKitErrorPlugInWillHandleLoad description") 55 56static pthread_once_t registerErrorsControl = PTHREAD_ONCE_INIT; 57static void registerErrors(void); 58 59@implementation NSError (WebKitExtras) 60 61static NSMutableDictionary *descriptions = nil; 62 63+ (void)_registerWebKitErrors 64{ 65 pthread_once(®isterErrorsControl, registerErrors); 66} 67 68-(id)_webkit_initWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL 69{ 70 NSDictionary *descriptionsDict; 71 NSString *localizedDesc; 72 NSDictionary *dict; 73 // insert a localized string here for those folks not savvy to our category methods 74 descriptionsDict = [descriptions objectForKey:domain]; 75 localizedDesc = descriptionsDict ? [descriptionsDict objectForKey:[NSNumber numberWithInt:code]] : nil; 76 dict = [NSDictionary dictionaryWithObjectsAndKeys: 77 URL, @"NSErrorFailingURLKey", 78 [URL absoluteString], @"NSErrorFailingURLStringKey", 79 localizedDesc, NSLocalizedDescriptionKey, 80 nil]; 81 return [self initWithDomain:domain code:code userInfo:dict]; 82} 83 84+(id)_webkit_errorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL 85{ 86 return [[[self alloc] _webkit_initWithDomain:domain code:code URL:URL] autorelease]; 87} 88 89+ (NSError *)_webKitErrorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL 90{ 91 [self _registerWebKitErrors]; 92 return [self _webkit_errorWithDomain:domain code:code URL:URL]; 93} 94 95+ (NSError *)_webKitErrorWithCode:(int)code failingURL:(NSString *)URLString 96{ 97 return [self _webKitErrorWithDomain:WebKitErrorDomain code:code URL:[NSURL _web_URLWithUserTypedString:URLString]]; 98} 99 100- (id)_initWithPluginErrorCode:(int)code 101 contentURL:(NSURL *)contentURL 102 pluginPageURL:(NSURL *)pluginPageURL 103 pluginName:(NSString *)pluginName 104 MIMEType:(NSString *)MIMEType 105{ 106 [[self class] _registerWebKitErrors]; 107 108 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; 109 NSDictionary *descriptionsForWebKitErrorDomain = [descriptions objectForKey:WebKitErrorDomain]; 110 NSString *localizedDescription = [descriptionsForWebKitErrorDomain objectForKey:[NSNumber numberWithInt:code]]; 111 if (localizedDescription) 112 [userInfo setObject:localizedDescription forKey:NSLocalizedDescriptionKey]; 113 if (contentURL) { 114 [userInfo setObject:contentURL forKey:@"NSErrorFailingURLKey"]; 115#if defined(BUILDING_ON_TIGER) || defined(BUILDING_ON_LEOPARD) 116 [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSErrorFailingURLStringKey]; 117#else 118 [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSURLErrorFailingURLStringErrorKey]; 119#endif 120 } 121 if (pluginPageURL) { 122 [userInfo setObject:[pluginPageURL _web_userVisibleString] forKey:WebKitErrorPlugInPageURLStringKey]; 123 } 124 if (pluginName) { 125 [userInfo setObject:pluginName forKey:WebKitErrorPlugInNameKey]; 126 } 127 if (MIMEType) { 128 [userInfo setObject:MIMEType forKey:WebKitErrorMIMETypeKey]; 129 } 130 131 NSDictionary *userInfoCopy = [userInfo count] > 0 ? [[NSDictionary alloc] initWithDictionary:userInfo] : nil; 132 [userInfo release]; 133 NSError *error = [self initWithDomain:WebKitErrorDomain code:code userInfo:userInfoCopy]; 134 [userInfoCopy release]; 135 136 return error; 137} 138 139+ (void)_webkit_addErrorsWithCodesAndDescriptions:(NSDictionary *)dictionary inDomain:(NSString *)domain 140{ 141 if (!descriptions) 142 descriptions = [[NSMutableDictionary alloc] init]; 143 144 [descriptions setObject:dictionary forKey:domain]; 145} 146 147static void registerErrors() 148{ 149 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 150 151 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: 152 // Policy errors 153 WebKitErrorDescriptionCannotShowMIMEType, [NSNumber numberWithInt: WebKitErrorCannotShowMIMEType], 154 WebKitErrorDescriptionCannotShowURL, [NSNumber numberWithInt: WebKitErrorCannotShowURL], 155 WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange, [NSNumber numberWithInt: WebKitErrorFrameLoadInterruptedByPolicyChange], 156 WebKitErrorDescriptionCannotUseRestrictedPort, [NSNumber numberWithInt: WebKitErrorCannotUseRestrictedPort], 157 158 // Plug-in and java errors 159 WebKitErrorDescriptionCannotFindPlugin, [NSNumber numberWithInt: WebKitErrorCannotFindPlugIn], 160 WebKitErrorDescriptionCannotLoadPlugin, [NSNumber numberWithInt: WebKitErrorCannotLoadPlugIn], 161 WebKitErrorDescriptionJavaUnavailable, [NSNumber numberWithInt: WebKitErrorJavaUnavailable], 162 WebKitErrorDescriptionPlugInCancelledConnection, [NSNumber numberWithInt: WebKitErrorPlugInCancelledConnection], 163 WebKitErrorDescriptionPlugInWillHandleLoad, [NSNumber numberWithInt: WebKitErrorPlugInWillHandleLoad], 164 nil]; 165 166 [NSError _webkit_addErrorsWithCodesAndDescriptions:dict inDomain:WebKitErrorDomain]; 167 168 [pool drain]; 169} 170 171@end 172