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