1/* 2 * Copyright (C) 2006, 2008 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 COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#import "config.h" 27#import "ResourceError.h" 28 29#import "BlockExceptions.h" 30#import "KURL.h" 31#import <CoreFoundation/CFError.h> 32#import <Foundation/Foundation.h> 33 34@interface NSError (WebExtras) 35- (NSString *)_web_localizedDescription; 36@end 37 38namespace WebCore { 39 40ResourceError::ResourceError(NSError *nsError) 41 : m_dataIsUpToDate(false) 42 , m_platformError(nsError) 43{ 44 m_isNull = !nsError; 45} 46 47ResourceError::ResourceError(CFErrorRef cfError) 48 : m_dataIsUpToDate(false) 49 , m_platformError((NSError *)cfError) 50{ 51 m_isNull = !cfError; 52} 53 54void ResourceError::platformLazyInit() 55{ 56 if (m_dataIsUpToDate) 57 return; 58 59 m_domain = [m_platformError.get() domain]; 60 m_errorCode = [m_platformError.get() code]; 61 62 NSString* failingURLString = [[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLStringKey"]; 63 if (!failingURLString) 64 failingURLString = [[[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLKey"] absoluteString]; 65 m_failingURL = failingURLString; 66 // Workaround for <rdar://problem/6554067> 67 m_localizedDescription = m_failingURL; 68 BEGIN_BLOCK_OBJC_EXCEPTIONS; 69 m_localizedDescription = [m_platformError.get() _web_localizedDescription]; 70 END_BLOCK_OBJC_EXCEPTIONS; 71 72 m_dataIsUpToDate = true; 73} 74 75bool ResourceError::platformCompare(const ResourceError& a, const ResourceError& b) 76{ 77 return a.nsError() == b.nsError(); 78} 79 80NSError *ResourceError::nsError() const 81{ 82 if (m_isNull) { 83 ASSERT(!m_platformError); 84 return nil; 85 } 86 87 if (!m_platformError) { 88 RetainPtr<NSMutableDictionary> userInfo(AdoptNS, [[NSMutableDictionary alloc] init]); 89 90 if (!m_localizedDescription.isEmpty()) 91 [userInfo.get() setValue:m_localizedDescription forKey:NSLocalizedDescriptionKey]; 92 93 if (!m_failingURL.isEmpty()) { 94 NSURL *cocoaURL = KURL(ParsedURLString, m_failingURL); 95 [userInfo.get() setValue:m_failingURL forKey:@"NSErrorFailingURLStringKey"]; 96 [userInfo.get() setValue:cocoaURL forKey:@"NSErrorFailingURLKey"]; 97 } 98 99 m_platformError.adoptNS([[NSError alloc] initWithDomain:m_domain code:m_errorCode userInfo:userInfo.get()]); 100 } 101 102 return m_platformError.get(); 103} 104 105ResourceError::operator NSError *() const 106{ 107 return nsError(); 108} 109 110CFErrorRef ResourceError::cfError() const 111{ 112 return (CFErrorRef)nsError(); 113} 114 115ResourceError::operator CFErrorRef() const 116{ 117 return cfError(); 118} 119 120} // namespace WebCore 121