merge commits of 2.2.1 to public
[platform/framework/native/image.git] / src / FMedia_ImageUriData.cpp
1
2 //
3 // Open Service Platform
4 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 //
6 // Licensed under the Apache License, Version 2.0 (the License);
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 //     http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18
19 /**
20  * @file   FMedia_ImageUriData.cpp
21  * @brief  This file contains the implementation of _ImageUriData class,
22  *               required internally by Image::DecodeUrl.
23  */
24
25 #include <unique_ptr.h>
26 #include <FSysSystemTime.h>
27 #include <FGrpBitmapCommon.h>
28 #include <FMediaIImageEventListener.h>
29
30 #include <FNetHttpHttpSession.h>
31 #include <FNetHttpHttpTransaction.h>
32 #include <FNetHttpHttpRequest.h>
33 #include <FNetHttp_HttpSessionImpl.h>
34 #include <FNetHttp_HttpTransactionImpl.h>
35 #include <FNetHttp_HttpRequestImpl.h>
36 #include <FIo_FileImpl.h>
37 #include <FBaseSysLog.h>
38
39 #include "FMedia_ImageDownloadListener.h"
40 #include "FMedia_ImageUriData.h"
41 #include "FMedia_ImageUriDataFactory.h"
42 #include "FMedia_ImageUriDataEvent.h"
43 #include "FMedia_ImageUriDataEventArg.h"
44
45 using namespace Tizen::Graphics;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Runtime;
48 using namespace Tizen::System;
49 using namespace Tizen::Io;
50 using namespace Tizen::Net::Http;
51
52
53 namespace Tizen{ namespace Media{
54
55 _ImageUriData::_ImageUriData(void):
56         __destWidth(0),
57         __destHeight(0),
58         __pSessionImpl(null),
59         __pTransactionImpl(null),
60         __pRequest(null),
61         __pRequestImpl(null),
62         __pDataEvent(null),
63         __colorFormat(BITMAP_PIXEL_FORMAT_RGB565),
64         __requestId(INVALID_REQUEST_ID),
65         __refCount(1),
66         __isTimerStarted(false)
67 {
68 }
69
70 _ImageUriData::~_ImageUriData(void)
71 {
72         if (__isTimerStarted)
73         {
74                 SysLog(NID_MEDIA, "Timeout Timer canceled, ReqeustId : %d", __requestId);
75                 __requestTimeout.Cancel();
76         }
77
78         if (__pSession.get() != null)
79         {
80                 __pSessionImpl = null;
81                 __pSession.reset(null);
82         }
83
84         if (__pTransaction.get() != null)
85         {
86                 __pTransactionImpl = null;
87                 __pTransaction.reset(null);
88         }
89
90 }
91
92 result
93 _ImageUriData::Construct(IEventListener& listener)
94 {
95         result r = E_SUCCESS;
96
97         __pDataEvent.reset(new (std::nothrow) _ImageUriDataEvent);
98         SysTryReturn(NID_MEDIA, __pDataEvent.get() != null, E_OUT_OF_MEMORY,
99           E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY].");
100
101         r = __pDataEvent->Construct();
102         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
103
104         r = __pDataEvent->AddListener (listener);
105         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
106
107         r = __requestTimeout.Construct(*this);
108         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
109
110         return r;
111 }
112
113 result
114 _ImageUriData::RequestDecode(const Utility::Uri &uri, BitmapPixelFormat colorFormat,
115                 const Dimension& destDim, RequestId& reqId, long msec)
116 {
117         result r = E_SUCCESS;
118
119         __srcUri = uri;
120         __destWidth = destDim.width;
121         __destHeight = destDim.height;
122         __colorFormat = colorFormat;
123         __requestId = reqId;
124
125         if (__pListener.get() == null)
126         {
127                 __pListener.reset(new (std::nothrow) _ImageDownloadListener);
128                 SysTryReturnResult(NID_MEDIA, __pListener.get() != null, E_OUT_OF_MEMORY,
129                   "Could not allocate _ImageDownloadListener.");
130         }
131
132         __pListener->SetImageUrlData(this);
133         __pListener->SetRequestId(reqId);
134         __pListener->SetImageUrlEvent(__pDataEvent.get());
135         __pListener->SetImagePixelFormat(__colorFormat);
136         __pListener->SetImageDestDimension(__destWidth, __destHeight);
137
138         r = RequestDownload(uri, reqId, msec);
139         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated..", GetErrorMessage(r));
140
141         // Start timer
142         if (msec != _TIMEOUT_INFINITE)
143         {
144                 r = __requestTimeout.Start(msec);
145                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated..", GetErrorMessage(r));
146                 SysLog(NID_MEDIA, "Timeout Timer started ReqeustId : %d, [%d msec]", __requestId, msec);
147                 __isTimerStarted = true;
148         }
149
150         return r;
151 }
152
153 result
154 _ImageUriData::Cancel(RequestId reqId, result res)
155 {
156         result r = E_SUCCESS;
157
158         SysTryCatch(NID_MEDIA, __pSessionImpl != null, r = E_OBJ_NOT_FOUND, r,
159           "[E_OBJ_NOT_FOUND] Session is null.");
160         SysTryCatch(NID_MEDIA, __pTransaction.get() != null, r = E_OBJ_NOT_FOUND, r,
161           "[E_OBJ_NOT_FOUND] Transaction is null.");
162
163         r = __pSessionImpl->CancelTransaction(*__pTransaction.get());
164         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Cancel failed.",
165           GetErrorMessage(r));
166
167         __pListener->OnDownloadCanceled(res);
168         __pTransactionImpl = null;
169
170         __pSessionImpl = null;
171
172         SetLastResult(r);
173         return r;
174
175 CATCH:
176
177         if (__pTransaction.get())
178         {
179                 __pTransactionImpl = null;
180         }
181         if (__pSession.get())
182         {
183                 __pSessionImpl = null;
184         }
185
186         return r;
187 }
188
189 RequestId
190 _ImageUriData::GetRequestId(void) const
191 {
192         return __requestId;
193 }
194
195 result
196 _ImageUriData::RequestDownload(const Utility::Uri &downloadUri,
197         RequestId& reqId, long msec)
198 {
199         result r = E_SUCCESS;
200         int sec = (msec == 0) ? 0 : (msec + 999) / 1000;
201
202         String srcPath;
203         String destPath;
204         String hostAddr(L"");
205         String proxyAddr(L"");
206
207         hostAddr = downloadUri.GetHost();
208
209         // Create HTTP session
210         if (__pSession.get() == null)
211         {
212                 __pSession.reset(new (std::nothrow) HttpSession);
213         }
214         SysTryCatch(NID_MEDIA, __pSession.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
215                            "[E_OUT_OF_MEMORY] Failed to create the HTTP session.");
216
217         r = __pSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, null, hostAddr, null);
218         SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM,
219           "[E_SYSTEM] Failed to construct HTTP session.");
220
221         __pSessionImpl = _HttpSessionImpl::GetInstance(*__pSession.get());
222         SysTryCatch(NID_MEDIA, __pSessionImpl != null, r = GetLastResult(), GetLastResult(),
223                            "[%s] Failed to get the managed httpsession.", GetErrorMessage(GetLastResult()));
224
225         __pTransaction.reset(__pSessionImpl->OpenTransactionN());
226         SysTryCatch(NID_MEDIA, __pTransaction.get() != null, r = E_SYSTEM, E_SYSTEM,
227           "[E_SYSTEM] Failed to create the HTTP transaction.");
228
229         __pTransactionImpl = _HttpTransactionImpl::GetInstance(*__pTransaction.get());
230         SysTryCatch(NID_MEDIA, __pTransactionImpl != null, r = GetLastResult(), GetLastResult(),
231                            "[%s] Failed to get the managed httptransaction.", GetErrorMessage(GetLastResult()));
232
233         r = __pTransactionImpl->SetTimeout(sec);
234         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to set timeout.", GetErrorMessage(r));
235
236         r = __pTransactionImpl->AddHttpTransactionListener(*(this->__pListener.get()));
237         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to add listener into the HTTP transaction.", GetErrorMessage(r));
238
239         __pRequest = __pTransactionImpl->GetRequest();
240         SysTryCatch(NID_MEDIA, __pRequest != null, r = GetLastResult(), GetLastResult(), "[%s] Failed to get response from the HTTP transaction.", GetErrorMessage(GetLastResult()));
241
242         __pRequestImpl = _HttpRequestImpl::GetInstance(*__pRequest);
243         SysTryCatch(NID_MEDIA, __pRequestImpl != null, r = GetLastResult(), GetLastResult(),
244                            "[%s] Failed to get the managed httprequest.", GetErrorMessage(GetLastResult()));
245
246         SysSecureLog(NID_MEDIA, "Uri = %ls", downloadUri.ToString().GetPointer());
247
248         r = __pRequestImpl->SetUri(downloadUri.ToString());
249         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to set uri into the HTTP request.", GetErrorMessage(r));
250
251         r = __pRequestImpl->SetMethod(NET_HTTP_METHOD_GET);
252         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to set method into the HTTP request.", GetErrorMessage(r));
253
254         r = __pTransactionImpl->Submit();
255         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to submit the HTTP request.", GetErrorMessage(r));
256
257         return r;
258
259 CATCH:
260
261         if (__pTransaction.get())
262         {
263                 __pTransactionImpl = null;
264         }
265         if (__pSession.get())
266         {
267                 __pSessionImpl = null;
268         }
269         return r;
270
271 }
272
273 void
274 _ImageUriData::OnTimerExpired(Timer& timer)
275 {
276         result r = E_SUCCESS;
277         _ImageUriDataErrorArg* pUriErrorArg = null;
278         _ImageUriDataDestroyArg* pUriDestroyArg = null;
279         RequestId reqId = GetRequestId();
280
281         SysLog(NID_MEDIA, "RequestId : %d", reqId);
282         __isTimerStarted = false;
283
284         r = Cancel(reqId, E_TIMEOUT);
285         if (r != E_SUCCESS)
286         {
287                 pUriErrorArg = new (std::nothrow) _ImageUriDataErrorArg;
288                 SysTryCatch(NID_MEDIA, pUriErrorArg != null, r = E_OUT_OF_MEMORY,
289                   E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
290
291                 pUriErrorArg->SetError(E_TIMEOUT);
292                 pUriErrorArg->SetErrorCode(L"E_TIMEOUT");
293                 pUriErrorArg->SetErrorMessage(L"E_TIMEOUT");
294                 pUriErrorArg->SetRequestId(reqId);
295                 pUriErrorArg->SetEventType(IMAGE_URI_DATA_EVENT_ERROR);
296
297                 r = __pDataEvent->FireAsync(*pUriErrorArg);
298                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
299
300                 SysLog(NID_MEDIA, "Send destroy event : %d", reqId);
301
302                 pUriDestroyArg = new (std::nothrow) _ImageUriDataDestroyArg;
303                 SysTryCatch(NID_MEDIA, pUriDestroyArg != null, r = E_OUT_OF_MEMORY,
304                   E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
305
306                 pUriDestroyArg->SetRequestId(this->GetRequestId());
307                 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
308
309                 r = __pDataEvent->FireAsync(*pUriDestroyArg);
310                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.",
311                   GetErrorMessage(r));
312
313         }
314
315         SetLastResult(E_SUCCESS);
316         return;
317
318 CATCH:
319         SetLastResult(r);
320         return;
321 }
322
323 }} // Tizen::Media