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