Change visibility API.(Request for setting page visibility state)
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / web_view_impl.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "runtime/browser/web_view_impl.h"
19
20 #include <ewk_chromium.h>
21 #include <functional>
22 #include <sstream>
23
24 #include "common/file_utils.h"
25 #include "common/logger.h"
26 #include "common/profiler.h"
27 #include "runtime/browser/native_window.h"
28
29 namespace runtime {
30
31 namespace {
32
33 const char* kKeyNameBack = "back";
34 const char* kKeyNameMenu = "menu";
35 const char* kDefaultEncoding = "UTF-8";
36 const char* kSmartClassUserDataKey = "__SC_USERDATA__";
37
38 static int ToWebRotation(int r) {
39   switch (r) {
40     case 90:
41       return -90;
42     case 270:
43       return 90;
44   }
45   return r;
46 }
47
48 static NativeWindow::ScreenOrientation ToNativeRotation(int r) {
49   if (r ==
50       (EWK_SCREEN_ORIENTATION_PORTRAIT_PRIMARY
51        | EWK_SCREEN_ORIENTATION_PORTRAIT_SECONDARY
52        | EWK_SCREEN_ORIENTATION_LANDSCAPE_PRIMARY
53        | EWK_SCREEN_ORIENTATION_LANDSCAPE_SECONDARY)) {
54     return NativeWindow::ScreenOrientation::ANY;
55   } else if (r ==
56       (EWK_SCREEN_ORIENTATION_PORTRAIT_PRIMARY
57        | EWK_SCREEN_ORIENTATION_LANDSCAPE_PRIMARY)) {
58     return NativeWindow::ScreenOrientation::NATURAL;
59   } else if (r & EWK_SCREEN_ORIENTATION_PORTRAIT_PRIMARY) {
60     return NativeWindow::ScreenOrientation::PORTRAIT_PRIMARY;
61   } else if (r & EWK_SCREEN_ORIENTATION_PORTRAIT_SECONDARY) {
62     return NativeWindow::ScreenOrientation::PORTRAIT_SECONDARY;
63   } else if (r & EWK_SCREEN_ORIENTATION_LANDSCAPE_PRIMARY) {
64     return NativeWindow::ScreenOrientation::LANDSCAPE_PRIMARY;
65   } else {
66     return NativeWindow::ScreenOrientation::LANDSCAPE_SECONDARY;
67   }
68 }
69
70 }  // namespace
71
72 WebViewImpl::WebViewImpl(WebView* view,
73                          NativeWindow* window,
74                          Ewk_Context* context)
75     : window_(window),
76       context_(context),
77       ewk_view_(NULL),
78       listener_(NULL),
79       rotation_handler_id_(0),
80       view_(view),
81       fullscreen_(false),
82       evas_smart_class_(NULL),
83       internal_popup_opened_(false),
84       ime_width_(0),
85       ime_height_(0) {
86   Initialize();
87 }
88
89 WebViewImpl::~WebViewImpl() {
90   if (internal_popup_opened_) {
91     ewk_view_javascript_alert_reply(ewk_view_);
92   }
93   Deinitialize();
94   evas_object_del(ewk_view_);
95   if (evas_smart_class_ != NULL)
96     evas_smart_free(evas_smart_class_);
97 }
98
99 void WebViewImpl::LoadUrl(const std::string& url, const std::string& mime) {
100   SCOPE_PROFILE();
101   if (!mime.empty()) {
102     mime_set_cb_ = [url, mime]
103                    (const char* request_url, const char* request_mime,
104                     char** new_mime, void* data) {
105       WebViewImpl* view = static_cast<WebViewImpl*>(data);
106       if (view != nullptr &&
107           common::utils::BaseName(url) ==
108           common::utils::BaseName(request_url)) {
109         *new_mime = strdup(mime.c_str());
110         LOGGER(DEBUG) << "ewk's new_mime: " << *new_mime;
111         return EINA_TRUE;
112       }
113       return EINA_FALSE;
114     };
115     auto mime_override_cb = [](const char* url, const char* mime,
116                                  char** new_mime, void* data) -> Eina_Bool {
117       WebViewImpl* view = static_cast<WebViewImpl*>(data);
118       return view->mime_set_cb_(url, mime, new_mime, data);
119     };
120     ewk_context_mime_override_callback_set(context_, mime_override_cb, this);
121   }
122   ewk_view_url_set(ewk_view_, url.c_str());
123 }
124
125 void WebViewImpl::Suspend() {
126   // suspend webview
127   ewk_view_suspend(ewk_view_);
128 }
129
130 void WebViewImpl::Resume() {
131   // resume webview
132   ewk_view_resume(ewk_view_);
133 }
134
135 void WebViewImpl::Reload() {
136   ewk_view_reload(ewk_view_);
137 }
138
139 bool WebViewImpl::Backward() {
140   if (ewk_view_back_possible(ewk_view_)) {
141     ewk_view_back(ewk_view_);
142     return EINA_TRUE;
143   }
144   return EINA_FALSE;
145 }
146
147 void WebViewImpl::SetVisibility(bool show) {
148   ewk_view_page_visibility_state_set(ewk_view_,
149                                      show ? EWK_PAGE_VISIBILITY_STATE_VISIBLE :
150                                             EWK_PAGE_VISIBILITY_STATE_HIDDEN,
151                                      EINA_FALSE);
152 }
153
154
155 bool WebViewImpl::EvalJavascript(const std::string& script) {
156   return ewk_view_script_execute(ewk_view_, script.c_str(), NULL, NULL);
157 }
158
159 void WebViewImpl::Initialize() {
160   ewk_smart_class_ = EWK_VIEW_SMART_CLASS_INIT_NAME_VERSION("WebView");
161   ewk_view_smart_class_set(&ewk_smart_class_);
162   ewk_smart_class_.orientation_lock = [](Ewk_View_Smart_Data *sd,
163                                          int orientation) {
164     WebViewImpl* self = static_cast<WebViewImpl*>(
165         evas_object_data_get(sd->self, kSmartClassUserDataKey));
166     if (self == NULL || self->listener_ == NULL)
167       return EINA_FALSE;
168     self->listener_->OnOrientationLock(self->view_,
169                                        true,
170                                        ToNativeRotation(orientation));
171     return EINA_TRUE;
172   };
173
174   ewk_smart_class_.orientation_unlock = [](Ewk_View_Smart_Data *sd) {
175     WebViewImpl* self = static_cast<WebViewImpl*>(
176         evas_object_data_get(sd->self, kSmartClassUserDataKey));
177     if (self == NULL || self->listener_ == NULL)
178       return;
179     self->listener_->OnOrientationLock(
180         self->view_,
181         false,
182         NativeWindow::ScreenOrientation::PORTRAIT_PRIMARY);
183   };
184
185   if (evas_smart_class_ != NULL)
186     evas_smart_free(evas_smart_class_);
187   evas_smart_class_ = evas_smart_class_new(&ewk_smart_class_.sc);
188   if (evas_smart_class_ == NULL) {
189     LOGGER(ERROR) << "Can't create evas smart class";
190     return;
191   }
192
193   Ewk_Page_Group* page_group = ewk_page_group_create("");
194   ewk_view_ = ewk_view_smart_add(evas_object_evas_get(window_->evas_object()),
195                                  evas_smart_class_,
196                                  context_,
197                                  page_group);
198   evas_object_data_set(ewk_view_, kSmartClassUserDataKey, this);
199
200   InitKeyCallback();
201   InitLoaderCallback();
202   InitPolicyDecideCallback();
203   InitQuotaExceededCallback();
204   InitIPCMessageCallback();
205   InitConsoleMessageCallback();
206   InitCustomContextMenuCallback();
207   InitRotationCallback();
208   InitWindowCreateCallback();
209   InitFullscreenCallback();
210   InitNotificationPermissionCallback();
211   InitGeolocationPermissionCallback();
212   InitAuthenticationCallback();
213   InitCertificateAllowCallback();
214   InitPopupWaitCallback();
215   InitUsermediaCallback();
216   InitEditorClientImeCallback();
217 #ifdef ROTARY_EVENT_FEATURE_SUPPORT
218   InitRotaryEventCallback();
219 #endif  // ROTARY_EVENT_FEATURE_SUPPORT
220
221   Ewk_Settings* settings = ewk_view_settings_get(ewk_view_);
222   ewk_settings_scripts_can_open_windows_set(settings, EINA_TRUE);
223   ewk_settings_default_text_encoding_name_set(settings, kDefaultEncoding);
224
225   // TODO(sngn.lee): "protocolhandler,registration,requested"
226   //                  custom protocol handler
227
228   // Show webview
229   evas_object_show(ewk_view_);
230 }
231
232 void WebViewImpl::Deinitialize() {
233   auto it = smart_callbacks_.begin();
234   for ( ; it != smart_callbacks_.end(); ++it) {
235     evas_object_smart_callback_del(
236         ewk_view_,
237         it->first.c_str(),
238         it->second);
239   }
240   eext_object_event_callback_del(ewk_view_,
241                                EEXT_CALLBACK_BACK,
242                                smart_callbacks_["key_callback"]);
243   ewk_view_exceeded_database_quota_callback_set(
244       ewk_view_,
245       NULL,
246       NULL);
247   ewk_view_exceeded_indexed_database_quota_callback_set(
248       ewk_view_,
249       NULL,
250       NULL);
251   ewk_view_exceeded_local_file_system_quota_callback_set(
252       ewk_view_,
253       NULL,
254       NULL);
255   ewk_view_notification_permission_callback_set(
256       ewk_view_,
257       NULL,
258       NULL);
259   ewk_view_geolocation_permission_callback_set(
260       ewk_view_,
261       NULL,
262       NULL);
263   ewk_view_user_media_permission_callback_set(
264       ewk_view_,
265       NULL,
266       NULL);
267   window_->RemoveRotationHandler(rotation_handler_id_);
268 }
269
270 void WebViewImpl::InitKeyCallback() {
271   auto key_callback = [](void* user_data,
272                          Evas_Object* /*obj*/,
273                          void* event_info) -> void {
274     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
275     Eext_Callback_Type key = static_cast<Eext_Callback_Type>(
276       reinterpret_cast<long long>(event_info));  // NOLINT
277     self->OnKeyEvent(key);
278   };
279   eext_object_event_callback_add(ewk_view_,
280                                EEXT_CALLBACK_BACK,
281                                key_callback,
282                                this);
283   eext_object_event_callback_add(ewk_view_,
284                                EEXT_CALLBACK_MORE,
285                                key_callback,
286                                this);
287   smart_callbacks_["key_callback"] = key_callback;
288 }
289
290 void WebViewImpl::InitLoaderCallback() {
291   // load statred callback
292   auto loadstart_callback = [](void* user_data,
293                                Evas_Object* /*obj*/,
294                                void*) {
295     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
296     if (self->listener_)
297       self->listener_->OnLoadStart(self->view_);
298   };
299   evas_object_smart_callback_add(ewk_view_,
300                                  "load,started",
301                                  loadstart_callback,
302                                  this);
303   // load finished callback
304   auto loadfinished_callback = [](void* user_data,
305                                   Evas_Object*,
306                                   void*) {
307     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
308     if (self->listener_)
309       self->listener_->OnLoadFinished(self->view_);
310   };
311   evas_object_smart_callback_add(ewk_view_,
312                                  "load,finished",
313                                  loadfinished_callback,
314                                  this);
315
316   // load progress callback
317   auto loadprogress_callback = [](void* user_data,
318                                   Evas_Object*,
319                                   void* event_info) {
320     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
321     double* progress = static_cast<double*>(event_info);
322     if (self->listener_)
323       self->listener_->OnLoadProgress(self->view_, *progress);
324   };
325   evas_object_smart_callback_add(ewk_view_,
326                                  "load,progress",
327                                  loadprogress_callback,
328                                  this);
329   // rendered callback
330   auto rendered_callback = [](void* user_data,
331                               Evas_Object*,
332                               void*) {
333     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
334     if (self->listener_)
335       self->listener_->OnRendered(self->view_);
336   };
337   evas_object_smart_callback_add(ewk_view_,
338                                  "frame,rendered",
339                                  rendered_callback,
340                                  this);
341   smart_callbacks_["load,started"] = loadstart_callback;
342   smart_callbacks_["load,finished"] = loadfinished_callback;
343   smart_callbacks_["load,progress"] = loadprogress_callback;
344   smart_callbacks_["frame,rendered"] = rendered_callback;
345 }
346
347 void WebViewImpl::InitPolicyDecideCallback() {
348   // "policy,navigation,decide"
349   auto navigation_decide_callback = [](void* user_data,
350                                        Evas_Object*,
351                                        void* event_info) {
352     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
353     Ewk_Policy_Decision* policy =
354             static_cast<Ewk_Policy_Decision*>(event_info);
355     const char* url = ewk_policy_decision_url_get(policy);
356
357     if (self->listener_) {
358       if (self->listener_->OnDidNavigation(self->view_, url))
359         ewk_policy_decision_use(policy);
360       else
361         ewk_policy_decision_ignore(policy);
362     } else {
363       ewk_policy_decision_use(policy);
364     }
365   };
366   evas_object_smart_callback_add(ewk_view_,
367                                  "policy,navigation,decide",
368                                  navigation_decide_callback,
369                                  this);
370
371   // policy,newwindow,decide
372   auto newwindow_decide_callback = [](void* user_data,
373                                       Evas_Object*,
374                                       void* event_info) {
375     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
376     Ewk_Policy_Decision* policy =
377             static_cast<Ewk_Policy_Decision*>(event_info);
378
379     const char* url = ewk_policy_decision_url_get(policy);
380
381     if (self->listener_) {
382       if (self->listener_->OnDidNavigation(self->view_, url) &&
383          self->listener_->OnDidOpenWindow(self->view_, url)) {
384          ewk_policy_decision_use(policy);
385       } else {
386         ewk_policy_decision_ignore(policy);
387       }
388     } else {
389       ewk_policy_decision_use(policy);
390     }
391   };
392   evas_object_smart_callback_add(ewk_view_,
393                                  "policy,newwindow,decide",
394                                  newwindow_decide_callback,
395                                  this);
396   smart_callbacks_["policy,navigation,decide"] = navigation_decide_callback;
397   smart_callbacks_["policy,newwindow,decide"] = newwindow_decide_callback;
398 }
399
400 void WebViewImpl::InitQuotaExceededCallback() {
401   // TODO(sngn.lee): Need callback interface - OnQutaExceed
402   // check http://tizen.org/privilege/unlimitedstorage
403
404   // callback for database quota exceeded
405   auto database_exceeded_callback = [](Evas_Object* view,
406                                        Ewk_Security_Origin* origin,
407                                        const char*,
408                                        unsigned long long, // NOLINT
409                                        void* user_data) -> Eina_Bool {
410     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
411     if (self == NULL || self->listener_ == NULL)
412       return EINA_TRUE;
413
414     auto result_handler = [view](bool result) {
415       LOGGER(DEBUG) << "database quota Permission Result : " << result;
416       ewk_view_exceeded_database_quota_reply(view, result);
417     };
418     std::stringstream url;
419     url << ewk_security_origin_protocol_get(origin)
420         << "://"
421         << ewk_security_origin_host_get(origin)
422         << ":"
423         << ewk_security_origin_port_get(origin);
424     self->listener_->OnQuotaExceed(
425         self->view_,
426         url.str(),
427         result_handler);
428     return EINA_TRUE;
429   };
430   ewk_view_exceeded_database_quota_callback_set(
431     ewk_view_,
432     database_exceeded_callback,
433     this);
434
435   // callback for indexed database quota exceeded
436   auto indexed_db_exceeded_callback = [](Evas_Object* view,
437                                        Ewk_Security_Origin* origin,
438                                        long long, // NOLINT
439                                        void* user_data) -> Eina_Bool {
440     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
441     if (self == NULL || self->listener_ == NULL)
442       return EINA_TRUE;
443
444     auto result_handler = [view](bool result) {
445       LOGGER(DEBUG) << "indexed db quota Permission Result : " << result;
446       ewk_view_exceeded_indexed_database_quota_reply(view, result);
447     };
448     std::stringstream url;
449     url << ewk_security_origin_protocol_get(origin)
450         << "://"
451         << ewk_security_origin_host_get(origin)
452         << ":"
453         << ewk_security_origin_port_get(origin);
454     self->listener_->OnQuotaExceed(
455         self->view_,
456         url.str(),
457         result_handler);
458     return EINA_TRUE;
459   };
460   ewk_view_exceeded_indexed_database_quota_callback_set(
461     ewk_view_,
462     indexed_db_exceeded_callback,
463     this);
464
465   // callback for localfile quota exceeded
466   auto localfile_exceeded_callback = [](Evas_Object* view,
467                                        Ewk_Security_Origin* origin,
468                                        long long, // NOLINT
469                                        void* user_data) -> Eina_Bool {
470     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
471     if (self == NULL || self->listener_ == NULL)
472       return EINA_TRUE;
473
474     auto result_handler = [view](bool result) {
475       LOGGER(DEBUG) << "local file quota Permission Result : " << result;
476       ewk_view_exceeded_local_file_system_quota_reply(view, result);
477     };
478     std::stringstream url;
479     url << ewk_security_origin_protocol_get(origin)
480         << "://"
481         << ewk_security_origin_host_get(origin)
482         << ":"
483         << ewk_security_origin_port_get(origin);
484     self->listener_->OnQuotaExceed(
485         self->view_,
486         url.str(),
487         result_handler);
488     return EINA_TRUE;
489   };
490   ewk_view_exceeded_local_file_system_quota_callback_set(
491     ewk_view_,
492     localfile_exceeded_callback,
493     this);
494 }
495
496 void WebViewImpl::InitIPCMessageCallback() {
497   // wrt,message
498   auto wrt_message_callback = [](void* user_data,
499                                  Evas_Object*,
500                                  void* event_info) {
501     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
502     Ewk_IPC_Wrt_Message_Data* msg =
503         static_cast<Ewk_IPC_Wrt_Message_Data*>(event_info);
504     if (self->listener_)
505       self->listener_->OnReceivedWrtMessage(self->view_, msg);
506   };
507   evas_object_smart_callback_add(ewk_view_,
508                                  "wrt,message",
509                                  wrt_message_callback,
510                                  this);
511   smart_callbacks_["wrt,message"] = wrt_message_callback;
512 }
513
514 void WebViewImpl::InitConsoleMessageCallback() {
515   // console log
516   auto console_message_callback = [](void* user_data,
517                                  Evas_Object*,
518                                  void* event_info) {
519     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
520     if (!self->listener_) {
521       return;
522     }
523     Ewk_Console_Message* msg = static_cast<Ewk_Console_Message*>(event_info);
524     unsigned int line_number = ewk_console_message_line_get(msg);
525
526     std::stringstream buf;
527     if (line_number) {
528         buf << common::utils::BaseName(ewk_console_message_source_get(msg))
529             << ":" << line_number << ": ";
530     }
531     buf << ewk_console_message_text_get(msg);
532     int level = ewk_console_message_level_get(msg);
533     self->listener_->OnConsoleMessage(buf.str(), level);
534   };
535   evas_object_smart_callback_add(ewk_view_,
536                                  "console,message",
537                                  console_message_callback,
538                                  this);
539   smart_callbacks_["console,message"] = console_message_callback;
540 }
541
542 void WebViewImpl::InitCustomContextMenuCallback() {
543   auto custom_context_menu_callback = [](void* user_data,
544                                          Evas_Object*,
545                                          void* event_info) {
546     Ewk_Context_Menu* contextmenu = static_cast<Ewk_Context_Menu*>(event_info);
547     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
548     bool disabled = false;
549     if (self->listener_ &&
550         self->listener_->OnContextMenuDisabled(self->view_)) {
551       disabled = true;
552     }
553     int cnt = ewk_context_menu_item_count(contextmenu);
554     for (int idx = cnt-1; idx >= 0; --idx) {
555       auto* item = ewk_context_menu_nth_item_get(contextmenu, idx);
556       Ewk_Context_Menu_Item_Tag tag = ewk_context_menu_item_tag_get(item);
557       switch (tag) {
558         case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_IMAGE_IN_NEW_WINDOW:
559         case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_LINK_IN_NEW_WINDOW:
560         case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_FRAME_IN_NEW_WINDOW:
561         case EWK_CONTEXT_MENU_ITEM_TAG_SEARCH_WEB:
562         case EWK_CONTEXT_MENU_ITEM_TAG_DOWNLOAD_IMAGE_TO_DISK:
563           ewk_context_menu_item_remove(contextmenu, item);
564           break;
565         default:
566           if (disabled)
567             ewk_context_menu_item_remove(contextmenu, item);
568       }
569     }
570   };
571   evas_object_smart_callback_add(ewk_view_,
572                                  "contextmenu,customize",
573                                  custom_context_menu_callback,
574                                  this);
575   smart_callbacks_["contextmenu,customize"] = custom_context_menu_callback;
576 }
577
578 void WebViewImpl::InitRotationCallback() {
579   // rotation support
580   ewk_view_orientation_send(ewk_view_, ToWebRotation(window_->rotation()));
581   rotation_handler_id_ = window_->AddRotationHandler(
582                                   std::bind(&WebViewImpl::OnRotation,
583                                   this,
584                                   std::placeholders::_1));
585 }
586
587 void WebViewImpl::InitWindowCreateCallback() {
588   auto create_callback = [](void* user_data,
589                             Evas_Object*,
590                             void* event_info) {
591     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
592     if (!self->listener_) {
593       return;
594     }
595     WebView* new_view = new WebView(self->window_, self->context_);
596     self->listener_->OnCreatedNewWebView(self->view_, new_view);
597     *(static_cast<Evas_Object **>(event_info)) = new_view->evas_object();
598   };
599
600   auto close_callback = [](void* user_data,
601                             Evas_Object*,
602                             void*) {
603     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
604     if (!self->listener_) {
605       return;
606     }
607     self->listener_->OnClosedWebView(self->view_);
608   };
609   evas_object_smart_callback_add(ewk_view_,
610                                  "create,window",
611                                  create_callback,
612                                  this);
613   evas_object_smart_callback_add(ewk_view_,
614                                  "close,window",
615                                  close_callback,
616                                  this);
617
618   smart_callbacks_["create,window"] = create_callback;
619   smart_callbacks_["close,window"] = close_callback;
620 }
621
622 void WebViewImpl::InitFullscreenCallback() {
623   auto enter_callback = [](void* user_data,
624                             Evas_Object*,
625                             void* /*event_info*/) {
626     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
627     self->fullscreen_ = true;
628     self->window_->FullScreen(true);
629   };
630   auto exit_callback =  [](void* user_data,
631                             Evas_Object*,
632                             void* /*event_info*/) {
633     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
634     self->fullscreen_ = false;
635     self->window_->FullScreen(false);
636   };
637   evas_object_smart_callback_add(ewk_view_,
638                                  "fullscreen,enterfullscreen",
639                                  enter_callback,
640                                  this);
641   evas_object_smart_callback_add(ewk_view_,
642                                  "fullscreen,exitfullscreen",
643                                  exit_callback,
644                                  this);
645   smart_callbacks_["fullscreen,enterfullscreen"] = enter_callback;
646   smart_callbacks_["fullscreen,exitfullscreen"] = exit_callback;
647 }
648
649 void WebViewImpl::InitNotificationPermissionCallback() {
650   auto request_callback = [](Evas_Object*,
651                              Ewk_Notification_Permission_Request* request,
652                              void* user_data) {
653     LOGGER(DEBUG) << "Notification Permission Request";
654     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
655     if (!self->listener_) {
656       ewk_notification_permission_reply(request, EINA_FALSE);
657       return EINA_TRUE;
658     }
659
660     ewk_notification_permission_request_suspend(request);
661     auto result_handler = [request](bool result) {
662       LOGGER(DEBUG) << "Notification Permission Result : %d" << result;
663       ewk_notification_permission_reply(request, result);
664     };
665     const Ewk_Security_Origin* ewk_origin =
666         ewk_notification_permission_request_origin_get(request);
667
668     std::stringstream url;
669     url << ewk_security_origin_protocol_get(ewk_origin)
670         << "://"
671         << ewk_security_origin_host_get(ewk_origin)
672         << ":"
673         << ewk_security_origin_port_get(ewk_origin);
674     self->listener_->OnNotificationPermissionRequest(
675         self->view_,
676         url.str(),
677         result_handler);
678     return EINA_TRUE;
679   };
680   ewk_view_notification_permission_callback_set(ewk_view_,
681                                                 request_callback,
682                                                 this);
683 }
684
685 void WebViewImpl::InitGeolocationPermissionCallback() {
686   auto permission_callback = [](
687       Evas_Object*,
688       Ewk_Geolocation_Permission_Request* request,
689       void* user_data) {
690     LOGGER(DEBUG) << "Geolocation Permission Request";
691     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
692     if (self == NULL || self->listener_ == NULL) {
693       ewk_geolocation_permission_reply(request, EINA_FALSE);
694       return EINA_TRUE;
695     }
696     ewk_geolocation_permission_request_suspend(request);
697
698     const Ewk_Security_Origin* ewk_origin =
699         ewk_geolocation_permission_request_origin_get(request);
700     auto result_handler = [request](bool result) {
701       LOGGER(DEBUG) << "Geolocation Permission Result : " << result;
702       ewk_geolocation_permission_reply(request, result);
703     };
704
705     std::stringstream url;
706     url << ewk_security_origin_protocol_get(ewk_origin)
707         << "://"
708         << ewk_security_origin_host_get(ewk_origin)
709         << ":"
710         << ewk_security_origin_port_get(ewk_origin);
711
712     self->listener_->OnGeolocationPermissionRequest(
713         self->view_,
714         url.str(),
715         result_handler);
716     return EINA_TRUE;
717   };
718   ewk_view_geolocation_permission_callback_set(ewk_view_,
719                                                permission_callback,
720                                                this);
721 }
722
723 void WebViewImpl::InitAuthenticationCallback() {
724   auto auth_callback = [](void* user_data,
725                           Evas_Object*,
726                           void* event_info) {
727     LOGGER(DEBUG) << "Authentication Request";
728     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
729     Ewk_Auth_Challenge* auth_challenge =
730         static_cast<Ewk_Auth_Challenge*>(event_info);
731
732     if (self == NULL || self->listener_ == NULL) {
733       ewk_auth_challenge_credential_cancel(auth_challenge);
734       return;
735     }
736     auto result_handler = [auth_challenge](bool submit,
737                                     const std::string& id,
738                                     const std::string& password) {
739       LOGGER(DEBUG) << "Authentication Result : submit = " << submit;
740       if (!submit) {
741         ewk_auth_challenge_credential_cancel(auth_challenge);
742         return;
743       }
744       ewk_auth_challenge_credential_use(auth_challenge,
745                                         id.c_str(),
746                                         password.c_str());
747     };
748     ewk_auth_challenge_suspend(auth_challenge);
749     const char* message =
750         ewk_auth_challenge_realm_get(auth_challenge);
751     std::string url = self->GetUrl();
752     self->listener_->OnAuthenticationRequest(self->view_,
753                                              url,
754                                              message,
755                                              result_handler);
756   };
757   // "authentication,challenge"
758   evas_object_smart_callback_add(ewk_view_,
759                                  "authentication,challenge",
760                                  auth_callback,
761                                  this);
762   smart_callbacks_["authentication,challenge"] = auth_callback;
763 }
764
765 void WebViewImpl::InitCertificateAllowCallback() {
766   auto certi_callback = [](void* user_data,
767                            Evas_Object*,
768                            void* event_info) {
769     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
770     Ewk_Certificate_Policy_Decision* policy =
771       static_cast<Ewk_Certificate_Policy_Decision*>(event_info);
772
773     if (self == NULL || self->listener_ == NULL) {
774       ewk_certificate_policy_decision_allowed_set(policy, EINA_FALSE);
775       return;
776     }
777
778     ewk_certificate_policy_decision_suspend(policy);
779     auto result_handler = [policy](bool allow) {
780       ewk_certificate_policy_decision_allowed_set(policy, allow);
781     };
782
783     auto ptr = ewk_certificate_policy_decision_url_get(policy);
784     std::string url(ptr ? ptr : "");
785     ptr = ewk_certificate_policy_decision_certificate_pem_get(policy);
786     std::string pem(ptr ? ptr : "");
787     self->listener_->OnCertificateAllowRequest(self->view_,
788                                                url,
789                                                pem,
790                                                result_handler);
791   };
792   evas_object_smart_callback_add(ewk_view_,
793                                  "request,certificate,confirm",
794                                  certi_callback,
795                                  this);
796   smart_callbacks_["request,certificate,confirm"] = certi_callback;
797 }
798
799 void WebViewImpl::InitPopupWaitCallback() {
800   evas_object_smart_callback_add(ewk_view_,
801       "popup,reply,wait,start",
802       [](void* user_data, Evas_Object* /*obj*/, void*) {
803         WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
804         self->internal_popup_opened_ = true;
805       }, this);
806   evas_object_smart_callback_add(ewk_view_,
807       "popup,reply,wait,finish",
808       [](void* user_data, Evas_Object* /*obj*/, void*) {
809         WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
810         self->internal_popup_opened_ = false;
811       }, this);
812 }
813
814 void WebViewImpl::InitUsermediaCallback() {
815   auto callback = [](Evas_Object*,
816                      Ewk_User_Media_Permission_Request* request,
817                      void* user_data) {
818     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
819     if (self == NULL || self->listener_ == NULL) {
820       ewk_user_media_permission_reply(request, EINA_FALSE);
821       return EINA_TRUE;
822     }
823
824     ewk_user_media_permission_request_suspend(request);
825     const Ewk_Security_Origin* origin =
826         ewk_user_media_permission_request_origin_get(request);
827     std::stringstream url;
828     url << ewk_security_origin_protocol_get(origin)
829         << "://"
830         << ewk_security_origin_host_get(origin)
831         << ":"
832         << ewk_security_origin_port_get(origin);
833
834     auto result_handler = [request](bool result) {
835       LOGGER(DEBUG) << "Getusermedia Permission Result : " << result;
836       ewk_user_media_permission_reply(request, result);
837     };
838     self->listener_->OnUsermediaPermissionRequest(self->view_,
839                                                   url.str(),
840                                                   result_handler);
841     return EINA_TRUE;
842   };
843   ewk_view_user_media_permission_callback_set(ewk_view_, callback, this);
844 }
845
846 void WebViewImpl::InitEditorClientImeCallback() {
847   auto ime_changed_callback = [](void* user_data,
848                            Evas_Object*,
849                            void* event_info) {
850     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
851
852     Eina_Rectangle *rect = static_cast<Eina_Rectangle *>(event_info);
853     self->ime_width_ = rect->w;
854     self->ime_height_ = rect->h;
855   };
856
857   auto ime_opened_callback = [](void* user_data,
858                            Evas_Object*,
859                            void* event_info) {
860     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
861
862     SoftKeyboardChangeEventValue softkeyboard_value;
863     softkeyboard_value.state = "on";
864     softkeyboard_value.width = self->ime_width_;
865     softkeyboard_value.height = self->ime_height_;
866
867     self->listener_->OnSoftKeyboardChangeEvent(self->view_, softkeyboard_value);
868   };
869
870   auto ime_closed_callback = [](void* user_data,
871                            Evas_Object*,
872                            void* event_info) {
873     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
874
875     SoftKeyboardChangeEventValue softkeyboard_value;
876     softkeyboard_value.state = "off";
877
878     self->listener_->OnSoftKeyboardChangeEvent(self->view_, softkeyboard_value);
879   };
880   evas_object_smart_callback_add(ewk_view_,
881                                  "inputmethod,changed",
882                                  ime_changed_callback,
883                                  this);
884   evas_object_smart_callback_add(ewk_view_,
885                                  "editorclient,ime,opened",
886                                  ime_opened_callback,
887                                  this);
888   evas_object_smart_callback_add(ewk_view_,
889                                  "editorclient,ime,closed",
890                                  ime_closed_callback,
891                                  this);
892   smart_callbacks_["inputmethod,changed"] = ime_changed_callback;
893   smart_callbacks_["editorclient,ime,opened"] = ime_opened_callback;
894   smart_callbacks_["editorclient,ime,closed"] = ime_closed_callback;
895 }
896
897 #ifdef ROTARY_EVENT_FEATURE_SUPPORT
898 void WebViewImpl::InitRotaryEventCallback() {
899   auto rotary_callback = [](void* user_data,
900                          Evas_Object*,
901                          Eext_Rotary_Event_Info* event_info) -> Eina_Bool {
902     WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
903     Eext_Rotary_Event_Info* rotary = event_info;
904
905     RotaryEventType type;
906     if (rotary->direction == EEXT_ROTARY_DIRECTION_CLOCKWISE)
907       type = RotaryEventType::CLOCKWISE;
908     else
909       type = RotaryEventType::COUNTER_CLOCKWISE;
910
911     self->listener_->OnRotaryEvent(self->view_, type);
912     return EINA_TRUE;
913   };
914
915   // add callback to handle rotary event
916   eext_rotary_object_event_callback_add(ewk_view_, rotary_callback, this);
917   eext_rotary_object_event_activated_set(ewk_view_, EINA_TRUE);
918 }
919 #endif  // ROTARY_EVENT_FEATURE_SUPPORT
920
921 std::string WebViewImpl::GetUrl() {
922   return std::string(ewk_view_url_get(ewk_view_));
923 }
924
925 Evas_Object* WebViewImpl::evas_object() const {
926   return ewk_view_;
927 }
928
929 void WebViewImpl::OnRotation(int degree) {
930   ewk_view_orientation_send(ewk_view_, ToWebRotation(degree));
931 }
932
933 void WebViewImpl::OnKeyEvent(Eext_Callback_Type key_type) {
934   std::string keyname;
935   if (key_type == EEXT_CALLBACK_BACK) {
936     if (fullscreen_) {
937       ewk_view_fullscreen_exit(ewk_view_);
938       return;
939     }
940     if (EINA_TRUE == ewk_view_text_selection_clear(ewk_view_)) {
941       return;
942     }
943     keyname = kKeyNameBack;
944   } else if (key_type == EEXT_CALLBACK_MORE) {
945     keyname = kKeyNameMenu;
946   } else {
947     return;
948   }
949
950   if (listener_) {
951     listener_->OnHardwareKey(view_, keyname);
952   }
953 }
954
955 void WebViewImpl::SetEventListener(WebView::EventListener* listener) {
956   listener_ = listener;
957 }
958
959 void WebViewImpl::SetAppInfo(const std::string& app_name,
960                              const std::string& version) {
961   std::string ua = app_name + "/" + version;
962   ewk_view_application_name_for_user_agent_set(ewk_view_, ua.c_str());
963 }
964 bool WebViewImpl::SetUserAgent(const std::string& user_agent) {
965   return ewk_view_user_agent_set(ewk_view_, user_agent.c_str());
966 }
967
968 void WebViewImpl::SetCSPRule(const std::string& rule, bool report_only) {
969   ewk_view_content_security_policy_set(
970       ewk_view_,
971       rule.c_str(),
972       report_only ? EWK_REPORT_ONLY : EWK_ENFORCE_POLICY);
973 }
974
975 void WebViewImpl::SetDefaultEncoding(const std::string& encoding) {
976   if (ewk_settings_is_encoding_valid(encoding.c_str())) {
977     Ewk_Settings* settings = ewk_view_settings_get(ewk_view_);
978     ewk_settings_default_text_encoding_name_set(settings, encoding.c_str());
979   }
980 }
981
982 }  // namespace runtime