Merge "Separated sync and async popup implementations" into tizen_2.1
[framework/osp/web.git] / src / controls / FWebCtrl_WebDataHandler.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                FWebCtrl_WebDataHandler.cpp
20  * @brief               The file contains the definition of _WebDataHandler class.
21  *
22  * The file contains the definition of _WebDataHandler class.
23  */
24
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"
36
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Net::Http;
41
42
43 namespace Tizen { namespace Web { namespace Controls
44 {
45
46 _WebDataHandler::_WebDataHandler(void)
47         : __pDownloadListener(null)
48         , __pWebEvent(null)
49         , __pHttpSession(null)
50 {
51 }
52
53
54 _WebDataHandler::~_WebDataHandler(void)
55 {
56
57 }
58
59
60 result
61 _WebDataHandler::StartDownload(const String& url)
62 {
63         SysLog(NID_WEB_CTRL, "Start Download %S", url.GetPointer());
64
65         SysTryReturnResult(NID_WEB_CTRL, __pHttpSession.get() == null, E_INVALID_OPERATION, "[%s] Already Downloading is in progress.", GetErrorMessage(E_INVALID_OPERATION));
66
67         Uri baseUri;
68         baseUri.SetUri(url);
69         ClearLastResult();
70
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));
73
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));
76
77         r = pHttpSession->SetAutoRedirectionEnabled(true);
78         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
79
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));
82
83         std::unique_ptr<HttpTransaction> pHttpTransaction( pSessionImpl->OpenTransactionN());
84         SysTryReturn(NID_WEB_CTRL, pHttpTransaction.get(), r = GetLastResult(), r, "[%s] propogating. OpenTransactionN Failed.", GetErrorMessage(r));
85
86         r = pHttpTransaction->AddHttpTransactionListener(*this);
87         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] propogating.", GetErrorMessage(r));
88
89
90         HttpRequest* pHttpRequest = const_cast< HttpRequest* >(pHttpTransaction->GetRequest());
91         SysTryReturn(NID_WEB_CTRL, pHttpRequest != null, r = GetLastResult(), r,"[%s] -propogating. GetRequest Failed.", GetErrorMessage(r));
92
93         r = pHttpRequest->SetUri(url);
94         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
95
96         r = pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);
97         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
98
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));
101
102         r = pHttpTransactionImpl->Submit();
103         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
104
105         __pHttpSession = std::move(pHttpSession);
106         pHttpTransaction.release();
107         return r;
108 }
109
110
111 Tizen::Web::Controls::IWebDownloadListener*
112 _WebDataHandler::GetDownloadListener(void) const
113 {
114         return __pDownloadListener;
115 }
116
117
118 void
119 _WebDataHandler::SetDownloadListener(const Tizen::Web::Controls::IWebDownloadListener* pDownloadListener)
120 {
121         __pDownloadListener = const_cast< IWebDownloadListener* >(pDownloadListener);
122 }
123
124
125 Tizen::Web::Controls::_WebEvent*
126 _WebDataHandler::GetWebEvent(void) const
127 {
128         return __pWebEvent;
129 }
130
131
132 void
133 _WebDataHandler::SetWebEvent(const Tizen::Web::Controls::_WebEvent* pWebEvent)
134 {
135         __pWebEvent = const_cast<_WebEvent* >(pWebEvent);
136 }
137
138
139 void
140 _WebDataHandler::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
141 {
142         SysLog(NID_WEB_CTRL, "The current value of totalReceivedSize is %d ", availableBodyLen);
143
144         ClearLastResult();
145         HttpResponse* pHttpResponse = httpTransaction.GetResponse();
146         SysTryReturnVoidResult(NID_WEB_CTRL, pHttpResponse != null, GetLastResult(), "[%s] Invalid Response.",GetErrorMessage(GetLastResult()));
147
148         HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
149         SysTryReturnVoidResult(NID_WEB_CTRL, pHttpHeader != null, GetLastResult(), "[%s] Download Invalid Response header.",GetErrorMessage(GetLastResult()));
150
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);
154 }
155
156 void
157 _WebDataHandler::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
158 {
159         LoadingErrorType errorType;
160
161         switch(r)
162         {
163          case   E_OUT_OF_MEMORY :
164                 errorType = WEB_OUT_OF_MEMORY;
165                 break;
166
167          case   E_IO :
168                 errorType = WEB_FILE_ACCESS_FAILED;
169                 break;
170
171          case   E_TIMEOUT :
172                 errorType = WEB_REQUEST_TIMEOUT;
173                 break;
174
175          case   E_NETWORK_UNAVAILABLE :
176                 //fall through
177          case   E_HOST_UNREACHABLE :
178                 //fall through
179          case   E_CONNECTION_RESET :
180                 errorType = WEB_NO_CONNECTION;
181                 break;
182
183          case   E_NOT_RESPONDING :
184          errorType = WEB_REQUEST_MAX_EXCEEDED;
185                 break;
186
187          case   E_INVALID_CONTENT :
188                 errorType = WEB_MIME_NOT_SUPPORTED;  //check if it is bad url
189                 break;
190
191          case   E_HTTP_USER :
192                 errorType = WEB_HTTP_RESPONSE;
193                 break;
194
195          case   E_NO_CERTIFICATE :
196                 errorType = WEB_INVALID_CERTIFICATE;
197                 break;
198
199          case   E_RESOURCE_UNAVAILABLE :
200                 errorType = WEB_FILE_ACCESS_FAILED;
201                 break;
202
203          case   E_UNSUPPORTED_SERVICE :
204                  //fall through
205          case   E_USER_AGENT_NOT_ALLOWED :
206                 //fall through
207          case   E_INVALID_ARG :
208                 //fall through
209          case   E_SYSTEM :
210                 //fall through
211          case   E_UNKNOWN :
212                  //fall through
213          default:
214                 errorType = WEB_ERROR_UNKNOWN;
215                 break;
216         }
217
218         __pDownloadListener->OnWebDownloadFailed(errorType);
219         __pDownloadListener->OnWebDataDownloadCompleted();
220
221         delete &httpTransaction;
222         __pHttpSession.reset();
223 }
224
225
226 void
227 _WebDataHandler::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
228 {
229 }
230
231
232 void
233 _WebDataHandler::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool authRequired)
234 {
235 }
236
237
238 void
239 _WebDataHandler::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
240 {
241         __pDownloadListener->OnWebDataDownloadCompleted();
242
243         delete &httpTransaction;
244         __pHttpSession.reset();
245 }
246
247
248 void
249 _WebDataHandler::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, String* pCert)
250 {
251 httpTransaction.Resume();
252
253         delete pCert;
254 }
255
256
257 }}} // Tizen::Web::Controls