Downloads in WK2 on Windows should write resume data to bundle
[WebKit-https.git] / Source / WebKit2 / WebProcess / Downloads / cf / DownloadCFNet.cpp
1 /*
2  * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "Download.h"
28
29 #include "DataReference.h"
30 #include "NotImplemented.h"
31
32 #pragma warning(push, 0)
33 #include <WebCore/DownloadBundle.h>
34 #include <WebCore/LoaderRunLoopCF.h>
35 #include <WebCore/ResourceError.h>
36 #include <WebCore/ResourceHandle.h>
37 #include <WebCore/ResourceResponse.h>
38 #pragma warning(pop)
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 // CFURLDownload Callbacks ----------------------------------------------------------------
45 static void didStartCallback(CFURLDownloadRef download, const void* clientInfo);
46 static CFURLRequestRef willSendRequestCallback(CFURLDownloadRef download, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void* clientInfo);
47 static void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef download, CFURLAuthChallengeRef challenge, const void* clientInfo);
48 static void didReceiveResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, const void* clientInfo);
49 static void willResumeWithResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, UInt64 startingByte, const void* clientInfo);
50 static void didReceiveDataCallback(CFURLDownloadRef download, CFIndex length, const void* clientInfo);
51 static Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef download, CFStringRef encodingType, const void* clientInfo);
52 static void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef download, CFStringRef objectName, const void* clientInfo);
53 static void didCreateDestinationCallback(CFURLDownloadRef download, CFURLRef path, const void* clientInfo);
54 static void didFinishCallback(CFURLDownloadRef download, const void* clientInfo);
55 static void didFailCallback(CFURLDownloadRef download, CFErrorRef error, const void* clientInfo);
56
57 void Download::start(WebPage*)
58 {
59     ASSERT(!m_download);
60
61     CFURLRequestRef cfRequest = m_request.cfURLRequest();
62
63     CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback, 
64                                   didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback, 
65                                   decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
66     m_download.adoptCF(CFURLDownloadCreate(0, cfRequest, &client));
67
68     // FIXME: Allow this to be changed by the client.
69     CFURLDownloadSetDeletesUponFailure(m_download.get(), false);
70
71     CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
72     CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
73 }
74
75 void Download::startWithHandle(WebPage*, ResourceHandle* handle, const ResourceRequest& initialRequest, const ResourceResponse& response)
76 {
77     ASSERT(!m_download);
78
79     CFURLConnectionRef connection = handle->connection();
80     if (!connection)
81         return;
82
83     CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback, 
84                                   didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
85                                   decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
86
87     m_download.adoptCF(CFURLDownloadCreateAndStartWithLoadingConnection(0, connection, initialRequest.cfURLRequest(), response.cfURLResponse(), &client));
88
89     // It is possible for CFURLDownloadCreateAndStartWithLoadingConnection() to fail if the passed in CFURLConnection is not in a "downloadable state"
90     // However, we should never hit that case
91     if (!m_download)
92         ASSERT_NOT_REACHED();
93
94     // The CFURLDownload either starts successfully and retains the CFURLConnection, 
95     // or it fails to creating and we have a now-useless connection with a dangling ref. 
96     // Either way, we need to release the connection to balance out ref counts
97     handle->releaseConnectionForDownload();
98     CFRelease(connection);
99 }
100
101 void Download::cancel()
102 {
103     ASSERT(m_download);
104     if (!m_download)
105         return;
106
107     CFURLDownloadSetDeletesUponFailure(m_download.get(), false);
108     CFURLDownloadCancel(m_download.get());
109
110     RetainPtr<CFDataRef> resumeData(AdoptCF, CFURLDownloadCopyResumeData(m_download.get()));
111     if (resumeData)
112         DownloadBundle::appendResumeData(resumeData.get(), m_bundlePath);
113
114     didCancel(CoreIPC::DataReference());
115 }
116
117 void Download::platformInvalidate()
118 {
119     m_download = nullptr;
120 }
121
122 // CFURLDownload Callbacks ----------------------------------------------------------------
123 static Download* downloadFromClientInfo(const void* clientInfo)
124 {
125     return reinterpret_cast<Download*>(const_cast<void*>(clientInfo));
126 }
127
128 void didStartCallback(CFURLDownloadRef, const void* clientInfo)
129
130     downloadFromClientInfo(clientInfo)->didStart(); 
131 }
132
133 CFURLRequestRef willSendRequestCallback(CFURLDownloadRef, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void* clientInfo)
134
135     // CFNetwork requires us to return a retained request.
136     CFRetain(request);
137     return request;
138 }
139
140 void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef, CFURLAuthChallengeRef challenge, const void* clientInfo)
141
142     // FIXME: implement.
143     notImplemented();
144 }
145
146 void didReceiveResponseCallback(CFURLDownloadRef, CFURLResponseRef response, const void* clientInfo)
147 {
148     downloadFromClientInfo(clientInfo)->didReceiveResponse(ResourceResponse(response)); 
149 }
150
151 void willResumeWithResponseCallback(CFURLDownloadRef, CFURLResponseRef response, UInt64 startingByte, const void* clientInfo)
152
153     // FIXME: implement.
154     notImplemented();
155 }
156
157 void didReceiveDataCallback(CFURLDownloadRef, CFIndex length, const void* clientInfo)
158
159     downloadFromClientInfo(clientInfo)->didReceiveData(length);
160 }
161
162 Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef, CFStringRef encodingType, const void* clientInfo)
163
164     return downloadFromClientInfo(clientInfo)->shouldDecodeSourceDataOfMIMEType(encodingType);
165 }
166
167 void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef, CFStringRef objectName, const void* clientInfo)
168
169     Download* download = downloadFromClientInfo(clientInfo);
170     bool allowOverwrite;
171     download->decideDestinationWithSuggestedFilename(objectName, allowOverwrite);
172 }
173
174 void didCreateDestinationCallback(CFURLDownloadRef, CFURLRef, const void* clientInfo)
175 {
176     // The concept of the ".download bundle" is internal to the Download, so we try to hide its
177     // existence by reporting the final destination was created, when in reality the bundle was created.
178
179     Download* download = downloadFromClientInfo(clientInfo);
180     download->didCreateDestination(download->destination());
181 }
182
183 void Download::didDecideDestination(const String& destination, bool allowOverwrite)
184 {
185     ASSERT(!destination.isEmpty());
186     if (destination.isEmpty())
187         return;
188
189     m_destination = destination;
190     m_bundlePath = destination + DownloadBundle::fileExtension();
191
192     RetainPtr<CFStringRef> bundlePath(AdoptCF, CFStringCreateWithCharactersNoCopy(0, reinterpret_cast<const UniChar*>(m_bundlePath.characters()), m_bundlePath.length(), kCFAllocatorNull));
193     RetainPtr<CFURLRef> bundlePathURL(AdoptCF, CFURLCreateWithFileSystemPath(0, bundlePath.get(), kCFURLWindowsPathStyle, false));
194     CFURLDownloadSetDestination(m_download.get(), bundlePathURL.get(), allowOverwrite);
195 }
196
197 void didFinishCallback(CFURLDownloadRef, const void* clientInfo)
198
199     downloadFromClientInfo(clientInfo)->didFinish();
200 }
201
202 void didFailCallback(CFURLDownloadRef, CFErrorRef error, const void* clientInfo)
203
204     CoreIPC::DataReference dataReference(0, 0);
205     downloadFromClientInfo(clientInfo)->didFail(ResourceError(error), dataReference);
206 }
207
208 } // namespace WebKit