Refactoring, removed unneeded check in WebkitEngineService
[profile/tv/apps/web/browser.git] / services / WebKitEngineService / WebView.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * WebView.cpp
19  *
20  *  Created on: Apr 1, 2014
21  *      Author: p.rafalski
22  */
23
24 #include "WebView.h"
25
26 #if defined(USE_EWEBKIT)
27 #include <ewk_chromium.h>
28 #endif
29
30 #include <boost/format.hpp>
31 #include <boost/regex.hpp>
32 #include <boost/algorithm/string/regex.hpp>
33 #include <boost/algorithm/string/classification.hpp>
34 #include <boost/algorithm/string/split.hpp>
35 #include <Elementary.h>
36 #include <Evas.h>
37
38 #include "AbstractWebEngine/AbstractWebEngine.h"
39 #include "AbstractWebEngine/TabThumbCache.h"
40 #include "BrowserAssert.h"
41 #include "BrowserLogger.h"
42 #include "EflTools.h"
43 #include "GeneralTools.h"
44 #include "Tools/WorkQueue.h"
45 #include "ServiceManager.h"
46
47 #define certificate_crt_path CERTS_DIR
48 #if MERGE_ME
49 #define APPLICATION_NAME_FOR_USER_AGENT "SamsungBrowser/1.0"
50 #else
51 #define APPLICATION_NAME_FOR_USER_AGENT "Mozilla/5.0 (X11; SMART-TV; Linux) AppleWebkit/538.1 (KHTML, like Gecko) Safari/538.1"
52 #endif
53
54 //TODO: temporary user agent for mobile display, change to proper one
55 #define APPLICATION_NAME_FOR_USER_AGENT_MOBILE "Mozilla/5.0 (Linux; Tizen 3.0; SAMSUNG SM-Z130H) AppleWebKit/538.1 (KHTML, like Gecko) SamsungBrowser/1.0 Mobile Safari/538.1"
56
57 using namespace tizen_browser::tools;
58
59 namespace tizen_browser {
60 namespace basic_webengine {
61 namespace webkitengine_service {
62
63 WebView::WebView(Evas_Object * obj, TabId tabId)
64     : m_parent(obj)
65     , m_tabId(tabId)
66     , m_ewkContext(ewk_context_new())
67     , m_title(std::string())
68     , m_ewkView(nullptr)
69     , m_isLoading(false)
70     , m_loadError(false)
71 {
72     config.load("whatever");
73 }
74
75 WebView::~WebView()
76 {
77     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
78
79     if (m_ewkView) {
80         unregisterCallbacks();
81     }
82
83     ewk_context_delete(m_ewkContext);
84 }
85
86 void WebView::init(bool desktopMode, Evas_Object * opener)
87 {
88 #if defined(USE_EWEBKIT)
89     m_ewkView = ewk_view_add_with_context(evas_object_evas_get(m_parent), m_ewkContext);
90
91     evas_object_data_set(m_ewkView, "_container", this);
92     BROWSER_LOGD("%s:%d %s self=%p", __FILE__, __LINE__, __func__, this);
93
94     evas_object_color_set(m_ewkView, 255, 255, 255, 255);
95     evas_object_size_hint_weight_set(m_ewkView, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
96     evas_object_size_hint_align_set(m_ewkView, EVAS_HINT_FILL, EVAS_HINT_FILL);
97     if (desktopMode) {
98         switchToDesktopMode();
99     } else {
100         switchToMobileMode();
101     }
102     //\todo: when value is other than 1.0, scroller is located improperly
103 //    ewk_view_device_pixel_ratio_set(m_ewkView, 1.0f);
104
105 #if PLATFORM(TIZEN)
106     ewk_view_resume(m_ewkView);
107 #endif
108
109     if (m_ewkView)
110     {
111         Ewk_Context *context = ewk_view_context_get(m_ewkView);
112         if (context)
113         {
114             ewk_context_cache_model_set(context, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
115         }
116     }
117
118 ///\note Odroid modification - not exists in WebKit API
119 //     ewk_cookie_manager_widget_cookie_directory_set(ewk_context_cookie_manager_get(context), webkit_path.c_str());
120     setupEwkSettings();
121     registerCallbacks();
122 #else
123     m_ewkView = evas_object_rectangle_add(evas_object_evas_get(m_parent));
124 #endif
125 }
126
127 void WebView::registerCallbacks()
128 {
129 #if defined(USE_EWEBKIT)
130     evas_object_smart_callback_add(m_ewkView, "load,started", __loadStarted, this);
131     evas_object_smart_callback_add(m_ewkView, "load,stop", __loadStop, this);
132     evas_object_smart_callback_add(m_ewkView, "load,finished", __loadFinished, this);
133     evas_object_smart_callback_add(m_ewkView, "load,progress", __loadProgress, this);
134     evas_object_smart_callback_add(m_ewkView, "load,error", __loadError, this);
135
136     evas_object_smart_callback_add(m_ewkView, "title,changed", __titleChanged, this);
137     evas_object_smart_callback_add(m_ewkView, "url,changed", __urlChanged, this);
138
139     evas_object_smart_callback_add(m_ewkView, "back,forward,list,changed", __backForwardListChanged, this);
140
141     evas_object_smart_callback_add(m_ewkView, "create,window", __newWindowRequest, this);
142     evas_object_smart_callback_add(m_ewkView, "close,window", __closeWindowRequest, this);
143
144     evas_object_smart_callback_add(m_ewkView, "geolocation,permission,request", __geolocationPermissionRequest, this);
145     evas_object_smart_callback_add(m_ewkView, "usermedia,permission,request", __usermediaPermissionRequest, this);
146     evas_object_smart_callback_add(m_ewkView, "notification,permission,request", __notificationPermissionRequest, this);
147     evas_object_smart_callback_add(m_ewkView, "authentication,request", __authenticationRequest, this);
148     evas_object_smart_callback_add(m_ewkView, "request,certificate,confirm", __requestCertificationConfirm, this);
149
150     evas_object_event_callback_add(m_ewkView, EVAS_CALLBACK_MOUSE_DOWN, __setFocusToEwkView, this);
151     evas_object_smart_callback_add(m_ewkView, "icon,received", __faviconChanged, this);
152
153     evas_object_smart_callback_add(m_ewkView, "editorclient,ime,closed", __IMEClosed, this);
154     evas_object_smart_callback_add(m_ewkView, "editorclient,ime,opened", __IMEOpened, this);
155 #endif
156 }
157
158 void WebView::unregisterCallbacks()
159 {
160 #if defined(USE_EWEBKIT)
161     evas_object_smart_callback_del_full(m_ewkView, "load,started", __loadStarted, this);
162     evas_object_smart_callback_del_full(m_ewkView, "load,stop", __loadStop, this);
163     evas_object_smart_callback_del_full(m_ewkView, "load,finished", __loadFinished, this);
164     evas_object_smart_callback_del_full(m_ewkView, "load,progress", __loadProgress, this);
165     evas_object_smart_callback_del_full(m_ewkView, "load,error", __loadError, this);
166
167     evas_object_smart_callback_del_full(m_ewkView, "title,changed", __titleChanged, this);
168     evas_object_smart_callback_del_full(m_ewkView, "url,changed", __urlChanged, this);
169
170     evas_object_smart_callback_del_full(m_ewkView, "back,forward,list,changed", __backForwardListChanged, this);
171
172     evas_object_smart_callback_del_full(m_ewkView, "create,window", __newWindowRequest, this);
173     evas_object_smart_callback_del_full(m_ewkView, "close,window", __closeWindowRequest, this);
174
175     evas_object_smart_callback_del_full(m_ewkView, "geolocation,permission,request", __geolocationPermissionRequest, this);
176     evas_object_smart_callback_del_full(m_ewkView, "usermedia,permission,request", __usermediaPermissionRequest, this);
177     evas_object_smart_callback_del_full(m_ewkView, "notification,permission,request", __notificationPermissionRequest, this);
178     evas_object_smart_callback_del_full(m_ewkView, "authentication,request", __authenticationRequest, this);
179     evas_object_smart_callback_del_full(m_ewkView, "request,certificate,confirm", __requestCertificationConfirm, this);
180
181     evas_object_event_callback_del(m_ewkView, EVAS_CALLBACK_MOUSE_DOWN, __setFocusToEwkView);
182     evas_object_smart_callback_del_full(m_ewkView, "icon,received", __faviconChanged, this);
183
184     evas_object_smart_callback_del_full(m_ewkView, "editorclient,ime,closed", __IMEClosed, this);
185     evas_object_smart_callback_del_full(m_ewkView, "editorclient,ime,opened", __IMEOpened, this);
186 #endif
187 }
188
189 void WebView::setupEwkSettings()
190 {
191 #if defined(USE_EWEBKIT)
192 #if PLATFORM(TIZEN)
193     Ewk_Settings * settings = ewk_view_settings_get(m_ewkView);
194     ewk_settings_uses_keypad_without_user_action_set(settings, EINA_FALSE);
195 #endif
196 #endif
197 }
198
199 Evas_Object * WebView::getLayout()
200 {
201     return m_ewkView;
202 }
203
204 void WebView::setURI(const std::string & uri)
205 {
206     BROWSER_LOGD("%s:%d %s uri=%s", __FILE__, __LINE__, __func__, uri.c_str());
207 #if defined(USE_EWEBKIT)
208     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
209     ewk_view_url_set(m_ewkView, uri.c_str());
210     m_loadError = false;
211 #endif
212 }
213
214 std::string WebView::getURI(void)
215 {
216 #if defined(USE_EWEBKIT)
217     BROWSER_LOGD("%s:%d %s uri=%s", __FILE__, __LINE__, __func__, ewk_view_url_get(m_ewkView));
218     return fromChar(ewk_view_url_get(m_ewkView));
219 #else
220     return std::string();
221 #endif
222 }
223
224 std::string WebView::getTitle(void)
225 {
226     return m_title;
227 }
228
229 void WebView::stopLoading(void)
230 {
231 #if defined(USE_EWEBKIT)
232     ewk_view_stop(m_ewkView);
233 #endif
234     loadStop();
235 }
236
237 void WebView::reload(void)
238 {
239 #if defined(USE_EWEBKIT)
240     if(m_loadError)
241     {
242         m_loadError = false;
243         ewk_view_url_set(m_ewkView, ewk_view_url_get(m_ewkView));
244     }
245     else
246         ewk_view_reload(m_ewkView);
247 #endif
248 }
249
250 void WebView::back(void)
251 {
252 #if defined(USE_EWEBKIT)
253     m_loadError = false;
254     ewk_view_back(m_ewkView);
255 #endif
256 }
257
258 void WebView::forward(void)
259 {
260 #if defined(USE_EWEBKIT)
261     m_loadError = false;
262     ewk_view_forward(m_ewkView);
263 #endif
264 }
265
266 bool WebView::isBackEnabled(void)
267 {
268 #if defined(USE_EWEBKIT)
269     return ewk_view_back_possible(m_ewkView);
270 #else
271     return false;
272 #endif
273 }
274
275 bool WebView::isForwardEnabled(void)
276 {
277 #if defined(USE_EWEBKIT)
278     return ewk_view_forward_possible(m_ewkView);
279 #else
280     return false;
281 #endif
282 }
283
284 bool WebView::isLoading()
285 {
286     return m_isLoading;
287 }
288
289 bool WebView::isLoadError() const
290 {
291     return m_loadError;
292 }
293
294 void WebView::setPrivateMode(bool state)
295 {
296     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
297     M_ASSERT(m_ewkView);
298
299 #if defined(USE_EWEBKIT)
300 #if PLATFORM(TIZEN)
301     Ewk_Settings * settings = ewk_view_settings_get(m_ewkView);
302 #else
303     Ewk_Settings * settings = ewk_page_group_settings_get(ewk_view_page_group_get(m_ewkView));
304 #endif
305     ewk_settings_private_browsing_enabled_set(settings, state);
306     if (m_ewkView)
307     {
308         Ewk_Context *context = ewk_view_context_get(m_ewkView);
309         if (context)
310         {
311             if(state)
312             {
313                  ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(context), EWK_COOKIE_ACCEPT_POLICY_NEVER);
314             }
315             else
316             {
317                  ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(context), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
318             }
319         }
320     }
321 #endif
322 }
323
324 void WebView::confirmationResult(WebConfirmationPtr confirmation)
325 {
326     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
327
328 #if defined(USE_EWEBKIT)
329 #if PLATFORM(TIZEN)
330     switch(confirmation->getConfirmationType()) {
331     case WebConfirmation::ConfirmationType::Geolocation: {
332         Ewk_Geolocation_Permission_Request *request = m_confirmationGeolocationMap[confirmation];
333         Eina_Bool result;
334         if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Confirmed)
335             result = EINA_TRUE;
336         else if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Rejected)
337             result = EINA_FALSE;
338         else {
339             BROWSER_LOGE("Wrong ConfirmationResult");
340             break;
341         }
342         // set geolocation permission
343         ewk_geolocation_permission_reply(request, result);
344         ewk_view_resume(m_ewkView);
345
346         // remove from map
347         m_confirmationGeolocationMap.erase(confirmation);
348         break;
349     }
350     case WebConfirmation::ConfirmationType::UserMedia: {
351         Ewk_User_Media_Permission_Request *request = m_confirmationUserMediaMap[confirmation];
352         Eina_Bool result;
353         if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Confirmed)
354             result = EINA_TRUE;
355         else if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Rejected)
356             result = EINA_FALSE;
357         else {
358             BROWSER_LOGE("Wrong ConfirmationResult");
359             break;
360         };
361
362         // set usermedia permission
363         ewk_user_media_permission_reply(request, result);
364         ewk_view_resume(m_ewkView);
365
366         // remove from map
367         m_confirmationUserMediaMap.erase(confirmation);
368         break;
369     }
370     case WebConfirmation::ConfirmationType::Notification: {
371         Ewk_Notification_Permission_Request *request = m_confirmationNotificationMap[confirmation];
372         Eina_Bool result;
373         if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Confirmed)
374             result = EINA_TRUE;
375         else if (confirmation->getResult() == WebConfirmation::ConfirmationResult::Rejected)
376             result = EINA_FALSE;
377         else {
378             BROWSER_LOGE("Wrong ConfirmationResult");
379             break;
380         }
381
382         // set notification permission
383         ewk_notification_permission_reply(request, result);
384         ewk_view_resume(m_ewkView);
385
386         // remove from map
387         m_confirmationNotificationMap.erase(confirmation);
388         break;
389     }
390     case WebConfirmation::ConfirmationType::CertificateConfirmation: {
391         CertificateConfirmationPtr cert = std::dynamic_pointer_cast<CertificateConfirmation, WebConfirmation>(confirmation);
392         Ewk_Certificate_Policy_Decision *request = m_confirmationCertificatenMap[cert];
393         Eina_Bool result;
394         if (cert->getResult() == WebConfirmation::ConfirmationResult::Confirmed)
395             result = EINA_TRUE;
396         else if (cert->getResult() == WebConfirmation::ConfirmationResult::Rejected)
397             result = EINA_FALSE;
398         else {
399             BROWSER_LOGE("Wrong ConfirmationResult");
400             break;
401         }
402
403         // set certificate confirmation
404         BROWSER_LOGE("NOT IMPLEMENTED: Certificate Confirmation handling!");
405
406         // remove from map
407         m_confirmationCertificatenMap.erase(cert);
408         break;
409     }
410     case WebConfirmation::ConfirmationType::Authentication: {
411         AuthenticationConfirmationPtr auth = std::dynamic_pointer_cast<AuthenticationConfirmation, WebConfirmation>(confirmation);
412         Ewk_Auth_Request *request = m_confirmationAuthenticationMap[auth];
413         if (auth->getResult() == WebConfirmation::ConfirmationResult::Confirmed) {
414             BROWSER_LOGE("NOT IMPLEMENTED: Autenthication Request Confirmation handling!");
415         } else if (auth->getResult() == WebConfirmation::ConfirmationResult::Rejected) {
416             BROWSER_LOGE("NOT IMPLEMENTED: Autenthication Request Rejection handling!");
417         } else {
418             BROWSER_LOGE("Wrong ConfirmationResult");
419             break;
420         }
421
422         // remove from map
423         m_confirmationAuthenticationMap.erase(auth);
424         break;
425     }
426     default:
427         break;
428     }
429 #else
430    (void)confirmation;
431 #endif
432 #endif
433 }
434
435 std::shared_ptr<BrowserImage> WebView::captureSnapshot(int targetWidth, int targetHeight)
436 {
437     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
438     M_ASSERT(m_ewkView);
439     M_ASSERT(targetWidth);
440     M_ASSERT(targetHeight);
441     Evas_Coord vw, vh;
442     std::shared_ptr<BrowserImage> noImage = std::make_shared<BrowserImage>();
443     evas_object_geometry_get(m_ewkView, nullptr, nullptr, &vw, &vh);
444     if (vw == 0 || vh == 0)
445         return noImage;
446
447     Eina_Rectangle area;
448     double snapshotProportions = (double)(targetWidth) /(double)(targetHeight);
449     double webkitProportions = (double)(vw) /(double)(vh);
450     if (webkitProportions >= snapshotProportions) {
451         // centring position of screenshot
452         area.x = (vw*getZoomFactor()/2) - (vh*getZoomFactor()*snapshotProportions/2);
453         area.y = 0;
454         area.w = vh*getZoomFactor()*snapshotProportions;
455         area.h = vh*getZoomFactor();
456     }
457     else {
458         area.x = 0;
459         area.y = 0;
460         area.w = vw*getZoomFactor();
461         area.h = vw*getZoomFactor()/snapshotProportions;
462     }
463     if (area.w == 0 || area.h == 0)
464         return noImage;
465
466
467     BROWSER_LOGD("[%s:%d] Before snapshot (screenshot) - look at the time of taking snapshot below",__func__, __LINE__);
468 #if defined(USE_EWEBKIT)
469 #if PLATFORM(TIZEN)
470     Evas_Object *snapshot = ewk_view_screenshot_contents_get( m_ewkView, area, 1.0, evas_object_evas_get(m_ewkView));
471     BROWSER_LOGD("[%s:%d] Snapshot (screenshot) catched, evas pointer: %p",__func__, __LINE__, snapshot);
472     if (snapshot)
473         return EflTools::getBrowserImage(snapshot);
474 #endif
475 #endif
476
477     return noImage;
478 }
479
480 #if defined(USE_EWEBKIT)
481 void WebView::__setFocusToEwkView(void * data, Evas * /* e */, Evas_Object * /* obj */, void * /* event_info */)
482 {
483     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
484
485     WebView * self = reinterpret_cast<WebView *>(data);
486
487     if(!self->hasFocus())
488         self->ewkViewClicked();
489 }
490
491 void WebView::__newWindowRequest(void *data, Evas_Object *, void *out)
492 {
493     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
494
495     WebView * self = reinterpret_cast<WebView *>(data);
496     BROWSER_LOGD("%s:%d %s self=%p", __FILE__, __LINE__, __func__, self);
497     BROWSER_LOGD("Window creating in tab: %s", self->getTabId().toString().c_str());
498     std::shared_ptr<basic_webengine::AbstractWebEngine<Evas_Object>>  m_webEngine;
499     m_webEngine = std::dynamic_pointer_cast
500     <
501         basic_webengine::AbstractWebEngine<Evas_Object>,tizen_browser::core::AbstractService
502     >
503     (tizen_browser::core::ServiceManager::getInstance().getService("org.tizen.browser.webkitengineservice"));
504     M_ASSERT(m_webEngine);
505
506     /// \todo: Choose newly created tab.
507     TabId id = m_webEngine->addTab(std::string(), &self->getTabId());
508     BROWSER_LOGD("Created tab: %s", id.toString().c_str());
509
510     Evas_Object* tab_ewk_view = m_webEngine->getTabView(id);
511     *static_cast<Evas_Object**>(out) = tab_ewk_view;
512 }
513
514 void WebView::__closeWindowRequest(void *data, Evas_Object *, void *)
515 {
516     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
517     WebView * self = reinterpret_cast<WebView *>(data);
518     std::shared_ptr<AbstractWebEngine<Evas_Object>> m_webEngine =
519                 std::dynamic_pointer_cast
520                 <basic_webengine::AbstractWebEngine<Evas_Object>,tizen_browser::core::AbstractService>
521                 (tizen_browser::core::ServiceManager::getInstance().getService("org.tizen.browser.webkitengineservice"));
522     m_webEngine->closeTab(self->getTabId());
523 }
524
525 void WebView::__loadStarted(void * data, Evas_Object * /* obj */, void * /* event_info */)
526 {
527     WebView * self = reinterpret_cast<WebView *>(data);
528
529     BROWSER_LOGD("%s:%d\n\t %s", __func__, __LINE__, ewk_view_url_get(self->m_ewkView));
530
531     self->m_isLoading = true;
532     self->loadStarted();
533     tizen_browser::services::TabThumbCache* cache = tizen_browser::services::TabThumbCache::getInstance();
534     cache->clearThumb(self->m_tabId);
535 }
536
537 void WebView::__loadStop(void * data, Evas_Object * /* obj */, void * /* event_info */)
538 {
539     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
540
541     WebView * self = reinterpret_cast<WebView *>(data);
542     self->m_isLoading = false;
543
544     self->loadStop();
545 }
546
547 void WebView::__loadFinished(void * data, Evas_Object * /* obj */, void * /* event_info */)
548 {
549     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
550
551     WebView * self = reinterpret_cast<WebView *>(data);
552     self->m_isLoading = false;
553     self->m_loadProgress = 1;
554
555     self->loadFinished();
556     self->loadProgress(self->m_loadProgress);
557     tizen_browser::services::TabThumbCache* cache = tizen_browser::services::TabThumbCache::getInstance();
558     cache->updateThumb(self->m_tabId);
559 }
560
561 void WebView::__loadProgress(void * data, Evas_Object * /* obj */, void * event_info)
562 {
563     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
564
565     WebView * self = reinterpret_cast<WebView *>(data);
566     self->m_loadProgress = *(double *)event_info;
567
568     self->loadProgress(self->m_loadProgress);
569 }
570
571 void WebView::__loadError(void* data, Evas_Object * obj, void* ewkError)
572 {
573     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
574
575     WebView *self = reinterpret_cast<WebView*>(data);
576     Ewk_Error *error = reinterpret_cast<Ewk_Error*>(ewkError);
577     Ewk_Error_Type errorType = ewk_error_type_get(error);
578
579     BROWSER_LOGD("[%s:%d] ewk_error_type: %d ",
580                  __PRETTY_FUNCTION__, __LINE__, errorType);
581
582     BROWSER_LOGD("[%s:%d] emiting signal ", __PRETTY_FUNCTION__, __LINE__);
583     int errorCode = ewk_error_code_get(error);
584     if(errorCode == EWK_ERROR_NETWORK_STATUS_CANCELLED)
585     {
586         BROWSER_LOGD("Stop signal emitted");
587         BROWSER_LOGD("Error description: %s", ewk_error_description_get(error));
588         evas_object_smart_callback_call(obj, "load,stop", nullptr);
589     }
590     else
591     {
592         self->loadError();
593         self->m_loadError=true;
594     }
595 }
596
597 void WebView::__titleChanged(void * data, Evas_Object * obj, void * /* event_info */)
598 {
599     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
600
601     WebView * self = reinterpret_cast<WebView *>(data);
602     self->m_title = fromChar(ewk_view_title_get(obj));
603
604     self->titleChanged(self->m_title);
605 }
606
607 void WebView::__urlChanged(void * data, Evas_Object * /* obj */, void * event_info)
608 {
609     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
610
611     WebView * self = reinterpret_cast<WebView *>(data);
612     BROWSER_LOGD("URL changed for tab: %s", self->getTabId().toString().c_str());
613     self->uriChanged(self->getURI());
614     self->tabIdChecker(self->m_tabId);
615 }
616
617 void WebView::__backForwardListChanged(void * data, Evas_Object * /* obj */, void * /* event_info */)
618 {
619     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
620
621     WebView * self = reinterpret_cast<WebView *>(data);
622     self->backwardEnableChanged(self->isBackEnabled());
623     self->forwardEnableChanged(self->isForwardEnabled());
624 }
625
626 void WebView::__faviconChanged(void* data, Evas_Object*, void*)
627 {
628     if(data)
629     {
630         WebView * self = static_cast<WebView *>(data);
631         Evas_Object * favicon = ewk_context_icon_database_icon_object_add(ewk_view_context_get(self->m_ewkView), ewk_view_url_get(self->m_ewkView),evas_object_evas_get(self->m_ewkView));
632         if (favicon) {
633             BROWSER_LOGD("[%s:%d] Favicon received", __PRETTY_FUNCTION__, __LINE__);
634             self->faviconImage = EflTools::getBrowserImage(favicon);
635             evas_object_unref(favicon);
636             self->favIconChanged(self->faviconImage);
637         }
638     }
639 }
640
641 void WebView::__IMEClosed(void* data, Evas_Object*, void*)
642 {
643     BROWSER_LOGD("%s", __func__);
644     WebView * self = reinterpret_cast<WebView *>(data);
645     self->IMEStateChanged(false);
646 }
647
648 void WebView::__IMEOpened(void* data, Evas_Object*, void*)
649 {
650     BROWSER_LOGD("%s", __func__);
651     WebView * self = reinterpret_cast<WebView *>(data);
652     self->IMEStateChanged(true);
653 }
654
655 std::string WebView::securityOriginToUri(const Ewk_Security_Origin *origin)
656 {
657     std::string protocol = fromChar(ewk_security_origin_protocol_get(origin));
658     std::string uri = fromChar(ewk_security_origin_host_get(origin));
659     std::string url = (boost::format("%1%://%2%") % protocol % uri).str();
660     return url;
661 }
662
663 void WebView::__geolocationPermissionRequest(void * data, Evas_Object * /* obj */, void * event_info)
664 {
665     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
666 #if PLATFORM(TIZEN)
667     WebView * self = reinterpret_cast<WebView *>(data);
668
669     Ewk_Geolocation_Permission_Request *request = reinterpret_cast<Ewk_Geolocation_Permission_Request *>(event_info);
670     if (!request)
671         return;
672
673     // suspend webview
674     ewk_view_suspend(self->m_ewkView);
675
676     std::string url = WebView::securityOriginToUri(ewk_geolocation_permission_request_origin_get(request));
677
678     ///\todo add translations
679     std::string message = (boost::format("%1% Requests your location") % url).str();
680
681     WebConfirmationPtr c = std::make_shared<WebConfirmation>(WebConfirmation::ConfirmationType::Geolocation, self->m_tabId, url, message);
682
683     // store
684     self->m_confirmationGeolocationMap[c] = request;
685
686     self->cofirmationRequest(c);
687 #endif
688 }
689
690 void WebView::__usermediaPermissionRequest(void * data, Evas_Object * /* obj */, void * event_info)
691 {
692     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
693 #if PLATFORM(TIZEN)
694     WebView * self = reinterpret_cast<WebView *>(data);
695
696     Ewk_User_Media_Permission_Request *request = reinterpret_cast<Ewk_User_Media_Permission_Request *>(event_info);
697     if (!request)
698         return;
699
700     // suspend webview
701     ewk_view_suspend(self->m_ewkView);
702
703     ///\todo add translations
704     std::string message = "User media permission request";
705
706     WebConfirmationPtr c = std::make_shared<WebConfirmation>(WebConfirmation::ConfirmationType::UserMedia, self->m_tabId, std::string(), message);
707
708     // store
709     self->m_confirmationUserMediaMap[c] = request;
710
711     self->cofirmationRequest(c);
712 #endif
713 }
714
715 void WebView::__notificationPermissionRequest(void * data, Evas_Object * /* obj */, void * event_info)
716 {
717     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
718 #if PLATFORM(TIZEN)
719     WebView * self = reinterpret_cast<WebView *>(data);
720
721     Ewk_Notification_Permission_Request *request = reinterpret_cast<Ewk_Notification_Permission_Request *>(event_info);
722     if (!request)
723         return;
724
725     // suspend webview
726     ewk_view_suspend(self->m_ewkView);
727
728     ///\todo add translations
729     std::string message = (boost::format("%1% wants to display notifications") % self->getURI()).str();
730
731     WebConfirmationPtr c = std::make_shared<WebConfirmation>(WebConfirmation::ConfirmationType::Notification, self->m_tabId, self->getURI(), message);
732
733     // store
734     self->m_confirmationNotificationMap[c] = request;
735
736     self->cofirmationRequest(c);
737 #endif
738 }
739
740 void WebView::__authenticationRequest(void * data, Evas_Object * /* obj */, void * event_info)
741 {
742     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
743 #if PLATFORM(TIZEN)
744     WebView * self = reinterpret_cast<WebView *>(data);
745
746     Ewk_Auth_Request *request = reinterpret_cast<Ewk_Auth_Request *>(event_info);
747     EINA_SAFETY_ON_NULL_RETURN(request);
748
749     std::string url = self->getURI();
750     std::string message = (boost::format("A username and password are being requested by %1%.") % url).str();
751
752     AuthenticationConfirmationPtr c = std::make_shared<AuthenticationConfirmation>(self->m_tabId, url, message);
753
754     self->m_confirmationAuthenticationMap[c] = request;
755
756     self->cofirmationRequest(c);
757 #endif
758 }
759
760 void WebView::__requestCertificationConfirm(void * data , Evas_Object * /* obj */, void * event_info)
761 {
762     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
763 #if PLATFORM(TIZEN)
764     WebView * self = reinterpret_cast<WebView *>(data);
765
766     Ewk_Certificate_Policy_Decision *request = reinterpret_cast<Ewk_Certificate_Policy_Decision *>(event_info);
767     if (!request)
768         return;
769
770     // suspend webview
771     ewk_view_suspend(self->m_ewkView);
772
773     std::string url = self->getURI();
774
775     ///\todo add translations
776     std::string message = (boost::format("There are problems with the security certificate for this site.<br>%1%") % url).str();
777
778     CertificateConfirmationPtr c = std::make_shared<CertificateConfirmation>(self->m_tabId, url, message);
779
780     c->setResult(tizen_browser::basic_webengine::WebConfirmation::ConfirmationResult::Confirmed);
781
782     // store
783     self->m_confirmationCertificatenMap[c] = request;
784
785     self->cofirmationRequest(c);
786 #endif
787 }
788 #endif
789
790 void WebView::setFocus()
791 {
792     elm_object_focus_set(m_ewkView, EINA_TRUE);
793 }
794
795 void WebView::clearFocus()
796 {
797     elm_object_focus_set(m_ewkView, EINA_FALSE);
798 }
799
800 bool WebView::hasFocus() const
801 {
802     return elm_object_focus_get(m_ewkView) == EINA_TRUE ? true : false;
803 }
804
805 double WebView::getZoomFactor() const
806 {
807     if(EINA_UNLIKELY(m_ewkView == nullptr)){
808         return 1.0;
809     }
810
811 #if defined(USE_EWEBKIT)
812     return ewk_view_page_zoom_get(m_ewkView);
813 #else
814     return 1.0;
815 #endif
816 }
817
818 void WebView::setZoomFactor(double zoomFactor)
819 {
820 #if defined(USE_EWEBKIT)
821     if(m_ewkView){
822         //using zoomFactor = 0 sets zoom "fit to screen"
823
824         if(zoomFactor != getZoomFactor()) 
825             ewk_view_page_zoom_set(m_ewkView, zoomFactor);
826     }
827 #endif
828 }
829
830 void WebView::scrollView(const int& dx, const int& dy)
831 {
832     ewk_view_scroll_by(m_ewkView, dx, dy);
833 }
834
835 const TabId& WebView::getTabId(){
836     return m_tabId;
837 }
838
839
840 std::shared_ptr<BrowserImage> WebView::getFavicon()
841 {
842     BROWSER_LOGD("%s:%d, TabId: %s", __PRETTY_FUNCTION__, __LINE__, m_tabId.toString().c_str());
843
844     Evas_Object * favicon = ewk_context_icon_database_icon_object_add(ewk_view_context_get(m_ewkView), ewk_view_url_get(m_ewkView),evas_object_evas_get(m_ewkView));
845     faviconImage = EflTools::getBrowserImage(favicon);
846     evas_object_unref(favicon);
847
848     if(faviconImage.get())
849         return faviconImage;
850
851     BROWSER_LOGD("[%s:%d] Returned favicon is empty!",  __PRETTY_FUNCTION__, __LINE__);
852     return std::make_shared<BrowserImage>();
853 }
854
855 void WebView::clearPrivateData()
856 {
857     BROWSER_LOGD("Clearing private data");
858 #if defined(USE_EWEBKIT)
859     if (m_ewkView)
860     {
861         Ewk_Context *context = ewk_view_context_get(m_ewkView);
862         if (context)
863         {
864             ewk_context_web_storage_delete_all(context);
865             ewk_cookie_manager_cookies_clear(ewk_context_cookie_manager_get(context));
866         }
867     }
868 #endif
869 }
870
871 void WebView::searchOnWebsite(const std::string & searchString, int flags)
872 {
873     ///\todo: it should be "0" instead of "1024" for unlimited match count but it doesn't work properly in WebKit
874     Eina_Bool result = ewk_view_text_find(m_ewkView, searchString.c_str(), static_cast<Ewk_Find_Options>(flags), 1024);
875     BROWSER_LOGD("Ewk search; word: %s, result: %d", searchString.c_str(), result);
876 }
877
878 void WebView::switchToDesktopMode() {
879     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
880     int res = ewk_view_user_agent_set(m_ewkView, APPLICATION_NAME_FOR_USER_AGENT);
881     m_desktopMode = true;
882 }
883
884 void WebView::switchToMobileMode() {
885     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
886     int res = ewk_view_user_agent_set(m_ewkView, APPLICATION_NAME_FOR_USER_AGENT_MOBILE);
887     m_desktopMode = false;
888 }
889
890 bool WebView::isDesktopMode() const {
891     return m_desktopMode;
892 }
893
894 } /* namespace webkitengine_service */
895 } /* end of basic_webengine */
896 } /* end of tizen_browser */
897