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