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/WebKitErrors.h>
31 #import "WebLocalizableStringsInternal.h"
32 #import <WebKit/WebKitErrorsPrivate.h>
33 #import <WebKit/WebNSURLExtras.h>
37 NSString *WebKitErrorDomain = @"WebKitErrorDomain";
39 NSString * const WebKitErrorMIMETypeKey = @"WebKitErrorMIMETypeKey";
40 NSString * const WebKitErrorPlugInNameKey = @"WebKitErrorPlugInNameKey";
41 NSString * const WebKitErrorPlugInPageURLStringKey = @"WebKitErrorPlugInPageURLStringKey";
44 #define WebKitErrorDescriptionCannotShowMIMEType UI_STRING_INTERNAL("Content with specified MIME type can’t be shown", "WebKitErrorCannotShowMIMEType description")
45 #define WebKitErrorDescriptionCannotShowURL UI_STRING_INTERNAL("The URL can’t be shown", "WebKitErrorCannotShowURL description")
46 #define WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange UI_STRING_INTERNAL("Frame load interrupted", "WebKitErrorFrameLoadInterruptedByPolicyChange description")
47 #define WebKitErrorDescriptionCannotUseRestrictedPort UI_STRING_INTERNAL("Not allowed to use restricted network port", "WebKitErrorCannotUseRestrictedPort description")
49 // Plug-in and java errors
50 #define WebKitErrorDescriptionCannotFindPlugin UI_STRING_INTERNAL("The plug-in can’t be found", "WebKitErrorCannotFindPlugin description")
51 #define WebKitErrorDescriptionCannotLoadPlugin UI_STRING_INTERNAL("The plug-in can’t be loaded", "WebKitErrorCannotLoadPlugin description")
52 #define WebKitErrorDescriptionJavaUnavailable UI_STRING_INTERNAL("Java is unavailable", "WebKitErrorJavaUnavailable description")
53 #define WebKitErrorDescriptionPlugInCancelledConnection UI_STRING_INTERNAL("Plug-in cancelled", "WebKitErrorPlugInCancelledConnection description")
54 #define WebKitErrorDescriptionPlugInWillHandleLoad UI_STRING_INTERNAL("Plug-in handled load", "WebKitErrorPlugInWillHandleLoad description")
56 // Geolocations errors
58 #define WebKitErrorDescriptionGeolocationLocationUnknown UI_STRING_INTERNAL("The current location cannot be found.", "WebKitErrorGeolocationLocationUnknown description")
60 static pthread_once_t registerErrorsControl = PTHREAD_ONCE_INIT;
61 static void registerErrors(void);
63 @implementation NSError (WebKitExtras)
65 static NSMutableDictionary *descriptions = nil;
67 + (void)_registerWebKitErrors
69 pthread_once(®isterErrorsControl, registerErrors);
72 -(id)_webkit_initWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
74 NSDictionary *descriptionsDict;
75 NSString *localizedDesc;
77 // insert a localized string here for those folks not savvy to our category methods
78 descriptionsDict = [descriptions objectForKey:domain];
79 localizedDesc = descriptionsDict ? [descriptionsDict objectForKey:[NSNumber numberWithInt:code]] : nil;
80 dict = [NSDictionary dictionaryWithObjectsAndKeys:
81 URL, @"NSErrorFailingURLKey",
82 [URL absoluteString], @"NSErrorFailingURLStringKey",
83 localizedDesc, NSLocalizedDescriptionKey,
85 return [self initWithDomain:domain code:code userInfo:dict];
88 +(id)_webkit_errorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
90 return [[[self alloc] _webkit_initWithDomain:domain code:code URL:URL] autorelease];
93 + (NSError *)_webKitErrorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
95 [self _registerWebKitErrors];
96 return [self _webkit_errorWithDomain:domain code:code URL:URL];
99 + (NSError *)_webKitErrorWithCode:(int)code failingURL:(NSString *)URLString
101 return [self _webKitErrorWithDomain:WebKitErrorDomain code:code URL:[NSURL _web_URLWithUserTypedString:URLString]];
104 - (id)_initWithPluginErrorCode:(int)code
105 contentURL:(NSURL *)contentURL
106 pluginPageURL:(NSURL *)pluginPageURL
107 pluginName:(NSString *)pluginName
108 MIMEType:(NSString *)MIMEType
110 [[self class] _registerWebKitErrors];
112 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
113 NSDictionary *descriptionsForWebKitErrorDomain = [descriptions objectForKey:WebKitErrorDomain];
114 NSString *localizedDescription = [descriptionsForWebKitErrorDomain objectForKey:[NSNumber numberWithInt:code]];
115 if (localizedDescription)
116 [userInfo setObject:localizedDescription forKey:NSLocalizedDescriptionKey];
118 [userInfo setObject:contentURL forKey:@"NSErrorFailingURLKey"];
119 #ifdef BUILDING_ON_LEOPARD
120 [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSErrorFailingURLStringKey];
122 [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSURLErrorFailingURLStringErrorKey];
126 [userInfo setObject:[pluginPageURL _web_userVisibleString] forKey:WebKitErrorPlugInPageURLStringKey];
129 [userInfo setObject:pluginName forKey:WebKitErrorPlugInNameKey];
132 [userInfo setObject:MIMEType forKey:WebKitErrorMIMETypeKey];
135 NSDictionary *userInfoCopy = [userInfo count] > 0 ? [[NSDictionary alloc] initWithDictionary:userInfo] : nil;
137 NSError *error = [self initWithDomain:WebKitErrorDomain code:code userInfo:userInfoCopy];
138 [userInfoCopy release];
143 + (void)_webkit_addErrorsWithCodesAndDescriptions:(NSDictionary *)dictionary inDomain:(NSString *)domain
146 descriptions = [[NSMutableDictionary alloc] init];
148 [descriptions setObject:dictionary forKey:domain];
151 static void registerErrors()
153 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
155 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
157 WebKitErrorDescriptionCannotShowMIMEType, [NSNumber numberWithInt: WebKitErrorCannotShowMIMEType],
158 WebKitErrorDescriptionCannotShowURL, [NSNumber numberWithInt: WebKitErrorCannotShowURL],
159 WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange, [NSNumber numberWithInt: WebKitErrorFrameLoadInterruptedByPolicyChange],
160 WebKitErrorDescriptionCannotUseRestrictedPort, [NSNumber numberWithInt: WebKitErrorCannotUseRestrictedPort],
162 // Plug-in and java errors
163 WebKitErrorDescriptionCannotFindPlugin, [NSNumber numberWithInt: WebKitErrorCannotFindPlugIn],
164 WebKitErrorDescriptionCannotLoadPlugin, [NSNumber numberWithInt: WebKitErrorCannotLoadPlugIn],
165 WebKitErrorDescriptionJavaUnavailable, [NSNumber numberWithInt: WebKitErrorJavaUnavailable],
166 WebKitErrorDescriptionPlugInCancelledConnection, [NSNumber numberWithInt: WebKitErrorPlugInCancelledConnection],
167 WebKitErrorDescriptionPlugInWillHandleLoad, [NSNumber numberWithInt: WebKitErrorPlugInWillHandleLoad],
169 // Geolocation errors
170 WebKitErrorDescriptionGeolocationLocationUnknown, [NSNumber numberWithInt: WebKitErrorGeolocationLocationUnknown],
173 [NSError _webkit_addErrorsWithCodesAndDescriptions:dict inDomain:WebKitErrorDomain];