WebErrorsMac.mm revision 2fc2651226baac27029e38c9d6ef883fa32084db
1/*
2 * Copyright (C) 2010 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "WebErrors.h"
28
29#import "WKError.h"
30#import "WebError.h"
31#import <WebCore/ResourceRequest.h>
32#import <WebCore/ResourceResponse.h>
33#import <pthread.h>
34
35using namespace WebCore;
36using namespace WebKit;
37
38// FIXME: We probably don't need to use NSErrors here.
39
40static NSString * const WebKitErrorMIMETypeKey =               @"WebKitErrorMIMETypeKey";
41static NSString * const WebKitErrorPlugInNameKey =             @"WebKitErrorPlugInNameKey";
42static NSString * const WebKitErrorPlugInPageURLStringKey =    @"WebKitErrorPlugInPageURLStringKey";
43
44// FIXME (WebKit2) <rdar://problem/8728860> WebKit2 needs to be localized
45#define UI_STRING(__str, __desc) [NSString stringWithUTF8String:__str]
46
47// Policy errors
48#define WebKitErrorDescriptionCannotShowMIMEType UI_STRING("Content with specified MIME type can’t be shown", "WebKitErrorCannotShowMIMEType description")
49#define WebKitErrorDescriptionCannotShowURL UI_STRING("The URL can’t be shown", "WebKitErrorCannotShowURL description")
50#define WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange UI_STRING("Frame load interrupted", "WebKitErrorFrameLoadInterruptedByPolicyChange description")
51#define WebKitErrorDescriptionCannotUseRestrictedPort UI_STRING("Not allowed to use restricted network port", "WebKitErrorCannotUseRestrictedPort description")
52
53// Plug-in and java errors
54#define WebKitErrorDescriptionCannotFindPlugin UI_STRING("The plug-in can’t be found", "WebKitErrorCannotFindPlugin description")
55#define WebKitErrorDescriptionCannotLoadPlugin UI_STRING("The plug-in can’t be loaded", "WebKitErrorCannotLoadPlugin description")
56#define WebKitErrorDescriptionJavaUnavailable UI_STRING("Java is unavailable", "WebKitErrorJavaUnavailable description")
57#define WebKitErrorDescriptionPlugInCancelledConnection UI_STRING("Plug-in cancelled", "WebKitErrorPlugInCancelledConnection description")
58#define WebKitErrorDescriptionPlugInWillHandleLoad UI_STRING("Plug-in handled load", "WebKitErrorPlugInWillHandleLoad description")
59
60static pthread_once_t registerErrorsControl = PTHREAD_ONCE_INIT;
61static void registerErrors(void);
62
63@interface NSError (WebKitExtras)
64+ (NSError *)_webKitErrorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL;
65@end
66
67@implementation NSError (WebKitExtras)
68
69static NSMutableDictionary *descriptions = nil;
70
71+ (void)_registerWebKitErrors
72{
73    pthread_once(&registerErrorsControl, registerErrors);
74}
75
76-(id)_webkit_initWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
77{
78    NSDictionary *descriptionsDict;
79    NSString *localizedDesc;
80    NSDictionary *dict;
81    // insert a localized string here for those folks not savvy to our category methods
82    descriptionsDict = [descriptions objectForKey:domain];
83    localizedDesc = descriptionsDict ? [descriptionsDict objectForKey:[NSNumber numberWithInt:code]] : nil;
84    dict = [NSDictionary dictionaryWithObjectsAndKeys:
85        URL, @"NSErrorFailingURLKey",
86        [URL absoluteString], @"NSErrorFailingURLStringKey",
87        localizedDesc, NSLocalizedDescriptionKey,
88        nil];
89    return [self initWithDomain:domain code:code userInfo:dict];
90}
91
92+(id)_webkit_errorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
93{
94    return [[[self alloc] _webkit_initWithDomain:domain code:code URL:URL] autorelease];
95}
96
97+ (NSError *)_webKitErrorWithDomain:(NSString *)domain code:(int)code URL:(NSURL *)URL
98{
99    [self _registerWebKitErrors];
100    return [self _webkit_errorWithDomain:domain code:code URL:URL];
101}
102
103+ (NSError *)_webKitErrorWithCode:(int)code failingURL:(NSString *)URLString
104{
105    return [self _webKitErrorWithDomain:WebError::webKitErrorDomain() code:code URL:[NSURL URLWithString:URLString]];
106}
107
108+ (void)_webkit_addErrorsWithCodesAndDescriptions:(NSDictionary *)dictionary inDomain:(NSString *)domain
109{
110    if (!descriptions)
111        descriptions = [[NSMutableDictionary alloc] init];
112
113    [descriptions setObject:dictionary forKey:domain];
114}
115
116static void registerErrors()
117{
118    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
119
120    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
121        // Policy errors
122        WebKitErrorDescriptionCannotShowMIMEType,                   [NSNumber numberWithInt: kWKErrorCodeCannotShowMIMEType],
123        WebKitErrorDescriptionCannotShowURL,                        [NSNumber numberWithInt: kWKErrorCodeCannotShowURL],
124        WebKitErrorDescriptionFrameLoadInterruptedByPolicyChange,   [NSNumber numberWithInt: kWKErrorCodeFrameLoadInterruptedByPolicyChange],
125        WebKitErrorDescriptionCannotUseRestrictedPort,              [NSNumber numberWithInt: kWKErrorCodeCannotUseRestrictedPort],
126
127        // Plug-in and java errors
128        WebKitErrorDescriptionCannotFindPlugin,                     [NSNumber numberWithInt: kWKErrorCodeCannotFindPlugIn],
129        WebKitErrorDescriptionCannotLoadPlugin,                     [NSNumber numberWithInt: kWKErrorCodeCannotLoadPlugIn],
130        WebKitErrorDescriptionJavaUnavailable,                      [NSNumber numberWithInt: kWKErrorCodeJavaUnavailable],
131        WebKitErrorDescriptionPlugInCancelledConnection,            [NSNumber numberWithInt: kWKErrorCodePlugInCancelledConnection],
132        WebKitErrorDescriptionPlugInWillHandleLoad,                 [NSNumber numberWithInt: kWKErrorCodePlugInWillHandleLoad],
133        nil];
134
135    [NSError _webkit_addErrorsWithCodesAndDescriptions:dict inDomain:WebError::webKitErrorDomain()];
136
137    [pool drain];
138}
139
140@end
141
142namespace WebKit {
143
144ResourceError cancelledError(const ResourceRequest& request)
145{
146    return [NSError _webKitErrorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled URL:request.url()];
147}
148
149ResourceError blockedError(const ResourceRequest& request)
150{
151    return [NSError _webKitErrorWithDomain:WebError::webKitErrorDomain() code:kWKErrorCodeCannotUseRestrictedPort URL:request.url()];
152}
153
154ResourceError cannotShowURLError(const ResourceRequest& request)
155{
156    return [NSError _webKitErrorWithDomain:WebError::webKitErrorDomain() code:kWKErrorCodeCannotShowURL URL:request.url()];
157}
158
159ResourceError interruptForPolicyChangeError(const ResourceRequest& request)
160{
161    return [NSError _webKitErrorWithDomain:WebError::webKitErrorDomain() code:kWKErrorCodeFrameLoadInterruptedByPolicyChange URL:request.url()];
162}
163
164ResourceError cannotShowMIMETypeError(const ResourceResponse& response)
165{
166    return [NSError _webKitErrorWithDomain:NSURLErrorDomain code:kWKErrorCodeCannotShowMIMEType URL:response.url()];
167}
168
169ResourceError fileDoesNotExistError(const ResourceResponse& response)
170{
171    return [NSError _webKitErrorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist URL:response.url()];
172}
173
174ResourceError pluginWillHandleLoadError(const ResourceResponse& response)
175{
176    return [NSError _webKitErrorWithDomain:WebError::webKitErrorDomain() code:kWKErrorCodePlugInWillHandleLoad URL:response.url()];
177}
178
179} // namespace WebKit
180