2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FMedia_ImageDownloadListener.cpp
20 * @brief This file contains the implementation of Image download listener class,
21 * required internally by Image::DecodeUrl.
24 #include <unique_ptr.h>
25 #include <FSysSystemTime.h>
26 #include <FGrpDimension.h>
27 #include <FGrpBitmapCommon.h>
28 #include <FNetHttpHttpSession.h>
29 #include <FNetHttpHttpResponse.h>
31 #include <FBaseSysLog.h>
33 #include "FMedia_ImageUriData.h"
34 #include "FMedia_ImageUriDataEvent.h"
35 #include "FMedia_ImageUriDataEventArg.h"
36 #include "FMedia_ImageDownloadListener.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Net::Http;
43 namespace Tizen{ namespace Media{
45 _ImageDownloadListener::_ImageDownloadListener()
47 __pImageUriEvent(null),
48 __pImageUriData(null),
49 __pDownloadedData(null),
50 __colorFormat(BITMAP_PIXEL_FORMAT_RGB565),
56 _ImageDownloadListener::~_ImageDownloadListener()
59 __pImageUriEvent = null;
61 __pImageUriData = null;
68 _ImageDownloadListener::OnTransactionReadyToRead(Tizen::Net::Http::HttpSession& httpSession,
69 Tizen::Net::Http::HttpTransaction& httpTransaction, int availableBodyLen)
72 std::unique_ptr<ByteBuffer> pBody;
73 HttpResponse* pHttpResponse = null;
74 _ImageUriDataDestroyArg* pUriDestroyArg = null;
75 _ImageUriDataErrorArg* pImageUriErrorArg = null;
77 pHttpResponse = httpTransaction.GetResponse();
79 SysTryCatch(NID_MEDIA, pHttpResponse != null, , r, "[%s] Get response is failed.", GetErrorMessage(r));
81 if (pHttpResponse->GetHttpStatusCode() != HTTP_STATUS_OK)
83 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] OnTransactionReadyToRead failed.");
88 pBody.reset(pHttpResponse->ReadBodyN());
89 SysTryCatch(NID_MEDIA, pBody.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
90 "[E_OUT_OF_MEMORY] OnTransactionReadyToRead failed.");
92 if (__pDownloadedData == null)
94 __pDownloadedData = new (std::nothrow) ByteBuffer;
95 SysTryCatch(NID_MEDIA, __pDownloadedData != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
96 "[E_OUT_OF_MEMORY] OnTransactionReadyToRead failed.");
97 r = __pDownloadedData->Construct(pBody->GetLimit());
98 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
99 "ByteBuffer construct failed.");
101 r = __pDownloadedData->CopyFrom(*pBody.get());
102 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
103 "ByteBuffer::CopyFrom failed.");
107 r = __pDownloadedData->ExpandCapacity(__pDownloadedData->GetLimit() + pBody->GetLimit());
108 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
109 "ByteBuffer::ExpandCapacity failed.");
111 r = __pDownloadedData->CopyFrom(*pBody.get());
112 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
113 "ByteBuffer::CopyFrom failed.");
120 pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
121 if (pImageUriErrorArg == null)
123 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
126 pImageUriErrorArg->SetEventType(IMAGE_URI_DATA_EVENT_ERROR);
127 pImageUriErrorArg->SetError(r);
128 pImageUriErrorArg->SetErrorCode(GetErrorMessage(r));
129 pImageUriErrorArg->SetErrorMessage(GetErrorMessage(r));
130 pImageUriErrorArg->SetRequestId(__requestID);
131 r = __pImageUriEvent->FireAsync(*pImageUriErrorArg);
134 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
135 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
138 // Send destroy event to destroy ImageUriData's resources. - start
139 if (pUriDestroyArg == null)
141 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
142 if ( pUriDestroyArg == null )
148 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
149 pUriDestroyArg->SetRequestId(__requestID);
150 r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
151 // Send destroy event to destroy ImageUriData's resources. - end
157 _ImageDownloadListener::OnTransactionAborted(Tizen::Net::Http::HttpSession& httpSession,
158 Tizen::Net::Http::HttpTransaction& httpTransaction, result res)
160 _ImageUriDataDestroyArg* pUriDestroyArg = null;
161 _ImageUriDataErrorArg* pImageUriErrorArg = null;
162 result r = E_SUCCESS;
165 pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
166 if (pImageUriErrorArg == null)
168 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
171 pImageUriErrorArg->SetEventType(IMAGE_URI_DATA_EVENT_ERROR);
172 pImageUriErrorArg->SetError(res);
173 pImageUriErrorArg->SetErrorCode(GetErrorMessage(res));
174 pImageUriErrorArg->SetErrorMessage(GetErrorMessage(res));
175 pImageUriErrorArg->SetRequestId(__requestID);
176 r = __pImageUriEvent->FireAsync(*pImageUriErrorArg);
179 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
180 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
183 // Send destroy event to destroy ImageUriData's resources. - start
184 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
185 if (pUriDestroyArg == null)
190 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
191 pUriDestroyArg->SetRequestId(__requestID);
192 r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
193 // Send destroy event to destroy ImageUriData's resources. - end
199 _ImageDownloadListener::OnTransactionCompleted(Tizen::Net::Http::HttpSession& httpSession,
200 Tizen::Net::Http::HttpTransaction& httpTransaction)
202 result r = E_SUCCESS;
203 std::unique_ptr<_ImageUriDataEventArg> pUriEventArg;
204 _ImageUriDataDestroyArg* pUriDestroyArg = null;
205 _ImageUriDataErrorArg* pImageUriErrorArg = null;
208 pUriEventArg.reset(new (std::nothrow) _ImageUriDataEventArg);
209 SysTryCatch(NID_MEDIA, pUriEventArg.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
210 "[E_OUT_OF_MEMORY] new _ImageUriDataEventArg");
212 pUriEventArg->SetEventType(IMAGE_URI_DATA_EVENT_SUCCESS);
213 pUriEventArg->SetImageFormat(IMG_FORMAT_NONE);
214 pUriEventArg->SetData(__pDownloadedData);
215 pUriEventArg->SetRequestId(__requestID);
216 pUriEventArg->SetResult(E_SUCCESS);
217 pUriEventArg->SetColorFormat(__colorFormat);
218 pUriEventArg->SetDestDimension(Dimension(__destWidth, __destHeight));
220 r = __pImageUriEvent->FireAsync(*pUriEventArg.release());
221 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to Fire _ImageUriDataEventArg", GetErrorMessage(r));
223 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
224 SysTryCatch(NID_MEDIA, pUriDestroyArg != null, r = E_OUT_OF_MEMORY,
225 E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] while creating destroy arg." );
227 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
229 pUriDestroyArg->SetRequestId(__requestID);
231 r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
232 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.",
235 // Send destroy event to destroy ImageUriData's resources. - end
240 pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
241 if (pImageUriErrorArg == null)
243 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
246 pImageUriErrorArg->SetEventType(IMAGE_URI_DATA_EVENT_ERROR);
247 pImageUriErrorArg->SetError(r);
248 pImageUriErrorArg->SetErrorCode(GetErrorMessage(r));
249 pImageUriErrorArg->SetErrorMessage(GetErrorMessage(r));
250 pImageUriErrorArg->SetRequestId(__requestID);
251 r = __pImageUriEvent->FireAsync(*pImageUriErrorArg);
254 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
255 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
258 // Send destroy event to destroy ImageUriData's resources. - start
259 if (pUriDestroyArg == null)
261 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
262 if ( pUriDestroyArg == null )
268 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
269 pUriDestroyArg->SetRequestId(__requestID);
270 r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
271 // Send destroy event to destroy ImageUriData's resources. - end
276 _ImageDownloadListener::OnTransactionCertVerificationRequiredN(Tizen::Net::Http::HttpSession& httpSession,
277 Tizen::Net::Http::HttpTransaction& httpTransaction, String* pCert)
283 _ImageDownloadListener::OnTransactionHeaderCompleted(Tizen::Net::Http::HttpSession& httpSession,
284 Tizen::Net::Http::HttpTransaction& httpTransaction, int headerLen, bool bAuthRequired)
290 _ImageDownloadListener::OnTransactionReadyToWrite(Tizen::Net::Http::HttpSession& httpSession,
291 Tizen::Net::Http::HttpTransaction& httpTransaction, int recommendedChunkSize)
297 _ImageDownloadListener::OnDownloadCanceled(result res)
299 std::unique_ptr<_ImageUriDataEventArg> pUriEventArg;
300 _ImageUriDataDestroyArg* pUriDestroyArg = null;
301 result r = E_SUCCESS;
304 pUriEventArg.reset(new (std::nothrow) _ImageUriDataEventArg);
305 if (pUriEventArg.get() == null)
307 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY,
308 "[E_OUT_OF_MEMORY] Failed to allocate memory _ImageUriDataEventArg");
312 pUriEventArg->SetEventType(IMAGE_URI_DATA_EVENT_CANCEL);
313 pUriEventArg->SetImageFormat(IMG_FORMAT_NONE);
314 pUriEventArg->SetData(null);
315 pUriEventArg->SetRequestId(__requestID);
316 pUriEventArg->SetResult(res);
318 r = __pImageUriEvent->FireAsync(*pUriEventArg.release());
321 SysLogException(NID_MEDIA, r, "[%s] Failed to Fire _ImageUriDataEventArg", GetErrorMessage(r));
324 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
325 if (pUriDestroyArg == null)
327 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] while creating destroy arg." );
331 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
333 pUriDestroyArg->SetRequestId(__requestID);
335 r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
338 SysLogException(NID_MEDIA, r, "[%s] Propagated.",
343 // Send destroy event to destroy ImageUriData's resources. - end
348 _ImageDownloadListener::SetImageUrlEvent(_ImageUriDataEvent* pImageUriEvent)
350 __pImageUriEvent = pImageUriEvent;
354 _ImageDownloadListener::SetImageUrlData(_ImageUriData* pImageUriData)
356 __pImageUriData = pImageUriData;
360 _ImageDownloadListener::SetImagePixelFormat(BitmapPixelFormat colorFormat )
362 __colorFormat = colorFormat;
366 _ImageDownloadListener::SetImageDestDimension(int destWidth, int destHeight )
368 __destWidth = destWidth;
369 __destHeight = destHeight;
373 _ImageDownloadListener::SetRequestId(RequestId reqId)