[iOS] Make WebKit2 build with public iOS SDK and more build fixes for DRT
[WebKit-https.git] / Source / WebKit2 / Shared / Downloads / ios / DownloadIOS.mm
1 /*
2  * Copyright (C) 2012 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 #import "config.h"
27 #import "Download.h"
28
29 #if PLATFORM(IOS)
30
31 #import "DataReference.h"
32 #import <WebCore/NotImplemented.h>
33 #import <WebCore/ResourceError.h>
34 #import <WebCore/ResourceHandle.h>
35 #import <WebCore/ResourceResponse.h>
36 #import <wtf/RetainPtr.h>
37 #import <wtf/RunLoop.h>
38
39 #if USE(CFNETWORK)
40 #import <CFNetwork/CFURLDownload.h>
41 #endif
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 #if USE(CFNETWORK)
48 // FIXME: If possible, we should consider moving some callbacks off the main thread or at least
49 // making them asynchonous calls.
50 static void dispatchOnMainThread(void(^block)())
51 {
52     if (RunLoop::isMain()) {
53         block();
54         return;
55     }
56
57     dispatch_sync(dispatch_get_main_queue(), block);
58 }
59
60 static inline Download* toDownload(const void* clientInfo)
61 {
62     return static_cast<Download*>(const_cast<void*>(clientInfo));
63 }
64
65 static void setUpDownloadClient(CFURLDownloadClient& client, Download& download)
66 {
67     memset(&client, 0, sizeof(client));
68     client.clientInfo = &download;
69
70     client.didStart = [](CFURLDownloadRef, const void* clientInfo) {
71         dispatchOnMainThread(^{
72             toDownload(clientInfo)->didStart();
73         });
74     };
75
76     client.willSendRequest = [](CFURLDownloadRef, CFURLRequestRef request, CFURLResponseRef, const void*) -> CFURLRequestRef {
77         return static_cast<CFURLRequestRef>(CFRetain(request));
78     };
79
80     client.didReceiveResponse = [](CFURLDownloadRef, CFURLResponseRef response, const void* clientInfo) {
81         dispatchOnMainThread(^{
82             toDownload(clientInfo)->didReceiveResponse(response);
83         });
84     };
85
86     client.didReceiveData = [](CFURLDownloadRef, CFIndex length, const void* clientInfo) {
87         dispatchOnMainThread(^{
88             toDownload(clientInfo)->didReceiveData(length);
89         });
90     };
91
92     client.shouldDecodeDataOfMIMEType = [](CFURLDownloadRef, CFStringRef encodingType, const void* clientInfo) -> Boolean {
93         __block BOOL returnValue = NO;
94         dispatchOnMainThread(^{
95             returnValue = toDownload(clientInfo)->shouldDecodeSourceDataOfMIMEType(encodingType);
96         });
97         return returnValue;
98     };
99
100     client.decideDestinationWithSuggestedObjectName = [](CFURLDownloadRef downloadRef, CFStringRef objectName, const void* clientInfo) {
101         dispatchOnMainThread(^{
102             bool allowOverwrite;
103             String destination = toDownload(clientInfo)->decideDestinationWithSuggestedFilename(objectName, allowOverwrite);
104             if (!destination.isNull())
105                 CFURLDownloadSetDestination(downloadRef, reinterpret_cast<CFURLRef>([NSURL fileURLWithPath:destination]), allowOverwrite);
106         });
107     };
108
109     client.didCreateDestination = [](CFURLDownloadRef, CFURLRef path, const void* clientInfo) {
110         dispatchOnMainThread(^{
111             toDownload(clientInfo)->didCreateDestination(CFURLGetString(path));
112         });
113     };
114
115     client.didFinish = [](CFURLDownloadRef, const void* clientInfo) {
116         dispatchOnMainThread(^{
117             toDownload(clientInfo)->didFinish();
118         });
119     };
120
121     client.didFail = [](CFURLDownloadRef downloadRef, CFErrorRef error, const void* clientInfo) {
122         dispatchOnMainThread(^{
123             auto resumeData = adoptCF(CFURLDownloadCopyResumeData(downloadRef));
124             toDownload(clientInfo)->didFail(error, IPC::DataReference(resumeData ? CFDataGetBytePtr(resumeData.get()) : nullptr, resumeData ? CFDataGetLength(resumeData.get()) : 0));
125         });
126     };
127 }
128 #endif // USE(CFNETWORK)
129
130 void Download::start()
131 {
132     notImplemented();
133 }
134
135 void Download::resume(const IPC::DataReference&, const String&, const SandboxExtension::Handle&)
136 {
137     notImplemented();
138 }
139
140 void Download::startWithHandle(ResourceHandle* handle, const ResourceResponse& response)
141 {
142 #if USE(CFNETWORK)
143     CFURLDownloadClient client;
144     setUpDownloadClient(client, *this);
145     m_download = adoptCF(CFURLDownloadCreateAndStartWithLoadingConnection(NULL, handle->releaseConnectionForDownload().get(), m_request.cfURLRequest(UpdateHTTPBody), response.cfURLResponse(), &client));
146 #else
147     notImplemented();
148 #endif
149 }
150
151 void Download::cancel()
152 {
153     notImplemented();
154 }
155
156 void Download::platformInvalidate()
157 {
158     notImplemented();
159 }
160
161 void Download::didDecideDestination(const String&, bool)
162 {
163     notImplemented();
164 }
165
166 void Download::platformDidFinish()
167 {
168     notImplemented();
169 }
170
171 void Download::receivedCredential(const AuthenticationChallenge&, const Credential&)
172 {
173     notImplemented();
174 }
175
176 void Download::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&)
177 {
178     notImplemented();
179 }
180
181 void Download::receivedCancellation(const AuthenticationChallenge&)
182 {
183     notImplemented();
184 }
185
186 void Download::receivedRequestToPerformDefaultHandling(const WebCore::AuthenticationChallenge&)
187 {
188     notImplemented();
189 }
190
191 void Download::receivedChallengeRejection(const WebCore::AuthenticationChallenge&)
192 {
193     notImplemented();
194 }
195
196 } // namespace WebKit
197
198 #endif // PLATFORM(IOS)