2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 * @file FCnt_DownloadManagerImpl.cpp
19 * @brief This is the implementation file for the _DownloadManagerImpl class.
27 #include <unique_ptr.h>
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>
38 #include <FCntDownloadRequest.h>
39 #include <FCntIDownloadListener.h>
41 #include <FCnt_DownloadRequestImpl.h>
42 #include "FCnt_DownloadManagerImpl.h"
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Runtime;
48 using namespace Tizen::Base::Collection;
50 namespace Tizen { namespace Content
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;
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;
71 _DownloadManagerImpl* _DownloadManagerImpl::__pInstance = null;
73 class _DownloadEventArg
90 unsigned long long __received;
91 unsigned long long __total;
98 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
100 IDownloadListener* pListener = dynamic_cast<IDownloadListener*> (&listener);
101 if (pListener != null)
103 const _DownloadEventArg* pArg = dynamic_cast<const _DownloadEventArg*>(&arg);
106 switch(pArg->__state)
112 case STATE_DOWNLOADING:
113 pListener->OnDownloadInProgress(pArg->__id, pArg->__received, pArg->__total);
117 pListener->OnDownloadPaused(pArg->__id);
120 case STATE_COMPLETED:
121 pListener->OnDownloadCompleted(pArg->__id, pArg->__path);
125 pListener->OnDownloadFailed(pArg->__id, pArg->__result, pArg->__errorCode);
128 case STATE_CANCELLED:
129 pListener->OnDownloadCanceled(pArg->__id);
141 OnStateChanged(int download_id, download_state_e state, void* data)
143 SysLog(NID_CNT, "OnStateChanged, id = %d, state = %d", download_id, state);
145 RequestId reqId = (long)download_id;
147 _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
157 case SLP_STATE_QUEUED:
158 case SLP_STATE_DOWNLOADING:
161 case SLP_STATE_PAUSED:
163 if (pDMImpl->__pEvent)
165 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
166 pEventArg->__id = reqId;
167 pEventArg->__state = STATE_PAUSED;
169 pDMImpl->__pEvent->Fire(*pEventArg);
175 case SLP_STATE_COMPLETED:
177 if (pDMImpl->__pEvent)
179 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
180 pEventArg->__id = reqId;
181 pEventArg->__state = STATE_COMPLETED;
184 download_get_downloaded_file_path(download_id, &path);
185 pEventArg->__path = path;
189 pDMImpl->__pEvent->Fire(*pEventArg);
192 // Remove the resource from url_download
193 pDMImpl->DestroyResources(reqId);
198 case SLP_STATE_FAILED:
200 if (pDMImpl->__pEvent)
202 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
203 pEventArg->__id = reqId;
204 pEventArg->__state = STATE_FAILED;
206 download_error_e error;
207 download_get_error(download_id, &error);
208 pEventArg->__result = pDMImpl->ConvertToResult(error);
211 download_get_http_status(download_id, &http_status);
212 pEventArg->__errorCode = Integer::ToString(http_status);
214 pDMImpl->__pEvent->Fire(*pEventArg);
217 // Comment out due to resume the failed request
218 //pDMImpl->DestroyResources(reqId);
223 case SLP_STATE_CANCELLED:
225 if (pDMImpl->__pEvent)
227 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
228 pEventArg->__id = reqId;
229 pEventArg->__state = STATE_CANCELLED;
231 pDMImpl->__pEvent->Fire(*pEventArg);
234 // Remove the resource from url_download
235 pDMImpl->DestroyResources(reqId);
247 OnProgress(int download_id, unsigned long long received, void* data)
249 RequestId reqId = (long)download_id;
251 _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
253 if (data && pDMImpl->__pEvent)
255 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
256 pEventArg->__id = reqId;
257 pEventArg->__state = STATE_DOWNLOADING;
258 pEventArg->__received = received;
260 unsigned long long total = 0;
261 download_get_content_size(download_id, &total);
262 pEventArg->__total = total;
264 SysLog(NID_CNT, "OnProgress, id = %d, received = %lld, total = %lld", download_id, received, total);
266 pDMImpl->__pEvent->Fire(*pEventArg);
271 _DownloadManagerImpl::_DownloadManagerImpl(void)
276 _DownloadManagerImpl::~_DownloadManagerImpl(void)
285 _DownloadManagerImpl::InitSingleton(void)
287 unique_ptr<_DownloadManagerImpl> pImpl(new (std::nothrow) _DownloadManagerImpl);
288 SysTryReturnVoidResult(NID_CNT, pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
290 __pInstance = pImpl.release();
292 std::atexit(DestroySingleton);
296 _DownloadManagerImpl::DestroySingleton(void)
301 _DownloadManagerImpl*
302 _DownloadManagerImpl::GetInstance(void)
304 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
305 if (__pInstance == null)
309 pthread_once(&onceBlock, InitSingleton);
310 result r = GetLastResult();
313 onceBlock = PTHREAD_ONCE_INIT;
321 _DownloadManagerImpl::Start(const DownloadRequest& request, RequestId& reqId)
323 SysLog(NID_CNT, "Start a download from Url = %ls, Path = %ls", request.GetUrl().GetPointer(), request.GetDirectoryPath().GetPointer());
325 result r = E_SUCCESS;
330 String url = request.GetUrl();
331 String dirPath = request.GetDirectoryPath();
332 String fileName = request.GetFileName();
333 NetworkType networkType = (NetworkType)request.GetNetworkType();
335 SysTryReturnResult(NID_CNT, !url.IsEmpty(), E_INVALID_ARG, "The url of the download request is empty.");
337 // Create a download id
338 ret = download_create(&download_id);
339 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
342 unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
344 ret = download_set_url(download_id, pUrl.get());
345 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
348 if (networkType == NETWORK_ALL)
350 ret = download_set_network_type(download_id, (download_network_type_e)DOWNLOAD_NETWORK_ALL);
354 ret = download_set_network_type(download_id, (download_network_type_e)networkType);
356 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
359 ret = download_set_notification(download_id, request.IsNotificationEnabled());
360 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
362 //Set notification extra data
363 std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(request.GetNotificationExtraData())->GetMapEnumeratorN());
365 while (pMapEnum->MoveNext() == E_SUCCESS)
367 String* pMapKey = dynamic_cast<String*>(pMapEnum->GetKey());
368 String* pMapValue = dynamic_cast<String*>(pMapEnum->GetValue());
370 if (pMapKey && pMapValue)
372 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
373 const char* pValue = _StringConverter::CopyToCharArrayN(*pMapValue);
375 ret = download_add_notification_extra_param(download_id, pKey.get(), &pValue, 1);
377 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
382 IMap* pRequestHeader = _DownloadRequestImpl::GetInstance(&request)->GetRequestHeader();
383 std::unique_ptr<IMapEnumerator> pMapEnume(const_cast< IMap* >(pRequestHeader)->GetMapEnumeratorN());
384 while (pMapEnume->MoveNext() == E_SUCCESS)
386 String* pMapKey = dynamic_cast<String*>(pMapEnume->GetKey());
387 String* pMapValue = dynamic_cast<String*>(pMapEnume->GetValue());
388 if (pMapKey && pMapValue)
390 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
391 unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pMapValue));
393 ret = download_add_http_header_field(download_id, pKey.get(), pValue.get());
394 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
399 // Check the download path
400 if (!dirPath.IsEmpty())
402 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(dirPath));
404 ret = access(pStr.get(), F_OK);
407 ret = access(pStr.get(), W_OK);
408 SysTryReturnResult(NID_CNT, ret == 0, E_ILLEGAL_ACCESS, "Access to the requested path is denied due to insufficient permission.");
411 ret = download_set_destination(download_id, pStr.get());
412 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
416 if (!fileName.IsEmpty())
418 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(fileName));
420 ret = download_set_file_name(download_id, pStr.get());
421 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
424 // Set the callback functions
425 r = RegisterCallback((long)download_id);
426 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "The internal system service is not available.");
428 // Start the download request
429 ret = download_start(download_id);
430 SysTryCatch(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
431 SysTryCatch(NID_CNT, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
434 reqId = (long)download_id;
439 UnregisterCallback((long)download_id);
445 _DownloadManagerImpl::Pause(RequestId reqId)
449 // Pause the download request
450 ret = download_pause((int)reqId);
451 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
452 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
453 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading.");
454 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
460 _DownloadManagerImpl::Resume(RequestId reqId)
464 // Resume the download request
465 ret = download_start((int)reqId);
466 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
467 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
468 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
469 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
475 _DownloadManagerImpl::Cancel(RequestId reqId)
479 // Stop the download request
480 ret = download_cancel((int)reqId);
481 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
482 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
483 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
489 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
491 result r = E_SUCCESS;
492 DownloadRequest* pRequest = null;
496 bool notification = false;
498 char** pFields = null;
500 char* pFileName = null;
501 download_network_type_e netType = (download_network_type_e)DOWNLOAD_NETWORK_DATA_NETWORK;
503 ret = download_get_url(reqId, &pUrl);
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_network_type(reqId, &netType);
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_destination(reqId, &pPath);
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_file_name(reqId, &pFileName);
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 ret = download_get_notification(reqId, ¬ification);
512 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");
513 ret = download_get_http_header_field_list(reqId, &pFields, &length);
514 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");
515 pRequest = new (std::nothrow) DownloadRequest(pUrl, pPath);
516 SysTryCatch(NID_CNT, pRequest != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
517 pRequest->SetFileName(pFileName);
518 pRequest->SetNotification(notification);
519 pRequest->SetNetworkType((DownloadNetworkType)netType);
520 //Get all the field values
521 for (int i = 0; i < length; i++)
523 ret = download_get_http_header_field(reqId, pFields[i], &pValue);
524 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");
525 pRequest->AddRequestHeader(pFields[i], pValue);
540 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
545 ret = download_get_mime_type((int)reqId, &pStr);
546 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.");
547 SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
548 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
557 _DownloadManagerImpl::GetState(RequestId reqId) const
561 download_state_e download_state = (download_state_e)SLP_STATE_NONE;
562 int state = STATE_NONE;
564 ret = download_get_state((int)reqId, &download_state);
565 SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
566 SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
568 switch(download_state)
574 case SLP_STATE_READY:
575 state = STATE_QUEUED;
578 case SLP_STATE_QUEUED:
579 state = STATE_QUEUED;
582 case SLP_STATE_DOWNLOADING:
583 state = STATE_DOWNLOADING;
586 case SLP_STATE_PAUSED:
587 state = STATE_PAUSED;
590 case SLP_STATE_COMPLETED:
591 state = STATE_COMPLETED;
594 case SLP_STATE_CANCELLED:
595 state = STATE_CANCELLED;
598 case SLP_STATE_FAILED:
599 state = STATE_FAILED;
607 SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
613 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
619 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
625 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
627 if (pListener != null)
629 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
630 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
632 pEvent->AddListener(*pListener);
638 if (__pEvent != null)
648 _DownloadManagerImpl::DestroyResources(RequestId reqId)
652 // Cancel the callback
653 UnregisterCallback(reqId);
655 // Remove the resource from url_download
656 ret = download_destroy((int)reqId);
657 SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
661 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
665 ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
666 SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
668 ret = download_set_progress_cb(reqId, OnProgress, this);
669 SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
674 UnregisterCallback(reqId);
680 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
684 ret = download_unset_state_changed_cb(reqId);
685 SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
687 ret = download_unset_progress_cb(reqId);
688 SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
692 _DownloadManagerImpl::ConvertToResult(int error)
694 result r = E_SUCCESS;
695 download_error_e download_error = (download_error_e)error;
697 SysLog(NID_CNT, "Download error = %d", download_error);
699 switch(download_error)
701 case DOWNLOAD_ERROR_NONE:
705 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
709 case DOWNLOAD_ERROR_IO_ERROR:
713 case DOWNLOAD_ERROR_INVALID_URL:
717 case DOWNLOAD_ERROR_ID_NOT_FOUND:
721 case DOWNLOAD_ERROR_CONNECTION_FAILED:
722 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
723 r = E_CONNECTION_FAILED;
726 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
730 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
734 case DOWNLOAD_ERROR_NO_SPACE:
738 case DOWNLOAD_ERROR_QUEUE_FULL:
740 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
741 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
742 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
743 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
751 } } // Tizen::Content