WebDownloadCurl.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2008 Brent Fulgham <bfulgham@gmail.com>. 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 "WebDownload.h"
  28. #include "DefaultDownloadDelegate.h"
  29. #include "MarshallingHelpers.h"
  30. #include "WebError.h"
  31. #include "WebKit.h"
  32. #include "WebKitLogging.h"
  33. #include "WebMutableURLRequest.h"
  34. #include "WebURLAuthenticationChallenge.h"
  35. #include "WebURLCredential.h"
  36. #include "WebURLResponse.h"
  37. #include <wtf/platform.h>
  38. #include <wtf/text/CString.h>
  39. #include <io.h>
  40. #include <sys/stat.h>
  41. #include <sys/types.h>
  42. #include <WebCore/BString.h>
  43. #include <WebCore/NotImplemented.h>
  44. #include <WebCore/ResourceError.h>
  45. #include <WebCore/ResourceHandle.h>
  46. #include <WebCore/ResourceRequest.h>
  47. #include <WebCore/ResourceResponse.h>
  48. using namespace WebCore;
  49. // WebDownload ----------------------------------------------------------------
  50. void WebDownload::init(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
  51. {
  52. notImplemented();
  53. }
  54. void WebDownload::init(const KURL& url, IWebDownloadDelegate* delegate)
  55. {
  56. m_delegate = delegate;
  57. m_download.init(this, url);
  58. }
  59. // IWebDownload -------------------------------------------------------------------
  60. HRESULT STDMETHODCALLTYPE WebDownload::initWithRequest(
  61. /* [in] */ IWebURLRequest* request,
  62. /* [in] */ IWebDownloadDelegate* delegate)
  63. {
  64. notImplemented();
  65. return E_FAIL;
  66. }
  67. HRESULT STDMETHODCALLTYPE WebDownload::initToResumeWithBundle(
  68. /* [in] */ BSTR bundlePath,
  69. /* [in] */ IWebDownloadDelegate* delegate)
  70. {
  71. notImplemented();
  72. return E_FAIL;
  73. }
  74. HRESULT STDMETHODCALLTYPE WebDownload::start()
  75. {
  76. if (!m_download.start())
  77. return E_FAIL;
  78. if (m_delegate)
  79. m_delegate->didBegin(this);
  80. return S_OK;
  81. }
  82. HRESULT STDMETHODCALLTYPE WebDownload::cancel()
  83. {
  84. if (!m_download.cancel())
  85. return E_FAIL;
  86. return S_OK;
  87. }
  88. HRESULT STDMETHODCALLTYPE WebDownload::cancelForResume()
  89. {
  90. notImplemented();
  91. return E_FAIL;
  92. }
  93. HRESULT STDMETHODCALLTYPE WebDownload::deletesFileUponFailure(
  94. /* [out, retval] */ BOOL* result)
  95. {
  96. *result = m_download.deletesFileUponFailure() ? TRUE : FALSE;
  97. return S_OK;
  98. }
  99. HRESULT STDMETHODCALLTYPE WebDownload::setDeletesFileUponFailure(
  100. /* [in] */ BOOL deletesFileUponFailure)
  101. {
  102. m_download.setDeletesFileUponFailure(deletesFileUponFailure);
  103. return S_OK;
  104. }
  105. HRESULT STDMETHODCALLTYPE WebDownload::setDestination(
  106. /* [in] */ BSTR path,
  107. /* [in] */ BOOL allowOverwrite)
  108. {
  109. size_t len = wcslen(path);
  110. m_destination = String(path, len);
  111. m_download.setDestination(m_destination);
  112. return S_OK;
  113. }
  114. // IWebURLAuthenticationChallengeSender -------------------------------------------------------------------
  115. HRESULT STDMETHODCALLTYPE WebDownload::cancelAuthenticationChallenge(
  116. /* [in] */ IWebURLAuthenticationChallenge*)
  117. {
  118. notImplemented();
  119. return E_FAIL;
  120. }
  121. HRESULT STDMETHODCALLTYPE WebDownload::continueWithoutCredentialForAuthenticationChallenge(
  122. /* [in] */ IWebURLAuthenticationChallenge* challenge)
  123. {
  124. notImplemented();
  125. return E_FAIL;
  126. }
  127. HRESULT STDMETHODCALLTYPE WebDownload::useCredential(
  128. /* [in] */ IWebURLCredential* credential,
  129. /* [in] */ IWebURLAuthenticationChallenge* challenge)
  130. {
  131. notImplemented();
  132. return E_FAIL;
  133. }
  134. void WebDownload::didReceiveResponse()
  135. {
  136. COMPtr<WebDownload> protect = this;
  137. if (m_delegate) {
  138. ResourceResponse response = m_download.getResponse();
  139. COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(response));
  140. m_delegate->didReceiveResponse(this, webResponse.get());
  141. String suggestedFilename = response.suggestedFilename();
  142. if (suggestedFilename.isEmpty())
  143. suggestedFilename = pathGetFileName(response.url().string());
  144. BString suggestedFilenameBSTR(suggestedFilename.characters(), suggestedFilename.length());
  145. m_delegate->decideDestinationWithSuggestedFilename(this, suggestedFilenameBSTR);
  146. }
  147. }
  148. void WebDownload::didReceiveDataOfLength(int size)
  149. {
  150. COMPtr<WebDownload> protect = this;
  151. if (m_delegate)
  152. m_delegate->didReceiveDataOfLength(this, size);
  153. }
  154. void WebDownload::didFinish()
  155. {
  156. COMPtr<WebDownload> protect = this;
  157. if (m_delegate)
  158. m_delegate->didFinish(this);
  159. }
  160. void WebDownload::didFail()
  161. {
  162. COMPtr<WebDownload> protect = this;
  163. if (m_delegate)
  164. m_delegate->didFailWithError(this, 0);
  165. }