2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Content.Download
24 /// The Request class provides the functions to create and manage a single download request.
26 /// <since_tizen> 3 </since_tizen>
27 public class Request : IDisposable
29 private int _downloadId;
30 private Notification _notificationProperties;
31 private IDictionary<string, string> _httpHeaders;
32 private EventHandler<StateChangedEventArgs> _downloadStateChanged;
33 private Interop.Download.StateChangedCallback _downloadStateChangedCallback;
34 private EventHandler<ProgressChangedEventArgs> _downloadProgressChanged;
35 private Interop.Download.ProgressChangedCallback _downloadProgressChangedCallback;
36 private bool _disposed = false;
39 /// Creates a Request object.
41 /// <since_tizen> 3 </since_tizen>
42 /// <param name="url">The URL to download.</param>
43 /// <privilege>http://tizen.org/privilege/download</privilege>
44 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
45 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
46 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
47 public Request(string url)
49 if (String.IsNullOrEmpty(url))
51 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
53 int ret = Interop.Download.CreateRequest(out _downloadId);
54 if (ret != (int)DownloadError.None)
56 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
58 ret = Interop.Download.SetUrl(_downloadId, url);
59 if (ret != (int)DownloadError.None)
61 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
63 _notificationProperties = new Notification(_downloadId);
64 _httpHeaders = new Dictionary<string, string>();
68 /// Creates a Request object.
70 /// <since_tizen> 3 </since_tizen>
71 /// <param name="url">The URL to download</param>
72 /// <param name="destinationPath">The directory path where downloaded file is stored.</param>
73 /// <param name="fileName">The name of the downloaded file.</param>
74 /// <param name="type">The network type which the download request must adhere to.</param>
75 /// <privilege>http://tizen.org/privilege/download</privilege>
76 /// <feature>http://tizen.org/feature/network.wifi</feature>
77 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
78 /// <feature>http://tizen.org/feature/network.telephony</feature>
79 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
80 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
81 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
82 /// <exception cref="NotSupportedException">Thrown when a feature is not supported.</exception>
83 public Request(string url, string destinationPath, string fileName, NetworkType type)
85 if (String.IsNullOrEmpty(url))
87 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
89 int ret = Interop.Download.CreateRequest(out _downloadId);
90 if (ret != (int)DownloadError.None)
92 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
95 ret = Interop.Download.SetUrl(_downloadId, url);
96 if (ret != (int)DownloadError.None)
98 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
101 ret = Interop.Download.SetDestination(_downloadId, destinationPath);
102 if (ret != (int)DownloadError.None)
104 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
107 ret = Interop.Download.SetFileName(_downloadId, fileName);
108 if (ret != (int)DownloadError.None)
110 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
113 ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
114 if (ret != (int)DownloadError.None)
116 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
119 _notificationProperties = new Notification(_downloadId);
120 _httpHeaders = new Dictionary<string, string>();
124 /// Creates a Request object.
126 /// <since_tizen> 3 </since_tizen>
127 /// <param name="url">The URL to download.</param>
128 /// <param name="destinationPath">The directory path where the downloaded file is stored.</param>
129 /// <param name="fileName">The name of the downloaded file.</param>
130 /// <param name="type">The network type which the download request must adhere to.</param>
131 /// <param name="httpHeaders">HTTP header fields for the download request.</param>
132 /// <privilege>http://tizen.org/privilege/download</privilege>
133 /// <feature>http://tizen.org/feature/network.wifi</feature>
134 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
135 /// <feature>http://tizen.org/feature/network.telephony</feature>
136 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
137 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
138 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
139 /// <exception cref="NotSupportedException">Thrown when a feature is not supported.</exception>
140 public Request(string url, string destinationPath, string fileName, NetworkType type, IDictionary<string, string> httpHeaders)
142 if (String.IsNullOrEmpty(url))
144 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
146 int ret = Interop.Download.CreateRequest(out _downloadId);
147 if (ret != (int)DownloadError.None)
149 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
152 ret = Interop.Download.SetUrl(_downloadId, url);
153 if (ret != (int)DownloadError.None)
155 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
158 ret = Interop.Download.SetDestination(_downloadId, destinationPath);
159 if (ret != (int)DownloadError.None)
161 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
164 ret = Interop.Download.SetFileName(_downloadId, fileName);
165 if (ret != (int)DownloadError.None)
167 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
170 ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
171 if (ret != (int)DownloadError.None)
173 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
176 _notificationProperties = new Notification(_downloadId);
177 _httpHeaders = httpHeaders;
181 /// Destructor of the Request class.
189 /// An event that occurs when the download state changes.
191 /// <since_tizen> 3 </since_tizen>
192 /// <privilege>http://tizen.org/privilege/download</privilege>
193 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
194 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
195 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
196 public event EventHandler<StateChangedEventArgs> StateChanged
200 if (_downloadStateChanged == null)
202 RegisterStateChangedEvent();
204 _downloadStateChanged += value;
208 _downloadStateChanged -= value;
209 if (_downloadStateChanged == null)
211 UnregisterStateChangedEvent();
217 /// An event that occurs when the download progress changes.
219 /// <since_tizen> 3 </since_tizen>
220 /// <privilege>http://tizen.org/privilege/download</privilege>
221 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
222 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
223 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
224 public event EventHandler<ProgressChangedEventArgs> ProgressChanged
228 if (_downloadProgressChanged == null)
230 RegisterProgressChangedEvent();
232 _downloadProgressChanged += value;
236 _downloadProgressChanged -= value;
237 if (_downloadProgressChanged == null)
239 UnregisterProgressChangedEvent();
245 /// The absolute path where the file will be downloaded.
246 /// If you try to get this property value before calling Start(), an empty string is returned.
248 /// <since_tizen> 3 </since_tizen>
249 /// <privilege>http://tizen.org/privilege/download</privilege>
251 /// Returns an empty string if the download is not completed or if a state has not yet changed to completed or if any other error occurs.
253 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
254 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
255 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
256 public string DownloadedPath
261 int ret = Interop.Download.GetDownloadedPath(_downloadId, out path);
262 if (ret != (int)DownloadError.None)
264 Log.Error(Globals.LogTag, "Failed to get DownloadedPath, " + (DownloadError)ret);
272 /// The MIME type of the downloaded content.
273 /// If you try to get this property value before calling Start(), an empty string is returned.
275 /// <since_tizen> 3 </since_tizen>
276 /// <privilege>http://tizen.org/privilege/download</privilege>
277 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
278 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
279 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
280 public string MimeType
285 int ret = Interop.Download.GetMimeType(_downloadId, out mime);
286 if (ret != (int)DownloadError.None)
288 Log.Error(Globals.LogTag, "Failed to get MimeType, " + (DownloadError)ret);
296 /// The current state of the download.
298 /// <since_tizen> 3 </since_tizen>
299 /// <privilege>http://tizen.org/privilege/download</privilege>
300 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
301 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
302 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
303 public DownloadState State
308 int ret = Interop.Download.GetState(_downloadId, out state);
309 if (ret != (int)DownloadError.None)
311 Log.Error(Globals.LogTag, "Failed to get DownloadState, " + (DownloadError)ret);
312 return DownloadState.None;
314 return (DownloadState)state;
319 /// The content name of the downloaded file.
320 /// This can be defined with reference of the HTTP response header data. The content name can be received when the HTTP response header is received.
321 /// If you try to get this property value before calling Start(), an empty string is returned.
323 /// <since_tizen> 3 </since_tizen>
324 /// <privilege>http://tizen.org/privilege/download</privilege>
325 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
326 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
327 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
328 public string ContentName
333 int ret = Interop.Download.GetContentName(_downloadId, out name);
334 if (ret != (int)DownloadError.None)
336 Log.Error(Globals.LogTag, "Failed to get ContentName, " + (DownloadError)ret);
344 /// The total size of the downloaded content.
345 /// This information is received from the server. If the server does not send the total size of the content, the content size is set to zero.
346 /// If you try to get this property value before calling Start(), 0 is returned.
348 /// <since_tizen> 3 </since_tizen>
349 /// <privilege>http://tizen.org/privilege/download</privilege>
350 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
351 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
352 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
353 public ulong ContentSize
358 int ret = Interop.Download.GetContentSize(_downloadId, out size);
359 if (ret != (int)DownloadError.None)
361 Log.Error(Globals.LogTag, "Failed to get ContentSize, " + (DownloadError)ret);
369 /// The HTTP status code when a download exception occurs.
370 /// If you try to get this property value before calling Start(), 0 is returned.
372 /// <since_tizen> 3 </since_tizen>
373 /// <privilege>http://tizen.org/privilege/download</privilege>
375 /// The state of the download request must be DownlodState.Failed.
377 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
378 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
379 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
380 public int HttpStatus
385 int ret = Interop.Download.GetHttpStatus(_downloadId, out status);
386 if (ret != (int)DownloadError.None)
388 Log.Error(Globals.LogTag, "Failed to get HttpStatus, " + (DownloadError)ret);
396 /// The ETag value from the HTTP response header when making a HTTP request for resume.
397 /// If you try to get this property value before calling Start() or if any other error occurs, an empty string is returned.
399 /// <since_tizen> 3 </since_tizen>
400 /// <privilege>http://tizen.org/privilege/download</privilege>
402 /// The ETag value is either available or not dependent on the web server. If not available, then, on getting the property, a null value is returned.
403 /// After the download is started, it can get the ETag value.
405 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
406 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation</exception>
407 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
408 public string ETagValue
413 int ret = Interop.Download.GetETag(_downloadId, out etag);
414 if (ret != (int)DownloadError.None)
416 Log.Error(Globals.LogTag, "Failed to get ETagValue, " + (DownloadError)ret);
424 /// Contains properties required for creating download notifications.
426 /// <since_tizen> 3 </since_tizen>
428 /// When the notification message is clicked, the action taken by the system is decided by the application control properties of the NotificationProperties instance.
429 /// If the app control is not set, the following default operation is executed when the notification message is clicked:
430 /// 1) The download completed state - the viewer application is executed according to the extension name of the downloaded content.
431 /// 2) The download failed state and ongoing state - the client application is executed.
432 /// This property should be set before calling Start().
434 public Notification NotificationProperties
438 return _notificationProperties;
443 /// The full path of the temporary file stores the downloaded content.
445 /// <since_tizen> 3 </since_tizen>
446 /// <privilege>http://tizen.org/privilege/download</privilege>
448 /// The download state must be one of the states after downloading.
450 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
451 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
452 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
453 public string TemporaryPath
458 int ret = Interop.Download.GetTempFilePath(_downloadId, out path);
459 if (ret != (int)DownloadError.None)
461 Log.Error(Globals.LogTag, "Failed to get TemporaryPath, " + (DownloadError)ret);
469 /// The URL to download.
471 /// <since_tizen> 3 </since_tizen>
472 /// <privilege>http://tizen.org/privilege/download</privilege>
474 /// Should be set before calling Start().
475 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
477 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
478 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
479 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
485 int ret = Interop.Download.GetUrl(_downloadId, out url);
486 if (ret != (int)DownloadError.None)
488 Log.Error(Globals.LogTag, "Failed to get Url, " + (DownloadError)ret);
495 int ret = Interop.Download.SetUrl(_downloadId, value);
496 if (ret != (int)DownloadError.None)
498 DownloadErrorFactory.ThrowException(ret, "Failed to set Url");
504 /// The allowed network type for downloading the file.
505 /// The file will be downloaded only under the allowed network.
506 /// If you try to get this property value before setting or if any other error occurs, the default value NetworkType All is returned.
508 /// <since_tizen> 3 </since_tizen>
509 /// <privilege>http://tizen.org/privilege/download</privilege>
510 /// <feature>http://tizen.org/feature/network.wifi</feature>
511 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
512 /// <feature>http://tizen.org/feature/network.telephony</feature>
514 /// Should be set before calling Start().
516 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
517 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
518 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
519 /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
520 public NetworkType AllowedNetworkType
525 int ret = Interop.Download.GetNetworkType(_downloadId, out type);
526 if (ret != (int)DownloadError.None)
528 Log.Error(Globals.LogTag, "Failed to get AllowedNetworkType, " + (DownloadError)ret);
529 return NetworkType.All;
531 return (NetworkType)type;
535 int ret = Interop.Download.SetNetworkType(_downloadId, (int)value);
536 if (ret != (int)DownloadError.None)
538 DownloadErrorFactory.ThrowException(ret, "Failed to set AllowedNetworkType");
544 /// The file will be downloaded to the set the destination file path. The downloaded file is saved to an auto-generated file name in the destination. If the destination is not specified, the file will be downloaded to the default storage.
545 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
547 /// <since_tizen> 3 </since_tizen>
548 /// <privilege>http://tizen.org/privilege/download</privilege>
550 /// Should be set before calling Start().
552 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
553 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
554 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
555 public string DestinationPath
560 int ret = Interop.Download.GetDestination(_downloadId, out path);
561 if (ret != (int)DownloadError.None)
563 Log.Error(Globals.LogTag, "Failed to get DestinationPath, " + (DownloadError)ret);
570 int ret = Interop.Download.SetDestination(_downloadId, value.ToString());
571 if (ret != (int)DownloadError.None)
573 DownloadErrorFactory.ThrowException(ret, "Failed to set DestinationPath");
579 /// The file will be saved in the specified destination or the default storage with the set file name. If the file name is not specified, the downloaded file will be saved with an auto-generated file name in the destination.
580 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
582 /// <since_tizen> 3 </since_tizen>
583 /// <privilege>http://tizen.org/privilege/download</privilege>
585 /// Should be set before calling Start().
587 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
588 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
589 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
590 public string FileName
595 int ret = Interop.Download.GetFileName(_downloadId, out name);
596 if (ret != (int)DownloadError.None)
598 Log.Error(Globals.LogTag, "Failed to get FileName, " + (DownloadError)ret);
605 int ret = Interop.Download.SetFileName(_downloadId, value.ToString());
606 if (ret != (int)DownloadError.None)
608 DownloadErrorFactory.ThrowException(ret, "Failed to set FileName");
614 /// Enables or disables auto download.
615 /// If this option is enabled, the previous downloading item is restarted automatically as soon as the download daemon is restarted. The download progress continues after the client process is terminated.
616 /// If you try to get this property value before setting, the default value false is returned.
618 /// <since_tizen> 3 </since_tizen>
619 /// <privilege>http://tizen.org/privilege/download</privilege>
621 /// The default value is false.
623 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
624 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
625 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
626 public bool AutoDownload
631 int ret = Interop.Download.GetAutoDownload(_downloadId, out value);
632 if (ret != (int)DownloadError.None)
634 Log.Error(Globals.LogTag, "Failed to get AutoDownload, " + (DownloadError)ret);
641 int ret = Interop.Download.SetAutoDownload(_downloadId, value);
642 if (ret != (int)DownloadError.None)
644 DownloadErrorFactory.ThrowException(ret, "Failed to set AutoDownload");
650 /// The HTTP header field and value pairs to the download request.
651 /// The HTTP header <field,value> pair is the <key,value> pair in the dictionary HttpHeaders.
652 /// The given HTTP header field will be included with the HTTP request of the download request.
653 /// If you try to get this property value before setting, an empty dictionary is returned.
655 /// <since_tizen> 3 </since_tizen>
657 /// HTTP header fields should be set before calling Start().
658 /// HTTP header fields can be removed before calling Start().
660 public IDictionary<string, string> HttpHeaders
669 /// Sets the directory path of a temporary file used in a previous download request.
670 /// This is only useful when resuming download to make the HTTP request header at the client side. Otherwise, the path is ignored.
672 /// <since_tizen> 3 </since_tizen>
673 /// <privilege>http://tizen.org/privilege/download</privilege>
675 /// If the ETag value is not present in the download database, it is not useful to set the temporary file path.
676 /// When resuming the download request, the data is attached at the end of this temporary file.
678 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
679 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
680 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
681 public void SetTemporaryFilePath(string path)
683 int ret = Interop.Download.SetTempFilePath(_downloadId, path);
684 if (ret != (int)DownloadError.None)
686 DownloadErrorFactory.ThrowException(ret, "Failed to set TemporaryFilePath");
691 /// Starts or resumes the download.
692 /// Starts to download the current URL, or resumes the download if paused.
694 /// <since_tizen> 3 </since_tizen>
695 /// <privilege>http://tizen.org/privilege/download</privilege>
697 /// The URL is the mandatory information to start the download.
699 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
700 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
701 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
704 int ret = (int)DownloadError.None;
705 foreach (KeyValuePair<string, string> entry in _httpHeaders)
707 ret = Interop.Download.AddHttpHeaderField(_downloadId, entry.Key, entry.Value);
708 if (ret != (int)DownloadError.None)
710 DownloadErrorFactory.ThrowException(ret, "Failed to set HttpHeaders");
714 ret = Interop.Download.StartDownload(_downloadId);
715 if (ret != (int)DownloadError.None)
717 DownloadErrorFactory.ThrowException(ret, "Failed to start download request");
722 /// Pauses the download request.
724 /// <since_tizen> 3 </since_tizen>
725 /// <privilege>http://tizen.org/privilege/download</privilege>
727 /// The paused download request can be restarted with Start() or canceled with Cancel().
729 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
730 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
731 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
734 int ret = Interop.Download.PauseDownload(_downloadId);
735 if (ret != (int)DownloadError.None)
737 DownloadErrorFactory.ThrowException(ret, "Failed to pause download request");
742 /// Cancels the download request.
744 /// <since_tizen> 3 </since_tizen>
745 /// <privilege>http://tizen.org/privilege/download</privilege>
747 /// The canceled download can be restarted with Start().
749 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
750 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
751 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
754 int ret = Interop.Download.CancelDownload(_downloadId);
755 if (ret != (int)DownloadError.None)
757 DownloadErrorFactory.ThrowException(ret, "Failed to cancel download request");
762 /// Releases all the resources used by the Request class.
764 /// <since_tizen> 3 </since_tizen>
765 /// <privilege>http://tizen.org/privilege/download</privilege>
767 /// After calling this method, the download request related data exists in the download database for a certain period of time. Within that time, it is possible to use other APIs with this data.
769 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
770 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
771 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
772 public void Dispose()
775 GC.SuppressFinalize(this);
779 /// Deletes the corresponding download request.
781 /// <since_tizen> 3 </since_tizen>
782 /// <privilege>http://tizen.org/privilege/download</privilege>
783 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
784 /// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
785 /// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
786 protected virtual void Dispose(bool disposing)
793 // Free managed objects.
795 Interop.Download.DestroyRequest(_downloadId);
799 static private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
801 managedArray = new string[size];
802 IntPtr[] IntPtrArray = new IntPtr[size];
804 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
806 for (int iterator = 0; iterator < size; iterator++)
808 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
812 private void RegisterStateChangedEvent()
814 _downloadStateChangedCallback = (int downloadId, int downloadState, IntPtr userData) =>
816 StateChangedEventArgs eventArgs = new StateChangedEventArgs((DownloadState)downloadState);
817 _downloadStateChanged?.Invoke(this, eventArgs);
820 int ret = Interop.Download.SetStateChangedCallback(_downloadId, _downloadStateChangedCallback, IntPtr.Zero);
821 if (ret != (int)DownloadError.None)
823 DownloadErrorFactory.ThrowException(ret, "Setting StateChanged callback failed");
827 private void UnregisterStateChangedEvent()
829 int ret = Interop.Download.UnsetStateChangedCallback(_downloadId);
830 if (ret != (int)DownloadError.None)
832 DownloadErrorFactory.ThrowException(ret, "Unsetting StateChanged callback failed");
836 private void RegisterProgressChangedEvent()
838 _downloadProgressChangedCallback = (int downloadId, ulong size, IntPtr userData) =>
840 ProgressChangedEventArgs eventArgs = new ProgressChangedEventArgs(size);
841 _downloadProgressChanged?.Invoke(this, eventArgs);
844 int ret = Interop.Download.SetProgressCallback(_downloadId, _downloadProgressChangedCallback, IntPtr.Zero);
845 if (ret != (int)DownloadError.None)
847 DownloadErrorFactory.ThrowException(ret, "Setting ProgressChanged callback failed");
851 private void UnregisterProgressChangedEvent()
853 int ret = Interop.Download.UnsetProgressCallback(_downloadId);
854 if (ret != (int)DownloadError.None)
856 DownloadErrorFactory.ThrowException(ret, "Unsetting ProgressChanged callback failed");