WebURLAuthenticationChallenge.cpp 8.5 KB

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