2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ResourceError.h"
32 // FIXME: Once <rdar://problem/5050881> is fixed in open source we
33 // can remove this extern "C"
35 #include <CFNetwork/CFNetworkErrors.h>
38 #include <CoreFoundation/CFError.h>
39 #include <WTF/RetainPtr.h>
43 const CFStringRef failingURLStringKey = CFSTR("NSErrorFailingURLStringKey");
44 const CFStringRef failingURLKey = CFSTR("NSErrorFailingURLKey");
46 // FIXME: Once <rdar://problem/5050841> is fixed we can remove this constructor.
47 ResourceError::ResourceError(CFStreamError error)
48 : m_errorCode(error.error)
50 , m_dataIsUpToDate(true)
52 switch(error.domain) {
53 case kCFStreamErrorDomainCustom:
54 m_domain ="NSCustomErrorDomain";
56 case kCFStreamErrorDomainPOSIX:
57 m_domain = "NSPOSIXErrorDomain";
59 case kCFStreamErrorDomainMacOSStatus:
60 m_domain = "NSOSStatusErrorDomain";
65 void ResourceError::unpackPlatformError()
70 CFStringRef domain = CFErrorGetDomain(m_platformError.get());
71 if (domain == kCFErrorDomainMach || domain == kCFErrorDomainCocoa)
72 m_domain ="NSCustomErrorDomain";
73 else if (domain == kCFErrorDomainCFNetwork)
74 m_domain = "CFURLErrorDomain";
75 else if (domain == kCFErrorDomainPOSIX)
76 m_domain = "NSPOSIXErrorDomain";
77 else if (domain == kCFErrorDomainOSStatus)
78 m_domain = "NSOSStatusErrorDomain";
79 else if (domain == kCFErrorDomainWinSock)
80 m_domain = "kCFErrorDomainWinSock";
82 m_errorCode = CFErrorGetCode(m_platformError.get());
84 RetainPtr<CFDictionaryRef> userInfo(AdoptCF, CFErrorCopyUserInfo(m_platformError.get()));
86 CFStringRef failingURLString = (CFStringRef) CFDictionaryGetValue(userInfo.get(), failingURLStringKey);
88 m_failingURL = String(failingURLString);
90 CFURLRef failingURL = (CFURLRef) CFDictionaryGetValue(userInfo.get(), failingURLKey);
92 RetainPtr<CFURLRef> absoluteURLRef(AdoptCF, CFURLCopyAbsoluteURL(failingURL));
93 if (absoluteURLRef.get()) {
94 failingURLString = CFURLGetString(absoluteURLRef.get());
95 m_failingURL = String(failingURLString);
99 m_localizedDescription = (CFStringRef) CFDictionaryGetValue(userInfo.get(), kCFErrorLocalizedDescriptionKey);
102 m_dataIsUpToDate = true;
105 ResourceError::operator CFErrorRef() const
108 ASSERT(!m_platformError);
112 if (!m_platformError) {
113 RetainPtr<CFMutableDictionaryRef> userInfo(AdoptCF, CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
115 if (!m_localizedDescription.isEmpty()) {
116 RetainPtr<CFStringRef> localizedDescriptionString(AdoptCF, m_localizedDescription.createCFString());
117 CFDictionarySetValue(userInfo.get(), kCFErrorLocalizedDescriptionKey, localizedDescriptionString.get());
120 if (!m_failingURL.isEmpty()) {
121 RetainPtr<CFStringRef> failingURLString(AdoptCF, m_failingURL.createCFString());
122 CFDictionarySetValue(userInfo.get(), failingURLStringKey, failingURLString.get());
123 RetainPtr<CFURLRef> url(AdoptCF, KURL(m_failingURL.deprecatedString()).createCFURL());
124 CFDictionarySetValue(userInfo.get(), failingURLKey, url.get());
127 RetainPtr<CFStringRef> domainString(AdoptCF, m_domain.createCFString());
128 m_platformError.adoptCF(CFErrorCreate(0, domainString.get(), m_errorCode, userInfo.get()));
131 return m_platformError.get();
134 ResourceError::operator CFStreamError() const
136 unpackPlatformErrorIfNeeded();
138 CFStreamError result;
139 result.error = m_errorCode;
141 if (m_domain == "NSCustomErrorDomain")
142 result.domain = kCFStreamErrorDomainCustom;
143 else if (m_domain == "NSPOSIXErrorDomain")
144 result.domain = kCFStreamErrorDomainPOSIX;
145 else if (m_domain == "NSOSStatusErrorDomain")
146 result.domain = kCFStreamErrorDomainMacOSStatus;
148 ASSERT_NOT_REACHED();
153 } // namespace WebCore
155 #endif // USE(CFNETWORK)