Remove Cocoa CFURLConnection loading code
[WebKit-https.git] / Source / WebCore / platform / network / mac / ResourceErrorMac.mm
index 9b65de8..709b699 100644 (file)
 #import "config.h"
 #import "ResourceError.h"
 
-#import "BlockExceptions.h"
 #import "URL.h"
 #import <CoreFoundation/CFError.h>
 #import <Foundation/Foundation.h>
-
-#if PLATFORM(IOS) && USE(CFNETWORK)
-#import <CFNetwork/CFSocketStreamPriv.h>
-#endif
+#import <wtf/BlockObjCExceptions.h>
 
 @interface NSError (WebExtras)
 - (NSString *)_web_localizedDescription;
@@ -91,85 +87,6 @@ static NSDictionary* dictionaryThatCanCode(NSDictionary* src)
 
 @end
 
-
-#if USE(CFNETWORK)
-static NSError *NSErrorFromCFError(CFErrorRef cfError, NSURL *url)
-{
-    CFIndex errCode = CFErrorGetCode(cfError);
-    if (CFEqual(CFErrorGetDomain(cfError), kCFErrorDomainCFNetwork) && errCode <= kCFURLErrorUnknown && errCode > -4000) {
-        // This is an URL error and needs to be translated to the NSURLErrorDomain
-        id keys[10], values[10];
-        CFDictionaryRef userInfo = CFErrorCopyUserInfo(cfError);
-        NSError *result;
-        NSInteger count = 0;
-
-        if (url) {
-            keys[count] = NSURLErrorFailingURLErrorKey;
-            values[count] = url;
-            count ++;
-
-            keys[count] = NSURLErrorFailingURLStringErrorKey;
-            values[count] = [url absoluteString];
-            count ++;
-        }
-
-        values[count] = (id)CFDictionaryGetValue(userInfo, kCFErrorLocalizedDescriptionKey);
-        if (values[count]) {
-            keys[count] = NSLocalizedDescriptionKey;
-            count ++;
-        }
-
-        values[count] = (id)CFDictionaryGetValue(userInfo, kCFErrorLocalizedFailureReasonKey);
-        if (values[count]) {
-            keys[count] = NSLocalizedFailureReasonErrorKey;
-            count ++;
-        }
-
-        values[count] = (id)CFDictionaryGetValue(userInfo, kCFErrorLocalizedRecoverySuggestionKey);
-        if (values[count]) {
-            keys[count] = NSLocalizedRecoverySuggestionErrorKey;
-            count ++;
-        }
-
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-        if (userInfo && (values[count] = (id)CFDictionaryGetValue(userInfo, kCFStreamPropertySSLPeerCertificates)) != nil) {
-            // Need to translate the cert
-            keys[count] = @"NSErrorPeerCertificateChainKey";
-            count ++;
-
-            values[count] = (id)CFDictionaryGetValue(userInfo, _kCFStreamPropertySSLClientCertificates);
-            if (values[count]) {
-                keys[count] = @"NSErrorClientCertificateChainKey";
-                count ++;
-            }
-
-            values[count] = (id)CFDictionaryGetValue(userInfo, _kCFStreamPropertySSLClientCertificateState);
-            if (values[count]) {
-                keys[count] = @"NSErrorClientCertificateStateKey";
-                count ++;
-            }
-        }
-#pragma clang diagnostic pop
-
-        if (userInfo && (values[count] = (id)CFDictionaryGetValue(userInfo, kCFStreamPropertySSLPeerTrust)) != nil) {
-            keys[count] = NSURLErrorFailingURLPeerTrustErrorKey;
-            count++;
-        }
-
-        keys[count] = NSUnderlyingErrorKey;
-        values[count] = (id)cfError;
-        count ++;
-
-        result = [WebCustomNSURLError errorWithDomain:NSURLErrorDomain code:(errCode == kCFURLErrorUnknown ? (CFIndex)NSURLErrorUnknown : errCode) userInfo:[NSDictionary dictionaryWithObjects:values forKeys:keys count:count]];
-        if (userInfo)
-            CFRelease(userInfo);
-        return result;
-    }
-    return (NSError *)cfError;
-}
-#endif // USE(CFNETWORK)
-
 #endif // PLATFORM(IOS)
 
 namespace WebCore {
@@ -182,77 +99,26 @@ static RetainPtr<NSError> createNSErrorFromResourceErrorBase(const ResourceError
         [userInfo.get() setValue:resourceError.localizedDescription() forKey:NSLocalizedDescriptionKey];
 
     if (!resourceError.failingURL().isEmpty()) {
-        RetainPtr<NSURL> cocoaURL = adoptNS([[NSURL alloc] initWithString:resourceError.failingURL()]);
-        [userInfo.get() setValue:resourceError.failingURL() forKey:@"NSErrorFailingURLStringKey"];
-        [userInfo.get() setValue:cocoaURL.get() forKey:@"NSErrorFailingURLKey"];
+        [userInfo.get() setValue:(NSString *)resourceError.failingURL().string() forKey:@"NSErrorFailingURLStringKey"];
+        if (NSURL *cocoaURL = (NSURL *)resourceError.failingURL())
+            [userInfo.get() setValue:cocoaURL forKey:@"NSErrorFailingURLKey"];
     }
 
     return adoptNS([[NSError alloc] initWithDomain:resourceError.domain() code:resourceError.errorCode() userInfo:userInfo.get()]);
 }
 
-#if USE(CFNETWORK)
-
-ResourceError::ResourceError(NSError *error)
-    : m_dataIsUpToDate(false)
-    , m_platformError(reinterpret_cast<CFErrorRef>(error))
-{
-    m_isNull = !error;
-    if (!m_isNull)
-        m_isTimeout = [error code] == NSURLErrorTimedOut;
-}
-
-NSError *ResourceError::nsError() const
-{
-    if (m_isNull) {
-        ASSERT(!m_platformError);
-        return nil;
-    }
-
-    if (m_platformNSError)
-        return m_platformNSError.get();
-
-    if (m_platformError) {
-        CFErrorRef error = m_platformError.get();
-        RetainPtr<CFDictionaryRef> userInfo = adoptCF(CFErrorCopyUserInfo(error));
-#if PLATFORM(IOS)
-        m_platformNSError = NSErrorFromCFError(error, (NSURL *)[(NSDictionary *)userInfo.get() objectForKey:(id) kCFURLErrorFailingURLErrorKey]);
-        // If NSErrorFromCFError created a new NSError for us, return that.
-        if (m_platformNSError.get() != (NSError *)error)
-            return m_platformNSError.get();
-
-        // Otherwise fall through to create a new NSError from the CFError.
-#endif
-        m_platformNSError = adoptNS([[NSError alloc] initWithDomain:(NSString *)CFErrorGetDomain(error) code:CFErrorGetCode(error) userInfo:(NSDictionary *)userInfo.get()]);
-        return m_platformNSError.get();
-    }
-
-    m_platformNSError = createNSErrorFromResourceErrorBase(*this);
-    return m_platformNSError.get();
-}
-
-ResourceError::operator NSError *() const
-{
-    return nsError();
-}
-
-#else
-
 ResourceError::ResourceError(NSError *nsError)
-    : m_dataIsUpToDate(false)
+    : ResourceErrorBase(Type::Null)
+    , m_dataIsUpToDate(false)
     , m_platformError(nsError)
 {
-    m_isNull = !nsError;
-    if (!m_isNull)
-        m_isTimeout = [m_platformError.get() code] == NSURLErrorTimedOut;
+    if (nsError)
+        setType(([m_platformError.get() code] == NSURLErrorTimedOut) ? Type::Timeout : Type::General);
 }
 
 ResourceError::ResourceError(CFErrorRef cfError)
-    : m_dataIsUpToDate(false)
-    , m_platformError((NSError *)cfError)
+    : ResourceError((NSError *)cfError)
 {
-    m_isNull = !cfError;
-    if (!m_isNull)
-        m_isTimeout = [m_platformError.get() code] == NSURLErrorTimedOut;
 }
 
 void ResourceError::platformLazyInit()
@@ -263,10 +129,10 @@ void ResourceError::platformLazyInit()
     m_domain = [m_platformError.get() domain];
     m_errorCode = [m_platformError.get() code];
 
-    NSString* failingURLString = [[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLStringKey"];
-    if (!failingURLString)
-        failingURLString = [[[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLKey"] absoluteString];
-    m_failingURL = failingURLString; 
+    if (NSString* failingURLString = [[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLStringKey"])
+        m_failingURL = URL(URL(), failingURLString);
+    else
+        m_failingURL = URL((NSURL *)[[m_platformError.get() userInfo] valueForKey:@"NSErrorFailingURLKey"]);
     // Workaround for <rdar://problem/6554067>
     m_localizedDescription = m_failingURL;
     BEGIN_BLOCK_OBJC_EXCEPTIONS;
@@ -281,15 +147,19 @@ bool ResourceError::platformCompare(const ResourceError& a, const ResourceError&
     return a.nsError() == b.nsError();
 }
 
+void ResourceError::doPlatformIsolatedCopy(const ResourceError&)
+{
+}
+
 NSError *ResourceError::nsError() const
 {
-    if (m_isNull) {
+    if (isNull()) {
         ASSERT(!m_platformError);
         return nil;
     }
 
     if (!m_platformError)
-        m_platformError = createNSErrorFromResourceErrorBase(*this);;
+        m_platformError = createNSErrorFromResourceErrorBase(*this);
 
     return m_platformError.get();
 }
@@ -309,6 +179,4 @@ ResourceError::operator CFErrorRef() const
     return cfError();
 }
 
-#endif // USE(CFNETWORK)
-
 } // namespace WebCore