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