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.
18 * @file FCnt_ContentTransferImpl.cpp
19 * @brief This is the implementation file for the %_ContentTransferImpl class.
21 * This file contains implementation of the %_ContentTransferImpl class.
24 #include <FBaseSysLog.h>
25 #include <FBaseString.h>
26 #include <FBaseColIList.h>
27 #include <FBaseUtilUri.h>
28 #include <FIoDirectory.h>
29 #include <FCntIContentTransferListener.h>
30 #include <FCntContentTransfer.h>
31 #include <FCntContentTransferInfo.h>
32 #include <FIoRegistry.h>
34 #include <FSysEnvironment.h>
35 #include <FApp_AppInfo.h>
36 #include <FCnt_ContentTransferImpl.h>
37 #include <FSys_EnvironmentImpl.h>
38 #include <FIo_FileImpl.h>
39 #include <FIo_FileAttributesImpl.h>
40 #include <FSys_SystemTimeImpl.h>
41 #include "FCnt_ContentDownloadHandler.h"
42 #include "FCnt_ContentTransferEvent.h"
44 using namespace Tizen;
45 using namespace Tizen::App;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Collection;
48 using namespace Tizen::Base::Utility;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Io;
51 using namespace Tizen::System;
53 static RequestId downloadRequestId = 0;
54 static bool initSlot = false;
55 static const int MAX_DOWNLOAD_COUNT = 5;
57 namespace Tizen { namespace Content
60 static const wchar_t CONTENT_DOWNLOAD_TEMP_DIRECTORY[] = L"/tmp/content/";
61 static const wchar_t CONTENT_DOWNLOAD_TEMP_FILE_PATH[] = L"/tmp/content/downloadtempfile";
62 static const wchar_t CONTENT_DOWNLOAD_PATH_MEDIA[] = L"/Media";
63 static const wchar_t CONTENT_DOWNLOAD_PATH_MEDIA_MMC[] = L"/Storagecard/Media";
64 static const wchar_t CONTENT_DOWNLOAD_PATH_HOME[] = L"/Home";
65 static const wchar_t CONTENT_DOWNLOAD_PATH_HOME_MMC[] = L"/HomeExt";
67 static const int CONTENT_TRANSFER_DEFAULT_PROGRESS_INTERVAL = 0;
68 static const int CONTENT_DOWNLOAD_CHECKING_PERIOD = 1000;
70 _ContentTransferImpl::_ContentTransferImpl(void)
71 : __requestId(INVALID_REQUEST_ID)
72 , __restRequestId(INVALID_REQUEST_ID)
74 , __percent(CONTENT_TRANSFER_DEFAULT_PROGRESS_INTERVAL)
75 , __timerStarted(false)
78 , __pContentDownloadUserData(null)
79 , __pContentDownloadUserDataSequence(null)
80 , __pContentDownloadHandler(null)
81 , __pContentDownloadHandlerSequence(null)
82 , __pTransferEvent(null)
83 , __pTransferInfoList(null)
89 _ContentTransferImpl::~_ContentTransferImpl(void)
95 _ContentTransferImpl::GetInstance(ContentTransfer& contentTransfer)
97 return contentTransfer.__pImpl;
100 const _ContentTransferImpl*
101 _ContentTransferImpl::GetInstance(const ContentTransfer& contentTransfer)
103 return contentTransfer.__pImpl;
107 _ContentTransferImpl::Construct(IContentTransferListener& listener)
109 // E_SUCCESS, E_OUT_OF_MEMORY, E_SYSTEM
112 result r = E_SUCCESS;
114 std::unique_ptr<_ContentDownloadHandler> pContentDownloadHandler(new (std::nothrow) _ContentDownloadHandler);
115 SysTryReturn(NID_CNT, pContentDownloadHandler != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
116 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadHandler.");
118 r = pContentDownloadHandler->Construct();
119 SysTryReturn(NID_CNT, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
120 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadHandler.");
122 std::unique_ptr<_ContentDownloadHandler[]> pContentDownloadHandlerSequence(new (std::nothrow) _ContentDownloadHandler[MAX_DOWNLOAD_COUNT]);
123 SysTryReturn(NID_CNT, pContentDownloadHandlerSequence != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
124 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadHandler array.");
126 for (int i = 0; i < MAX_DOWNLOAD_COUNT; i++)
128 r = (pContentDownloadHandlerSequence.get())[i].Construct();
129 SysTryReturn(NID_CNT, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
130 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadHandler array.");
133 std::unique_ptr<_ContentDownloadUserData> pContentDownloadUserData(new (std::nothrow) _ContentDownloadUserData);
134 SysTryReturn(NID_CNT, pContentDownloadUserData != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
135 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadUserData.");
137 std::unique_ptr<_ContentDownloadUserData[]> pContentDownloadUserDataSequence(new (std::nothrow) _ContentDownloadUserData[MAX_DOWNLOAD_COUNT]);
138 SysTryReturn(NID_CNT, pContentDownloadUserDataSequence != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
139 "[E_OUT_OF_MEMORY] Failed to construct ContentDownloadUserData.");
141 std::unique_ptr<_ContentTransferEvent> pTransferEvent(new (std::nothrow) _ContentTransferEvent);
142 SysTryReturn(NID_CNT, pTransferEvent != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
143 "[E_OUT_OF_MEMORY] Failed to construct ContentTransferEvent.");
145 r = pTransferEvent->AddListener(listener, true);
146 SysTryReturn(NID_CNT, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to perform AddListener to ContentTransferEvent.");
148 std::unique_ptr<ArrayList, AllElementsDeleter> pTransferInfoList(new (std::nothrow) ArrayList());
149 SysTryReturn(NID_CNT, pTransferInfoList != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
150 "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
152 r = pTransferInfoList->Construct();
153 SysTryReturn(NID_CNT, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
154 "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
156 std::unique_ptr<Timer> pTimer(new (std::nothrow) Timer());
157 SysTryReturn(NID_CNT, pTimer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct Timer.");
159 r = pTimer->Construct(*this);
160 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to construct Timer.", GetErrorMessage(r));
162 InitializeEmptySlotInternal(pContentDownloadUserData.get());
164 SysTryReturn(NID_CNT, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to perform initiate the download slot.");
166 __pContentDownloadHandler = std::move(pContentDownloadHandler);
167 __pContentDownloadHandlerSequence = std::move(pContentDownloadHandlerSequence);
169 __pContentDownloadUserData = std::move(pContentDownloadUserData);
170 __pContentDownloadUserDataSequence = std::move(pContentDownloadUserDataSequence);
172 __pTransferEvent = std::move(pTransferEvent);
174 __pTransferInfoList = std::move(pTransferInfoList);
176 __pTimer = std::move(pTimer);
182 _ContentTransferImpl::InitializeEmptySlotInternal(_ContentDownloadUserData* pUserData)
185 result r = E_SUCCESS;
187 String tempPath(CONTENT_DOWNLOAD_TEMP_DIRECTORY);
190 SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_SYSTEM,
191 "[E_SYSTEM] ContentDownloadUserData instance must not be null.");
193 pUserData->SetMaxCount(MAX_DOWNLOAD_COUNT);
197 for (i = 0; i < MAX_DOWNLOAD_COUNT; i++)
199 pUserData->SetCheckDownloading(i, false);
203 check = _FileImpl::IsFileExist(tempPath);
205 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r,
206 "[%s] Failed to perform IsFileExist operation for [%ls] path.", GetErrorMessage(r), tempPath.GetPointer());
210 r = Directory::Create(tempPath, false);
211 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r,
212 "[%s] Failed to perform Create operation for [%ls] path.", GetErrorMessage(r), tempPath.GetPointer());
221 _ContentTransferImpl::GetEmptySlotInternal(void) const
224 for (i = 0; i < MAX_DOWNLOAD_COUNT; i++)
226 if (!(__pContentDownloadUserData->GetCheckDownloading(i)))
235 _ContentTransferImpl::SetEmptySlotInternal(void)
237 for (int i = 0; i < MAX_DOWNLOAD_COUNT; i++)
239 if (!((__pContentDownloadUserDataSequence.get())[i].GetSlotFlag()))
241 __pContentDownloadUserData->SetCheckDownloading(i, false);
247 _ContentTransferImpl::SetUsedSlotInternal(int slot)
249 // TODO : If the error is occured in this command, set the mutex.
250 if (slot >= 0 && slot < MAX_DOWNLOAD_COUNT)
252 __pContentDownloadUserData->SetCheckDownloading(slot, true);
253 (__pContentDownloadUserDataSequence.get())[slot].SetSlotFlag(true);
258 _ContentTransferImpl::Cancel(RequestId reqId)
260 // E_SUCCESS, E_OBJ_NOT_FOUND, E_INVALID_ARG, E_INVALID_STATE
262 SysLog(NID_CNT, "Cancel the request ID(%d) in download.", reqId);
265 result r = E_SUCCESS;
266 String destFilePath = null;
267 ContentTransferInfo* pContentTransferInfo = null;
270 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
272 pContentTransferInfo = GetContentTransferInfoByRequestId(reqId);
273 SysTryReturn(NID_CNT, pContentTransferInfo != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
274 "[E_OBJ_NOT_FOUND] Unable to find the current Download operation.");
276 if (pContentTransferInfo->GetContentTransferStatus() == CONTENT_TRANSFER_STATUS_DOWNLOADING)
278 slot = pContentTransferInfo->GetSlotId();
279 r = (__pContentDownloadHandlerSequence.get())[slot].Cancel(pContentTransferInfo->GetRestRequestId(),
280 &(__pContentDownloadUserDataSequence.get())[slot]);
281 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to perform Cancel operation.");
285 // TODO : check timing issue.
287 SysLogException(NID_CNT, E_INVALID_STATE, "[E_INVALID_STATE] This [%d] request isn't under download.", reqId);
294 _ContentTransferImpl::SetDefaultTimeout(int sec)
307 _ContentTransferImpl::GetDefaultTimeout(void) const
313 _ContentTransferImpl::Remove(RequestId reqId)
315 // E_SUCCESS, E_OBJ_NOT_FOUND, E_OUT_OF_MEMORY, E_INVALID_STATE
317 SysLog(NID_CNT, "Remove the download request ID(%d) in queue.", reqId);
320 result r = E_SUCCESS;
321 ContentTransferInfo* pTransferInfo = null;
322 std::unique_ptr<IEnumerator> pEnum;
326 SysAssertf(__pTransferInfoList != null, "Not yet constructed. Construct() should be called before use.");
328 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
329 SysTryReturn(NID_CNT, pEnum != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
330 "[E_OUT_OF_MEMORY] Remove operation can not perform for [%d] request ID.", reqId);
332 while (pEnum->MoveNext() == E_SUCCESS)
334 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
335 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
336 "[E_OBJ_NOT_FOUND] ContentTransferInfo instance must not be null.");
338 if (pTransferInfo->GetRequestId() == reqId)
340 ContentTransferStatus status = pTransferInfo->GetContentTransferStatus();
341 if (status == CONTENT_TRANSFER_STATUS_DOWNLOAD_READY ||
342 status == CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED)
347 else if (status == CONTENT_TRANSFER_STATUS_DOWNLOADING)
349 SysLogException(NID_CNT, E_INVALID_STATE, "[E_INVALID_STATE] This [%d] request operation is under download.", reqId);
350 return E_INVALID_STATE;
358 SysLogException(NID_CNT, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] [%d] request operation cannot found.", reqId);
359 return E_OBJ_NOT_FOUND;
363 __pTransferInfoList->RemoveAt(findIdx, true);
365 SysTryReturn(NID_CNT, !IsFailed(r), r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
366 "[E_OBJ_NOT_FOUND] Failed to perform RemoveAt operation for a content in transfer info list.");
373 _ContentTransferImpl::RemoveAll(void)
375 // E_SUCCESS, E_OUT_OF_MEMORY, E_INVALID_STATE
377 SysLog(NID_CNT, "Remove all download request ID in queue.");
380 result r = E_SUCCESS;
381 ContentTransferInfo* pTransferInfo = null;
382 std::unique_ptr<IEnumerator> pTransferEnum;
383 std::unique_ptr<IEnumerator> pDeleteEnum;
384 ArrayList deleteList;
386 SysAssertf(__pTransferInfoList != null, "Not yet constructed. Construct() should be called before use.");
388 pTransferEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
389 SysTryReturn(NID_CNT, pTransferEnum != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
390 "[E_OUT_OF_MEMORY] Failed to perform GetEnumeratorN operation.");
392 while (pTransferEnum->MoveNext() == E_SUCCESS)
394 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pTransferEnum->GetCurrent());
395 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_INVALID_STATE, r,
396 "[E_INVALID_STATE] ContentTransferInfo instance must not be null.");
398 ContentTransferStatus status = pTransferInfo->GetContentTransferStatus();
399 if (status == CONTENT_TRANSFER_STATUS_DOWNLOAD_READY || status == CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED)
401 deleteList.Add(*pTransferInfo);
405 pDeleteEnum = std::unique_ptr<IEnumerator>(deleteList.GetEnumeratorN());
406 SysTryReturn(NID_CNT, pDeleteEnum != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
407 "[E_OUT_OF_MEMORY] Failed to perform GetEnumeratorN operation.");
409 while (pDeleteEnum->MoveNext() == E_SUCCESS)
411 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pDeleteEnum->GetCurrent());
412 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_INVALID_STATE, r,
413 "[E_INVALID_STATE] ContentTransferInfo instance must not be null.");
415 __pTransferInfoList->Remove(*pTransferInfo, true);
417 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_STATE, E_INVALID_STATE,
418 "[E_INVALID_STATE] Failed to perform Remove operation for a content in transfer info list.");
425 _ContentTransferImpl::CancelAll(void)
427 // E_SUCCESS, E_OUT_OF_MEMORY, E_INVALID_STATE
429 SysLog(NID_CNT, "Cancel the all request ID in download.");
432 result r = E_SUCCESS;
433 std::unique_ptr<IEnumerator> pEnum;
434 ContentTransferInfo* pTransferInfo = null;
436 SysAssertf(__pTransferInfoList != null && __pContentDownloadUserDataSequence != null,
437 "Not yet constructed. Construct() should be called before use.");
439 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
440 SysTryReturn(NID_CNT, pEnum != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
441 "[E_OUT_OF_MEMORY] Failed to perform GetEnumeratorN operation.");
443 while (pEnum->MoveNext() == E_SUCCESS)
445 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
446 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_INVALID_STATE, r,
447 "[E_INVALID_STATE] ContentTransferInfo instance must not be null.");
449 ContentTransferStatus status = pTransferInfo->GetContentTransferStatus();
450 if (status == CONTENT_TRANSFER_STATUS_DOWNLOADING)
452 SysLog(NID_CNT, "========== Canceled download slot : %d ==========", pTransferInfo->GetSlotId());
454 r = (__pContentDownloadHandlerSequence.get())[pTransferInfo->GetSlotId()].Cancel(
455 pTransferInfo->GetRestRequestId(), &(__pContentDownloadUserDataSequence.get())[pTransferInfo->GetSlotId()]);
456 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_STATE, E_INVALID_STATE,
457 "[E_INVALID_STATE] Failed to perform Cancel operation.");
465 _ContentTransferImpl::GetContentTransferInfoListN(void) const
467 std::unique_ptr<ArrayList, AllElementsDeleter> pTransferInfoList;
468 std::unique_ptr<IEnumerator> pEnum;
469 ContentTransferInfo* pTransferInfoCopy = null;
470 ContentTransferInfo* pTransferInfo = null;
472 SysAssertf(__pTransferInfoList != null, "Not yet constructed. Construct() should be called before use.");
474 pTransferInfoList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
475 SysTryLogReturn(NID_CNT, pTransferInfoList != null, null, "Failed to construct ArrayList.");
477 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
478 SysTryLogReturn(NID_CNT, pEnum != null, null, "Failed to perform GetEnumeratorN operation.");
480 while (pEnum->MoveNext() == E_SUCCESS)
482 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
483 SysTryLogReturn(NID_CNT, pTransferInfo != null, null, "ContentTransferInfo instance must not be null.");
485 pTransferInfoCopy = pTransferInfo->CopyN();
487 pTransferInfoList->Add(*pTransferInfoCopy);
490 return pTransferInfoList.release();
494 _ContentTransferImpl::GetContentTransferInfoListInProgressN(void) const
496 std::unique_ptr<ArrayList, AllElementsDeleter> pTransferInfoList;
497 std::unique_ptr<IEnumerator> pEnum;
498 ContentTransferInfo* pTransferInfoCopy = null;
499 ContentTransferInfo* pTransferInfo = null;
501 SysAssertf(__pTransferInfoList != null, "Not yet constructed. Construct() should be called before use.");
503 pTransferInfoList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
504 SysTryLogReturn(NID_CNT, pTransferInfoList != null, null, "Failed to construct ArrayList.");
506 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
507 SysTryLogReturn(NID_CNT, pEnum != null, null, "Failed to perform GetEnumerator operation.");
509 while (pEnum->MoveNext() == E_SUCCESS)
511 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
512 SysTryLogReturn(NID_CNT, pTransferInfo != null, null, "ContentTransferInfo instance must not be null.");
514 ContentTransferStatus status = pTransferInfo->GetContentTransferStatus();
515 if (status == CONTENT_TRANSFER_STATUS_DOWNLOADING)
517 pTransferInfoCopy = pTransferInfo->CopyN();
518 pTransferInfoList->Add(*pTransferInfoCopy);
522 return pTransferInfoList.release();
526 _ContentTransferImpl::SetProgressIntervalByPercent(int percent)
528 if (percent > 0 && percent <= 100)
539 _ContentTransferImpl::Download(const Utility::Uri& uri, int fileSize, const String& destFilePath, bool replace,
540 RequestId& requestID, IContentTransferListener* pListener, int sec)
542 // E_SUCCESS, E_INVALID_ARG, E_INVALID_STATE, E_ILLEGAL_ACCESS, E_FILE_ALREADY_EXIST, E_PRIVILEGE_DENIED, E_IN_PROGRESS
544 SysLog(NID_CNT, "Download operation start with listener.");
547 result r = E_SUCCESS;
548 String sourcePath(L"");
549 String destPath(L"");
550 String tempPath(L"");
552 String checkName(L"");
553 String checkPath(L"");
556 int lengthString = 0;
557 FileAttributes fileAttr;
559 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
561 SysTryReturn(NID_CNT, fileSize >= 0, r = E_INVALID_ARG, E_INVALID_ARG,
562 "[E_INVALID_ARG] The size of the source file is less than 0.");
563 SysTryReturn(NID_CNT, !destFilePath.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
564 "[E_INVALID_ARG] The destination file path is empty.");
565 SysTryReturn(NID_CNT, !uri.ToString().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
566 "[E_INVALID_ARG] The source URI is empty.");
567 SysTryReturn(NID_CNT, !uri.GetHost().IsEmpty() && !uri.GetPath().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
568 "[E_INVALID_ARG] The source URI is empty.");
569 SysTryReturn(NID_CNT, sec >= 0, r = E_INVALID_ARG, E_INVALID_ARG,
570 "[E_INVALID_ARG] The timeout value is less than 0.");
572 destPath = destFilePath;
574 if (_FileImpl::IsSystemPath(destPath))
576 SysLogException(NID_CNT, E_ILLEGAL_ACCESS, "[E_ILLEGAL_ACCESS] [%ls] path isn't permitted.", destFilePath.GetPointer());
578 return E_ILLEGAL_ACCESS;
581 if (_FileImpl::IsFileExist(destPath))
585 SysLogException(NID_CNT, E_FILE_ALREADY_EXIST, "[E_FILE_ALREADY_EXIST] The download file is already exist.");
587 return E_FILE_ALREADY_EXIST;
590 SysTryReturn(NID_CNT, _FileImpl::IsMediaPath(destPath) == true || _FileImpl::IsAppPath(destPath) == true,
591 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The file path is not a media or an app path.");
593 // read-only, directory or file
594 // check whether the destination file is read-only.
595 // check whether the destination path without the file extension is a directory or not.
596 r = _FileImpl::GetAttributes(destFilePath, fileAttr);
597 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
598 "[E_INVALID_ARG] Failed to perform GetAttributes operation for the destination path. ");
599 SysTryReturn(NID_CNT, !fileAttr.IsDirectory(), r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
600 "[E_ILLEGAL_ACCESS] The file path is directory.");
601 SysTryReturn(NID_CNT, !fileAttr.IsReadOnly(), r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
602 "[E_ILLEGAL_ACCESS] The file path is read only.");
606 // check the destination file path
608 SysTryReturn(NID_CNT, _FileImpl::IsMediaPath(destPath) == true || _FileImpl::IsAppPath(destPath) == true,
609 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The file path is not a media or an app path.");
612 // check whether the destination path includes the file name
613 checkName = _FileImpl::GetFileName(destPath);
614 SysTryReturn(NID_CNT, !checkName.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
615 "[E_INVALID_ARG] The file name is empty.");
617 // 2nd: parent directory
618 lengthString = destFilePath.String::GetLength();
619 r = destPath.String::LastIndexOf('/', lengthString - 1, copySize);
620 SysTryReturn(NID_CNT, copySize > 0 && copySize < maxSize, r = E_INVALID_ARG, E_INVALID_ARG,
621 "[E_INVALID_ARG] The length of path is invalid size.");
623 // copy the parent path
624 r = destPath.SubString(0, copySize + 1, checkPath);
625 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
626 "[E_INVALID_ARG] Failed to perform SubString operation.");
628 // check the validation of the parent path
629 r = _FileImpl::GetAttributes(checkPath, fileAttr);
630 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
631 "[E_INVALID_ARG] Failed to perform GetAttributes operation.");
632 SysTryReturn(NID_CNT, fileAttr.IsDirectory(), r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
633 "[E_ILLEGAL_ACCESS] The path is not directory path.");
636 __destFilePath = destPath;
637 scheme = uri.GetScheme();
639 if (scheme.CompareTo(L"http") == 0 || scheme.CompareTo(L"https") == 0)
641 sourcePath = uri.GetEncodedString();
643 r = AddTransferItem(uri, destPath, sourcePath, fileSize, CONTENT_TRANSFER_STATUS_DOWNLOAD_READY,
644 pListener, sec, false, requestID);
645 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to add a transfer item to queue.", GetErrorMessage(r));
647 // TODO : if timer is started, it doesn't need call it..
648 ContentTransferHandler();
650 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to handle the content transfer.", GetErrorMessage(r));
654 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] It is unsupported URI(%ls) address format.", uri.ToString().GetPointer());
656 return E_INVALID_ARG;
663 _ContentTransferImpl::DownloadToBuffer(const Utility::Uri& uri, int fileSize, RequestId& reqId,
664 IContentTransferListener* pListener, int sec)
666 // E_SUCCESS, E_INVALID_ARG, E_INVALID_STATE, E_PRIVILEGE_DENIED
668 SysLog(NID_CNT, "DownloadToBuffer operation start with listener.");
671 result r = E_SUCCESS;
672 String tempFilePath(CONTENT_DOWNLOAD_TEMP_FILE_PATH);
673 String sourcePath(L"");
677 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
679 SysTryReturn(NID_CNT, fileSize >= 0, r = E_INVALID_ARG, E_INVALID_ARG,
680 "[E_INVALID_ARG] The size of the source file is less than 0.");
681 SysTryReturn(NID_CNT, !uri.ToString().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
682 "[E_INVALID_ARG] The source URI is empty.");
683 SysTryReturn(NID_CNT, !uri.GetHost().IsEmpty() && !uri.GetPath().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
684 "[E_INVALID_ARG] The source URI is empty.");
685 SysTryReturn(NID_CNT, sec >= 0, E_INVALID_ARG, E_INVALID_ARG,
686 "[E_INVALID_ARG] The timeout value is less than 0.");
688 scheme = uri.GetScheme();
690 if (scheme.CompareTo(L"http") == 0 || scheme.CompareTo(L"https") == 0)
692 r = _SystemTimeImpl::GetTicks(ticks);
693 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_STATE, E_INVALID_STATE,
694 "[E_INVALID_STATE] Failed to perform GetTicks operation.");
696 r = tempFilePath.Append(ticks);
697 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_STATE, E_INVALID_STATE,
698 "[E_INVALID_STATE] Failed to append long long type to temporary file path.");
700 r = tempFilePath.Append(downloadRequestId);
701 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
702 "[E_INVALID_ARG] Failed to append int type to temporary file path.");
704 sourcePath = uri.GetEncodedString();
706 r = AddTransferItem(uri, tempFilePath, sourcePath, fileSize,
707 CONTENT_TRANSFER_STATUS_DOWNLOAD_READY, pListener, sec, true, reqId);
708 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
709 "[E_INVALID_ARG] Failed to add a transfer item to queue.");
711 // TODO : if timer is started, it doesn't need call it..
712 __destFilePath = tempFilePath;
713 ContentTransferHandler();
715 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to handle the content transfer.", GetErrorMessage(r));
719 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] It is unsupported URI(%ls) address format.", uri.ToString().GetPointer());
721 return E_INVALID_ARG;
728 _ContentTransferImpl::Download(const Utility::Uri& uri, const String& filePath,
729 RequestId& reqId, bool replace, int timeout, int progressInterval)
731 // E_SUCCESS, E_PRIVILEGE_DENIED, E_INVALID_ARG, E_INVALID_STATE, E_ILLEGAL_ACCESS, E_FILE_ALREADY_EXIST, E_IN_PROGRESS, E_OUT_OF_MEMORY, E_SYSTEM
733 SysLog(NID_CNT, "Download operation start without listener.");
736 result r = E_SUCCESS;
737 String srcFilePath(L"");
740 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
742 SysTryReturn(NID_CNT, !filePath.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
743 "[E_INVALID_ARG] The destination file path is empty.");
744 SysTryReturn(NID_CNT, !uri.ToString().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
745 "[E_INVALID_ARG] The source URI is empty.");
746 SysTryReturn(NID_CNT, !uri.GetHost().IsEmpty() && !uri.GetPath().IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
747 "[E_INVALID_ARG] The source URI is empty.");
748 SysTryReturn(NID_CNT, timeout >= 0, r = E_INVALID_ARG, E_INVALID_ARG,
749 "[E_INVALID_ARG] The timeout value is less than 0.");
750 SysTryReturn(NID_CNT, progressInterval >= 0 && progressInterval <= 100, r = E_INVALID_ARG, E_INVALID_ARG,
751 "[E_INVALID_ARG] The progress interval is less than 0.");
753 if (!_AppInfo::IsOspCompat())
755 if (filePath.StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA, 0) || filePath.StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA_MMC, 0) ||
756 filePath.StartsWith(CONTENT_DOWNLOAD_PATH_HOME, 0) || filePath.StartsWith(CONTENT_DOWNLOAD_PATH_HOME_MMC, 0))
758 SysLogException(NID_CNT, E_INVALID_ARG,
759 "[E_INVALID_ARG] /Media/ , /Storagecard/Media/ , /Home/ , /HomeExt/ are not supported from Tizen 2.0.");
761 return E_INVALID_ARG;
763 if (!(filePath.StartsWith(Tizen::App::App::GetInstance()->GetAppRootPath(), 0) || filePath.StartsWith(Environment::GetMediaPath(), 0) ||
764 filePath.StartsWith(Environment::GetExternalStoragePath(), 0)))
766 SysLogException(NID_CNT, E_INVALID_ARG,
767 "[E_INVALID_ARG] The [%ls] path is not supported.", filePath.GetPointer());
769 return E_INVALID_ARG;
775 if (filePath.StartsWith(Tizen::App::App::GetInstance()->GetAppRootPath(), 0) || filePath.StartsWith(Environment::GetMediaPath(), 0) ||
776 filePath.StartsWith(Environment::GetExternalStoragePath(), 0))
778 SysLogException(NID_CNT, E_INVALID_ARG,
779 "[E_INVALID_ARG] This [%ls] path can be used from Tizen 2.0.", filePath.GetPointer());
781 return E_INVALID_ARG;
783 if (!(filePath.StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA, 0) || filePath.StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA_MMC, 0) ||
784 filePath.StartsWith(CONTENT_DOWNLOAD_PATH_HOME, 0) || filePath.StartsWith(CONTENT_DOWNLOAD_PATH_HOME_MMC, 0)))
786 SysLogException(NID_CNT, E_INVALID_ARG,
787 "[E_INVALID_ARG] The [%ls] is not supported.", filePath.GetPointer());
789 return E_INVALID_ARG;
793 __destFilePath = filePath;
795 r = CheckDestPath(__destFilePath, replace);
796 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to perform CheckDestPath operation.", GetErrorMessage(r));
798 scheme = uri.GetScheme();
800 if (scheme.CompareTo(L"http") == 0 || scheme.CompareTo(L"https") == 0)
802 srcFilePath = uri.GetEncodedString();
804 r = AddTransferItem(uri, __destFilePath, srcFilePath, CONTENT_TRANSFER_STATUS_DOWNLOAD_READY,
805 timeout, progressInterval, false, reqId);
806 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to add a transfer item to queue.", GetErrorMessage(r));
810 SysLogException(NID_CNT, E_INVALID_ARG,
811 "[E_INVALID_ARG] It is unsupported URI(%ls) address format.", uri.ToString().GetPointer());
813 return E_INVALID_ARG;
816 ContentTransferHandler();
818 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to handle the content transfer.", GetErrorMessage(r));
824 _ContentTransferImpl::DownloadToBuffer(const Utility::Uri& uri, RequestId& reqId, int timeout, int progressInterval)
826 // E_SUCCESS, E_PRIVILEGE_DENIED, E_INVALID_ARG, E_INVALID_STATE, E_ILLEGAL_ACCESS, E_IN_PROGRESS, E_OUT_OF_MEMORY
828 SysLog(NID_CNT, "DownloadToBuffer operation start without listener.");
830 result r = E_SUCCESS;
831 String srcFilePath(L"");
832 String tempDestFilePath(CONTENT_DOWNLOAD_TEMP_FILE_PATH);
836 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
838 SysTryReturn(NID_CNT, !uri.ToString().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
839 "[E_INVALID_ARG] The source URI is empty.");
840 SysTryReturn(NID_CNT, !uri.GetHost().IsEmpty() && !uri.GetPath().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
841 "[E_INVALID_ARG] The source URI is empty.");
842 SysTryReturn(NID_CNT, timeout >= 0, E_INVALID_ARG, E_INVALID_ARG,
843 "[E_INVALID_ARG] The timeout value is less than 0.");
844 SysTryReturn(NID_CNT, progressInterval >= 0 && progressInterval <= 100, E_INVALID_ARG, E_INVALID_ARG,
845 "[E_INVALID_ARG] The progress interval is less than 0.");
847 scheme = uri.GetScheme();
848 if (scheme.CompareTo(L"http") == 0 || scheme.CompareTo(L"https") == 0)
850 r = _SystemTimeImpl::GetTicks(ticks);
851 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to perform GetTicks operation.", GetErrorMessage(r));
853 r = tempDestFilePath.Append(ticks);
854 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to append long long type to temporary file path.", GetErrorMessage(r));
856 r = tempDestFilePath.Append(downloadRequestId);
857 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to append int type to temporary file path.", GetErrorMessage(r));
859 srcFilePath = uri.GetEncodedString();
861 r = AddTransferItem(uri, tempDestFilePath, srcFilePath, CONTENT_TRANSFER_STATUS_DOWNLOAD_READY,
862 timeout, progressInterval, true, reqId);
863 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to add a transfer item to queue.", GetErrorMessage(r));
867 SysLogException(NID_CNT, E_INVALID_ARG,
868 "[E_INVALID_ARG] It is unsupported URI(%ls) address format.", uri.ToString().GetPointer());
870 return E_INVALID_ARG;
873 ContentTransferHandler();
875 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to handle the content transfer.", GetErrorMessage(r));
881 _ContentTransferImpl::OnTimerExpired(Runtime::Timer& timer)
885 check = ContentTransferHandler();
887 SysAssertf(__pTimer != null, "Not yet constructed. Construct() should be called before use.");
891 __pTimer->Start(CONTENT_DOWNLOAD_CHECKING_PERIOD);
892 __timerStarted = true;
896 __timerStarted = false;
901 _ContentTransferImpl::GetRequestId(void) const
903 return ++downloadRequestId;
907 _ContentTransferImpl::GetContentTransferInfoByRequestId(RequestId requestId) const
910 result r = E_SUCCESS;
911 std::unique_ptr<IEnumerator> pEnum;
912 ContentTransferInfo* pTransferInfo = null;
914 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
919 SysLogException(NID_CNT, r, "[%s] Failed to perform GetEnumeratorN operation.", GetErrorMessage(r));
923 while (pEnum->MoveNext() == E_SUCCESS)
925 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
926 SysTryReturn(NID_CNT, pTransferInfo != null, null, E_INVALID_ARG,
927 "[E_INVALID_ARG] ContentTransferInfo instance must not be null.");
929 if (pTransferInfo->GetRequestId() == requestId)
933 pTransferInfo = null;
936 return pTransferInfo;
940 _ContentTransferImpl::StartTimer(void)
943 result r = E_SUCCESS;
945 if (!(__timerStarted))
947 r = __pTimer->Start(CONTENT_DOWNLOAD_CHECKING_PERIOD);
948 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, "[E_INVALID_ARG] Failed to start timer.");
950 __timerStarted = true;
955 _ContentTransferImpl::GetEmptySlot(int& slot) const
958 result r = E_SUCCESS;
960 slot = GetEmptySlotInternal();
962 if (slot < 0 || slot > MAX_DOWNLOAD_COUNT)
965 SysLog(NID_CNT, "Failed to get empty slot for download.");
972 _ContentTransferImpl::CheckDestPath(const String& destPath, bool replace)
974 result r = E_SUCCESS;
975 String destFilePath(L"");
976 String checkName(L"");
977 String checkPath(L"");
978 FileAttributes fileAttr;
981 int lengthString = 0;
984 destFilePath = destPath;
985 SysTryReturn(NID_CNT, !destFilePath.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
986 "[E_INVALID_ARG] The destination file path is empty.");
988 if (!_AppInfo::IsOspCompat())
990 if (!(destFilePath.String::StartsWith(Environment::GetMediaPath(), 0) ||
991 destFilePath.String::StartsWith(Environment::GetExternalStoragePath(), 0) ||
992 destFilePath.String::StartsWith(Tizen::App::App::GetInstance()->GetAppRootPath(), 0)))
994 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] The destination path is not allowed path.");
995 return E_INVALID_ARG;
1000 // the allowed destination path
1001 // /Media, /Storagecard/Media
1003 if (!(destFilePath.String::StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA, 0) ||
1004 destFilePath.String::StartsWith(CONTENT_DOWNLOAD_PATH_MEDIA_MMC, 0) ||
1005 destFilePath.String::StartsWith(CONTENT_DOWNLOAD_PATH_HOME, 0) ||
1006 destFilePath.String::StartsWith(CONTENT_DOWNLOAD_PATH_HOME_MMC, 0)))
1008 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] The destination path is not allowed path.");
1009 return E_INVALID_ARG;
1014 if (_FileImpl::IsFileExist(destFilePath) == true)
1016 // not allowed overwrite
1017 if (replace != true)
1019 SysLogException(NID_CNT, E_FILE_ALREADY_EXIST, "[E_FILE_ALREADY_EXIST] The file already exists in destination path.");
1020 return E_FILE_ALREADY_EXIST;
1023 // allowed overwrite
1024 r = _FileImpl::GetAttributes(destFilePath, fileAttr);
1025 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
1026 "[E_INVALID_ARG] Failed to perform GetAttributes operation. ");
1027 SysTryReturn(NID_CNT, !(fileAttr.IsDirectory()), r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
1028 "[E_ILLEGAL_ACCESS] The file path is directory path.");
1029 SysTryReturn(NID_CNT, !(fileAttr.IsReadOnly()), r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
1030 "[E_ILLEGAL_ACCESS] The file can only read.");
1035 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1037 // check the destination file path
1039 SysTryReturn(NID_CNT, _FileImpl::IsMediaPath(destFilePath) == true || _FileImpl::IsAppPath(destFilePath) == true,
1040 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The file path is not a media or an app path.");
1044 // check whether the destination path includes the file name
1045 checkName = _FileImpl::GetFileName(destFilePath);
1046 SysTryReturn(NID_CNT, !checkName.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG,
1047 "[E_INVALID_ARG] Failed to get the file name from a path.");
1049 // 2nd: parent directory
1050 lengthString = destFilePath.String::GetLength();
1051 r = destFilePath.String::LastIndexOf('/', lengthString - 1, copySize);
1052 SysTryReturn(NID_CNT, copySize > 0 && copySize < maxSize, r = E_INVALID_ARG, E_INVALID_ARG,
1053 "[E_INVALID_ARG] Failed to get the size of path.");
1055 // copy the parent path
1056 r = destFilePath.SubString(0, copySize + 1, checkPath);
1057 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
1058 "[E_INVALID_ARG] Failed to perform SubString operation.");
1060 if (!_AppInfo::IsOspCompat())
1062 SysTryReturn(NID_CNT, destFilePath.String::StartsWith(Environment::GetMediaPath(), 0) ||
1063 destFilePath.String::StartsWith(Environment::GetExternalStoragePath(), 0) ||
1064 _FileImpl::IsAppPath(destFilePath) == true,
1065 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The path is invalid media path.");
1067 if (!(_FileImpl::IsFileExist(checkPath)))
1069 r = dir.Create(checkPath);
1070 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
1071 "[E_INVALID_ARG] Failed to create the directory.");
1075 // check the validation of the parent path
1076 r = _FileImpl::GetAttributes(checkPath, fileAttr);
1077 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG,
1078 "[E_INVALID_ARG] Failed to perform GetAttributes operation.");
1079 SysTryReturn(NID_CNT, fileAttr.IsDirectory() == true, r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
1080 "[E_ILLEGAL_ACCESS] The path is not a directory path.");
1087 _ContentTransferImpl::CheckDownloadStatus(const Uri& uri, const String& destPath)
1090 std::unique_ptr<IEnumerator> pEnum;
1091 ContentTransferInfo* pTransferInfoTemp = null;
1092 ContentTransferStatus statusTemp = CONTENT_TRANSFER_STATUS_NONE;
1093 result r = E_SUCCESS;
1095 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
1096 SysTryReturn(NID_CNT, pEnum != null, r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
1097 "[%s] Failed to perform GetEnumeratorN operation.", GetErrorMessage(r));
1099 while (pEnum->MoveNext() == E_SUCCESS)
1101 pTransferInfoTemp = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
1102 SysTryReturn(NID_CNT, pTransferInfoTemp != null, r = E_INVALID_STATE, E_INVALID_STATE,
1103 "[E_INVALID_STATE] ContentTransferInfo instance must not be null.");
1105 statusTemp = pTransferInfoTemp->GetContentTransferStatus();
1107 if (statusTemp == CONTENT_TRANSFER_STATUS_DOWNLOAD_READY ||
1108 statusTemp == CONTENT_TRANSFER_STATUS_DOWNLOADING)
1110 if (destPath.CompareTo(pTransferInfoTemp->GetDestPath()) == 0)
1112 if (uri.CompareTo(pTransferInfoTemp->GetUri()) == 0)
1114 SysLogException(NID_CNT, E_IN_PROGRESS, "[E_IN_PROGRESS] Transfer is in progress.");
1115 return E_IN_PROGRESS;
1119 SysLogException(NID_CNT, E_ILLEGAL_ACCESS, "[E_ILLEGAL_ACCESS] Access to source is illegal access.");
1120 return E_ILLEGAL_ACCESS;
1130 _ContentTransferImpl::AddTransferItem(const Uri& uri, const String& destPath, const String& sourcePath, int sourceFileSize,
1131 ContentTransferStatus status, IContentTransferListener* pListener, int sec, bool isBuffer, RequestId& requestId)
1134 result r = E_SUCCESS;
1135 std::unique_ptr<IEnumerator> pEnum;
1136 std::unique_ptr<ContentTransferInfo> pTransferInfo;
1137 ContentTransferInfo* pTransferInfoTemp = null;
1138 ContentTransferStatus statusTemp = CONTENT_TRANSFER_STATUS_NONE;
1139 requestId = INVALID_REQUEST_ID;
1141 SysAssertf(__pTransferInfoList != null, "Not yet constructed. Construct() should be called before use.");
1143 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
1144 SysTryReturn(NID_CNT, pEnum != null, r = E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS,
1145 "[E_ILLEGAL_ACCESS] Failed to perform GetEnumeratorN operation.");
1147 while (pEnum->MoveNext() == E_SUCCESS)
1149 pTransferInfoTemp = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
1150 SysTryReturn(NID_CNT, pTransferInfoTemp != null, r = E_INVALID_ARG, E_INVALID_ARG,
1151 "[E_INVALID_ARG] ContentTransferInfo instance must not be null.");
1153 statusTemp = pTransferInfoTemp->GetContentTransferStatus();
1155 if (statusTemp == CONTENT_TRANSFER_STATUS_DOWNLOAD_READY || statusTemp == CONTENT_TRANSFER_STATUS_DOWNLOADING)
1157 if (destPath.CompareTo(pTransferInfoTemp->GetDestPath()) == 0)
1159 if (uri.CompareTo(pTransferInfoTemp->GetUri()) == 0)
1161 SysLogException(NID_CNT, E_IN_PROGRESS, "[E_IN_PROGRESS] The request is already in progress.");
1162 return E_IN_PROGRESS;
1166 SysLogException(NID_CNT, E_ILLEGAL_ACCESS,
1167 "[E_ILLEGAL_ACCESS] The destination path [%ls] is already in progress.", destPath.GetPointer());
1168 return E_ILLEGAL_ACCESS;
1174 pTransferInfo = std::unique_ptr<ContentTransferInfo>(new (std::nothrow) ContentTransferInfo);
1175 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
1176 "[E_OUT_OF_MEMORY] Failed to construct ContentTransferInfo.");
1178 requestId = GetRequestId();
1180 pTransferInfo->SetAllInfo(requestId, uri, destPath, sourcePath, sourceFileSize, status);
1184 pTransferInfo->SetListener(pListener);
1190 pTransferInfo->SetTimeout(sec);
1195 pTransferInfo->SetTimeout(0);
1198 if (__percent > 0 && __percent <= 100)
1200 pTransferInfo->SetProgressInterval(__percent);
1204 pTransferInfo->SetProgressInterval(0);
1207 if (isBuffer == true)
1209 __isBuffer = isBuffer;
1210 pTransferInfo->SetIsBuffer(true);
1213 r = __pTransferInfoList->Add(*(pTransferInfo.release()));
1214 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to add item to info list.");
1220 _ContentTransferImpl::AddTransferItem(const Uri& uri, const String& destPath, const String& sourcePath,
1221 ContentTransferStatus status, int timeout, int interval, bool isBuffer, RequestId& requestId)
1224 result r = E_SUCCESS;
1225 std::unique_ptr<ContentTransferInfo> pTransferInfo;
1226 requestId = INVALID_REQUEST_ID;
1228 r = CheckDownloadStatus(uri, destPath);
1229 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to perform CheckDownloadStatus operation.", GetErrorMessage(r));
1231 // pTransferInfo will be deallocated in the Response routine
1232 pTransferInfo = std::unique_ptr<ContentTransferInfo>(new (std::nothrow) ContentTransferInfo);
1233 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
1234 "[%s] Failed to construct ContentTransferInfo.", GetErrorMessage(r));
1236 requestId = GetRequestId();
1238 pTransferInfo->SetAllInfo(requestId, uri, destPath, sourcePath, 0, status);
1242 __timeout = timeout;
1243 pTransferInfo->SetTimeout(timeout);
1248 pTransferInfo->SetTimeout(0);
1251 if (interval > 0 && interval <= 100)
1253 __percent = interval;
1254 pTransferInfo->SetProgressInterval(interval);
1259 pTransferInfo->SetProgressInterval(0);
1262 if (isBuffer == true)
1264 __isBuffer = isBuffer;
1265 pTransferInfo->SetIsBuffer(true);
1268 r = __pTransferInfoList->Add(*(pTransferInfo.release()));
1269 SysTryReturn(NID_CNT, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to add item to info list.");
1275 _ContentTransferImpl::SetDownloadSlotInfo(ContentTransferInfo* pTransferInfo, int& slot)
1278 result r = E_SUCCESS;
1279 String destPath(L"");
1281 SysAssertf(__pContentDownloadHandler != null, "Not yet constructed. Construct() should be called before use.");
1283 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
1284 "[E_INVALID_ARG] ContentTransferInfo instance must not be null.");
1286 destPath = pTransferInfo->GetDestPath();
1288 r = GetEmptySlot(slot);
1289 SysLog(NID_CNT, "Set the download slot [%d].", slot);
1290 SysTryReturn(NID_CNT, !IsFailed(r) && slot != -1, r = E_INVALID_ARG, E_INVALID_ARG,
1291 "[E_INVALID_ARG] Failed to get the empty slot.");
1293 SetUsedSlotInternal(slot);
1295 (__pContentDownloadHandlerSequence.get())[slot].SetSlot(slot);
1296 (__pContentDownloadHandlerSequence.get())[slot].SetRequestId(pTransferInfo->GetRequestId());
1297 (__pContentDownloadHandlerSequence.get())[slot].SetTimeout(pTransferInfo->GetTimeout());
1298 (__pContentDownloadHandlerSequence.get())[slot].SetProgressIntervalByPercent(pTransferInfo->GetProgressInterval());
1299 (__pContentDownloadHandlerSequence.get())[slot].SetDownloadPath(destPath);
1301 (__pContentDownloadUserDataSequence.get())[slot].SetSlot(slot);
1302 (__pContentDownloadUserDataSequence.get())[slot].SetContentTransferInfo(pTransferInfo);
1303 (__pContentDownloadUserDataSequence.get())[slot].SetRequestId(pTransferInfo->GetRequestId());
1304 (__pContentDownloadUserDataSequence.get())[slot].SetPercent(pTransferInfo->GetProgressInterval());
1305 (__pContentDownloadUserDataSequence.get())[slot].SetContentTransferEvent(__pTransferEvent.get());
1306 (__pContentDownloadUserDataSequence.get())[slot].SetDestPath(destPath);
1308 if (__isBuffer == true)
1310 (__pContentDownloadUserDataSequence.get())[slot].SetDownloadBufferFlag(true);
1313 pTransferInfo->SetSlotId(slot);
1319 _ContentTransferImpl::RequestErrorEvent(result requestErrorCode, ContentTransferInfo* pTransferInfo)
1322 result r = E_SUCCESS;
1323 std::unique_ptr<_ContentTransferEventArg> pTransferEventArg;
1325 pTransferEventArg = std::unique_ptr<_ContentTransferEventArg>(new (std::nothrow) _ContentTransferEventArg);
1326 SysTryReturn(NID_CNT, pTransferEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
1327 "[E_OUT_OF_MEMORY] Failed to construct ContentTransferEventArg.");
1329 pTransferEventArg->SetResult(requestErrorCode);
1331 if (pTransferInfo->GetIsBuffer() == true)
1333 pTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_DOWNLOAD_TO_BUFFER_COMPLETED);
1338 pTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_DOWNLOAD_COMPLETED);
1341 pTransferEventArg->SetRequestId(pTransferInfo->GetRequestId());
1343 r = __pTransferEvent->Fire(*(pTransferEventArg.release()));
1344 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to perform fire operation on ContentTransferEvent.", GetErrorMessage(r));
1346 pTransferInfo->SetDownloadStatus(CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED);
1352 _ContentTransferImpl::RemoveCompletedTransferInfo(ArrayList* pTransferInfoList)
1355 std::unique_ptr<IEnumerator> pEnum;
1356 ContentTransferInfo* pTransferInfo = null;
1357 result r = E_SUCCESS;
1359 if (pTransferInfoList == null)
1364 pEnum = std::unique_ptr<IEnumerator>(pTransferInfoList->GetEnumeratorN());
1365 SysTryReturn(NID_CNT, pEnum != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
1366 "[E_OUT_OF_MEMORY] Failed to perform GetEnumeratorN operation.");
1368 while (pEnum->MoveNext() == E_SUCCESS)
1370 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
1371 SysTryReturn(NID_CNT, pTransferInfo != null, r = E_INVALID_ARG, r,
1372 "[E_INVALID_ARG] ContentTransferInfo instance must not be null.");
1374 __pTransferInfoList->Remove(*pTransferInfo, true);
1375 r = GetLastResult();
1376 SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Failed to remove a item from transfer info list.", GetErrorMessage(r));
1383 _ContentTransferImpl::ContentTransferHandler(void)
1386 result r = E_SUCCESS;
1387 std::unique_ptr<IEnumerator> pEnum;
1388 std::unique_ptr<ArrayList> pDeleteList;
1389 ContentTransferInfo* pTransferInfo = null;
1390 ContentTransferStatus status = CONTENT_TRANSFER_STATUS_NONE;
1391 int downloadCount = 0;
1392 RequestId restRequestId = INVALID_REQUEST_ID;
1397 String destPath(L"");
1398 String sourcePath(L"");
1400 pEnum = std::unique_ptr<IEnumerator>(__pTransferInfoList->GetEnumeratorN());
1406 while (pEnum->MoveNext() == E_SUCCESS)
1408 pTransferInfo = dynamic_cast<ContentTransferInfo*>(pEnum->GetCurrent());
1409 SysTryReturn(NID_CNT, pTransferInfo != null, check, E_INVALID_ARG,
1410 "[E_INVALID_ARG] ContentTransferInfo instance must not be null.");
1412 status = pTransferInfo->GetContentTransferStatus();
1414 if (status == CONTENT_TRANSFER_STATUS_DOWNLOADING)
1417 if (downloadCount == MAX_DOWNLOAD_COUNT)
1427 else if (status == CONTENT_TRANSFER_STATUS_DOWNLOAD_READY)
1432 r = GetLastResult();
1433 SysTryReturn(NID_CNT, !IsFailed(r), check, r, "[%s] Failed to perform StartTimer operation.", GetErrorMessage(r));
1439 // get empty buffer slot from download buffer
1440 // set slot information
1441 r = SetDownloadSlotInfo(pTransferInfo, slot);
1444 SysLogException(NID_CNT, r, "[%s] Failed to find an empty slot.", GetErrorMessage(r));
1451 SysLogException(NID_CNT, r, "[%s] Propagating.", GetErrorMessage(r));
1455 destPath = pTransferInfo->GetDestPath();
1456 sourcePath = pTransferInfo->GetUri().ToString();
1457 (__pContentDownloadHandlerSequence.get())[slot].SetSlot(slot);
1460 restRequestId = (__pContentDownloadHandlerSequence.get())[slot].HttpDownload(sourcePath,
1461 pTransferInfo->GetSourceFileSize(), destPath, &(__pContentDownloadUserDataSequence.get())[slot]);
1462 r = GetLastResult();
1463 SysTryLog(NID_CNT, restRequestId != INVALID_REQUEST_ID,
1464 "[%s] The rest request ID is invalid.", GetErrorMessage(r));
1468 r = RequestErrorEvent(r, pTransferInfo);
1469 SysTryReturn(NID_CNT, !IsFailed(r), check, E_INVALID_ARG,
1470 "[E_INVALID_ARG] Failed to perform RequestErrorEvent operation.");
1472 (__pContentDownloadUserDataSequence.get())[slot].SetSlotFlag(false);
1480 pTransferInfo->SetRestRequestId(restRequestId);
1481 pTransferInfo->SetDownloadStatus(CONTENT_TRANSFER_STATUS_DOWNLOADING);
1484 if (downloadCount == MAX_DOWNLOAD_COUNT)
1493 else if (status == CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED)
1495 SetEmptySlotInternal();
1497 // TODO : should i check to sync
1498 if (pDeleteList == null)
1500 pDeleteList = std::unique_ptr<ArrayList>(new (std::nothrow) ArrayList());
1501 SysTryReturn(NID_CNT, !IsFailed(r), check, E_OUT_OF_MEMORY,
1502 "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
1504 pDeleteList->Add(*pTransferInfo);
1509 r = RemoveCompletedTransferInfo(pDeleteList.get());
1510 SysTryReturn(NID_CNT, !IsFailed(r), check, E_INVALID_ARG,
1511 "[E_INVALID_ARG] Failed to remove the completed transfer info.");