5e855e47e68b9aae69f84e2eae643ed8f597f42e
[platform/framework/native/content.git] / src / FCnt_DownloadManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file        FCnt_DownloadManagerImpl.cpp
19  * @brief       This is the implementation file for the _DownloadManagerImpl class.
20  *
21  */
22
23 #include <errno.h>
24 #include <unistd.h>
25 #include <cstdlib>
26 #include <pthread.h>
27 #include <unique_ptr.h>
28
29 #include <download.h>
30
31 #include <FBaseSysLog.h>
32 #include <FBase_StringConverter.h>
33 #include <FBaseInteger.h>
34 #include <FBaseRtIEventArg.h>
35 #include <FBaseColIMap.h>
36 #include <FApp_AppInfo.h>
37
38 #include <FCntDownloadRequest.h>
39 #include <FCntIDownloadListener.h>
40
41 #include <FCnt_DownloadRequestImpl.h>
42 #include "FCnt_DownloadManagerImpl.h"
43
44 using namespace std;
45
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Runtime;
48 using namespace Tizen::Base::Collection;
49
50 namespace Tizen { namespace Content
51 {
52
53 static const int STATE_NONE = 0;
54 static const int STATE_QUEUED = 1;
55 static const int STATE_DOWNLOADING = 2;
56 static const int STATE_PAUSED = 3;
57 static const int STATE_CANCELLED = 4;
58 static const int STATE_COMPLETED = 5;
59 static const int STATE_FAILED = 6;
60
61
62 static const int SLP_STATE_NONE = 0;
63 static const int SLP_STATE_READY = 1;
64 static const int SLP_STATE_QUEUED = 2;
65 static const int SLP_STATE_DOWNLOADING = 3;
66 static const int SLP_STATE_PAUSED = 4;
67 static const int SLP_STATE_COMPLETED = 5;
68 static const int SLP_STATE_FAILED = 6;
69 static const int SLP_STATE_CANCELLED = 7;
70
71 _DownloadManagerImpl* _DownloadManagerImpl::__pInstance = null;
72
73 class _DownloadEventArg
74         : public IEventArg
75 {
76 public:
77         _DownloadEventArg()
78                 : __id(-1)
79                 , __state(STATE_NONE)
80                 , __result(-1)
81                 , __received(-1)
82                 , __total(-1)
83         {
84         }
85         RequestId __id;
86         int __state;
87         String __path;
88         result __result;
89         String __errorCode;
90         unsigned long long __received;
91         unsigned long long __total;
92 };
93
94 class _DownloadEvent
95         : public Event
96 {
97 protected:
98         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
99         {
100                 IDownloadListener* pListener = dynamic_cast<IDownloadListener*> (&listener);
101                 if (pListener != null)
102                 {
103                         const _DownloadEventArg* pArg = dynamic_cast<const _DownloadEventArg*>(&arg);
104                         if (pArg != null)
105                         {
106                                 switch(pArg->__state)
107                                 {
108                                         case STATE_NONE:
109                                         case STATE_QUEUED:
110                                                 break;
111
112                                         case STATE_DOWNLOADING:
113                                                 pListener->OnDownloadInProgress(pArg->__id, pArg->__received, pArg->__total);
114                                                 break;
115
116                                         case STATE_PAUSED:
117                                                 pListener->OnDownloadPaused(pArg->__id);
118                                                 break;
119
120                                         case STATE_COMPLETED:
121                                                 pListener->OnDownloadCompleted(pArg->__id, pArg->__path);
122                                                 break;
123
124                                         case STATE_FAILED:
125                                                 pListener->OnDownloadFailed(pArg->__id, pArg->__result, pArg->__errorCode);
126                                                 break;
127
128                                         case STATE_CANCELLED:
129                                                 pListener->OnDownloadCanceled(pArg->__id);
130                                                 break;
131
132                                         default:
133                                                 break;
134                                 }
135                         }
136                 }
137         }
138 };
139
140 static void
141 OnStateChanged(int download_id, download_state_e state, void* data)
142 {
143         SysLog(NID_CNT, "OnStateChanged, id = %d, state = %d", download_id, state);
144
145         RequestId reqId = (long)download_id;
146
147         _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
148
149         if (!data || !pDMImpl->__pEvent)
150         {
151                 return;
152         }
153
154         switch(state)
155         {
156                 case SLP_STATE_NONE:
157                 case SLP_STATE_QUEUED:
158                 case SLP_STATE_DOWNLOADING:
159                         break;
160
161                 case SLP_STATE_PAUSED:
162                 {
163                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
164                         pEventArg->__id = reqId;
165                         pEventArg->__state = STATE_PAUSED;
166
167                         pDMImpl->__pEvent->Fire(*pEventArg);
168
169                         break;
170                 }
171
172                 case SLP_STATE_COMPLETED:
173                 {
174                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
175                         pEventArg->__id = reqId;
176                         pEventArg->__state = STATE_COMPLETED;
177
178                         char* path = null;
179                         download_get_downloaded_file_path(download_id, &path);
180                         pEventArg->__path = path;
181
182                         delete[] path;
183
184                         pDMImpl->__pEvent->Fire(*pEventArg);
185
186                         // Remove the resource from url_download
187                         pDMImpl->DestroyResources(reqId);
188
189                         break;
190                 }
191
192                 case SLP_STATE_FAILED:
193                 {
194                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
195                         pEventArg->__id = reqId;
196                         pEventArg->__state = STATE_FAILED;
197
198                         download_error_e error;
199                         download_get_error(download_id, &error);
200                         pEventArg->__result = pDMImpl->ConvertToResult(error);
201
202                         int http_status = 0;
203                         download_get_http_status(download_id, &http_status);
204                         pEventArg->__errorCode = Integer::ToString(http_status);
205
206                         pDMImpl->__pEvent->Fire(*pEventArg);
207
208                         // Comment out due to resume the failed request
209                         //pDMImpl->DestroyResources(reqId);
210
211                         break;
212                 }
213
214                 case SLP_STATE_CANCELLED:
215                 {
216                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
217                         pEventArg->__id = reqId;
218                         pEventArg->__state = STATE_CANCELLED;
219
220                         pDMImpl->__pEvent->Fire(*pEventArg);
221
222                         // Remove the resource from url_download
223                         pDMImpl->DestroyResources(reqId);
224
225                         break;
226                 }
227
228                 default:
229                         break;
230         }
231
232 }
233
234 static void
235 OnProgress(int download_id, unsigned long long received, void* data)
236 {
237         RequestId reqId = (long)download_id;
238
239         _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
240
241         if (data && pDMImpl->__pEvent)
242         {
243                 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
244                 pEventArg->__id = reqId;
245                 pEventArg->__state = STATE_DOWNLOADING;
246                 pEventArg->__received = received;
247
248                 unsigned long long total = 0;
249                 download_get_content_size(download_id, &total);
250                 pEventArg->__total = total;
251
252                 SysLog(NID_CNT, "OnProgress, id = %d, received = %lld, total = %lld", download_id, received, total);
253
254                 pDMImpl->__pEvent->Fire(*pEventArg);
255         }
256 }
257
258
259 _DownloadManagerImpl::_DownloadManagerImpl(void)
260         : __pEvent(null)
261 {
262 }
263
264 _DownloadManagerImpl::~_DownloadManagerImpl(void)
265 {
266         if (__pEvent)
267         {
268                 delete __pEvent;
269         }
270 }
271
272 void
273 _DownloadManagerImpl::InitSingleton(void)
274 {
275         unique_ptr<_DownloadManagerImpl> pImpl(new (std::nothrow) _DownloadManagerImpl);
276         SysTryReturnVoidResult(NID_CNT, pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
277
278         __pInstance = pImpl.release();
279
280         std::atexit(DestroySingleton);
281 }
282
283 void
284 _DownloadManagerImpl::DestroySingleton(void)
285 {
286         delete __pInstance;
287 }
288
289 _DownloadManagerImpl*
290 _DownloadManagerImpl::GetInstance(void)
291 {
292         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
293         if (__pInstance == null)
294         {
295                 ClearLastResult();
296
297                 pthread_once(&onceBlock, InitSingleton);
298                 result r = GetLastResult();
299                 if(IsFailed(r))
300                 {
301                         onceBlock = PTHREAD_ONCE_INIT;
302                 }
303         }
304
305         return __pInstance;
306 }
307
308 result
309 _DownloadManagerImpl::Start(const DownloadRequest& request, RequestId& reqId)
310 {
311         SysLog(NID_CNT, "Start a download from Url = %ls, Path = %ls", request.GetUrl().GetPointer(), request.GetDirectoryPath().GetPointer());
312
313         result r = E_SUCCESS;
314
315         int ret = 0;
316         int download_id = 0;
317
318         String url = request.GetUrl();
319         String dirPath = request.GetDirectoryPath();
320         String fileName = request.GetFileName();
321         NetworkType networkType = (NetworkType)request.GetNetworkType();
322
323         SysTryReturnResult(NID_CNT, !url.IsEmpty(), E_INVALID_ARG, "The url of the download request is empty.");
324
325         // Create a download id
326         ret = download_create(&download_id);
327         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
328
329         // Set the url
330         unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
331
332         ret = download_set_url(download_id, pUrl.get());
333         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
334
335         //Set network type
336         if (networkType == NETWORK_ALL)
337         {
338                 ret = download_set_network_type(download_id, (download_network_type_e)DOWNLOAD_NETWORK_ALL);
339         }
340         else
341         {
342                 ret = download_set_network_type(download_id, (download_network_type_e)networkType);
343         }
344         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
345
346         //Set notification
347         ret = download_set_notification(download_id, request.IsNotificationEnabled());
348         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
349
350         //Set notification extra data
351         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(request.GetNotificationExtraData())->GetMapEnumeratorN());
352
353         while (pMapEnum->MoveNext() == E_SUCCESS)
354         {
355                 String* pMapKey = dynamic_cast<String*>(pMapEnum->GetKey());
356                 String* pMapValue = dynamic_cast<String*>(pMapEnum->GetValue());
357
358                 if (pMapKey && pMapValue)
359                 {
360                         unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
361                         const char* pValue = _StringConverter::CopyToCharArrayN(*pMapValue);
362
363                         ret = download_add_notification_extra_param(download_id, pKey.get(), &pValue, 1);
364                         delete[] pValue;
365                         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
366                 }
367         }
368
369         //Add http headers
370         IMap* pRequestHeader = _DownloadRequestImpl::GetInstance(&request)->GetRequestHeader();
371         std::unique_ptr<IMapEnumerator> pMapEnume(const_cast< IMap* >(pRequestHeader)->GetMapEnumeratorN());
372         while (pMapEnume->MoveNext() == E_SUCCESS)
373         {
374                 String* pMapKey = dynamic_cast<String*>(pMapEnume->GetKey());
375                 String* pMapValue = dynamic_cast<String*>(pMapEnume->GetValue());
376                 if (pMapKey && pMapValue)
377                 {
378                         unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
379                         unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pMapValue));
380
381                         ret = download_add_http_header_field(download_id, pKey.get(), pValue.get());
382                         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
383
384                 }
385         }
386
387         // Check the download path
388         if (!dirPath.IsEmpty())
389         {
390                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(dirPath));
391
392                 ret = access(pStr.get(), F_OK);
393                 if (ret == 0)
394                 {
395                         ret = access(pStr.get(), W_OK);
396                         SysTryReturnResult(NID_CNT, ret == 0, E_ILLEGAL_ACCESS, "Access to the requested path is denied due to insufficient permission.");
397                 }
398
399                 ret = download_set_destination(download_id, pStr.get());
400                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
401         }
402
403         // Set the file name
404         if (!fileName.IsEmpty())
405         {
406                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(fileName));
407
408                 ret = download_set_file_name(download_id, pStr.get());
409                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
410         }
411
412         // Set the callback functions
413         r = RegisterCallback((long)download_id);
414         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "The internal system service is not available.");
415
416         // Start the download request
417         ret = download_start(download_id);
418         SysTryCatch(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
419         SysTryCatch(NID_CNT, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
420
421         // Set a request Id
422         reqId = (long)download_id;
423
424         return E_SUCCESS;
425
426 CATCH:
427         UnregisterCallback((long)download_id);
428
429         return r;
430 }
431
432 result
433 _DownloadManagerImpl::Pause(RequestId reqId)
434 {
435         result r = E_SUCCESS;
436
437         int ret = 0;
438         int state = STATE_NONE;
439
440         // Pause the download request
441         ret = download_pause((int)reqId);
442         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
443         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
444         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading.");
445         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
446
447         return E_SUCCESS;
448 }
449
450 result
451 _DownloadManagerImpl::Resume(RequestId reqId)
452 {
453         result r = E_SUCCESS;
454
455         int ret = 0;
456         int state = STATE_NONE;
457
458         // Resume the download request
459         ret = download_start((int)reqId);
460         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
461         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
462         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
463         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
464
465         return E_SUCCESS;
466 }
467
468 result
469 _DownloadManagerImpl::Cancel(RequestId reqId)
470 {
471         int ret = 0;
472         int state = STATE_NONE;
473
474         // Stop the download request
475         ret = download_cancel((int)reqId);
476         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
477         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
478         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
479
480         return E_SUCCESS;
481 }
482
483 DownloadRequest*
484 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
485 {
486         result r = E_SUCCESS;
487         DownloadRequest* pRequest = null;
488         int ret = 0;
489         char* pUrl = null;
490         char* pPath = null;
491         bool notification = false;
492         int length = 0;
493         char** pFields = null;
494         char* pValue = null;
495         char* pFileName = null;
496         download_network_type_e netType = (download_network_type_e)DOWNLOAD_NETWORK_DATA_NETWORK;
497
498         ret = download_get_url(reqId, &pUrl);
499         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
500         ret = download_get_network_type(reqId, &netType);
501         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
502         ret = download_get_destination(reqId, &pPath);
503         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
504         ret = download_get_file_name(reqId, &pFileName);
505         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
506         ret = download_get_notification(reqId, &notification);
507         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
508         ret = download_get_http_header_field_list(reqId, &pFields, &length);
509         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
510         pRequest = new (std::nothrow) DownloadRequest(pUrl, pPath);
511         SysTryCatch(NID_CNT, pRequest != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
512         pRequest->SetFileName(pFileName);
513         pRequest->SetNotification(notification);
514         pRequest->SetNetworkType((DownloadNetworkType)netType);
515         //Get all the field values
516         for (int i = 0; i < length; i++)
517         {
518                 ret = download_get_http_header_field(reqId, pFields[i], &pValue);
519                 SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
520                 pRequest->AddRequestHeader(pFields[i], pValue);
521                 free(pValue);
522                 free(pFields[i]);
523                 pValue = null;
524         }
525
526 CATCH:
527         free(pUrl);
528         free(pPath);
529         free(pFileName);
530         SetLastResult(r);
531         return pRequest;
532 }
533
534 result
535 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
536 {
537         int ret = 0;
538         char* pStr = null;
539
540         ret = download_get_mime_type((int)reqId, &pStr);
541         SysTryReturnResult(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), E_INVALID_ARG, "There is no download request for the request ID.");
542         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
543         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
544
545         mimeType = pStr;
546         delete[] pStr;
547
548         return E_SUCCESS;
549 }
550
551 int
552 _DownloadManagerImpl::GetState(RequestId reqId) const
553 {
554         int ret = 0;
555
556         download_state_e download_state = (download_state_e)SLP_STATE_NONE;
557         int state = STATE_NONE;
558
559         ret = download_get_state((int)reqId, &download_state);
560         SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
561         SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
562
563         switch(download_state)
564         {
565                 case SLP_STATE_NONE:
566                         state = STATE_NONE;
567                         break;
568
569                 case SLP_STATE_READY:
570                         state = STATE_QUEUED;
571                         break;
572
573                 case SLP_STATE_QUEUED:
574                         state = STATE_QUEUED;
575                         break;
576
577                 case SLP_STATE_DOWNLOADING:
578                         state = STATE_DOWNLOADING;
579                         break;
580
581                 case SLP_STATE_PAUSED:
582                         state = STATE_PAUSED;
583                         break;
584
585                 case SLP_STATE_COMPLETED:
586                         state = STATE_COMPLETED;
587                         break;
588
589                 case SLP_STATE_CANCELLED:
590                         state = STATE_CANCELLED;
591                         break;
592
593                 case SLP_STATE_FAILED:
594                         state = STATE_FAILED;
595                         break;
596
597                 default:
598                         state = STATE_NONE;
599                         break;
600         }
601
602         SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
603
604         return state;
605 }
606
607 result
608 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
609 {
610         return E_SUCCESS;
611 }
612
613 result
614 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
615 {
616         return E_SUCCESS;
617 }
618
619 void
620 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
621 {
622         if (pListener != null)
623         {
624                 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
625                 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
626
627                 pEvent->AddListener(*pListener);
628
629                 __pEvent = pEvent;
630         }
631         else
632         {
633                 if (__pEvent != null)
634                 {
635                         delete __pEvent;
636                 }
637
638                 __pEvent = null;
639         }
640 }
641
642 void
643 _DownloadManagerImpl::DestroyResources(RequestId reqId)
644 {
645         int ret = 0;
646         result r = E_SUCCESS;
647
648         // Cancel the callback
649         UnregisterCallback(reqId);
650
651         // Remove the resource from url_download
652         ret = download_destroy((int)reqId);
653         SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
654 }
655
656 result
657 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
658 {
659         int ret = 0;
660
661         ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
662         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
663
664         ret = download_set_progress_cb(reqId, OnProgress, this);
665         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
666
667         return E_SUCCESS;
668
669 CATCH:
670         UnregisterCallback(reqId);
671
672         return E_SYSTEM;
673 }
674
675 void
676 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
677 {
678         int ret = 0;
679
680         ret = download_unset_state_changed_cb(reqId);
681         SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
682
683         ret = download_unset_progress_cb(reqId);
684         SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
685 }
686
687 result
688 _DownloadManagerImpl::ConvertToResult(int error)
689 {
690         result r = E_SUCCESS;
691         download_error_e download_error = (download_error_e)error;
692
693         SysLog(NID_CNT, "Download error = %d", download_error);
694
695         switch(download_error)
696         {
697                 case DOWNLOAD_ERROR_NONE:
698                         r = E_SUCCESS;
699                         break;
700
701                 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
702                         r = E_OUT_OF_MEMORY;
703                         break;
704
705                 case DOWNLOAD_ERROR_IO_ERROR:
706                         r = E_SYSTEM;
707                         break;
708
709                 case DOWNLOAD_ERROR_INVALID_URL:
710                         r = E_INVALID_URL;
711                         break;
712
713                 case DOWNLOAD_ERROR_ID_NOT_FOUND:
714                         r = E_INVALID_ARG;
715                         break;
716
717                 case DOWNLOAD_ERROR_CONNECTION_FAILED:
718                 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
719                         r = E_CONNECTION_FAILED;
720                         break;
721
722                 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
723                         r = E_TIMEOUT;
724                         break;
725
726                 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
727                         r = E_MAX_EXCEEDED;
728                         break;
729
730                 case DOWNLOAD_ERROR_NO_SPACE:
731                         r = E_STORAGE_FULL;
732                         break;
733
734                 case DOWNLOAD_ERROR_QUEUE_FULL:
735
736                 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
737                 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
738                 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
739                 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
740                 default:
741                         r = E_SYSTEM;
742         }
743
744         return r;
745 }
746
747 } } // Tizen::Content