webengine/osswebengine/WebKit/Misc/WebKitErrors.m
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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("Cannot show content with specified mime type", "WebKitErrorCannotShowMIMEType description")
       
    45 #define WebKitErrorDescriptionCannotShowURL UI_STRING("Cannot show URL", "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("Cannot find plug-in", "WebKitErrorCannotFindPlugin description")
       
    51 #define WebKitErrorDescriptionCannotLoadPlugin UI_STRING("Cannot load plug-in", "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 static pthread_once_t registerErrorsControl = PTHREAD_ONCE_INIT;
       
    57 static void registerErrors(void);
       
    58 
       
    59 @implementation NSError (WebKitExtras)
       
    60 
       
    61 static NSMutableDictionary *descriptions = nil;
       
    62 
       
    63 + (void)_registerWebKitErrors
       
    64 {
       
    65     pthread_once(&registerErrorsControl, 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     if (contentURL) {
       
   110         [userInfo setObject:contentURL forKey:@"NSErrorFailingURLKey"];
       
   111         [userInfo setObject:[contentURL _web_userVisibleString] forKey:NSErrorFailingURLStringKey];
       
   112     }
       
   113     if (pluginPageURL) {
       
   114         [userInfo setObject:[pluginPageURL _web_userVisibleString] forKey:WebKitErrorPlugInPageURLStringKey];
       
   115     }
       
   116     if (pluginName) {
       
   117         [userInfo setObject:pluginName forKey:WebKitErrorPlugInNameKey];
       
   118     }
       
   119     if (MIMEType) {
       
   120         [userInfo setObject:MIMEType forKey:WebKitErrorMIMETypeKey];
       
   121     }
       
   122 
       
   123     NSDictionary *userInfoCopy = [userInfo count] > 0 ? [[NSDictionary alloc] initWithDictionary:userInfo] : nil;
       
   124     [userInfo release];
       
   125     NSError *error = [self initWithDomain:WebKitErrorDomain code:code userInfo:userInfoCopy];
       
   126     [userInfoCopy release];
       
   127     
       
   128     return error;
       
   129 }
       
   130 
       
   131 + (void)_webkit_addErrorsWithCodesAndDescriptions:(NSDictionary *)dictionary inDomain:(NSString *)domain
       
   132 {
       
   133     if (!descriptions)
       
   134         descriptions = [[NSMutableDictionary alloc] init];
       
   135 
       
   136     [descriptions setObject:dictionary forKey:domain];
       
   137 }
       
   138 
       
   139 static void registerErrors()
       
   140 {
       
   141     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   142 
       
   143     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
       
   144         // Policy errors
       
   145         WebKitErrorDescriptionCannotShowMIMEType,                   [NSNumber numberWithInt: WebKitErrorCannotShowMIMEType],
       
   146         WebKitErrorDescriptionCannotShowURL,                        [NSNumber numberWithInt: WebKitErrorCannotShowURL],
       
   147         WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange,   [NSNumber numberWithInt: WebKitErrorFrameLoadInterruptedByPolicyChange],
       
   148         WebKitErrorDescriptionCannotUseRestrictedPort,              [NSNumber numberWithInt: WebKitErrorCannotUseRestrictedPort],
       
   149         
       
   150         // Plug-in and java errors
       
   151         WebKitErrorDescriptionCannotFindPlugin,                     [NSNumber numberWithInt: WebKitErrorCannotFindPlugIn],
       
   152         WebKitErrorDescriptionCannotLoadPlugin,                     [NSNumber numberWithInt: WebKitErrorCannotLoadPlugIn],
       
   153         WebKitErrorDescriptionJavaUnavailable,                      [NSNumber numberWithInt: WebKitErrorJavaUnavailable],
       
   154         WebKitErrorDescriptionPlugInCancelledConnection,            [NSNumber numberWithInt: WebKitErrorPlugInCancelledConnection],
       
   155         WebKitErrorDescriptionPlugInWillHandleLoad,                 [NSNumber numberWithInt: WebKitErrorPlugInWillHandleLoad],
       
   156         nil];
       
   157 
       
   158     [NSError _webkit_addErrorsWithCodesAndDescriptions:dict inDomain:WebKitErrorDomain];
       
   159 
       
   160     [pool drain];
       
   161 }
       
   162 
       
   163 @end