Fix : The original content is restored again if CreateContent API fail
[platform/framework/native/content.git] / src / FCnt_ContentDownloadListener.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  * @file                FCnt_ContentDownloadListener.cpp
18  * @brief               This is the implementation file for the %_ContentDownloadListener class.
19  *
20  * This file contains implementation of the %_ContentDownloadListener class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FIoDirectory.h>
26 #include <FSysEnvironment.h>
27 #include <FCntImageContentInfo.h>
28 #include <FCntVideoContentInfo.h>
29 #include <FCntAudioContentInfo.h>
30 #include <FCntOtherContentInfo.h>
31 #include <FApp_AppInfo.h>
32 #include <FCnt_ContentManagerImpl.h>
33 #include <FCnt_ContentManagerUtilImpl.h>
34 #include <FIo_FileImpl.h>
35 #include "FCnt_ContentDownloadListener.h"
36
37 using namespace Tizen;
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Io;
42 using namespace Tizen::System;
43 using namespace Tizen::Net;
44 using namespace Tizen::Net::Http;
45
46 namespace Tizen { namespace Content
47 {
48
49 static const int INVALID_PROGRESS_INTERVAL = -1;
50 static const int CONTENT_TRANSFER_COUNTER = 20;
51
52 _ContentDownloadListener::_ContentDownloadListener(void)
53 {
54 }
55 _ContentDownloadListener::~_ContentDownloadListener(void)
56 {
57 }
58
59 void
60 _ContentDownloadListener::OnTransactionReadyToRead(HttpSession& httpSession,
61                                                                                                    HttpTransaction& httpTransaction, int availableBodyLen)
62 {
63         SysLog(NID_CNT, "OnTransactionReadyToRead event fired.");
64         ClearLastResult();
65         result r = E_SUCCESS;
66         std::unique_ptr<File> pFile;
67         std::unique_ptr<ByteBuffer> pBody;
68         HttpResponse* pHttpResponse = null;
69
70         _ContentDownloadUserData* pUserData = dynamic_cast<_ContentDownloadUserData*>(httpTransaction.GetUserObject());
71         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_SYSTEM,
72                         "[E_SYSTEM] ContentDownloadUserData instance must not be null.");
73
74         SysLog(NID_CNT, "##### Request ID : %d #####", pUserData->GetRequestId());
75         SysLog(NID_CNT, "##### Download path : %ls #####", pUserData->GetDestPath().GetPointer());
76         SysLog(NID_CNT, "##### Download URL : %ls #####", pUserData->GetUrl().GetPointer());
77         SysLog(NID_CNT, "##### Slot number : %d #####", pUserData->GetSlot());
78         SysLog(NID_CNT, "##### Total size : %lld #####", pUserData->GetTotalSize());
79         SysLog(NID_CNT, "##### Prev data size : %lld #####", pUserData->GetPrevData());
80
81         pHttpResponse = httpTransaction.GetResponse();
82         r = GetLastResult();
83         SysTryReturnVoidResult(NID_CNT, pHttpResponse != null, r, "[%s] Failed to get the HTTP transaction response.", GetErrorMessage(r));
84
85         if (pHttpResponse->GetHttpStatusCode() != HTTP_STATUS_OK)
86         {
87                 SysLogException(NID_CNT, E_INVALID_STATE, "[E_INVALID_STATE] HTTP status code is not HTTP_STATUS_OK.");
88                 return;
89         }
90
91         pBody = std::unique_ptr<ByteBuffer>(pHttpResponse->ReadBodyN());
92         SysTryReturnVoidResult(NID_CNT, pBody != null, r = E_OUT_OF_MEMORY,
93                            "[E_OUT_OF_MEMORY] Failed to perform ReadBodyN operation to ByteBuffer.");
94
95         pFile = std::unique_ptr<File>(new (std::nothrow) File);
96         SysTryReturnVoidResult(NID_CNT, pFile != null, r = E_OUT_OF_MEMORY,
97                            "[E_OUT_OF_MEMORY] Failed to construct File.");
98
99         r = pFile->Construct(pUserData->GetDestPath(), L"a+");
100         SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r, "[%s] Failed to construct File.", GetErrorMessage(r));
101
102         r = pFile->Write(*(pBody.get()));
103         SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r, "[%s] Failed to perform write operation to File.", GetErrorMessage(r));
104 }
105
106 void
107 _ContentDownloadListener::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result res)
108 {
109         SysLog(NID_CNT, "OnTransactionAborted event fired.");
110
111         HttpResponse* pHttpResponse = null;
112         String errorMsg(L"");
113         int statusCode = -1;
114         result r = E_SUCCESS;
115
116         SysLog(NID_CNT, "Input result parameter of OnTransactionAborted is [%s]", GetErrorMessage(res));
117
118         _ContentDownloadUserData* pUserData = dynamic_cast<_ContentDownloadUserData*>(httpTransaction.GetUserObject());
119         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_SYSTEM,
120                         "[E_SYSTEM] ContentDownloadUserData instance must not be null.");
121
122         pHttpResponse = httpTransaction.GetResponse();
123         r = GetLastResult();
124         if (IsFailed(r))
125         {
126                 SysLog(NID_CNT, "[%s] Failed to get the HTTP transaction response.", GetErrorMessage(r));
127         }
128         else
129         {
130                 statusCode = pHttpResponse->GetHttpStatusCode();
131                 SysTryReturnVoidResult(NID_CNT, statusCode == HTTP_STATUS_OK, r = E_INVALID_STATE,
132                                            "[E_INVALID_STATE] HTTP status code is not HTTP_STATUS_OK.");
133
134                 errorMsg = pHttpResponse->GetStatusText();
135                 r = GetLastResult();
136                 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r = E_INVALID_STATE,
137                                 "[E_INVALID_STATE] The server is unavailable.");
138         }
139
140         // delete downloaded file because of error
141         if (_FileImpl::IsFileExist(pUserData->GetDestPath()))
142         {
143                 r = _FileImpl::Remove(pUserData->GetDestPath());
144                 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r,
145                                 "[%s] Failed to remove the downloaded file on OnTransactionAborted.", GetErrorMessage(r));
146         }
147
148         DownloadCanceled(pUserData, statusCode, errorMsg, E_SERVER);
149 }
150
151 void
152 _ContentDownloadListener::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
153 {
154         SysLog(NID_CNT, "OnTransactionCompleted event fired.");
155
156         ClearLastResult();
157         result r = E_SUCCESS;
158         HttpResponse* pHttpResponse = null;
159         String errorMsg(L"");
160         int statusCode = 0;
161         String destPath(L"");
162
163         _ContentDownloadUserData* pUserData = dynamic_cast<_ContentDownloadUserData*>(httpTransaction.GetUserObject());
164         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_SYSTEM,
165                         "[E_SYSTEM] ContentDownloadUserData instance must not be null.");
166
167         pHttpResponse = httpTransaction.GetResponse();
168         SysTryCatch(NID_CNT, pHttpResponse != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get the HTTP transaction response.");
169
170         pUserData->SetDownloadCount(0);
171
172         statusCode = pHttpResponse->GetHttpStatusCode();
173         SysTryCatch(NID_CNT, statusCode == HTTP_STATUS_OK, r = E_SERVER, E_SERVER, "[E_SERVER] HTTP status code is not HTTP_STATUS_OK.");
174
175         errorMsg = pHttpResponse->GetStatusText();
176         r = GetLastResult();
177         SysTryCatch(NID_CNT, !IsFailed(r), r = E_SERVER, E_SERVER, "[E_SERVER] The server is unavailable.");
178
179         if (!_AppInfo::IsOspCompat())
180         {
181                 destPath = pUserData->GetDestPath();
182
183                 // download success
184                 // register content
185                 if (destPath.StartsWith(Environment::GetMediaPath(), 0) || destPath.StartsWith(Environment::GetExternalStoragePath(), 0))
186                 {
187                         r = RegisterMediaFile(httpSession, httpTransaction, pUserData);
188                         SysTryCatch(NID_CNT, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to perform RegisterMediaFile operation.");
189
190                         DownloadCompleted(httpSession, httpTransaction, pUserData, statusCode, errorMsg, r);
191                 }
192                 else
193                 {
194                         DownloadCompleted(httpSession, httpTransaction, pUserData, statusCode, errorMsg, r);
195                 }
196         }
197         else
198         {
199                 if (_FileImpl::IsMediaPath(pUserData->GetDestPath()))
200                 {
201                         r = RegisterMediaFile(httpSession, httpTransaction, pUserData);
202                         SysTryCatch(NID_CNT, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to perform RegisterMediaFile operation.");
203
204                         DownloadCompleted(httpSession, httpTransaction, pUserData, statusCode, errorMsg, r);
205                 }
206                 else
207                 {
208                         DownloadCompleted(httpSession, httpTransaction, pUserData, statusCode, errorMsg, r);
209                 }
210         }
211
212         return;
213
214 CATCH:
215         statusCode = pHttpResponse->GetHttpStatusCode();
216         errorMsg = pHttpResponse->GetStatusText();
217
218         if (_FileImpl::IsFileExist(pUserData->GetDestPath()))
219         {
220                 result res = _FileImpl::Remove(pUserData->GetDestPath());
221                 SysTryLog(NID_CNT, !IsFailed(res), "[%s] Failed to remove the downloaded file on OnTransactionCompleted.", GetErrorMessage(res));
222         }
223
224         DownloadCompleted(httpSession, httpTransaction, pUserData, statusCode, errorMsg, r);
225 }
226
227 void
228 _ContentDownloadListener::OnHttpDownloadInProgress(HttpSession& httpSession, HttpTransaction& httpTransaction,
229                                                                                                    long long currentLength, long long totalLength)
230 {
231         SysLog(NID_CNT, "OnHttpDownloadInProgress event fired.");
232
233         ClearLastResult();
234         result r = E_SUCCESS;
235         int count = 0;
236         double currentData = 0.0;
237         double totalData = 0.0;
238
239         _ContentDownloadUserData* pUserData = dynamic_cast<_ContentDownloadUserData*>(httpTransaction.GetUserObject());
240         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_SYSTEM,
241                         "[E_SYSTEM] ContentDownloadUserData instance must not be null.");
242
243         count = pUserData->GetDownloadCount();
244         currentData = currentLength;
245         totalData = totalLength;
246
247         if (totalLength == 0)
248         {
249                 count++;
250                 pUserData->SetDownloadCount(count);
251
252                 if (pUserData->GetDownloadCount() == CONTENT_TRANSFER_COUNTER)
253                 {
254                         TransferProgress(httpSession, httpTransaction, currentLength, totalLength, pUserData);
255                         pUserData->SetDownloadCount(0);
256                 }
257         }
258         else
259         {
260                 int cmp = 0;
261                 Double data1(totalData);
262                 Double data2(currentData);
263                 DoubleComparer comparer;
264
265                 r = comparer.Compare(data1, data2, cmp);
266                 SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r,
267                                 "[%s] Failed to compare total size with current size for a content.", GetErrorMessage(r));
268
269                 if ((pUserData->GetPercent() == INVALID_PROGRESS_INTERVAL)
270                                 || ((cmp == 0) && (pUserData->GetPrevData() != currentLength))
271                                 || (((currentData - pUserData->GetPrevData()) / totalData * 100) >= pUserData->GetPercent()))
272                 {
273                         pUserData->SetPrevData(currentLength);
274                         pUserData->SetTotalSize(totalLength);
275                         TransferProgress(httpSession, httpTransaction, currentLength, totalLength, pUserData);
276                 }
277         }
278 }
279
280 void
281 _ContentDownloadListener::OnTransactionCertVerificationRequiredN(HttpSession& httpSession,
282                                                                                                                                  HttpTransaction& httpTransaction, String* pCert)
283 {
284         SysLog(NID_CNT, "OnTransactionCertVerificationRequiredN event fired.");
285 }
286
287 void
288 _ContentDownloadListener::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction,
289                                                                                                            int headerLen, bool bAuthRequired)
290 {
291         SysLog(NID_CNT, "OnTransactionHeaderCompleted event fired.");
292 }
293
294 void
295 _ContentDownloadListener::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction,
296                                                                                                         int recommendedChunkSize)
297 {
298         SysLog(NID_CNT, "OnTransactionReadyToWrite event fired.");
299 }
300
301 void
302 _ContentDownloadListener::OnHttpUploadInProgress(HttpSession& httpSession, HttpTransaction& httpTransaction,
303                                                                                                  long long currentLength, long long totalLength)
304 {
305         SysLog(NID_CNT, "OnHttpUploadInProgress event fired.");
306 }
307
308 void
309 _ContentDownloadListener::DownloadCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction,
310                                                                                           _ContentDownloadUserData* pUserData, int errorCode, String errorMsg, result res)
311 {
312         SysLog(NID_CNT, "DownloadCompleted operation start after OnTransactionCompleted event fire.");
313
314         ClearLastResult();
315         result r = E_SUCCESS;
316         std::unique_ptr<_ContentTransferEventArg> pContentTransferEventArg;
317         ContentId contentId;
318         RequestId resultRequestId = INVALID_REQUEST_ID;
319         long long fileSize = 0;
320         std::unique_ptr<ByteBuffer> pBuffer;
321         File file;
322         FileAttributes attribute;
323         String destPath(L"");
324
325         pContentTransferEventArg = std::unique_ptr<_ContentTransferEventArg>(new (std::nothrow) _ContentTransferEventArg());
326         SysTryCatch(NID_CNT, pContentTransferEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
327                            "[E_OUT_OF_MEMORY] Failed to construct ContentTrasnferEventArg.");
328
329         SysTryCatch(NID_CNT, pUserData != null, r = E_INVALID_STATE, E_INVALID_STATE,
330                         "[E_INVALID_STATE] ContentDownloadUserData instance must not be null.");
331
332         resultRequestId = pUserData->GetRequestId();
333         destPath = pUserData->GetDestPath();
334         contentId = pUserData->GetContentId();
335
336         pUserData->SetRequestId(INVALID_REQUEST_ID);
337
338         if (!(pUserData->GetDownloadBufferFlag()))
339         {
340                 if (pUserData->GetContentId() != UuId::GetInvalidUuId())
341                 {
342                         pUserData->SetContentId(UuId::GetInvalidUuId());
343                 }
344                 else
345                 {
346                         contentId = UuId::GetInvalidUuId();
347                 }
348
349                 pContentTransferEventArg->SetResult(res);
350                 pContentTransferEventArg->SetErrorMsg(Integer::ToString(errorCode), errorMsg);
351                 pContentTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_DOWNLOAD_COMPLETED);
352                 pContentTransferEventArg->SetContentId(contentId);
353
354                 // TODO : needs request ID mapping
355                 pContentTransferEventArg->SetRequestId(resultRequestId);
356
357                 SysTryCatch(NID_CNT, pUserData->GetContentTransferEvent() != null, r = E_INVALID_STATE, E_INVALID_STATE,
358                                    "[E_INVALID_STATE] ContentTransferEvent in UserData must not be null.");
359
360                 r = pUserData->GetContentTransferEvent()->Fire(*(pContentTransferEventArg.release()));
361                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[%s] Failed to perform fire operation on ContentTransferEvent.", GetErrorMessage(r));
362
363                 pUserData->SetSlotFlag(false);
364
365                 if (pUserData->GetContentTransferInfo() != null)
366                 {
367                         pUserData->GetContentTransferInfo()->SetDownloadStatus(Content::CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED);
368                 }
369         }
370         else
371         {
372                 pContentTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_DOWNLOAD_TO_BUFFER_COMPLETED);
373                 pContentTransferEventArg->SetRequestId(resultRequestId);
374                 pUserData->SetDownloadBufferFlag(false);
375
376                 r = file.Construct(destPath, L"r");
377                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[E_OUT_OF_MEMORY] Failed to construct File.");
378
379                 r = _FileImpl::GetAttributes(destPath, attribute);
380                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[%s] Failed to perform GetAttributes operation.", GetErrorMessage(r));
381
382                 fileSize = attribute.GetFileSize();
383                 SysTryCatch(NID_CNT, fileSize != 0, , r, "[%s] Failed to perform GetFileSize.", GetErrorMessage(r));
384
385                 pBuffer = std::unique_ptr<ByteBuffer>(new (std::nothrow) ByteBuffer());
386                 SysTryCatch(NID_CNT, pBuffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
387                                    "[E_OUT_OF_MEMROY] Failed to construct ByteBuffer.");
388
389                 r = pBuffer->Construct(static_cast<int>(fileSize));
390                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[E_OUT_OF_MEMORY] Failed to construct ByteBuffer.");
391
392                 r = file.Read(*(pBuffer.get()));
393                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[%s] Failed to perform Read operation from File to ByteBuffer.", GetErrorMessage(r));
394
395                 r = pBuffer->SetPosition(fileSize);
396                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[%s] Failed to perform SetPosition operation.", GetErrorMessage(r));
397
398                 r = file.Remove(destPath);
399                 SysTryCatch(NID_CNT, !IsFailed(r), , r, "[%s] Failed to perform Remove operation for download file.", GetErrorMessage(r));
400
401                 pBuffer->Flip();
402                 pContentTransferEventArg->SetBuffer(pBuffer.release());
403                 pContentTransferEventArg->SetResult(res);
404                 pContentTransferEventArg->SetErrorMsg(Integer::ToString(errorCode), errorMsg);
405
406                 SysTryCatch(NID_CNT, pUserData->GetContentTransferEvent() != null, r = E_INVALID_STATE, E_INVALID_STATE,
407                                    "[E_INVALID_STATE] ContentTransferEvent in UserData must not be null.");
408
409                 r = pUserData->GetContentTransferEvent()->Fire(*(pContentTransferEventArg.release()));
410                 SysTryCatch(NID_CNT, !IsFailed(r), , r,
411                                 "[%s] Failed to perform fire operation on ContentTransferEvent.", GetErrorMessage(r));
412
413                 pUserData->SetSlotFlag(false);
414
415                 if (pUserData->GetContentTransferInfo() != null)
416                 {
417                         pUserData->GetContentTransferInfo()->SetDownloadStatus(Content::CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED);
418                 }
419         }
420
421         return;
422
423 CATCH:
424         _ContentManagerImpl contentManager;
425
426         r = contentManager.Construct();
427         SysTryLog(NID_CNT, !IsFailed(r), "[%s] Failed to construct ContentManager.", GetErrorMessage(r));
428
429         r = contentManager.DeleteContent(contentId);
430         SysTryLog(NID_CNT, !IsFailed(r), "[%s] Failed to perform DeleteContent operation.", GetErrorMessage(r));
431
432         if (pContentTransferEventArg)
433         {
434                 pContentTransferEventArg->SetResult(r);
435                 pContentTransferEventArg->SetErrorMsg(Integer::ToString(errorCode), errorMsg);
436
437                 r = pUserData->GetContentTransferEvent()->Fire(*(pContentTransferEventArg.release()));
438                 SysTryLog(NID_CNT, !IsFailed(r), "[%s] Failed to perform fire operation on ContentTransferEvent.", GetErrorMessage(r));
439
440                 pUserData->SetSlotFlag(false);
441         }
442
443         if (_FileImpl::IsFileExist(destPath))
444         {
445                 r = file.Remove(destPath);
446                 SysTryLog(NID_CNT, !IsFailed(r), "[%s] Failed to perform Remove operation for download file.", GetErrorMessage(r));
447         }
448
449         if (pUserData->GetContentTransferInfo() != null)
450         {
451                 pUserData->GetContentTransferInfo()->SetDownloadStatus(Content::CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED);
452         }
453
454         return;
455 }
456
457 result
458 _ContentDownloadListener::RegisterMediaFile(HttpSession& httpSession, HttpTransaction& httpTransaction,
459                                                                                           _ContentDownloadUserData* pUserData)
460 {
461         ClearLastResult();
462         result r = E_SUCCESS;
463         ContentId contentId;
464         _ContentManagerImpl contentManager;
465         ContentType contentType = CONTENT_TYPE_UNKNOWN;
466         ImageContentInfo imageContentInfo;
467         AudioContentInfo audioContentInfo;
468         VideoContentInfo videoContentInfo;
469         OtherContentInfo otherContentInfo;
470         String contentPath(L"");
471
472         SysTryReturnResult(NID_CNT, pUserData != null, r = E_INVALID_STATE,
473                         "ContentDownloadUserData instance must not be null.");
474
475         contentPath = pUserData->GetDestPath();
476
477         contentType = Content::_ContentManagerUtilImpl::CheckContentType(pUserData->GetDestPath());
478         r = GetLastResult();
479         SysTryReturnResult(NID_CNT, r == E_SUCCESS || r == E_UNSUPPORTED_FORMAT, r,
480                         "Failed to perform CheckContentType for [%ls].", (pUserData->GetDestPath()).GetPointer());
481
482         r = contentManager.Construct();
483         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to construct ContentManager.");
484
485         if (contentType == CONTENT_TYPE_IMAGE)
486         {
487                 SysLog(NID_CNT, "Register the image type to database.");
488                 r = imageContentInfo.Construct(&contentPath);
489                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to construct ImageContentInfo.");
490
491                 contentId = contentManager.CreateContent(imageContentInfo);
492                 SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), GetLastResult(),
493                                 "Failed to perform CreateContent operation for image type.");
494         }
495         else if (contentType == CONTENT_TYPE_AUDIO)
496         {
497                 SysLog(NID_CNT, "Register the audio type to database.");
498                 r = audioContentInfo.Construct(&contentPath);
499                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to construct AudioContentInfo.");
500
501                 contentId = contentManager.CreateContent(audioContentInfo);
502                 SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), GetLastResult(),
503                                 "Failed to perform CreateContent operation for audio type.");
504         }
505         else if (contentType == CONTENT_TYPE_VIDEO)
506         {
507                 SysLog(NID_CNT, "Register the video type to database.");
508                 r = videoContentInfo.Construct(&contentPath);
509                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to construct VideoContentInfo.");
510
511                 contentId = contentManager.CreateContent(videoContentInfo);
512                 SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), GetLastResult(),
513                                 "Failed to perform CreateContent operation for video type.");
514         }
515         else if (contentType == CONTENT_TYPE_OTHER || contentType == CONTENT_TYPE_UNKNOWN)
516         {
517                 SysLog(NID_CNT, "Register the other type to database.");
518                 r = otherContentInfo.Construct(&contentPath);
519                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to construct OtherContentInfo.");
520
521                 contentId = contentManager.CreateContent(otherContentInfo);
522                 SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), GetLastResult(),
523                                 "Failed to construct CreateContent operation for other type.");
524         }
525         else
526         {
527                 SysLogException(NID_CNT, r = E_SYSTEM, "[%s]contentType is CONTENT_TYPE_UNKNOWN.", GetErrorMessage(E_SYSTEM));
528
529                 return r;
530         }
531
532         pUserData->SetContentId(contentId);
533
534         return r;
535 }
536
537 void
538 _ContentDownloadListener::DownloadCanceled(_ContentDownloadUserData* pUserData, int statusCode, String errorMsg, result res)
539 {
540         SysLog(NID_CNT, "Cancel the download operation with status code(%d).", statusCode);
541
542         ClearLastResult();
543         result r = E_SUCCESS;
544         std::unique_ptr<_ContentTransferEventArg> pTransferEventArg(new (std::nothrow) _ContentTransferEventArg);
545         SysTryReturnVoidResult(NID_CNT, pTransferEventArg != null, E_OUT_OF_MEMORY,
546                            "[E_OUT_OF_MEMORY] pTransferEventArg construct failed.");
547
548         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_INVALID_STATE,
549                         "[E_INVALID_STATE] ContentDownloadUserData instance must not be null.");
550
551         pTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_CANCELED);
552         pTransferEventArg->SetRequestId(pUserData->GetRequestId());
553         pTransferEventArg->SetResult(res);
554         pTransferEventArg->SetErrorMsg(Integer::ToString(statusCode), errorMsg);
555
556         SysTryReturnVoidResult(NID_CNT, pUserData->GetContentTransferEvent() != null, r = E_INVALID_STATE,
557                         "[E_INVALID_STATE] ContentTransferEvent in UserData must not be null.");
558
559         r = pUserData->GetContentTransferEvent()->Fire(*(pTransferEventArg.release()));
560         SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r,
561                         "[%s] Failed to perform fire operation on ContentTransferEvent.", GetErrorMessage(r));
562
563         if (pUserData->GetDownloadBufferFlag())
564         {
565                 pUserData->SetDownloadBufferFlag(false);
566         }
567
568         pUserData->SetSlotFlag(false);
569
570         if (pUserData->GetContentTransferInfo() != null)
571         {
572                 pUserData->GetContentTransferInfo()->SetDownloadStatus(Content::CONTENT_TRANSFER_STATUS_DOWNLOAD_COMPLETED);
573         }
574         else
575         {
576                 SysLogException(NID_CNT, E_INVALID_STATE, "[E_INVALID_STATE] ContentTransferInfo is not exist in UserData.");
577         }
578 }
579
580 void
581 _ContentDownloadListener::TransferProgress(HttpSession& httpSession, HttpTransaction& httpTransaction,
582                                                                                          long long currentLength, long long totalLength, _ContentDownloadUserData* pUserData)
583 {
584         ClearLastResult();
585         result r = E_SUCCESS;
586         std::unique_ptr<_ContentTransferEventArg> pTransferEventArg(new (std::nothrow) _ContentTransferEventArg);
587         SysTryReturnVoidResult(NID_CNT, pTransferEventArg != null, r = E_OUT_OF_MEMORY,
588                            "[E_OUT_OF_MEMORY] Failed to construct ContentTransferEventArg.");
589
590         SysTryReturnVoidResult(NID_CNT, pUserData != null, r = E_INVALID_STATE,
591                         "[E_INVALID_STATE] ContentDownloadUserData must not be null.");
592
593         pTransferEventArg->SetEventType(Content::CONTENT_TRANSFER_EVENT_TRANSFER_IN_PROGRESS);
594         pTransferEventArg->SetRequestId(pUserData->GetRequestId());
595         pTransferEventArg->SetTotalTransferedSize(currentLength);
596
597         SysTryReturnVoidResult(NID_CNT, pUserData->GetContentTransferEvent() != null, r = E_INVALID_STATE,
598                            "[E_INVALID_STATE] ContentTransferEvent in UserData must not be null.");
599
600         r = pUserData->GetContentTransferEvent()->Fire(*(pTransferEventArg.release()));
601         SysTryReturnVoidResult(NID_CNT, !IsFailed(r), r = E_INVALID_STATE,
602                            "[E_INVALID_STATE] Failed to perform fire operation on ContentTransferEvent.");
603 }
604
605 }}
606