Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.Download / Tizen.Content.Download / Request.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.Content.Download
22 {
23     /// <summary>
24     /// The Request class provides functions to create and manage a single download request.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class Request : IDisposable
28     {
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;
37
38         /// <summary>
39         /// Creates a Request object.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         /// <param name="url"> 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 invalid operation</exception>
46         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
47         public Request(string url)
48         {
49             if (String.IsNullOrEmpty(url))
50             {
51                 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
52             }
53             int ret = Interop.Download.CreateRequest(out _downloadId);
54             if (ret != (int)DownloadError.None)
55             {
56                 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
57             }
58             ret = Interop.Download.SetUrl(_downloadId, url);
59             if (ret != (int)DownloadError.None)
60             {
61                 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
62             }
63             _notificationProperties = new Notification(_downloadId);
64             _httpHeaders = new Dictionary<string, string>();
65         }
66
67         /// <summary>
68         /// Creates a Request object.
69         /// </summary>
70         /// <since_tizen> 3 </since_tizen>
71         /// <param name="url"> URL to download</param>
72         /// <param name="destinationPath"> Directory path where downloaded file is stored </param>
73         /// <param name="fileName"> Name of the downloaded file </param>
74         /// <param name="type"> 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 invalid operation</exception>
81         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
82         /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
83         public Request(string url, string destinationPath, string fileName, NetworkType type)
84         {
85             if (String.IsNullOrEmpty(url))
86             {
87                 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
88             }
89             int ret = Interop.Download.CreateRequest(out _downloadId);
90             if (ret != (int)DownloadError.None)
91             {
92                 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
93             }
94
95             ret = Interop.Download.SetUrl(_downloadId, url);
96             if (ret != (int)DownloadError.None)
97             {
98                 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
99             }
100
101             ret = Interop.Download.SetDestination(_downloadId, destinationPath);
102             if (ret != (int)DownloadError.None)
103             {
104                 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
105             }
106
107             ret = Interop.Download.SetFileName(_downloadId, fileName);
108             if (ret != (int)DownloadError.None)
109             {
110                 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
111             }
112
113             ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
114             if (ret != (int)DownloadError.None)
115             {
116                 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
117             }
118
119             _notificationProperties = new Notification(_downloadId);
120             _httpHeaders = new Dictionary<string, string>();
121         }
122
123         /// <summary>
124         /// Creates a Request object.
125         /// </summary>
126         /// <since_tizen> 3 </since_tizen>
127         /// <param name="url"> URL to download</param>
128         /// <param name="destinationPath"> Directory path where downloaded file is stored </param>
129         /// <param name="fileName"> Name of the downloaded file </param>
130         /// <param name="type"> Network type which the download request must adhere to </param>
131         /// <param name="httpHeaders"> HTTP header fields for 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 invalid operation</exception>
138         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
139         /// <exception cref="NotSupportedException">Thrown when features is not supported.</exception>
140         public Request(string url, string destinationPath, string fileName, NetworkType type, IDictionary<string, string> httpHeaders)
141         {
142             if (String.IsNullOrEmpty(url))
143             {
144                 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
145             }
146             int ret = Interop.Download.CreateRequest(out _downloadId);
147             if (ret != (int)DownloadError.None)
148             {
149                 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
150             }
151
152             ret = Interop.Download.SetUrl(_downloadId, url);
153             if (ret != (int)DownloadError.None)
154             {
155                 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
156             }
157
158             ret = Interop.Download.SetDestination(_downloadId, destinationPath);
159             if (ret != (int)DownloadError.None)
160             {
161                 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
162             }
163
164             ret = Interop.Download.SetFileName(_downloadId, fileName);
165             if (ret != (int)DownloadError.None)
166             {
167                 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
168             }
169
170             ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
171             if (ret != (int)DownloadError.None)
172             {
173                 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
174             }
175
176             _notificationProperties = new Notification(_downloadId);
177             _httpHeaders = httpHeaders;
178         }
179
180         ~Request()
181         {
182             Dispose(false);
183         }
184
185         /// <summary>
186         /// Event that occurs when the download state changes.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         /// <privilege>http://tizen.org/privilege/download</privilege>
190         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
191         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
192         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
193         public event EventHandler<StateChangedEventArgs> StateChanged
194         {
195             add
196             {
197                 if (_downloadStateChanged == null)
198                 {
199                     RegisterStateChangedEvent();
200                 }
201                 _downloadStateChanged += value;
202             }
203             remove
204             {
205                 _downloadStateChanged -= value;
206                 if (_downloadStateChanged == null)
207                 {
208                     UnregisterStateChangedEvent();
209                 }
210             }
211         }
212
213         /// <summary>
214         /// Event that occurs when the download progress changes.
215         /// </summary>
216         /// <since_tizen> 3 </since_tizen>
217         /// <privilege>http://tizen.org/privilege/download</privilege>
218         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
219         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
220         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
221         public event EventHandler<ProgressChangedEventArgs> ProgressChanged
222         {
223             add
224             {
225                 if (_downloadProgressChanged == null)
226                 {
227                     RegisterProgressChangedEvent();
228                 }
229                 _downloadProgressChanged += value;
230             }
231             remove
232             {
233                 _downloadProgressChanged -= value;
234                 if (_downloadProgressChanged == null)
235                 {
236                     UnregisterProgressChangedEvent();
237                 }
238             }
239         }
240
241         /// <summary>
242         /// Absolute path where the file will be downloaded.
243         /// If you try to get this property value before calling Start(), an empty string is returned.
244         /// </summary>
245         /// <since_tizen> 3 </since_tizen>
246         /// <privilege>http://tizen.org/privilege/download</privilege>
247         /// <remarks>
248         /// Returns empty string if download is not completed or if state has not yet changed to Completed or if any other error occurs.
249         /// </remarks>
250         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
251         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
252         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
253         public string DownloadedPath
254         {
255             get
256             {
257                 string path;
258                 int ret = Interop.Download.GetDownloadedPath(_downloadId, out path);
259                 if (ret != (int)DownloadError.None)
260                 {
261                     Log.Error(Globals.LogTag, "Failed to get DownloadedPath, " + (DownloadError)ret);
262                     return String.Empty;
263                 }
264                 return path;
265             }
266         }
267
268         /// <summary>
269         /// MIME type of the downloaded content.
270         /// If you try to get this property value before calling Start(), an empty string is returned.
271         /// </summary>
272         /// <since_tizen> 3 </since_tizen>
273         /// <privilege>http://tizen.org/privilege/download</privilege>
274         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
275         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
276         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
277         public string MimeType
278         {
279             get
280             {
281                 string mime;
282                 int ret = Interop.Download.GetMimeType(_downloadId, out mime);
283                 if (ret != (int)DownloadError.None)
284                 {
285                     Log.Error(Globals.LogTag, "Failed to get MimeType, " + (DownloadError)ret);
286                     return String.Empty;
287                 }
288                 return mime;
289             }
290         }
291
292         /// <summary>
293         /// Current state of the download.
294         /// </summary>
295         /// <since_tizen> 3 </since_tizen>
296         /// <privilege>http://tizen.org/privilege/download</privilege>
297         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
298         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
299         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
300         public DownloadState State
301         {
302             get
303             {
304                 int state;
305                 int ret = Interop.Download.GetState(_downloadId, out state);
306                 if (ret != (int)DownloadError.None)
307                 {
308                     Log.Error(Globals.LogTag, "Failed to get DownloadState, " + (DownloadError)ret);
309                     return DownloadState.None;
310                 }
311                 return (DownloadState)state;
312             }
313         }
314
315         /// <summary>
316         /// The content name of the downloaded file.
317         /// This can be defined with reference of HTTP response header data. The content name can be received when HTTP response header is received.
318         /// If you try to get this property value before calling Start(), an empty string is returned.
319         /// </summary>
320         /// <since_tizen> 3 </since_tizen>
321         /// <privilege>http://tizen.org/privilege/download</privilege>
322         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
323         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
324         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
325         public string ContentName
326         {
327             get
328             {
329                 string name;
330                 int ret = Interop.Download.GetContentName(_downloadId, out name);
331                 if (ret != (int)DownloadError.None)
332                 {
333                     Log.Error(Globals.LogTag, "Failed to get ContentName, " + (DownloadError)ret);
334                     return String.Empty;
335                 }
336                 return name;
337             }
338         }
339
340         /// <summary>
341         /// Total size of downloaded content.
342         /// 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.
343         /// If you try to get this property value before calling Start(), 0 is returned.
344         /// </summary>
345         /// <since_tizen> 3 </since_tizen>
346         /// <privilege>http://tizen.org/privilege/download</privilege>
347         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
348         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
349         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
350         public ulong ContentSize
351         {
352             get
353             {
354                 ulong size;
355                 int ret = Interop.Download.GetContentSize(_downloadId, out size);
356                 if (ret != (int)DownloadError.None)
357                 {
358                     Log.Error(Globals.LogTag, "Failed to get ContentSize, " + (DownloadError)ret);
359                     return 0;
360                 }
361                 return size;
362             }
363         }
364
365         /// <summary>
366         /// HTTP status code when a download exception occurs.
367         /// If you try to get this property value before calling Start(), 0 is returned.
368         /// </summary>
369         /// <since_tizen> 3 </since_tizen>
370         /// <privilege>http://tizen.org/privilege/download</privilege>
371         /// <remarks>
372         /// State of download request must be DownlodState.Failed.
373         /// </remarks>
374         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
375         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
376         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
377         public int HttpStatus
378         {
379             get
380             {
381                 int status;
382                 int ret = Interop.Download.GetHttpStatus(_downloadId, out status);
383                 if (ret != (int)DownloadError.None)
384                 {
385                     Log.Error(Globals.LogTag, "Failed to get HttpStatus, " + (DownloadError)ret);
386                     return 0;
387                 }
388                 return status;
389             }
390         }
391
392         /// <summary>
393         /// ETag value from the HTTP response header when making a HTTP request for resume.
394         /// If you try to get this property value before calling Start() or if any other error occurs, an empty string is returned.
395         /// </summary>
396         /// <since_tizen> 3 </since_tizen>
397         /// <privilege>http://tizen.org/privilege/download</privilege>
398         /// <remarks>
399         /// The etag value is available or not depending on the web server. If not available, then on get of the property null is returned.
400         /// After download is started, it can get the etag value.
401         /// </remarks>
402         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
403         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
404         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
405         public string ETagValue
406         {
407             get
408             {
409                 string etag;
410                 int ret = Interop.Download.GetETag(_downloadId, out etag);
411                 if (ret != (int)DownloadError.None)
412                 {
413                     Log.Error(Globals.LogTag, "Failed to get ETagValue, " + (DownloadError)ret);
414                     return String.Empty;
415                 }
416                 return etag;
417             }
418         }
419
420         /// <summary>
421         /// Contains properties required for creating download notifications.
422         /// </summary>
423         /// <since_tizen> 3 </since_tizen>
424         /// <remarks>
425         /// When the notification message is clicked, the action taken by the system is decided by the app control properties of the NotificationProperties instance.
426         /// If the app control is not set, the following default operation is executed when the notification message is clicked:
427         ///  1) download completed state - the viewer application is executed according to extension name of downloaded content,
428         ///  2) download failed state and ongoing state - the client application is executed.
429         /// This property should be set before calling Start().
430         /// </remarks>
431         public Notification NotificationProperties
432         {
433             get
434             {
435                 return _notificationProperties;
436             }
437         }
438
439         /// <summary>
440         /// Full path of the temporary file which stores downloaded content.
441         /// </summary>
442         /// <since_tizen> 3 </since_tizen>
443         /// <privilege>http://tizen.org/privilege/download</privilege>
444         /// <remarks>
445         /// The download state must be one of the states after Downloading.
446         /// </remarks>
447         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
448         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
449         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
450         public string TemporaryPath
451         {
452             get
453             {
454                 string path;
455                 int ret = Interop.Download.GetTempFilePath(_downloadId, out path);
456                 if (ret != (int)DownloadError.None)
457                 {
458                     Log.Error(Globals.LogTag, "Failed to get TemporaryPath, " + (DownloadError)ret);
459                     return String.Empty;
460                 }
461                 return path;
462             }
463         }
464
465         /// <summary>
466         /// URL to download.
467         /// </summary>
468         /// <since_tizen> 3 </since_tizen>
469         /// <privilege>http://tizen.org/privilege/download</privilege>
470         /// <remarks>
471         /// Should be set before calling Start().
472         /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
473         /// </remarks>
474         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
475         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
476         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
477         public string Url
478         {
479             get
480             {
481                 string url;
482                 int ret = Interop.Download.GetUrl(_downloadId, out url);
483                 if (ret != (int)DownloadError.None)
484                 {
485                     Log.Error(Globals.LogTag, "Failed to get Url, " + (DownloadError)ret);
486                     return String.Empty;
487                 }
488                 return url;
489             }
490             set
491             {
492                 int ret = Interop.Download.SetUrl(_downloadId, value);
493                 if (ret != (int)DownloadError.None)
494                 {
495                     DownloadErrorFactory.ThrowException(ret, "Failed to set Url");
496                 }
497             }
498         }
499
500         /// <summary>
501         /// Allowed network type for downloading the file.
502         /// The file will be downloaded only under the allowed network.
503         /// If you try to get this property value before setting or if any other error occurs, default value NetworkType All is returned.
504         /// </summary>
505         /// <since_tizen> 3 </since_tizen>
506         /// <privilege>http://tizen.org/privilege/download</privilege>
507         /// <feature>http://tizen.org/feature/network.wifi</feature>
508         /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
509         /// <feature>http://tizen.org/feature/network.telephony</feature>
510         /// <remarks>
511         /// Should be set before calling Start().
512         /// </remarks>
513         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
514         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
515         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
516         /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
517         public NetworkType AllowedNetworkType
518         {
519             get
520             {
521                 int type;
522                 int ret = Interop.Download.GetNetworkType(_downloadId, out type);
523                 if (ret != (int)DownloadError.None)
524                 {
525                     Log.Error(Globals.LogTag, "Failed to get AllowedNetworkType, " + (DownloadError)ret);
526                     return NetworkType.All;
527                 }
528                 return (NetworkType)type;
529             }
530             set
531             {
532                 int ret = Interop.Download.SetNetworkType(_downloadId, (int)value);
533                 if (ret != (int)DownloadError.None)
534                 {
535                     DownloadErrorFactory.ThrowException(ret, "Failed to set AllowedNetworkType");
536                 }
537             }
538         }
539
540         /// <summary>
541         /// The file will be downloaded to the set 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 default storage.
542         /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
543         /// </summary>
544         /// <since_tizen> 3 </since_tizen>
545         /// <privilege>http://tizen.org/privilege/download</privilege>
546         /// <remarks>
547         /// Should be set before calling Start().
548         /// </remarks>
549         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
550         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
551         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
552         public string DestinationPath
553         {
554             get
555             {
556                 string path;
557                 int ret = Interop.Download.GetDestination(_downloadId, out path);
558                 if (ret != (int)DownloadError.None)
559                 {
560                     Log.Error(Globals.LogTag, "Failed to get DestinationPath, " + (DownloadError)ret);
561                     return String.Empty;
562                 }
563                 return path;
564             }
565             set
566             {
567                 int ret = Interop.Download.SetDestination(_downloadId, value.ToString());
568                 if (ret != (int)DownloadError.None)
569                 {
570                     DownloadErrorFactory.ThrowException(ret, "Failed to set DestinationPath");
571                 }
572             }
573         }
574
575         /// <summary>
576         /// The file will be saved in the specified destination or 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.
577         /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
578         /// </summary>
579         /// <since_tizen> 3 </since_tizen>
580         /// <privilege>http://tizen.org/privilege/download</privilege>
581         /// <remarks>
582         /// Should be set before calling Start().
583         /// </remarks>
584         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
585         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
586         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
587         public string FileName
588         {
589             get
590             {
591                 string name;
592                 int ret = Interop.Download.GetFileName(_downloadId, out name);
593                 if (ret != (int)DownloadError.None)
594                 {
595                     Log.Error(Globals.LogTag, "Failed to get FileName, " + (DownloadError)ret);
596                     return String.Empty;
597                 }
598                 return name;
599             }
600             set
601             {
602                 int ret = Interop.Download.SetFileName(_downloadId, value.ToString());
603                 if (ret != (int)DownloadError.None)
604                 {
605                     DownloadErrorFactory.ThrowException(ret, "Failed to set FileName");
606                 }
607             }
608         }
609
610         /// <summary>
611         /// Enables or disables auto download.
612         /// 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.
613         /// If you try to get this property value before setting, default value false is returned.
614         /// </summary>
615         /// <since_tizen> 3 </since_tizen>
616         /// <privilege>http://tizen.org/privilege/download</privilege>
617         /// <remarks>
618         /// The default value is false.
619         /// </remarks>
620         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
621         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
622         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
623         public bool AutoDownload
624         {
625             get
626             {
627                 bool value;
628                 int ret = Interop.Download.GetAutoDownload(_downloadId, out value);
629                 if (ret != (int)DownloadError.None)
630                 {
631                     Log.Error(Globals.LogTag, "Failed to get AutoDownload, " + (DownloadError)ret);
632                     return false;
633                 }
634                 return value;
635             }
636             set
637             {
638                 int ret = Interop.Download.SetAutoDownload(_downloadId, value);
639                 if (ret != (int)DownloadError.None)
640                 {
641                     DownloadErrorFactory.ThrowException(ret, "Failed to set AutoDownload");
642                 }
643             }
644         }
645
646         /// <summary>
647         /// HTTP header field and value pairs to the download request.
648         /// HTTP header &lt;field,value&gt; pair is the &lt;key,value&gt; pair in the Dictionary HttpHeaders
649         /// The given HTTP header field will be included with the HTTP request of the download request.
650         /// If you try to get this property value before setting, an empty dictionary is returned.
651         /// </summary>
652         /// <since_tizen> 3 </since_tizen>
653         /// <remarks>
654         /// HTTP header fields should be set before calling Start().
655         /// HTTP header fields can be removed before calling Start().
656         /// </remarks>
657         public IDictionary<string, string> HttpHeaders
658         {
659             get
660             {
661                 return _httpHeaders;
662             }
663         }
664
665         /// <summary>
666         /// Sets the directory path of a temporary file used in a previous download request.
667         /// This is only useful when resuming download to make HTTP request header at the client side. Otherwise, the path is ignored.
668         /// </summary>
669         /// <since_tizen> 3 </since_tizen>
670         /// <privilege>http://tizen.org/privilege/download</privilege>
671         /// <remarks>
672         /// If the etag value is not present in the download database, it is not useful to set the temporary file path.
673         /// When resuming the download request, the data is attached at the end of this temporary file.
674         /// </remarks>
675         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
676         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
677         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
678         public void SetTemporaryFilePath(string path)
679         {
680                 int ret = Interop.Download.SetTempFilePath(_downloadId, path);
681                 if (ret != (int)DownloadError.None)
682                 {
683                     DownloadErrorFactory.ThrowException(ret, "Failed to set TemporaryFilePath");
684                 }
685         }
686
687         /// <summary>
688         /// Starts or resumes download.
689         /// Starts to download the current URL, or resumes the download if paused.
690         /// </summary>
691         /// <since_tizen> 3 </since_tizen>
692         /// <privilege>http://tizen.org/privilege/download</privilege>
693         /// <remarks>
694         /// The URL is the mandatory information to start the download.
695         /// </remarks>
696         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
697         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
698         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
699         public void Start()
700         {
701             int ret = (int)DownloadError.None;
702             foreach (KeyValuePair<string, string> entry in _httpHeaders)
703             {
704                 ret = Interop.Download.AddHttpHeaderField(_downloadId, entry.Key, entry.Value);
705                 if (ret != (int)DownloadError.None)
706                 {
707                     DownloadErrorFactory.ThrowException(ret, "Failed to set HttpHeaders");
708                 }
709             }
710
711             ret = Interop.Download.StartDownload(_downloadId);
712             if (ret != (int)DownloadError.None)
713             {
714                 DownloadErrorFactory.ThrowException(ret, "Failed to start download request");
715             }
716         }
717
718         /// <summary>
719         /// Pauses download request.
720         /// </summary>
721         /// <since_tizen> 3 </since_tizen>
722         /// <privilege>http://tizen.org/privilege/download</privilege>
723         /// <remarks>
724         /// The paused download request can be restarted with Start() or canceled with Cancel().
725         /// </remarks>
726         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
727         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
728         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
729         public void Pause()
730         {
731             int ret = Interop.Download.PauseDownload(_downloadId);
732             if (ret != (int)DownloadError.None)
733             {
734                 DownloadErrorFactory.ThrowException(ret, "Failed to pause download request");
735             }
736         }
737
738         /// <summary>
739         /// Cancels download request.
740         /// </summary>
741         /// <since_tizen> 3 </since_tizen>
742         /// <privilege>http://tizen.org/privilege/download</privilege>
743         /// <remarks>
744         /// The canceled download can be restarted with Start().
745         /// </remarks>
746         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
747         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
748         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
749         public void Cancel()
750         {
751             int ret = Interop.Download.CancelDownload(_downloadId);
752             if (ret != (int)DownloadError.None)
753             {
754                 DownloadErrorFactory.ThrowException(ret, "Failed to cancel download request");
755             }
756         }
757
758         /// <summary>
759         /// Releases all resources used by the Request class.
760         /// </summary>
761         /// <since_tizen> 3 </since_tizen>
762         /// <privilege>http://tizen.org/privilege/download</privilege>
763         /// <remarks>
764         /// After calling this method, 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.
765         /// </remarks>
766         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
767         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
768         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
769         public void Dispose()
770         {
771             Dispose(true);
772             GC.SuppressFinalize(this);
773         }
774
775         /// <summary>
776         /// Deletes the corresponding download request.
777         /// </summary>
778         /// <since_tizen> 3 </since_tizen>
779         /// <privilege>http://tizen.org/privilege/download</privilege>
780         /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
781         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
782         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
783         protected virtual void Dispose(bool disposing)
784         {
785             if (_disposed)
786                 return;
787
788             if (disposing)
789             {
790                 // Free managed objects.
791             }
792             Interop.Download.DestroyRequest(_downloadId);
793             _disposed = true;
794         }
795
796         static private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
797         {
798             managedArray = new string[size];
799             IntPtr[] IntPtrArray = new IntPtr[size];
800
801             Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
802
803             for (int iterator = 0; iterator < size; iterator++)
804             {
805                 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
806             }
807         }
808
809         private void RegisterStateChangedEvent()
810         {
811             _downloadStateChangedCallback = (int downloadId, int downloadState, IntPtr userData) =>
812             {
813                 StateChangedEventArgs eventArgs = new StateChangedEventArgs((DownloadState)downloadState);
814                 _downloadStateChanged?.Invoke(this, eventArgs);
815             };
816
817             int ret = Interop.Download.SetStateChangedCallback(_downloadId, _downloadStateChangedCallback, IntPtr.Zero);
818             if (ret != (int)DownloadError.None)
819             {
820                 DownloadErrorFactory.ThrowException(ret, "Setting StateChanged callback failed");
821             }
822         }
823
824         private void UnregisterStateChangedEvent()
825         {
826             int ret = Interop.Download.UnsetStateChangedCallback(_downloadId);
827             if (ret != (int)DownloadError.None)
828             {
829                 DownloadErrorFactory.ThrowException(ret, "Unsetting StateChanged callback failed");
830             }
831         }
832
833         private void RegisterProgressChangedEvent()
834         {
835             _downloadProgressChangedCallback = (int downloadId, ulong size, IntPtr userData) =>
836             {
837                 ProgressChangedEventArgs eventArgs = new ProgressChangedEventArgs(size);
838                 _downloadProgressChanged?.Invoke(this, eventArgs);
839             };
840
841             int ret = Interop.Download.SetProgressCallback(_downloadId, _downloadProgressChangedCallback, IntPtr.Zero);
842             if (ret != (int)DownloadError.None)
843             {
844                 DownloadErrorFactory.ThrowException(ret, "Setting ProgressChanged callback failed");
845             }
846         }
847
848         private void UnregisterProgressChangedEvent()
849         {
850             int ret = Interop.Download.UnsetProgressCallback(_downloadId);
851             if (ret != (int)DownloadError.None)
852             {
853                 DownloadErrorFactory.ThrowException(ret, "Unsetting ProgressChanged callback failed");
854             }
855         }
856     }
857 }
858