2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FCnt_DownloadManagerImpl.cpp
20 * @brief This is the implementation file for the _DownloadManagerImpl class.
28 #include <unique_ptr.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include <FBaseInteger.h>
35 #include <FBaseRtIEventArg.h>
36 #include <FBaseColIMap.h>
37 #include <FApp_AppInfo.h>
39 #include <FCntDownloadRequest.h>
40 #include <FCntIDownloadListener.h>
42 #include <FCnt_DownloadRequestImpl.h>
43 #include "FCnt_DownloadManagerImpl.h"
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Runtime;
49 using namespace Tizen::Base::Collection;
51 namespace Tizen { namespace Content
54 static const int STATE_NONE = 0;
55 static const int STATE_QUEUED = 1;
56 static const int STATE_DOWNLOADING = 2;
57 static const int STATE_PAUSED = 3;
58 static const int STATE_CANCELLED = 4;
59 static const int STATE_COMPLETED = 5;
60 static const int STATE_FAILED = 6;
63 static const int SLP_STATE_NONE = 0;
64 static const int SLP_STATE_READY = 1;
65 static const int SLP_STATE_QUEUED = 2;
66 static const int SLP_STATE_DOWNLOADING = 3;
67 static const int SLP_STATE_PAUSED = 4;
68 static const int SLP_STATE_COMPLETED = 5;
69 static const int SLP_STATE_FAILED = 6;
70 static const int SLP_STATE_CANCELLED = 7;
72 _DownloadManagerImpl* _DownloadManagerImpl::__pInstance = null;
74 class _DownloadEventArg
91 unsigned long long __received;
92 unsigned long long __total;
99 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
101 IDownloadListener* pListener = dynamic_cast<IDownloadListener*> (&listener);
102 if (pListener != null)
104 const _DownloadEventArg* pArg = dynamic_cast<const _DownloadEventArg*>(&arg);
107 switch(pArg->__state)
113 case STATE_DOWNLOADING:
114 pListener->OnDownloadInProgress(pArg->__id, pArg->__received, pArg->__total);
118 pListener->OnDownloadPaused(pArg->__id);
121 case STATE_COMPLETED:
122 pListener->OnDownloadCompleted(pArg->__id, pArg->__path);
126 pListener->OnDownloadFailed(pArg->__id, pArg->__result, pArg->__errorCode);
129 case STATE_CANCELLED:
130 pListener->OnDownloadCanceled(pArg->__id);
142 OnStateChanged(int download_id, download_state_e state, void* data)
144 SysLog(NID_CNT, "OnStateChanged, id = %d, state = %d", download_id, state);
146 RequestId reqId = (long)download_id;
148 _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
150 if (!data || !pDMImpl->__pEvent)
158 case SLP_STATE_QUEUED:
159 case SLP_STATE_DOWNLOADING:
162 case SLP_STATE_PAUSED:
164 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
165 pEventArg->__id = reqId;
166 pEventArg->__state = STATE_PAUSED;
168 pDMImpl->__pEvent->Fire(*pEventArg);
173 case SLP_STATE_COMPLETED:
175 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
176 pEventArg->__id = reqId;
177 pEventArg->__state = STATE_COMPLETED;
180 download_get_downloaded_file_path(download_id, &path);
181 pEventArg->__path = path;
185 pDMImpl->__pEvent->Fire(*pEventArg);
187 // Remove the resource from url_download
188 pDMImpl->DestroyResources(reqId);
193 case SLP_STATE_FAILED:
195 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
196 pEventArg->__id = reqId;
197 pEventArg->__state = STATE_FAILED;
199 download_error_e error;
200 download_get_error(download_id, &error);
201 pEventArg->__result = pDMImpl->ConvertToResult(error);
204 download_get_http_status(download_id, &http_status);
205 pEventArg->__errorCode = Integer::ToString(http_status);
207 pDMImpl->__pEvent->Fire(*pEventArg);
209 // Comment out due to resume the failed request
210 //pDMImpl->DestroyResources(reqId);
215 case SLP_STATE_CANCELLED:
217 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
218 pEventArg->__id = reqId;
219 pEventArg->__state = STATE_CANCELLED;
221 pDMImpl->__pEvent->Fire(*pEventArg);
223 // Remove the resource from url_download
224 pDMImpl->DestroyResources(reqId);
236 OnProgress(int download_id, unsigned long long received, void* data)
238 RequestId reqId = (long)download_id;
240 _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
242 if (data && pDMImpl->__pEvent)
244 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
245 pEventArg->__id = reqId;
246 pEventArg->__state = STATE_DOWNLOADING;
247 pEventArg->__received = received;
249 unsigned long long total = 0;
250 download_get_content_size(download_id, &total);
251 pEventArg->__total = total;
253 SysLog(NID_CNT, "OnProgress, id = %d, received = %lld, total = %lld", download_id, received, total);
255 pDMImpl->__pEvent->Fire(*pEventArg);
260 _DownloadManagerImpl::_DownloadManagerImpl(void)
265 _DownloadManagerImpl::~_DownloadManagerImpl(void)
274 _DownloadManagerImpl::InitSingleton(void)
276 unique_ptr<_DownloadManagerImpl> pImpl(new (std::nothrow) _DownloadManagerImpl);
277 SysTryReturnVoidResult(NID_CNT, pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
279 __pInstance = pImpl.release();
281 std::atexit(DestroySingleton);
285 _DownloadManagerImpl::DestroySingleton(void)
290 _DownloadManagerImpl*
291 _DownloadManagerImpl::GetInstance(void)
293 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
294 if (__pInstance == null)
298 pthread_once(&onceBlock, InitSingleton);
299 result r = GetLastResult();
302 onceBlock = PTHREAD_ONCE_INIT;
310 _DownloadManagerImpl::Start(const DownloadRequest& request, RequestId& reqId)
312 SysLog(NID_CNT, "Start a download from Url = %ls, Path = %ls", request.GetUrl().GetPointer(), request.GetDirectoryPath().GetPointer());
314 result r = E_SUCCESS;
319 String url = request.GetUrl();
320 String dirPath = request.GetDirectoryPath();
321 String fileName = request.GetFileName();
322 NetworkType networkType = (NetworkType)request.GetNetworkType();
324 SysTryReturnResult(NID_CNT, !url.IsEmpty(), E_INVALID_ARG, "The url of the download request is empty.");
326 // Create a download id
327 ret = download_create(&download_id);
328 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
331 unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
333 ret = download_set_url(download_id, pUrl.get());
334 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
337 if (networkType == NETWORK_ALL)
339 ret = download_set_network_type(download_id, (download_network_type_e)DOWNLOAD_NETWORK_ALL);
343 ret = download_set_network_type(download_id, (download_network_type_e)networkType);
345 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
348 ret = download_set_notification(download_id, request.IsNotificationEnabled());
349 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
351 //Set notification extra data
352 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(request.GetNotificationExtraData())->GetMapEnumeratorN());
354 while (pMapEnum->MoveNext() == E_SUCCESS)
356 String* pMapKey = dynamic_cast<String*>(pMapEnum->GetKey());
357 String* pMapValue = dynamic_cast<String*>(pMapEnum->GetValue());
359 if (pMapKey && pMapValue)
361 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
362 const char* pValue = _StringConverter::CopyToCharArrayN(*pMapValue);
364 ret = download_add_notification_extra_param(download_id, pKey.get(), &pValue, 1);
366 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
371 IMap* pRequestHeader = _DownloadRequestImpl::GetInstance(&request)->GetRequestHeader();
372 std::unique_ptr<IMapEnumerator> pMapEnume(const_cast< IMap* >(pRequestHeader)->GetMapEnumeratorN());
373 while (pMapEnume->MoveNext() == E_SUCCESS)
375 String* pMapKey = dynamic_cast<String*>(pMapEnume->GetKey());
376 String* pMapValue = dynamic_cast<String*>(pMapEnume->GetValue());
377 if (pMapKey && pMapValue)
379 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
380 unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pMapValue));
382 ret = download_add_http_header_field(download_id, pKey.get(), pValue.get());
383 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
388 // Check the download path
389 if (!dirPath.IsEmpty())
391 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(dirPath));
393 ret = access(pStr.get(), F_OK);
396 ret = access(pStr.get(), W_OK);
397 SysTryReturnResult(NID_CNT, ret == 0, E_ILLEGAL_ACCESS, "Access to the requested path is denied due to insufficient permission.");
400 ret = download_set_destination(download_id, pStr.get());
401 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
405 if (!fileName.IsEmpty())
407 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(fileName));
409 ret = download_set_file_name(download_id, pStr.get());
410 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
413 // Set the callback functions
414 r = RegisterCallback((long)download_id);
415 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "The internal system service is not available.");
417 // Start the download request
418 ret = download_start(download_id);
419 SysTryCatch(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
420 SysTryCatch(NID_CNT, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
423 reqId = (long)download_id;
428 UnregisterCallback((long)download_id);
434 _DownloadManagerImpl::Pause(RequestId reqId)
436 result r = E_SUCCESS;
439 int state = STATE_NONE;
441 // Pause the download request
442 ret = download_pause((int)reqId);
443 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
444 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
445 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading.");
446 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
452 _DownloadManagerImpl::Resume(RequestId reqId)
454 result r = E_SUCCESS;
457 int state = STATE_NONE;
459 // Resume the download request
460 ret = download_start((int)reqId);
461 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
462 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
463 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
464 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
470 _DownloadManagerImpl::Cancel(RequestId reqId)
473 int state = STATE_NONE;
475 // Stop the download request
476 ret = download_cancel((int)reqId);
477 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
478 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
479 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
485 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
487 result r = E_SUCCESS;
488 DownloadRequest* pRequest = null;
492 bool notification = false;
494 char** pFields = null;
496 char* pFileName = null;
497 download_network_type_e netType = (download_network_type_e)DOWNLOAD_NETWORK_DATA_NETWORK;
499 ret = download_get_url(reqId, &pUrl);
500 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");
501 ret = download_get_network_type(reqId, &netType);
502 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");
503 ret = download_get_destination(reqId, &pPath);
504 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");
505 ret = download_get_file_name(reqId, &pFileName);
506 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");
507 ret = download_get_notification(reqId, ¬ification);
508 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");
509 ret = download_get_http_header_field_list(reqId, &pFields, &length);
510 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");
511 pRequest = new (std::nothrow) DownloadRequest(pUrl, pPath);
512 SysTryCatch(NID_CNT, pRequest != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
513 pRequest->SetFileName(pFileName);
514 pRequest->SetNotification(notification);
515 pRequest->SetNetworkType((DownloadNetworkType)netType);
516 //Get all the field values
517 for (int i = 0; i < length; i++)
519 ret = download_get_http_header_field(reqId, pFields[i], &pValue);
520 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");
521 pRequest->AddRequestHeader(pFields[i], pValue);
536 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
541 ret = download_get_mime_type((int)reqId, &pStr);
542 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.");
543 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
544 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
553 _DownloadManagerImpl::GetState(RequestId reqId) const
557 download_state_e download_state = (download_state_e)SLP_STATE_NONE;
558 int state = STATE_NONE;
560 ret = download_get_state((int)reqId, &download_state);
561 SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
562 SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
564 switch(download_state)
570 case SLP_STATE_READY:
571 state = STATE_QUEUED;
574 case SLP_STATE_QUEUED:
575 state = STATE_QUEUED;
578 case SLP_STATE_DOWNLOADING:
579 state = STATE_DOWNLOADING;
582 case SLP_STATE_PAUSED:
583 state = STATE_PAUSED;
586 case SLP_STATE_COMPLETED:
587 state = STATE_COMPLETED;
590 case SLP_STATE_CANCELLED:
591 state = STATE_CANCELLED;
594 case SLP_STATE_FAILED:
595 state = STATE_FAILED;
603 SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
609 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
615 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
621 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
623 if (pListener != null)
625 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
626 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
628 pEvent->AddListener(*pListener);
634 if (__pEvent != null)
644 _DownloadManagerImpl::DestroyResources(RequestId reqId)
647 result r = E_SUCCESS;
649 // Cancel the callback
650 UnregisterCallback(reqId);
652 // Remove the resource from url_download
653 ret = download_destroy((int)reqId);
654 SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
658 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
662 ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
663 SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
665 ret = download_set_progress_cb(reqId, OnProgress, this);
666 SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
671 UnregisterCallback(reqId);
677 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
681 ret = download_unset_state_changed_cb(reqId);
682 SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
684 ret = download_unset_progress_cb(reqId);
685 SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
689 _DownloadManagerImpl::ConvertToResult(int error)
691 result r = E_SUCCESS;
692 download_error_e download_error = (download_error_e)error;
694 SysLog(NID_CNT, "Download error = %d", download_error);
696 switch(download_error)
698 case DOWNLOAD_ERROR_NONE:
702 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
706 case DOWNLOAD_ERROR_IO_ERROR:
710 case DOWNLOAD_ERROR_INVALID_URL:
714 case DOWNLOAD_ERROR_ID_NOT_FOUND:
718 case DOWNLOAD_ERROR_CONNECTION_FAILED:
719 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
720 r = E_CONNECTION_FAILED;
723 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
727 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
731 case DOWNLOAD_ERROR_NO_SPACE:
735 case DOWNLOAD_ERROR_QUEUE_FULL:
737 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
738 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
739 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
740 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
748 } } // Tizen::Content