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