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 FWebCtrl_WebDataHandler.cpp
20 * @brief The file contains the definition of _WebDataHandler class.
22 * The file contains the definition of _WebDataHandler class.
25 #include <FBaseInteger.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilUri.h>
28 #include <FWebCtrlILoadingListener.h>
29 #include <FWebCtrlIWebDownloadListener.h>
30 #include <FBase_StringConverter.h>
31 #include <FNetHttp_HttpSessionImpl.h>
32 #include <FNetHttp_HttpTransactionImpl.h>
33 #include "FWebCtrl_WebDataHandler.h"
34 #include "FWebCtrl_WebEvent.h"
35 #include "FWebCtrl_WebEventArg.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Net::Http;
43 namespace Tizen { namespace Web { namespace Controls
46 _WebDataHandler::_WebDataHandler(void)
47 : __pDownloadListener(null)
49 , __pHttpSession(null)
54 _WebDataHandler::~_WebDataHandler(void)
61 _WebDataHandler::StartDownload(const String& url)
63 SysLog(NID_WEB_CTRL, "Start Download %S", url.GetPointer());
65 SysTryReturnResult(NID_WEB_CTRL, __pHttpSession.get() == null, E_INVALID_OPERATION, "[%s] Already Downloading is in progress.", GetErrorMessage(E_INVALID_OPERATION));
71 std::unique_ptr<HttpSession> pHttpSession(new (std::nothrow) HttpSession());
72 SysTryReturnResult(NID_WEB_CTRL, pHttpSession.get() , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
74 result r = pHttpSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, null, baseUri.GetHost(), null);
75 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
77 r = pHttpSession->SetAutoRedirectionEnabled(true);
78 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
80 _HttpSessionImpl* pSessionImpl = _HttpSessionImpl::GetInstance(*(pHttpSession.get()));
81 SysTryReturn(NID_WEB_CTRL, pSessionImpl != null, r = GetLastResult(), r, "[%s] propogating. Failed to get _HttpSessionImpl", GetErrorMessage(r));
83 std::unique_ptr<HttpTransaction> pHttpTransaction( pSessionImpl->OpenTransactionN());
84 SysTryReturn(NID_WEB_CTRL, pHttpTransaction.get(), r = GetLastResult(), r, "[%s] propogating. OpenTransactionN Failed.", GetErrorMessage(r));
86 r = pHttpTransaction->AddHttpTransactionListener(*this);
87 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] propogating.", GetErrorMessage(r));
90 HttpRequest* pHttpRequest = const_cast< HttpRequest* >(pHttpTransaction->GetRequest());
91 SysTryReturn(NID_WEB_CTRL, pHttpRequest != null, r = GetLastResult(), r,"[%s] -propogating. GetRequest Failed.", GetErrorMessage(r));
93 r = pHttpRequest->SetUri(url);
94 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
96 r = pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);
97 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
99 _HttpTransactionImpl* pHttpTransactionImpl = _HttpTransactionImpl::GetInstance(*pHttpTransaction);
100 SysTryReturn(NID_WEB_CTRL, pHttpTransactionImpl != null, r = GetLastResult(), r,"[%s] -propogating. Failed to get instance of _HttpTransactionImpl.", GetErrorMessage(r));
102 r = pHttpTransactionImpl->Submit();
103 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
105 __pHttpSession = std::move(pHttpSession);
106 pHttpTransaction.release();
111 Tizen::Web::Controls::IWebDownloadListener*
112 _WebDataHandler::GetDownloadListener(void) const
114 return __pDownloadListener;
119 _WebDataHandler::SetDownloadListener(const Tizen::Web::Controls::IWebDownloadListener* pDownloadListener)
121 __pDownloadListener = const_cast< IWebDownloadListener* >(pDownloadListener);
125 Tizen::Web::Controls::_WebEvent*
126 _WebDataHandler::GetWebEvent(void) const
133 _WebDataHandler::SetWebEvent(const Tizen::Web::Controls::_WebEvent* pWebEvent)
135 __pWebEvent = const_cast<_WebEvent* >(pWebEvent);
140 _WebDataHandler::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
142 SysLog(NID_WEB_CTRL, "The current value of totalReceivedSize is %d ", availableBodyLen);
145 HttpResponse* pHttpResponse = httpTransaction.GetResponse();
146 SysTryReturnVoidResult(NID_WEB_CTRL, pHttpResponse != null, GetLastResult(), "[%s] Invalid Response.",GetErrorMessage(GetLastResult()));
148 HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
149 SysTryReturnVoidResult(NID_WEB_CTRL, pHttpHeader != null, GetLastResult(), "[%s] Download Invalid Response header.",GetErrorMessage(GetLastResult()));
151 std::unique_ptr<ByteBuffer> pBuffer(pHttpResponse->ReadBodyN());
152 SysTryReturnVoidResult(NID_WEB_CTRL, pBuffer.get(), GetLastResult(), "[%s] Propogating. ReadBodyN failed.",GetErrorMessage(GetLastResult()));
153 __pDownloadListener->OnWebChunkedDataReceived(*pBuffer);
157 _WebDataHandler::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
159 LoadingErrorType errorType;
163 case E_OUT_OF_MEMORY :
164 errorType = WEB_OUT_OF_MEMORY;
168 errorType = WEB_FILE_ACCESS_FAILED;
172 errorType = WEB_REQUEST_TIMEOUT;
175 case E_NETWORK_UNAVAILABLE :
177 case E_HOST_UNREACHABLE :
179 case E_CONNECTION_RESET :
180 errorType = WEB_NO_CONNECTION;
183 case E_NOT_RESPONDING :
184 errorType = WEB_REQUEST_MAX_EXCEEDED;
187 case E_INVALID_CONTENT :
188 errorType = WEB_MIME_NOT_SUPPORTED; //check if it is bad url
192 errorType = WEB_HTTP_RESPONSE;
195 case E_NO_CERTIFICATE :
196 errorType = WEB_INVALID_CERTIFICATE;
199 case E_RESOURCE_UNAVAILABLE :
200 errorType = WEB_FILE_ACCESS_FAILED;
203 case E_UNSUPPORTED_SERVICE :
205 case E_USER_AGENT_NOT_ALLOWED :
214 errorType = WEB_ERROR_UNKNOWN;
218 __pDownloadListener->OnWebDownloadFailed(errorType);
219 __pDownloadListener->OnWebDataDownloadCompleted();
221 delete &httpTransaction;
222 __pHttpSession.reset();
227 _WebDataHandler::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
233 _WebDataHandler::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool authRequired)
239 _WebDataHandler::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
241 __pDownloadListener->OnWebDataDownloadCompleted();
243 delete &httpTransaction;
244 __pHttpSession.reset();
249 _WebDataHandler::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, String* pCert)
251 httpTransaction.Resume();
257 }}} // Tizen::Web::Controls