WebKit/mac/Misc/WebKitErrors.m
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 
       
    37 NSString *WebKitErrorDomain = @"WebKitErrorDomain";
       
    38 
       
    39 NSString * const WebKitErrorMIMETypeKey =               @"WebKitErrorMIMETypeKey";
       
    40 NSString * const WebKitErrorPlugInNameKey =             @"WebKitErrorPlugInNameKey";
       
    41 NSString * 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 
       
    56 // Geolocations errors
       
    57 
       
    58 #define WebKitErrorDescriptionGeolocationLocationUnknown UI_STRING("The current location cannot be found.", "WebKitErrorGeolocationLocationUnknown description")
       
    59 
       
    60 static pthread_once_t registerErrorsControl = PTHREAD_ONCE_INIT;
       
    61 static void registerErrors(void);
       
    62 
       
    63 @implementation NSError (WebKitExtras)
       
    64 
       
    65 static NSMutableDictionary *descriptions = nil;
       
    66 
       
    67 + (void)_registerWebKitErrors
       
    68 {
       
    69     pthread_once(&registerErrorsControl, registerErrors);
       
    70 }
       
    71 
       
    72 -(id)_webkit_initWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
       
    73 {
       
    74     NSDictionary *descriptionsDict;
       
    75     NSString *localizedDesc;
       
    76     NSDictionary *dict;
       
    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,
       
    84         nil];
       
    85     return [self initWithDomain:domain code:code userInfo:dict];
       
    86 }
       
    87 
       
    88 +(id)_webkit_errorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
       
    89 {
       
    90     return [[[self alloc] _webkit_initWithDomain:domain code:code URL:URL] autorelease];
       
    91 }
       
    92 
       
    93 + (NSError *)_webKitErrorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
       
    94 {
       
    95     [self _registerWebKitErrors];
       
    96     return [self _webkit_errorWithDomain:domain code:code URL:URL];
       
    97 }
       
    98 
       
    99 + (NSError *)_webKitErrorWithCode:(int)code failingURL:(NSString *)URLString
       
   100 {
       
   101     return [self _webKitErrorWithDomain:WebKitErrorDomain code:code URL:[NSURL _web_URLWithUserTypedString:URLString]];
       
   102 }
       
   103 
       
   104 - (id)_initWithPluginErrorCode:(int)code
       
   105                     contentURL:(NSURL *)contentURL
       
   106                  pluginPageURL:(NSURL *)pluginPageURL
       
   107                     pluginName:(NSString *)pluginName
       
   108                       MIMEType:(NSString *)MIMEType
       
   109 {
       
   110     [[self class] _registerWebKitErrors];
       
   111     
       
   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];
       
   117     if (contentURL) {
       
   118         [userInfo setObject:contentURL forKey:@"NSErrorFailingURLKey"];
       
   119 #if defined(BUILDING_ON_TIGER) || defined(BUILDING_ON_LEOPARD)
       
   120         [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSErrorFailingURLStringKey];
       
   121 #else
       
   122         [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSURLErrorFailingURLStringErrorKey];
       
   123 #endif
       
   124     }
       
   125     if (pluginPageURL) {
       
   126         [userInfo setObject:[pluginPageURL _web_userVisibleString] forKey:WebKitErrorPlugInPageURLStringKey];
       
   127     }
       
   128     if (pluginName) {
       
   129         [userInfo setObject:pluginName forKey:WebKitErrorPlugInNameKey];
       
   130     }
       
   131     if (MIMEType) {
       
   132         [userInfo setObject:MIMEType forKey:WebKitErrorMIMETypeKey];
       
   133     }
       
   134 
       
   135     NSDictionary *userInfoCopy = [userInfo count] > 0 ? [[NSDictionary alloc] initWithDictionary:userInfo] : nil;
       
   136     [userInfo release];
       
   137     NSError *error = [self initWithDomain:WebKitErrorDomain code:code userInfo:userInfoCopy];
       
   138     [userInfoCopy release];
       
   139     
       
   140     return error;
       
   141 }
       
   142 
       
   143 + (void)_webkit_addErrorsWithCodesAndDescriptions:(NSDictionary *)dictionary inDomain:(NSString *)domain
       
   144 {
       
   145     if (!descriptions)
       
   146         descriptions = [[NSMutableDictionary alloc] init];
       
   147 
       
   148     [descriptions setObject:dictionary forKey:domain];
       
   149 }
       
   150 
       
   151 static void registerErrors()
       
   152 {
       
   153     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   154 
       
   155     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
       
   156         // Policy errors
       
   157         WebKitErrorDescriptionCannotShowMIMEType,                   [NSNumber numberWithInt: WebKitErrorCannotShowMIMEType],
       
   158         WebKitErrorDescriptionCannotShowURL,                        [NSNumber numberWithInt: WebKitErrorCannotShowURL],
       
   159         WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange,   [NSNumber numberWithInt: WebKitErrorFrameLoadInterruptedByPolicyChange],
       
   160         WebKitErrorDescriptionCannotUseRestrictedPort,              [NSNumber numberWithInt: WebKitErrorCannotUseRestrictedPort],
       
   161         
       
   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],
       
   168 
       
   169         // Geolocation errors
       
   170         WebKitErrorDescriptionGeolocationLocationUnknown,           [NSNumber numberWithInt: WebKitErrorGeolocationLocationUnknown],
       
   171         nil];
       
   172 
       
   173     [NSError _webkit_addErrorsWithCodesAndDescriptions:dict inDomain:WebKitErrorDomain];
       
   174 
       
   175     [pool drain];
       
   176 }
       
   177 
       
   178 @end