1 /* 2 * Copyright (C) 2007 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 #include "config.h" 27 #include "WebKitDLL.h" 28 #include "WebURLAuthenticationChallenge.h" 29 30 #include "COMPtr.h" 31 #include "WebError.h" 32 #include "WebKit.h" 33 #include "WebURLAuthenticationChallengeSender.h" 34 #include "WebURLCredential.h" 35 #include "WebURLProtectionSpace.h" 36 #include "WebURLResponse.h" 37 #include "WebKit.h" 38 39 #pragma warning(push, 0) 40 #include <WebCore/BString.h> 41 #include <WebCore/ResourceHandle.h> 42 #pragma warning(pop) 43 44 using namespace WebCore; 45 46 // WebURLAuthenticationChallenge ---------------------------------------------------------------- 47 48 WebURLAuthenticationChallenge::WebURLAuthenticationChallenge(const AuthenticationChallenge& authenticationChallenge, 49 IWebURLAuthenticationChallengeSender* sender) 50 : m_refCount(0) 51 , m_authenticationChallenge(authenticationChallenge) 52 , m_sender(sender) 53 { 54 gClassCount++; 55 gClassNameCount.add("WebURLAuthenticationChallenge"); 56 } 57 58 WebURLAuthenticationChallenge::~WebURLAuthenticationChallenge() 59 { 60 gClassCount--; 61 gClassNameCount.remove("WebURLAuthenticationChallenge"); 62 } 63 64 WebURLAuthenticationChallenge* WebURLAuthenticationChallenge::createInstance(const AuthenticationChallenge& authenticationChallenge) 65 { 66 WebURLAuthenticationChallenge* instance = new WebURLAuthenticationChallenge(authenticationChallenge, 0); 67 instance->AddRef(); 68 return instance; 69 } 70 71 WebURLAuthenticationChallenge* WebURLAuthenticationChallenge::createInstance(const AuthenticationChallenge& authenticationChallenge, 72 IWebURLAuthenticationChallengeSender* sender) 73 { 74 WebURLAuthenticationChallenge* instance = new WebURLAuthenticationChallenge(authenticationChallenge, sender); 75 instance->AddRef(); 76 return instance; 77 } 78 79 // IUnknown ------------------------------------------------------------------- 80 81 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::QueryInterface(REFIID riid, void** ppvObject) 82 { 83 *ppvObject = 0; 84 if (IsEqualGUID(riid, IID_IUnknown)) 85 *ppvObject = static_cast<IUnknown*>(this); 86 else if (IsEqualGUID(riid, __uuidof(this))) 87 *ppvObject = static_cast<WebURLAuthenticationChallenge*>(this); 88 else if (IsEqualGUID(riid, IID_IWebURLAuthenticationChallenge)) 89 *ppvObject = static_cast<IWebURLAuthenticationChallenge*>(this); 90 else 91 return E_NOINTERFACE; 92 93 AddRef(); 94 return S_OK; 95 } 96 97 ULONG STDMETHODCALLTYPE WebURLAuthenticationChallenge::AddRef(void) 98 { 99 return ++m_refCount; 100 } 101 102 ULONG STDMETHODCALLTYPE WebURLAuthenticationChallenge::Release(void) 103 { 104 ULONG newRef = --m_refCount; 105 if (!newRef) 106 delete(this); 107 108 return newRef; 109 } 110 111 // IWebURLAuthenticationChallenge ------------------------------------------------------------------- 112 113 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::initWithProtectionSpace( 114 /* [in] */ IWebURLProtectionSpace* space, 115 /* [in] */ IWebURLCredential* proposedCredential, 116 /* [in] */ int previousFailureCount, 117 /* [in] */ IWebURLResponse* failureResponse, 118 /* [in] */ IWebError* error, 119 /* [in] */ IWebURLAuthenticationChallengeSender* sender) 120 { 121 LOG_ERROR("Calling the ala carte init for WebURLAuthenticationChallenge - is this really what you want to do?"); 122 123 if (!space || !proposedCredential || !failureResponse || !sender) 124 return E_POINTER; 125 126 HRESULT hr = S_OK; 127 COMPtr<WebURLProtectionSpace> webSpace; 128 hr = space->QueryInterface(&webSpace); 129 if (FAILED(hr)) 130 return hr; 131 132 COMPtr<WebURLCredential> webCredential(Query, proposedCredential); 133 if (!webCredential) 134 return E_NOINTERFACE; 135 136 COMPtr<WebURLResponse> webResponse; 137 hr = failureResponse->QueryInterface(&webResponse); 138 if (FAILED(hr)) 139 return hr; 140 141 COMPtr<WebError> webError; 142 hr = error->QueryInterface(CLSID_WebError, (void**)&webError); 143 if (FAILED(hr)) 144 return hr; 145 146 COMPtr<WebURLAuthenticationChallengeSender> webSender(Query, sender); 147 if (!webSender) 148 return E_NOINTERFACE; 149 150 // FIXME: After we change AuthenticationChallenge to use "ResourceHandle" as the abstract "Sender" or "Source of this Auth Challenge", then we'll 151 // construct the AuthenticationChallenge with that as obtained from the webSender 152 #if USE(CFNETWORK) 153 m_authenticationChallenge = AuthenticationChallenge(webSpace->protectionSpace(), webCredential->credential(), 154 previousFailureCount, webResponse->resourceResponse(), webError->resourceError()); 155 #endif 156 return S_OK; 157 } 158 159 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::initWithAuthenticationChallenge( 160 /* [in] */ IWebURLAuthenticationChallenge* challenge, 161 /* [in] */ IWebURLAuthenticationChallengeSender* sender) 162 { 163 if (!challenge || !sender) 164 return E_POINTER; 165 166 COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge); 167 if (!webChallenge) 168 return E_NOINTERFACE; 169 170 COMPtr<WebURLAuthenticationChallengeSender> webSender(Query, sender); 171 if (!webSender) 172 return E_NOINTERFACE; 173 174 #if USE(CFNETWORK) 175 m_authenticationChallenge = AuthenticationChallenge(webChallenge->authenticationChallenge().cfURLAuthChallengeRef(), webSender->authenticationClient()); 176 177 return S_OK; 178 #else 179 180 return E_FAIL; 181 #endif 182 } 183 184 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::error( 185 /* [out, retval] */ IWebError** result) 186 { 187 *result = WebError::createInstance(m_authenticationChallenge.error()); 188 return S_OK; 189 } 190 191 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::failureResponse( 192 /* [out, retval] */ IWebURLResponse** result) 193 { 194 *result = WebURLResponse::createInstance(m_authenticationChallenge.failureResponse()); 195 return S_OK; 196 } 197 198 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::previousFailureCount( 199 /* [out, retval] */ UINT* result) 200 { 201 *result = m_authenticationChallenge.previousFailureCount(); 202 return S_OK; 203 } 204 205 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::proposedCredential( 206 /* [out, retval] */ IWebURLCredential** result) 207 { 208 *result = WebURLCredential::createInstance(m_authenticationChallenge.proposedCredential()); 209 return S_OK; 210 } 211 212 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::protectionSpace( 213 /* [out, retval] */ IWebURLProtectionSpace** result) 214 { 215 *result = WebURLProtectionSpace::createInstance(m_authenticationChallenge.protectionSpace()); 216 return S_OK; 217 } 218 219 HRESULT STDMETHODCALLTYPE WebURLAuthenticationChallenge::sender( 220 /* [out, retval] */ IWebURLAuthenticationChallengeSender** sender) 221 { 222 if (!m_sender) { 223 AuthenticationClient* client = m_authenticationChallenge.authenticationClient(); 224 m_sender.adoptRef(WebURLAuthenticationChallengeSender::createInstance(client)); 225 } 226 227 return m_sender.copyRefTo(sender); 228 } 229 230 // WebURLAuthenticationChallenge ------------------------------------------------------------------- 231 const AuthenticationChallenge& WebURLAuthenticationChallenge::authenticationChallenge() const 232 { 233 return m_authenticationChallenge; 234 } 235