Fixed a result when decodeUrl() isn't connect correct url
[platform/framework/native/image.git] / src / FMedia_ImageDownloadListener.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 /**
19  * @file   FMedia_ImageDownloadListener.cpp
20  * @brief  This file contains the implementation of Image download listener class,
21  *               required internally by Image::DecodeUrl.
22  */
23
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>
30
31 #include <FBaseSysLog.h>
32
33 #include "FMedia_ImageUriData.h"
34 #include "FMedia_ImageUriDataEvent.h"
35 #include "FMedia_ImageUriDataEventArg.h"
36 #include "FMedia_ImageDownloadListener.h"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Net::Http;
42
43 namespace Tizen{ namespace Media{
44
45 _ImageDownloadListener::_ImageDownloadListener()
46         : __requestID(0),
47         __pImageUriEvent(null),
48         __pImageUriData(null),
49         __pDownloadedData(null),
50         __colorFormat(BITMAP_PIXEL_FORMAT_RGB565),
51         __destWidth(0),
52         __destHeight(0)
53 {
54 }
55
56 _ImageDownloadListener::~_ImageDownloadListener()
57 {
58
59         __pImageUriEvent = null;
60
61         __pImageUriData = null;
62
63         __requestID = 0;
64
65 }
66
67 void
68 _ImageDownloadListener::OnTransactionReadyToRead(Tizen::Net::Http::HttpSession& httpSession,
69           Tizen::Net::Http::HttpTransaction& httpTransaction, int availableBodyLen)
70 {
71         result r = E_SUCCESS;
72         std::unique_ptr<ByteBuffer> pBody;
73         HttpResponse* pHttpResponse = null;
74         _ImageUriDataDestroyArg* pUriDestroyArg = null;
75         _ImageUriDataErrorArg* pImageUriErrorArg = null;
76
77         pHttpResponse = httpTransaction.GetResponse();
78         r = GetLastResult();
79         SysTryCatch(NID_MEDIA, pHttpResponse != null, , r, "[%s] Get response is failed.", GetErrorMessage(r));
80
81         if (pHttpResponse->GetHttpStatusCode() != HTTP_STATUS_OK)
82         {
83                 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] OnTransactionReadyToRead failed.");
84                 r = E_INVALID_STATE;
85                 goto CATCH;
86         }
87
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.");
91
92         if (__pDownloadedData == null)
93         {
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.");
100
101                 r = __pDownloadedData->CopyFrom(*pBody.get());
102                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
103                            "ByteBuffer::CopyFrom failed.");
104         }
105         else
106         {
107                 r = __pDownloadedData->ExpandCapacity(__pDownloadedData->GetLimit() + pBody->GetLimit());
108                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
109                            "ByteBuffer::ExpandCapacity failed.");
110
111                 r = __pDownloadedData->CopyFrom(*pBody.get());
112                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
113                            "ByteBuffer::CopyFrom failed.");
114         }
115         return;
116
117 CATCH:
118
119         // Send error event.
120         pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
121         if (pImageUriErrorArg == null)
122         {
123                 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
124                 return;
125         }
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);
132         if (r != E_SUCCESS)
133         {
134                 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
135                 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
136         }
137
138         // Send destroy event to destroy ImageUriData's resources. - start
139         if (pUriDestroyArg == null)
140         {
141                 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
142                 if ( pUriDestroyArg == null )
143                 {
144                         return;
145                 }
146         }
147
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
152
153         return;
154 }
155
156 void
157 _ImageDownloadListener::OnTransactionAborted(Tizen::Net::Http::HttpSession& httpSession,
158           Tizen::Net::Http::HttpTransaction& httpTransaction, result res)
159 {
160         _ImageUriDataDestroyArg* pUriDestroyArg = null;
161         _ImageUriDataErrorArg* pImageUriErrorArg = null;
162         result r = E_SUCCESS;
163
164         // Send error event.
165         pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
166         if (pImageUriErrorArg == null)
167         {
168                 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
169                 return;
170         }
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);
177         if (r != E_SUCCESS)
178         {
179                 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
180                 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
181         }
182
183         // Send destroy event to destroy ImageUriData's resources. - start
184         pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
185         if (pUriDestroyArg == null)
186         {
187                 return;
188         }
189
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
194
195         return;
196 }
197
198 void
199 _ImageDownloadListener::OnTransactionCompleted(Tizen::Net::Http::HttpSession& httpSession,
200           Tizen::Net::Http::HttpTransaction& httpTransaction)
201 {
202         result r = E_SUCCESS;
203         std::unique_ptr<_ImageUriDataEventArg> pUriEventArg;
204         _ImageUriDataDestroyArg* pUriDestroyArg = null;
205         _ImageUriDataErrorArg* pImageUriErrorArg = null;
206
207         // Send event.
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");
211
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));
219
220         r = __pImageUriEvent->FireAsync(*pUriEventArg.release());
221         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to Fire _ImageUriDataEventArg", GetErrorMessage(r));
222
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." );
226
227         pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
228
229         pUriDestroyArg->SetRequestId(__requestID);
230
231         r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
232         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.",
233           GetErrorMessage(r));
234
235         // Send destroy event to destroy ImageUriData's resources. - end
236         return;
237 CATCH:
238
239         // Send error event.
240         pImageUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
241         if (pImageUriErrorArg == null)
242         {
243                 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "Could not create pImageUriErrorArg! Exiting.");
244                 return;
245         }
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);
252         if (r != E_SUCCESS)
253         {
254                 SysLogException(NID_MEDIA, r, "Failed to Fire ImageUriEvent!");
255                 SysLogException(NID_MEDIA, r, "[%s] Propagated.", GetErrorMessage(r));
256         }
257
258         // Send destroy event to destroy ImageUriData's resources. - start
259         if (pUriDestroyArg == null)
260         {
261                 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
262                 if ( pUriDestroyArg == null )
263                 {
264                         return;
265                 }
266         }
267
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
272         return;
273 }
274
275 void
276 _ImageDownloadListener::OnTransactionCertVerificationRequiredN(Tizen::Net::Http::HttpSession& httpSession,
277           Tizen::Net::Http::HttpTransaction& httpTransaction, String* pCert)
278 {
279         // Not used.
280 }
281
282 void
283 _ImageDownloadListener::OnTransactionHeaderCompleted(Tizen::Net::Http::HttpSession& httpSession,
284           Tizen::Net::Http::HttpTransaction& httpTransaction, int headerLen, bool bAuthRequired)
285 {
286         // Not used.
287 }
288
289 void
290 _ImageDownloadListener::OnTransactionReadyToWrite(Tizen::Net::Http::HttpSession& httpSession,
291           Tizen::Net::Http::HttpTransaction& httpTransaction, int recommendedChunkSize)
292 {
293         // Not used.
294 }
295
296 void
297 _ImageDownloadListener::OnDownloadCanceled(result res)
298 {
299         std::unique_ptr<_ImageUriDataEventArg> pUriEventArg;
300         _ImageUriDataDestroyArg* pUriDestroyArg = null;
301         result r = E_SUCCESS;
302
303         // Send event.
304         pUriEventArg.reset(new (std::nothrow) _ImageUriDataEventArg);
305         if (pUriEventArg.get() == null)
306         {
307                 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY,
308                         "[E_OUT_OF_MEMORY] Failed to allocate memory _ImageUriDataEventArg");
309                 return;
310         }
311
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);
317
318         r = __pImageUriEvent->FireAsync(*pUriEventArg.release());
319         if (r != E_SUCCESS)
320         {
321                 SysLogException(NID_MEDIA, r, "[%s] Failed to Fire _ImageUriDataEventArg", GetErrorMessage(r));
322         }
323
324         pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
325         if (pUriDestroyArg == null)
326         {
327                 SysLogException(NID_MEDIA, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] while creating destroy arg." );
328                 return;
329         }
330
331         pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
332
333         pUriDestroyArg->SetRequestId(__requestID);
334
335         r = __pImageUriEvent->FireAsync(*pUriDestroyArg);
336         if (r != E_SUCCESS)
337         {
338                 SysLogException(NID_MEDIA, r, "[%s] Propagated.",
339                   GetErrorMessage(r));
340                 return;
341         }
342
343         // Send destroy event to destroy ImageUriData's resources. - end
344
345 }
346
347 void
348 _ImageDownloadListener::SetImageUrlEvent(_ImageUriDataEvent* pImageUriEvent)
349 {
350         __pImageUriEvent = pImageUriEvent;
351 }
352
353 void
354 _ImageDownloadListener::SetImageUrlData(_ImageUriData* pImageUriData)
355 {
356         __pImageUriData = pImageUriData;
357 }
358
359 void
360 _ImageDownloadListener::SetImagePixelFormat(BitmapPixelFormat colorFormat )
361 {
362         __colorFormat = colorFormat;
363 }
364
365 void
366 _ImageDownloadListener::SetImageDestDimension(int destWidth, int destHeight )
367 {
368         __destWidth = destWidth;
369         __destHeight = destHeight;
370 }
371
372 void
373 _ImageDownloadListener::SetRequestId(RequestId reqId)
374 {
375         __requestID = reqId;
376 }
377
378
379 }} // Tizen::Media