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 SysSecureLog(NID_WEB_CTRL, "The current value of url is %ls", url.GetPointer());
65 SysTryReturnResult(NID_WEB_CTRL, __pHttpSession.get() == null, E_INVALID_OPERATION, "[%s] Already Downloading is in progress.", GetErrorMessage(E_INVALID_OPERATION));
70 String domain(baseUri.GetScheme());
71 domain.Append(L"://");
72 domain.Append(baseUri.GetHost());
73 if (baseUri.GetPort() != -1)
76 domain.Append(Integer(baseUri.GetPort()).ToString());
79 std::unique_ptr<HttpSession> pHttpSession(new (std::nothrow) HttpSession());
80 SysTryReturnResult(NID_WEB_CTRL, pHttpSession.get() , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
82 result r = pHttpSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, null, domain, null);
83 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
85 r = pHttpSession->SetAutoRedirectionEnabled(true);
86 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
88 _HttpSessionImpl* pSessionImpl = _HttpSessionImpl::GetInstance(*(pHttpSession.get()));
89 SysTryReturn(NID_WEB_CTRL, pSessionImpl != null, r = GetLastResult(), r, "[%s] propogating. Failed to get _HttpSessionImpl", GetErrorMessage(r));
91 std::unique_ptr<HttpTransaction> pHttpTransaction( pSessionImpl->OpenTransactionN());
92 SysTryReturn(NID_WEB_CTRL, pHttpTransaction.get(), r = GetLastResult(), r, "[%s] propogating. OpenTransactionN Failed.", GetErrorMessage(r));
94 r = pHttpTransaction->AddHttpTransactionListener(*this);
95 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] propogating.", GetErrorMessage(r));
98 HttpRequest* pHttpRequest = const_cast< HttpRequest* >(pHttpTransaction->GetRequest());
99 SysTryReturn(NID_WEB_CTRL, pHttpRequest != null, r = GetLastResult(), r,"[%s] -propogating. GetRequest Failed.", GetErrorMessage(r));
101 r = pHttpRequest->SetUri(url);
102 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
104 r = pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);
105 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
107 _HttpTransactionImpl* pHttpTransactionImpl = _HttpTransactionImpl::GetInstance(*pHttpTransaction);
108 SysTryReturn(NID_WEB_CTRL, pHttpTransactionImpl != null, r = GetLastResult(), r,"[%s] -propogating. Failed to get instance of _HttpTransactionImpl.", GetErrorMessage(r));
110 r = pHttpTransactionImpl->Submit();
111 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
113 __pHttpSession = std::move(pHttpSession);
114 pHttpTransaction.release();
119 Tizen::Web::Controls::IWebDownloadListener*
120 _WebDataHandler::GetDownloadListener(void) const
122 return __pDownloadListener;
127 _WebDataHandler::SetDownloadListener(const Tizen::Web::Controls::IWebDownloadListener* pDownloadListener)
129 __pDownloadListener = const_cast< IWebDownloadListener* >(pDownloadListener);
133 Tizen::Web::Controls::_WebEvent*
134 _WebDataHandler::GetWebEvent(void) const
141 _WebDataHandler::SetWebEvent(const Tizen::Web::Controls::_WebEvent* pWebEvent)
143 __pWebEvent = const_cast<_WebEvent* >(pWebEvent);
148 _WebDataHandler::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
150 SysLog(NID_WEB_CTRL, "The current value of totalReceivedSize is %d ", availableBodyLen);
153 HttpResponse* pHttpResponse = httpTransaction.GetResponse();
154 SysTryReturnVoidResult(NID_WEB_CTRL, pHttpResponse != null, GetLastResult(), "[%s] Invalid Response.",GetErrorMessage(GetLastResult()));
156 HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
157 SysTryReturnVoidResult(NID_WEB_CTRL, pHttpHeader != null, GetLastResult(), "[%s] Download Invalid Response header.",GetErrorMessage(GetLastResult()));
159 std::unique_ptr<ByteBuffer> pBuffer(pHttpResponse->ReadBodyN());
160 SysTryReturnVoidResult(NID_WEB_CTRL, pBuffer.get(), GetLastResult(), "[%s] Propogating. ReadBodyN failed.",GetErrorMessage(GetLastResult()));
161 __pDownloadListener->OnWebChunkedDataReceived(*pBuffer);
165 _WebDataHandler::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
167 LoadingErrorType errorType;
171 case E_OUT_OF_MEMORY :
172 errorType = WEB_OUT_OF_MEMORY;
176 errorType = WEB_FILE_ACCESS_FAILED;
180 errorType = WEB_REQUEST_TIMEOUT;
183 case E_NETWORK_UNAVAILABLE :
185 case E_HOST_UNREACHABLE :
187 case E_CONNECTION_RESET :
188 errorType = WEB_NO_CONNECTION;
191 case E_NOT_RESPONDING :
192 errorType = WEB_REQUEST_MAX_EXCEEDED;
195 case E_INVALID_CONTENT :
196 errorType = WEB_MIME_NOT_SUPPORTED; //check if it is bad url
200 errorType = WEB_HTTP_RESPONSE;
203 case E_NO_CERTIFICATE :
204 errorType = WEB_INVALID_CERTIFICATE;
207 case E_RESOURCE_UNAVAILABLE :
208 errorType = WEB_FILE_ACCESS_FAILED;
211 case E_UNSUPPORTED_SERVICE :
213 case E_USER_AGENT_NOT_ALLOWED :
222 errorType = WEB_ERROR_UNKNOWN;
226 __pDownloadListener->OnWebDownloadFailed(errorType);
227 __pDownloadListener->OnWebDataDownloadCompleted();
229 delete &httpTransaction;
230 __pHttpSession.reset();
235 _WebDataHandler::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
241 _WebDataHandler::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool authRequired)
247 _WebDataHandler::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
249 __pDownloadListener->OnWebDataDownloadCompleted();
251 delete &httpTransaction;
252 __pHttpSession.reset();
257 _WebDataHandler::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, String* pCert)
259 httpTransaction.Resume();
265 }}} // Tizen::Web::Controls