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 "WebDownload.h"
29
30 #include "DefaultDownloadDelegate.h"
31 #include "MarshallingHelpers.h"
32 #include "WebError.h"
33 #include "WebKit.h"
34 #include "WebKitLogging.h"
35 #include "WebMutableURLRequest.h"
36 #include "WebURLAuthenticationChallenge.h"
37 #include "WebURLCredential.h"
38 #include "WebURLResponse.h"
39 #include <wtf/text/CString.h>
40
41 #include <WebCore/BString.h>
42 #include <WebCore/DownloadBundle.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 #include <wtf/CurrentTime.h>
49 #include <wtf/StdLibExtras.h>
50
51 using namespace WebCore;
52
53 // WebDownload ----------------------------------------------------------------
54
WebDownload()55 WebDownload::WebDownload()
56 : m_refCount(0)
57 {
58 gClassCount++;
59 gClassNameCount.add("WebDownload");
60 }
61
~WebDownload()62 WebDownload::~WebDownload()
63 {
64 LOG(Download, "WebDownload - Destroying download (%p)", this);
65 cancel();
66 gClassCount--;
67 gClassNameCount.remove("WebDownload");
68 }
69
createInstance()70 WebDownload* WebDownload::createInstance()
71 {
72 WebDownload* instance = new WebDownload();
73 instance->AddRef();
74 return instance;
75 }
76
createInstance(ResourceHandle * handle,const ResourceRequest & request,const ResourceResponse & response,IWebDownloadDelegate * delegate)77 WebDownload* WebDownload::createInstance(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
78 {
79 WebDownload* instance = new WebDownload();
80 instance->AddRef();
81 instance->init(handle, request, response, delegate);
82 return instance;
83 }
84
createInstance(const KURL & url,IWebDownloadDelegate * delegate)85 WebDownload* WebDownload::createInstance(const KURL& url, IWebDownloadDelegate* delegate)
86 {
87 WebDownload* instance = new WebDownload();
88 instance->AddRef();
89 instance->init(url, delegate);
90 return instance;
91 }
92
93 // IUnknown -------------------------------------------------------------------
94
QueryInterface(REFIID riid,void ** ppvObject)95 HRESULT STDMETHODCALLTYPE WebDownload::QueryInterface(REFIID riid, void** ppvObject)
96 {
97 *ppvObject = 0;
98 if (IsEqualGUID(riid, IID_IUnknown))
99 *ppvObject = static_cast<IWebDownload*>(this);
100 else if (IsEqualGUID(riid, IID_IWebDownload))
101 *ppvObject = static_cast<IWebDownload*>(this);
102 else if (IsEqualGUID(riid, IID_IWebURLAuthenticationChallengeSender))
103 *ppvObject = static_cast<IWebURLAuthenticationChallengeSender*>(this);
104 else if (IsEqualGUID(riid, CLSID_WebDownload))
105 *ppvObject = static_cast<WebDownload*>(this);
106 else
107 return E_NOINTERFACE;
108
109 AddRef();
110 return S_OK;
111 }
112
AddRef(void)113 ULONG STDMETHODCALLTYPE WebDownload::AddRef(void)
114 {
115 return ++m_refCount;
116 }
117
Release(void)118 ULONG STDMETHODCALLTYPE WebDownload::Release(void)
119 {
120 ULONG newRef = --m_refCount;
121 if (!newRef)
122 delete(this);
123
124 return newRef;
125 }
126
127 // IWebDownload -------------------------------------------------------------------
128
canResumeDownloadDecodedWithEncodingMIMEType(BSTR,BOOL *)129 HRESULT STDMETHODCALLTYPE WebDownload::canResumeDownloadDecodedWithEncodingMIMEType(
130 /* [in] */ BSTR,
131 /* [out, retval] */ BOOL*)
132 {
133 notImplemented();
134 return E_FAIL;
135 }
136
bundlePathForTargetPath(BSTR targetPath,BSTR * bundlePath)137 HRESULT STDMETHODCALLTYPE WebDownload::bundlePathForTargetPath(
138 /* [in] */ BSTR targetPath,
139 /* [out, retval] */ BSTR* bundlePath)
140 {
141 if (!targetPath)
142 return E_INVALIDARG;
143
144 String bundle(targetPath, SysStringLen(targetPath));
145 if (bundle.isEmpty())
146 return E_INVALIDARG;
147
148 if (bundle[bundle.length()-1] == '/')
149 bundle.truncate(1);
150
151 bundle += DownloadBundle::fileExtension();
152 *bundlePath = SysAllocStringLen(bundle.characters(), bundle.length());
153 if (!*bundlePath)
154 return E_FAIL;
155 return S_OK;
156 }
157
request(IWebURLRequest ** request)158 HRESULT STDMETHODCALLTYPE WebDownload::request(
159 /* [out, retval] */ IWebURLRequest** request)
160 {
161 if (request) {
162 *request = m_request.get();
163 if (*request)
164 (*request)->AddRef();
165 }
166 return S_OK;
167 }
168