PageRenderTime 24ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/development/fontCache/WebKit/win/DefaultDownloadDelegate.cpp

https://github.com/weissms/owb-mirror
C++ | 243 lines | 197 code | 20 blank | 26 comment | 10 complexity | 3cd655d00f2762bda7e86660d63ecac3 MD5 | raw file
  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 "DefaultDownloadDelegate.h"
  28. #include "MarshallingHelpers.h"
  29. #include "WebKit.h"
  30. #include "WebKitLogging.h"
  31. #include "WebMutableURLRequest.h"
  32. #include <shlobj.h>
  33. #include <tchar.h>
  34. #pragma warning(push, 0)
  35. #include <WebCore/BString.h>
  36. #pragma warning(pop)
  37. using namespace WebCore;
  38. // DefaultDownloadDelegate ----------------------------------------------------------------
  39. DefaultDownloadDelegate::DefaultDownloadDelegate()
  40. : m_refCount(0)
  41. {
  42. gClassCount++;
  43. }
  44. DefaultDownloadDelegate::~DefaultDownloadDelegate()
  45. {
  46. gClassCount--;
  47. HashSet<IWebDownload*>::iterator i = m_downloads.begin();
  48. for (;i != m_downloads.end(); ++i)
  49. (*i)->Release();
  50. }
  51. DefaultDownloadDelegate* DefaultDownloadDelegate::sharedInstance()
  52. {
  53. static COMPtr<DefaultDownloadDelegate> shared;
  54. if (!shared)
  55. shared.adoptRef(DefaultDownloadDelegate::createInstance());
  56. return shared.get();
  57. }
  58. DefaultDownloadDelegate* DefaultDownloadDelegate::createInstance()
  59. {
  60. DefaultDownloadDelegate* instance = new DefaultDownloadDelegate();
  61. instance->AddRef();
  62. return instance;
  63. }
  64. // IUnknown -------------------------------------------------------------------
  65. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::QueryInterface(REFIID riid, void** ppvObject)
  66. {
  67. *ppvObject = 0;
  68. if (IsEqualGUID(riid, IID_IUnknown))
  69. *ppvObject = static_cast<IUnknown*>(this);
  70. else if (IsEqualGUID(riid, IID_IWebDownloadDelegate))
  71. *ppvObject = static_cast<IWebDownloadDelegate*>(this);
  72. else
  73. return E_NOINTERFACE;
  74. AddRef();
  75. return S_OK;
  76. }
  77. ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::AddRef()
  78. {
  79. return ++m_refCount;
  80. }
  81. ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::Release()
  82. {
  83. ULONG newRef = --m_refCount;
  84. if (!newRef)
  85. delete(this);
  86. return newRef;
  87. }
  88. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::decideDestinationWithSuggestedFilename(IWebDownload *download, BSTR filename)
  89. {
  90. LOG(Download, "DefaultDownloadDelegate %p - decideDestinationWithSuggestedFilename %s", download, String(filename, SysStringLen(filename)).ascii().data());
  91. TCHAR pathChars[MAX_PATH];
  92. if (FAILED(SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, 0, 0, pathChars))) {
  93. if (FAILED(download->setDestination(filename, true))) {
  94. LOG_ERROR("Failed to set destination on file");
  95. return E_FAIL;
  96. }
  97. return S_OK;
  98. }
  99. size_t fullLength = _tcslen(pathChars) + SysStringLen(filename) + 2;
  100. BSTR full = SysAllocStringLen(0, (UINT)fullLength);
  101. if (!full)
  102. return E_OUTOFMEMORY;
  103. _tcscpy_s(full, fullLength, pathChars);
  104. _tcscat_s(full, fullLength, _T("\\"));
  105. _tcscat_s(full, fullLength, filename);
  106. BString fullPath;
  107. fullPath.adoptBSTR(full);
  108. #ifndef NDEBUG
  109. String debug((BSTR)fullPath, SysStringLen(BSTR(fullPath)));
  110. LOG(Download, "Setting path to %s", debug.ascii().data());
  111. #endif
  112. if (FAILED(download->setDestination(fullPath, true))) {
  113. LOG_ERROR("Failed to set destination on file");
  114. return E_FAIL;
  115. }
  116. return S_OK;
  117. }
  118. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCancelAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
  119. {
  120. LOG(Download, "DefaultDownloadDelegate %p - didCancelAuthenticationChallenge %p", download, challenge);
  121. download = 0;
  122. challenge = 0;
  123. return S_OK;
  124. }
  125. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCreateDestination(IWebDownload* download, BSTR destination)
  126. {
  127. LOG(Download, "DefaultDownloadDelegate %p - didCreateDestination %s", download, String(destination, SysStringLen(destination)).ascii().data());
  128. download = 0;
  129. destination = 0;
  130. return S_OK;
  131. }
  132. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
  133. {
  134. LOG(Download, "DefaultDownloadDelegate %p - didReceiveAuthenticationChallenge %p", download, challenge);
  135. download = 0;
  136. challenge = 0;
  137. return S_OK;
  138. }
  139. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveDataOfLength(IWebDownload* download, unsigned length)
  140. {
  141. LOG(Download, "DefaultDownloadDelegate %p - didReceiveDataOfLength %i", download, length);
  142. download = 0;
  143. length = 0;
  144. return S_OK;
  145. }
  146. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveResponse(IWebDownload* download, IWebURLResponse* response)
  147. {
  148. LOG(Download, "DefaultDownloadDelegate %p - didReceiveResponse %p", download, response);
  149. download = 0;
  150. response = 0;
  151. return S_OK;
  152. }
  153. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::shouldDecodeSourceDataOfMIMEType(IWebDownload* download, BSTR encodingType, BOOL* shouldDecode)
  154. {
  155. LOG(Download, "DefaultDownloadDelegate %p - shouldDecodeSourceDataOfMIMEType %s", download, String(encodingType, SysStringLen(encodingType)).ascii().data());
  156. download = 0;
  157. encodingType = 0;
  158. *shouldDecode = false;
  159. return S_OK;
  160. }
  161. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willResumeWithResponse(IWebDownload* download, IWebURLResponse* response, long long fromByte)
  162. {
  163. LOG(Download, "DefaultDownloadDelegate %p - willResumeWithResponse %p, %q", download, response, fromByte);
  164. download = 0;
  165. response = 0;
  166. fromByte = 0;
  167. return S_OK;
  168. }
  169. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willSendRequest(IWebDownload* download, IWebMutableURLRequest* request,
  170. IWebURLResponse* redirectResponse, IWebMutableURLRequest** finalRequest)
  171. {
  172. LOG(Download, "DefaultDownloadDelegate %p - willSendRequest %p %p", download, request, redirectResponse);
  173. download = 0;
  174. redirectResponse = 0;
  175. *finalRequest = request;
  176. return S_OK;
  177. }
  178. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didBegin(IWebDownload* download)
  179. {
  180. LOG(Download, "DefaultDownloadDelegate %p - didBegin", download);
  181. registerDownload(download);
  182. return S_OK;
  183. }
  184. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFinish(IWebDownload* download)
  185. {
  186. LOG(Download, "DefaultDownloadDelegate %p - didFinish", download);
  187. unregisterDownload(download);
  188. return S_OK;
  189. }
  190. HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFailWithError(IWebDownload* download, IWebError* error)
  191. {
  192. LOG(Download, "DefaultDownloadDelegate %p - didFailWithError %p", download, error);
  193. unregisterDownload(download);
  194. error = 0;
  195. return S_OK;
  196. }
  197. void DefaultDownloadDelegate::registerDownload(IWebDownload* download)
  198. {
  199. if (m_downloads.contains(download))
  200. return;
  201. download->AddRef();
  202. m_downloads.add(download);
  203. }
  204. void DefaultDownloadDelegate::unregisterDownload(IWebDownload* download)
  205. {
  206. if (m_downloads.contains(download)) {
  207. download->Release();
  208. m_downloads.remove(download);
  209. }
  210. }