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