objc2_foundation/generated/NSURLDownload.rs
1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6
7use crate::*;
8
9extern_class!(
10 /// A NSURLDownload loads a request and saves the downloaded data to a file. The progress of the download
11 /// is reported via the NSURLDownloadDelegate protocol. Note: The word "download" is used to refer to the process
12 /// of loading data off a network, decoding the data if necessary and saving the data to a file.
13 ///
14 /// See also [Apple's documentation](https://developer.apple.com/documentation/foundation/nsurldownload?language=objc)
15 #[unsafe(super(NSObject))]
16 #[derive(Debug, PartialEq, Eq, Hash)]
17 pub struct NSURLDownload;
18);
19
20extern_conformance!(
21 unsafe impl NSObjectProtocol for NSURLDownload {}
22);
23
24impl NSURLDownload {
25 extern_methods!(
26 #[cfg(feature = "NSString")]
27 /// Returns whether or not NSURLDownload can resume a download that was decoded with a given encoding MIME type.
28 ///
29 /// Parameter `MIMEType`: The encoding MIME type.
30 /// canResumeDownloadDecodedWithEncodingMIMEType: returns whether or not NSURLDownload can resume a download
31 /// that was decoded with a given encoding MIME type. NSURLDownload cannot resume a download that was partially decoded
32 /// in the gzip format for example. In order to ensure that a download can be later resumed,
33 /// canResumeDownloadDecodedWithEncodingMIMEType: should be used when download:shouldDecodeSourceDataOfMIMEType: is called.
34 #[unsafe(method(canResumeDownloadDecodedWithEncodingMIMEType:))]
35 #[unsafe(method_family = none)]
36 pub fn canResumeDownloadDecodedWithEncodingMIMEType(mime_type: &NSString) -> bool;
37
38 #[cfg(feature = "NSURLRequest")]
39 /// Initializes a NSURLDownload object and starts the download.
40 ///
41 /// Parameter `request`: The request to download. Must not be nil.
42 ///
43 /// Parameter `delegate`: The delegate of the download.
44 ///
45 /// Returns: An initialized NSURLDownload object.
46 #[deprecated = "Use NSURLSession downloadTask (see NSURLSession.h)"]
47 #[unsafe(method(initWithRequest:delegate:))]
48 #[unsafe(method_family = init)]
49 pub fn initWithRequest_delegate(
50 this: Allocated<Self>,
51 request: &NSURLRequest,
52 delegate: Option<&ProtocolObject<dyn NSURLDownloadDelegate>>,
53 ) -> Retained<Self>;
54
55 #[cfg(all(feature = "NSData", feature = "NSString"))]
56 /// Initializes a NSURLDownload object for resuming a previous download.
57 ///
58 /// Parameter `resumeData`: The resume data from the previous download.
59 ///
60 /// Parameter `delegate`: The delegate of the download.
61 ///
62 /// Parameter `path`: The path of the incomplete downloaded file.
63 ///
64 /// Returns: An initialized NSURLDownload object.
65 #[deprecated = "Use NSURLSession downloadTask (see NSURLSession.h)"]
66 #[unsafe(method(initWithResumeData:delegate:path:))]
67 #[unsafe(method_family = init)]
68 pub fn initWithResumeData_delegate_path(
69 this: Allocated<Self>,
70 resume_data: &NSData,
71 delegate: Option<&ProtocolObject<dyn NSURLDownloadDelegate>>,
72 path: &NSString,
73 ) -> Retained<Self>;
74
75 /// Cancels the download and deletes the downloaded file.
76 #[unsafe(method(cancel))]
77 #[unsafe(method_family = none)]
78 pub fn cancel(&self);
79
80 #[cfg(feature = "NSString")]
81 /// Sets the destination path of the downloaded file.
82 ///
83 /// Parameter `path`: The destination path of the downloaded file.
84 ///
85 /// Parameter `allowOverwrite`: Allows a file of the same path to be overwritten.
86 ///
87 /// This method can be called after the download is created or in response to the
88 /// decideDestinationWithSuggestedFilename: delegate method. It should only be called once.
89 /// If NO is passed for allowOverwrite and a file of the same path exists, a number will be
90 /// appended to the filename to prevent the overwrite. Because of this, use the path
91 /// passed with didCreateDestination: to determine the actual path of the downloaded file.
92 #[unsafe(method(setDestination:allowOverwrite:))]
93 #[unsafe(method_family = none)]
94 pub fn setDestination_allowOverwrite(&self, path: &NSString, allow_overwrite: bool);
95
96 #[cfg(feature = "NSURLRequest")]
97 /// Returns the request of the download.
98 ///
99 /// Returns: The request of the download.
100 #[unsafe(method(request))]
101 #[unsafe(method_family = none)]
102 pub fn request(&self) -> Retained<NSURLRequest>;
103
104 #[cfg(feature = "NSData")]
105 /// Returns the resume data of a download that is incomplete.
106 ///
107 /// Returns: The resume data.
108 /// resumeData returns the resume data of a download that is incomplete. This data represents the necessary
109 /// state information that NSURLDownload needs to resume a download. The resume data can later be used when initializing
110 /// a download with initWithResumeData:delegate:path:. Non-nil is returned if resuming the download seems possible.
111 /// Non-nil is returned if the download was cancelled or ended in error after some but not all data has been received.
112 /// The protocol of the download as well as the server must support resuming for non-nil to be returned.
113 /// In order to later resume a download, be sure to call setDeletesFileUponFailure: with NO.
114 #[unsafe(method(resumeData))]
115 #[unsafe(method_family = none)]
116 pub fn resumeData(&self) -> Option<Retained<NSData>>;
117
118 /// Sets whether or not the downloaded file should be deleted upon failure.
119 /// 1
120 /// To allow the download to be resumed in case the download ends prematurely,
121 /// deletesFileUponFailure must be set to NO as soon as possible to prevent the downloaded file
122 /// from being deleted. deletesFileUponFailure is YES by default.
123 #[unsafe(method(deletesFileUponFailure))]
124 #[unsafe(method_family = none)]
125 pub fn deletesFileUponFailure(&self) -> bool;
126
127 /// Setter for [`deletesFileUponFailure`][Self::deletesFileUponFailure].
128 #[unsafe(method(setDeletesFileUponFailure:))]
129 #[unsafe(method_family = none)]
130 pub fn setDeletesFileUponFailure(&self, deletes_file_upon_failure: bool);
131 );
132}
133
134/// Methods declared on superclass `NSObject`.
135impl NSURLDownload {
136 extern_methods!(
137 #[unsafe(method(init))]
138 #[unsafe(method_family = init)]
139 pub fn init(this: Allocated<Self>) -> Retained<Self>;
140
141 #[unsafe(method(new))]
142 #[unsafe(method_family = new)]
143 pub fn new() -> Retained<Self>;
144 );
145}
146
147impl DefaultRetained for NSURLDownload {
148 #[inline]
149 fn default_retained() -> Retained<Self> {
150 Self::new()
151 }
152}
153
154extern_protocol!(
155 /// The NSURLDownloadDelegate delegate is used to report the progress of the download.
156 ///
157 /// See also [Apple's documentation](https://developer.apple.com/documentation/foundation/nsurldownloaddelegate?language=objc)
158 pub unsafe trait NSURLDownloadDelegate: NSObjectProtocol {
159 /// This method is called immediately after the download has started.
160 ///
161 /// Parameter `download`: The download that just started downloading.
162 #[optional]
163 #[unsafe(method(downloadDidBegin:))]
164 #[unsafe(method_family = none)]
165 fn downloadDidBegin(&self, download: &NSURLDownload);
166
167 #[cfg(all(feature = "NSURLRequest", feature = "NSURLResponse"))]
168 /// This method is called if the download must load another request because the previous
169 /// request was redirected.
170 ///
171 /// Parameter `download`: The download that will send the request.
172 ///
173 /// Parameter `request`: The request that will be used to continue loading.
174 ///
175 /// Returns: The request to be used; either the request parameter or a replacement. If nil is returned,
176 /// the download is cancelled.
177 ///
178 /// This method gives the delegate an opportunity to inspect the request
179 /// that will be used to continue loading the request, and modify it if necessary.
180 #[optional]
181 #[unsafe(method(download:willSendRequest:redirectResponse:))]
182 #[unsafe(method_family = none)]
183 fn download_willSendRequest_redirectResponse(
184 &self,
185 download: &NSURLDownload,
186 request: &NSURLRequest,
187 redirect_response: Option<&NSURLResponse>,
188 ) -> Option<Retained<NSURLRequest>>;
189
190 #[cfg(feature = "NSURLProtectionSpace")]
191 /// This method gives the delegate an opportunity to inspect an NSURLProtectionSpace before an authentication attempt is made.
192 ///
193 /// If implemented, will be called before connection:didReceiveAuthenticationChallenge
194 /// to give the delegate a chance to inspect the protection space that will be authenticated against. Delegates should determine
195 /// if they are prepared to respond to the authentication method of the protection space and if so, return YES, or NO to
196 /// allow default processing to handle the authentication. If this delegate is not implemented, then default
197 /// processing will occur (typically, consulting
198 /// the user's keychain and/or failing the connection attempt.
199 ///
200 /// Parameter `connection`: an NSURLConnection that has an NSURLProtectionSpace ready for inspection
201 ///
202 /// Parameter `protectionSpace`: an NSURLProtectionSpace that will be used to generate an authentication challenge
203 ///
204 /// Returns: a boolean value that indicates the willingness of the delegate to handle the authentication
205 #[optional]
206 #[unsafe(method(download:canAuthenticateAgainstProtectionSpace:))]
207 #[unsafe(method_family = none)]
208 fn download_canAuthenticateAgainstProtectionSpace(
209 &self,
210 connection: &NSURLDownload,
211 protection_space: &NSURLProtectionSpace,
212 ) -> bool;
213
214 #[cfg(feature = "NSURLAuthenticationChallenge")]
215 /// Start authentication for a given challenge
216 ///
217 /// Parameter `download`: The download that needs authentication.
218 ///
219 /// Parameter `challenge`: The NSURLAuthenticationChallenge for which to start authentication.
220 ///
221 /// Call useCredential:forAuthenticationChallenge:,
222 /// continueWithoutCredentialForAuthenticationChallenge: or cancel on
223 /// the connection sender when done.
224 #[optional]
225 #[unsafe(method(download:didReceiveAuthenticationChallenge:))]
226 #[unsafe(method_family = none)]
227 fn download_didReceiveAuthenticationChallenge(
228 &self,
229 download: &NSURLDownload,
230 challenge: &NSURLAuthenticationChallenge,
231 );
232
233 #[cfg(feature = "NSURLAuthenticationChallenge")]
234 /// Cancel authentication for a given request
235 ///
236 /// Parameter `download`: The download that's cancelling
237 ///
238 /// Parameter `challenge`: The NSURLAuthenticationChallenge to cancel authentication for
239 #[optional]
240 #[unsafe(method(download:didCancelAuthenticationChallenge:))]
241 #[unsafe(method_family = none)]
242 fn download_didCancelAuthenticationChallenge(
243 &self,
244 download: &NSURLDownload,
245 challenge: &NSURLAuthenticationChallenge,
246 );
247
248 /// This method allows the delegate to inform the url loader that it
249 /// should not consult the credential storage for the download.
250 ///
251 /// This method will be called before any attempt to authenticate is
252 /// attempted on a download. By returning NO the delegate is telling the
253 /// download to not consult the credential storage and taking responsibility
254 /// for providing any credentials for authentication challenges. Not implementing
255 /// this method is the same as returning YES. The delegate is free to consult the
256 /// credential storage itself when it receives a didReceiveAuthenticationChallenge
257 /// message.
258 ///
259 /// Parameter `download`: the NSURLDownload object asking if it should consult the credential storage.
260 ///
261 /// Returns: NO if the download should not consult the credential storage, Yes if it should.
262 #[optional]
263 #[unsafe(method(downloadShouldUseCredentialStorage:))]
264 #[unsafe(method_family = none)]
265 fn downloadShouldUseCredentialStorage(&self, download: &NSURLDownload) -> bool;
266
267 #[cfg(feature = "NSURLResponse")]
268 /// This method is called when the download has received a response from the server.
269 ///
270 /// Parameter `download`: The download that now has a NSURLResponse available for inspection.
271 ///
272 /// Parameter `response`: The NSURLResponse object for the given download.
273 ///
274 /// In some rare cases, multiple responses may be received for a single download.
275 /// This occurs with multipart/x-mixed-replace, or "server push". In this case, the client
276 /// should assume that each new response resets progress so far for the resource back to 0,
277 /// and should check the new response for the expected content length.
278 #[optional]
279 #[unsafe(method(download:didReceiveResponse:))]
280 #[unsafe(method_family = none)]
281 fn download_didReceiveResponse(&self, download: &NSURLDownload, response: &NSURLResponse);
282
283 #[cfg(feature = "NSURLResponse")]
284 /// This method is called when the download has received a response from the server after attempting to
285 /// resume a download.
286 ///
287 /// Parameter `download`: The download that now has a NSURLResponse available for inspection.
288 ///
289 /// Parameter `response`: The NSURLResponse object for the given download.
290 ///
291 /// Parameter `startingByte`: The number of bytes from where the download will resume. 0 indicates that the download will
292 /// restart from the beginning.
293 ///
294 /// download:willResumeWithResponse:fromByte: is called instead of download:didReceiveResponse:
295 /// when a download is initialized with initWithResumeData:delegate:path:.
296 #[optional]
297 #[unsafe(method(download:willResumeWithResponse:fromByte:))]
298 #[unsafe(method_family = none)]
299 fn download_willResumeWithResponse_fromByte(
300 &self,
301 download: &NSURLDownload,
302 response: &NSURLResponse,
303 starting_byte: c_longlong,
304 );
305
306 /// This method is called when the download has loaded data.
307 ///
308 /// Parameter `download`: The download that has received data.
309 ///
310 /// Parameter `length`: The length of the received data.
311 ///
312 /// This method will be called one or more times.
313 #[optional]
314 #[unsafe(method(download:didReceiveDataOfLength:))]
315 #[unsafe(method_family = none)]
316 fn download_didReceiveDataOfLength(&self, download: &NSURLDownload, length: NSUInteger);
317
318 #[cfg(feature = "NSString")]
319 /// This method is called if the download detects that the downloading file is encoded.
320 ///
321 /// Parameter `download`: The download that has detected that the downloading file is encoded.
322 ///
323 /// Parameter `encodingType`: A MIME type expressing the encoding type.
324 ///
325 /// Returns: Return YES to decode the file, NO to not decode the file.
326 ///
327 /// An encoded file is encoded in MacBinary, BinHex or gzip format. This method may be
328 /// called more than once if the file is encoded multiple times. This method is not called if the
329 /// download is not encoded.
330 #[optional]
331 #[unsafe(method(download:shouldDecodeSourceDataOfMIMEType:))]
332 #[unsafe(method_family = none)]
333 fn download_shouldDecodeSourceDataOfMIMEType(
334 &self,
335 download: &NSURLDownload,
336 encoding_type: &NSString,
337 ) -> bool;
338
339 #[cfg(feature = "NSString")]
340 /// This method is called when enough information has been loaded to decide a destination
341 /// for the downloaded file.
342 ///
343 /// Parameter `download`: The download that requests the download path.
344 ///
345 /// Parameter `filename`: The suggested filename for deciding the path of the downloaded file. The filename is either
346 /// derived from the last path component of the URL and the MIME type or if the download was encoded,
347 /// it is the filename specified in the encoding.
348 ///
349 /// Once the delegate has decided a path, it should call setDestination:allowOverwrite:.
350 /// The delegate may respond immediately or later. This method is not called if
351 /// setDestination:allowOverwrite: has already been called.
352 #[optional]
353 #[unsafe(method(download:decideDestinationWithSuggestedFilename:))]
354 #[unsafe(method_family = none)]
355 fn download_decideDestinationWithSuggestedFilename(
356 &self,
357 download: &NSURLDownload,
358 filename: &NSString,
359 );
360
361 #[cfg(feature = "NSString")]
362 /// This method is called after the download creates the downloaded file.
363 ///
364 /// Parameter `download`: The download that created the downloaded file.
365 ///
366 /// Parameter `path`: The path of the downloaded file.
367 #[optional]
368 #[unsafe(method(download:didCreateDestination:))]
369 #[unsafe(method_family = none)]
370 fn download_didCreateDestination(&self, download: &NSURLDownload, path: &NSString);
371
372 /// This method is called when the download has finished downloading.
373 ///
374 /// Parameter `download`: The download that has finished downloading.
375 ///
376 /// This method is called after all the data has been received and written to disk.
377 /// This method or download:didFailWithError: will only be called once.
378 #[optional]
379 #[unsafe(method(downloadDidFinish:))]
380 #[unsafe(method_family = none)]
381 fn downloadDidFinish(&self, download: &NSURLDownload);
382
383 #[cfg(feature = "NSError")]
384 /// This method is called when the download has failed.
385 ///
386 /// Parameter `download`: The download that ended in error.
387 ///
388 /// Parameter `error`: The error caused the download to fail.
389 ///
390 /// This method is called when the download encounters a network or file I/O related error.
391 /// This method or downloadDidFinish: will only be called once.
392 #[optional]
393 #[unsafe(method(download:didFailWithError:))]
394 #[unsafe(method_family = none)]
395 fn download_didFailWithError(&self, download: &NSURLDownload, error: &NSError);
396 }
397);