Change log for secure
[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         SysSecureLog(NID_WEB_CTRL, "The current value of url is %ls", 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
70         String domain(baseUri.GetScheme());
71         domain.Append(L"://");
72         domain.Append(baseUri.GetHost());
73         if (baseUri.GetPort() != -1)
74         {
75                 domain.Append(L":");
76                 domain.Append(Integer(baseUri.GetPort()).ToString());
77         }
78
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));
81
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));
84
85         r = pHttpSession->SetAutoRedirectionEnabled(true);
86         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] propogating.", GetErrorMessage(r));
87
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));
90
91         std::unique_ptr<HttpTransaction> pHttpTransaction( pSessionImpl->OpenTransactionN());
92         SysTryReturn(NID_WEB_CTRL, pHttpTransaction.get(), r = GetLastResult(), r, "[%s] propogating. OpenTransactionN Failed.", GetErrorMessage(r));
93
94         r = pHttpTransaction->AddHttpTransactionListener(*this);
95         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] propogating.", GetErrorMessage(r));
96
97
98         HttpRequest* pHttpRequest = const_cast< HttpRequest* >(pHttpTransaction->GetRequest());
99         SysTryReturn(NID_WEB_CTRL, pHttpRequest != null, r = GetLastResult(), r,"[%s] -propogating. GetRequest Failed.", GetErrorMessage(r));
100
101         r = pHttpRequest->SetUri(url);
102         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
103
104         r = pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);
105         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
106
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));
109
110         r = pHttpTransactionImpl->Submit();
111         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r,"[%s] Propogating.", GetErrorMessage(r));
112
113         __pHttpSession = std::move(pHttpSession);
114         pHttpTransaction.release();
115         return r;
116 }
117
118
119 Tizen::Web::Controls::IWebDownloadListener*
120 _WebDataHandler::GetDownloadListener(void) const
121 {
122         return __pDownloadListener;
123 }
124
125
126 void
127 _WebDataHandler::SetDownloadListener(const Tizen::Web::Controls::IWebDownloadListener* pDownloadListener)
128 {
129         __pDownloadListener = const_cast< IWebDownloadListener* >(pDownloadListener);
130 }
131
132
133 Tizen::Web::Controls::_WebEvent*
134 _WebDataHandler::GetWebEvent(void) const
135 {
136         return __pWebEvent;
137 }
138
139
140 void
141 _WebDataHandler::SetWebEvent(const Tizen::Web::Controls::_WebEvent* pWebEvent)
142 {
143         __pWebEvent = const_cast<_WebEvent* >(pWebEvent);
144 }
145
146
147 void
148 _WebDataHandler::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
149 {
150         SysLog(NID_WEB_CTRL, "The current value of totalReceivedSize is %d ", availableBodyLen);
151
152         ClearLastResult();
153         HttpResponse* pHttpResponse = httpTransaction.GetResponse();
154         SysTryReturnVoidResult(NID_WEB_CTRL, pHttpResponse != null, GetLastResult(), "[%s] Invalid Response.",GetErrorMessage(GetLastResult()));
155
156         HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
157         SysTryReturnVoidResult(NID_WEB_CTRL, pHttpHeader != null, GetLastResult(), "[%s] Download Invalid Response header.",GetErrorMessage(GetLastResult()));
158
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);
162 }
163
164 void
165 _WebDataHandler::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
166 {
167         LoadingErrorType errorType;
168
169         switch(r)
170         {
171          case   E_OUT_OF_MEMORY :
172                 errorType = WEB_OUT_OF_MEMORY;
173                 break;
174
175          case   E_IO :
176                 errorType = WEB_FILE_ACCESS_FAILED;
177                 break;
178
179          case   E_TIMEOUT :
180                 errorType = WEB_REQUEST_TIMEOUT;
181                 break;
182
183          case   E_NETWORK_UNAVAILABLE :
184                 //fall through
185          case   E_HOST_UNREACHABLE :
186                 //fall through
187          case   E_CONNECTION_RESET :
188                 errorType = WEB_NO_CONNECTION;
189                 break;
190
191          case   E_NOT_RESPONDING :
192          errorType = WEB_REQUEST_MAX_EXCEEDED;
193                 break;
194
195          case   E_INVALID_CONTENT :
196                 errorType = WEB_MIME_NOT_SUPPORTED;  //check if it is bad url
197                 break;
198
199          case   E_HTTP_USER :
200                 errorType = WEB_HTTP_RESPONSE;
201                 break;
202
203          case   E_NO_CERTIFICATE :
204                 errorType = WEB_INVALID_CERTIFICATE;
205                 break;
206
207          case   E_RESOURCE_UNAVAILABLE :
208                 errorType = WEB_FILE_ACCESS_FAILED;
209                 break;
210
211          case   E_UNSUPPORTED_SERVICE :
212                  //fall through
213          case   E_USER_AGENT_NOT_ALLOWED :
214                 //fall through
215          case   E_INVALID_ARG :
216                 //fall through
217          case   E_SYSTEM :
218                 //fall through
219          case   E_UNKNOWN :
220                  //fall through
221          default:
222                 errorType = WEB_ERROR_UNKNOWN;
223                 break;
224         }
225
226         __pDownloadListener->OnWebDownloadFailed(errorType);
227         __pDownloadListener->OnWebDataDownloadCompleted();
228
229         delete &httpTransaction;
230         __pHttpSession.reset();
231 }
232
233
234 void
235 _WebDataHandler::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
236 {
237 }
238
239
240 void
241 _WebDataHandler::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool authRequired)
242 {
243 }
244
245
246 void
247 _WebDataHandler::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
248 {
249         __pDownloadListener->OnWebDataDownloadCompleted();
250
251         delete &httpTransaction;
252         __pHttpSession.reset();
253 }
254
255
256 void
257 _WebDataHandler::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, String* pCert)
258 {
259         httpTransaction.Resume();
260
261         delete pCert;
262 }
263
264
265 }}} // Tizen::Web::Controls