3 // Open Service Platform
4 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
10 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @file FMedia_ImageUriData.cpp
21 * @brief This file contains the implementation of _ImageUriData class,
22 * required internally by Image::DecodeUrl.
25 #include <unique_ptr.h>
26 #include <FSysSystemTime.h>
27 #include <FGrpBitmapCommon.h>
28 #include <FMediaIImageEventListener.h>
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>
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"
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;
53 namespace Tizen{ namespace Media{
55 _ImageUriData::_ImageUriData(void):
59 __pTransactionImpl(null),
63 __colorFormat(BITMAP_PIXEL_FORMAT_RGB565),
64 __requestId(INVALID_REQUEST_ID),
66 __isTimerStarted(false)
70 _ImageUriData::~_ImageUriData(void)
74 SysLog(NID_MEDIA, "Timeout Timer canceled, ReqeustId : %d", __requestId);
75 __requestTimeout.Cancel();
78 if (__pSession.get() != null)
80 __pSessionImpl = null;
81 __pSession.reset(null);
84 if (__pTransaction.get() != null)
86 __pTransactionImpl = null;
87 __pTransaction.reset(null);
93 _ImageUriData::Construct(IEventListener& listener)
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].");
101 r = __pDataEvent->Construct();
102 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
104 r = __pDataEvent->AddListener (listener);
105 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
107 r = __requestTimeout.Construct(*this);
108 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
114 _ImageUriData::RequestDecode(const Utility::Uri &uri, BitmapPixelFormat colorFormat,
115 const Dimension& destDim, RequestId& reqId, long msec)
117 result r = E_SUCCESS;
120 __destWidth = destDim.width;
121 __destHeight = destDim.height;
122 __colorFormat = colorFormat;
125 if (__pListener.get() == null)
127 __pListener.reset(new (std::nothrow) _ImageDownloadListener);
128 SysTryReturnResult(NID_MEDIA, __pListener.get() != null, E_OUT_OF_MEMORY,
129 "Could not allocate _ImageDownloadListener.");
132 __pListener->SetImageUrlData(this);
133 __pListener->SetRequestId(reqId);
134 __pListener->SetImageUrlEvent(__pDataEvent.get());
135 __pListener->SetImagePixelFormat(__colorFormat);
136 __pListener->SetImageDestDimension(__destWidth, __destHeight);
138 r = RequestDownload(uri, reqId, msec);
139 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated..", GetErrorMessage(r));
142 if (msec != _TIMEOUT_INFINITE)
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;
154 _ImageUriData::Cancel(RequestId reqId, result res)
156 result r = E_SUCCESS;
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.");
163 r = __pSessionImpl->CancelTransaction(*__pTransaction.get());
164 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Cancel failed.",
167 __pListener->OnDownloadCanceled(res);
168 __pTransactionImpl = null;
170 __pSessionImpl = null;
177 if (__pTransaction.get())
179 __pTransactionImpl = null;
181 if (__pSession.get())
183 __pSessionImpl = null;
190 _ImageUriData::GetRequestId(void) const
196 _ImageUriData::RequestDownload(const Utility::Uri &downloadUri,
197 RequestId& reqId, long msec)
199 result r = E_SUCCESS;
200 int sec = (msec == 0) ? 0 : (msec + 999) / 1000;
204 String hostAddr(L"");
205 String proxyAddr(L"");
207 hostAddr = downloadUri.GetHost();
209 // Create HTTP session
210 if (__pSession.get() == null)
212 __pSession.reset(new (std::nothrow) HttpSession);
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.");
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.");
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()));
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.");
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()));
233 r = __pTransactionImpl->SetTimeout(sec);
234 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to set timeout.", GetErrorMessage(r));
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));
239 __pRequest = __pTransactionImpl->GetRequest();
240 SysTryCatch(NID_MEDIA, __pRequest != null, r = GetLastResult(), GetLastResult(), "[%s] Failed to get response from the HTTP transaction.", GetErrorMessage(GetLastResult()));
242 __pRequestImpl = _HttpRequestImpl::GetInstance(*__pRequest);
243 SysTryCatch(NID_MEDIA, __pRequestImpl != null, r = GetLastResult(), GetLastResult(),
244 "[%s] Failed to get the managed httprequest.", GetErrorMessage(GetLastResult()));
246 SysSecureLog(NID_MEDIA, "Uri = %ls", downloadUri.ToString().GetPointer());
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));
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));
254 r = __pTransactionImpl->Submit();
255 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to submit the HTTP request.", GetErrorMessage(r));
261 if (__pTransaction.get())
263 __pTransactionImpl = null;
265 if (__pSession.get())
267 __pSessionImpl = null;
274 _ImageUriData::OnTimerExpired(Timer& timer)
276 result r = E_SUCCESS;
277 _ImageUriDataErrorArg* pUriErrorArg = null;
278 _ImageUriDataDestroyArg* pUriDestroyArg = null;
279 RequestId reqId = GetRequestId();
281 SysLog(NID_MEDIA, "RequestId : %d", reqId);
282 __isTimerStarted = false;
284 r = Cancel(reqId, E_TIMEOUT);
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]");
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);
297 r = __pDataEvent->FireAsync(*pUriErrorArg);
298 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
300 SysLog(NID_MEDIA, "Send destroy event : %d", reqId);
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]");
306 pUriDestroyArg->SetRequestId(this->GetRequestId());
307 pUriDestroyArg->SetEventType(IMAGE_URI_DATA_EVENT_DESTROY);
309 r = __pDataEvent->FireAsync(*pUriDestroyArg);
310 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.",
315 SetLastResult(E_SUCCESS);