#include "WebKitDLL.h"
#include "WebError.h"
#include "WebKit.h"
#include <WebCore/BString.h>
#if USE(CG)
#include <CoreGraphics/CoreGraphics.h>
#endif
#if USE(CFURLCONNECTION)
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#endif
using namespace WebCore;
WebError::WebError(const ResourceError& error, IPropertyBag* userInfo)
: m_error(error)
, m_userInfo(userInfo)
{
gClassCount++;
gClassNameCount().add("WebError");
}
WebError::~WebError()
{
gClassCount--;
gClassNameCount().remove("WebError");
}
WebError* WebError::createInstance(const ResourceError& error, IPropertyBag* userInfo)
{
WebError* instance = new WebError(error, userInfo);
instance->AddRef();
return instance;
}
WebError* WebError::createInstance()
{
return createInstance(ResourceError());
}
HRESULT WebError::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
{
if (!ppvObject)
return E_POINTER;
*ppvObject = nullptr;
if (IsEqualGUID(riid, IID_IUnknown))
*ppvObject = static_cast<IWebError*>(this);
else if (IsEqualGUID(riid, CLSID_WebError))
*ppvObject = static_cast<WebError*>(this);
else if (IsEqualGUID(riid, IID_IWebError))
*ppvObject = static_cast<IWebError*>(this);
else if (IsEqualGUID(riid, IID_IWebErrorPrivate))
*ppvObject = static_cast<IWebErrorPrivate*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
ULONG WebError::AddRef()
{
return ++m_refCount;
}
ULONG WebError::Release()
{
ULONG newRef = --m_refCount;
if (!newRef)
delete(this);
return newRef;
}
HRESULT WebError::init(_In_ BSTR domain, int code, _In_ BSTR url)
{
m_error = ResourceError(String(domain, SysStringLen(domain)), code, URL(URL(), String(url, SysStringLen(url))), String());
return S_OK;
}
HRESULT WebError::code(_Out_ int* result)
{
if (!result)
return E_POINTER;
*result = m_error.errorCode();
return S_OK;
}
HRESULT WebError::domain(__deref_opt_out BSTR* result)
{
if (!result)
return E_POINTER;
*result = BString(m_error.domain()).release();
return S_OK;
}
HRESULT WebError::localizedDescription(__deref_opt_out BSTR* result)
{
if (!result)
return E_POINTER;
*result = BString(m_error.localizedDescription()).release();
#if USE(CFURLCONNECTION)
if (!*result) {
if (int code = m_error.errorCode())
*result = BString(wkCFNetworkErrorGetLocalizedDescription(code)).release();
}
#endif
return S_OK;
}
HRESULT WebError::localizedFailureReason(__deref_opt_out BSTR* result)
{
ASSERT_NOT_REACHED();
if (!result)
return E_POINTER;
*result = nullptr;
return E_NOTIMPL;
}
HRESULT WebError::localizedRecoveryOptions(__deref_opt_out IEnumVARIANT** result)
{
ASSERT_NOT_REACHED();
if (!result)
return E_POINTER;
*result = nullptr;
return E_NOTIMPL;
}
HRESULT WebError::localizedRecoverySuggestion(__deref_opt_out BSTR* result)
{
ASSERT_NOT_REACHED();
if (!result)
return E_POINTER;
*result = nullptr;
return E_NOTIMPL;
}
HRESULT WebError::recoverAttempter(__deref_opt_out IUnknown** result)
{
ASSERT_NOT_REACHED();
if (!result)
return E_POINTER;
*result = nullptr;
return E_NOTIMPL;
}
HRESULT WebError::userInfo(_COM_Outptr_opt_ IPropertyBag** result)
{
if (!result)
return E_POINTER;
*result = nullptr;
if (!m_userInfo)
return E_FAIL;
return m_userInfo.copyRefTo(result);
}
HRESULT WebError::failingURL(__deref_opt_out BSTR* result)
{
if (!result)
return E_POINTER;
*result = BString(m_error.failingURL()).release();
return S_OK;
}
HRESULT WebError::isPolicyChangeError(_Out_ BOOL* result)
{
if (!result)
return E_POINTER;
*result = m_error.domain() == String(WebKitErrorDomain)
&& m_error.errorCode() == WebKitErrorFrameLoadInterruptedByPolicyChange;
return S_OK;
}
HRESULT WebError::sslPeerCertificate(_Out_ ULONG_PTR* result)
{
if (!result)
return E_POINTER;
*result = 0;
#if USE(CFURLCONNECTION)
if (!m_cfErrorUserInfoDict) {
CFErrorRef cfError = m_error;
m_cfErrorUserInfoDict = adoptCF(CFErrorCopyUserInfo(cfError));
}
if (!m_cfErrorUserInfoDict)
return E_FAIL;
void* data = wkGetSSLPeerCertificateDataBytePtr(m_cfErrorUserInfoDict.get());
if (!data)
return E_FAIL;
*result = reinterpret_cast<ULONG_PTR>(data);
#endif
return *result ? S_OK : E_FAIL;
}
const ResourceError& WebError::resourceError() const
{
return m_error;
}