WebKitErrors.m 8.6 KB

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