1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "runtime/web_view.h"
7 #include <ewk_chromium.h>
10 #include "runtime/native_window.h"
16 // TODO(sngn.lee) : It should be declare in common header
17 const char* kKeyNameBack = "back";
18 const char* kKeyNameMenu = "menu";
20 static int ToWebRotation(int r) {
30 static int ToNativeRotation(int r) {
43 WebView::WebView(NativeWindow* window, Ewk_Context* context)
52 window_->RemoveRotationHandler(rotation_handler_id_);
53 evas_object_del(ewk_view_);
56 void WebView::LoadUrl(const std::string& url) {
57 ewk_view_url_set(ewk_view_, url.c_str());
60 void WebView::Suspend() {
62 ewk_view_suspend(ewk_view_);
65 void WebView::Resume() {
67 ewk_view_resume(ewk_view_);
70 void WebView::Reload() {
71 ewk_view_reload(ewk_view_);
74 void WebView::SetVisibility(bool show) {
75 ewk_view_visibility_set(ewk_view_, show ? EINA_TRUE : EINA_FALSE);
79 bool WebView::EvalJavascript(const std::string& script) {
80 return ewk_view_script_execute(ewk_view_, script.c_str(), NULL, NULL);
83 void WebView::Initialize() {
84 ewk_view_ = ewk_view_add_with_context(window_->evas_object(), context_);
86 // TODO(sngn.lee): To be implemented - orientation lock
89 auto key_callback = [](void* user_data,
91 void* event_info) -> void {
92 WebView* self = static_cast<WebView*>(user_data);
93 Ea_Callback_Type key = static_cast<Ea_Callback_Type>(
94 reinterpret_cast<int>(event_info));
95 self->OnKeyEvent(key);
97 ea_object_event_callback_add(ewk_view_,
101 ea_object_event_callback_add(ewk_view_,
107 // load statred callback
108 auto loadstart_callback = [](void* user_data,
109 Evas_Object* /*obj*/,
111 WebView* self = static_cast<WebView*>(user_data);
113 self->listener_->OnLoadStart(self);
115 evas_object_smart_callback_add(ewk_view_,
119 // load finished callback
120 auto loadfinished_callback = [](void* user_data,
123 WebView* self = static_cast<WebView*>(user_data);
125 self->listener_->OnLoadFinished(self);
127 evas_object_smart_callback_add(ewk_view_,
129 loadfinished_callback,
132 // load progress callback
133 auto loadprogress_callback = [](void* user_data,
136 WebView* self = static_cast<WebView*>(user_data);
137 double* progress = static_cast<double*>(event_info);
139 self->listener_->OnLoadProgress(self, *progress);
141 evas_object_smart_callback_add(ewk_view_,
143 loadprogress_callback,
146 auto rendered_callback = [](void* user_data,
149 WebView* self = static_cast<WebView*>(user_data);
151 self->listener_->OnRendered(self);
153 evas_object_smart_callback_add(ewk_view_,
158 // "policy,navigation,decide"
159 auto navigation_decide_callback = [](void* user_data,
162 WebView* self = static_cast<WebView*>(user_data);
163 Ewk_Policy_Decision* policy =
164 static_cast<Ewk_Policy_Decision*>(event_info);
165 const char* url = ewk_policy_decision_url_get(policy);
167 if (self->listener_) {
168 if (self->listener_->OnDidNavigation(self, url))
169 ewk_policy_decision_use(policy);
171 ewk_policy_decision_ignore(policy);
173 ewk_policy_decision_use(policy);
176 evas_object_smart_callback_add(ewk_view_,
177 "policy,navigation,decide",
178 navigation_decide_callback,
181 // policy,newwindow,decide
182 auto newwindow_decide_callback = [](void* user_data,
185 WebView* self = static_cast<WebView*>(user_data);
186 Ewk_Policy_Decision* policy =
187 static_cast<Ewk_Policy_Decision*>(event_info);
189 const char* url = ewk_policy_decision_url_get(policy);
191 if (self->listener_) {
192 if (self->listener_->OnDidNavigation(self, url) &&
193 self->listener_->OnDidOpenWindow(self, url)) {
194 ewk_policy_decision_use(policy);
196 ewk_policy_decision_ignore(policy);
199 ewk_policy_decision_use(policy);
202 evas_object_smart_callback_add(ewk_view_,
203 "policy,newwindow,decide",
204 newwindow_decide_callback,
207 // callback for database quota exceeded
208 auto database_exceeded_callback = [](Evas_Object* view,
209 Ewk_Security_Origin* origin,
212 void*) -> Eina_Bool {
213 std::string protocol(ewk_security_origin_protocol_get(origin));
214 if (protocol == "file://" || protocol == "app://") {
215 // Allow for local origin
216 ewk_view_exceeded_database_quota_reply(view, EINA_TRUE);
218 // Deny for remote origin
219 ewk_view_exceeded_database_quota_reply(view, EINA_FALSE);
223 ewk_view_exceeded_database_quota_callback_set(
225 database_exceeded_callback,
228 // callback for indexed database quota exceeded
229 auto indexed_db_exceeded_callback = [](Evas_Object* view,
230 Ewk_Security_Origin* origin,
232 void*) -> Eina_Bool {
233 std::string protocol(ewk_security_origin_protocol_get(origin));
234 if (protocol == "file://" || protocol == "app://") {
235 // Allow for local origin
236 ewk_view_exceeded_indexed_database_quota_reply(view, EINA_TRUE);
238 // Deny for remote origin
239 ewk_view_exceeded_indexed_database_quota_reply(view, EINA_FALSE);
243 ewk_view_exceeded_indexed_database_quota_callback_set(
245 indexed_db_exceeded_callback,
248 // callback for localfile quota exceeded
249 auto localfile_exceeded_callback = [](Evas_Object* view,
250 Ewk_Security_Origin* origin,
252 void*) -> Eina_Bool {
253 std::string protocol(ewk_security_origin_protocol_get(origin));
254 if (protocol == "file://" || protocol == "app://") {
255 // Allow for local origin
256 ewk_view_exceeded_local_file_system_quota_reply(view, EINA_TRUE);
258 // Deny for remote origin
259 ewk_view_exceeded_local_file_system_quota_reply(view, EINA_FALSE);
263 ewk_view_exceeded_local_file_system_quota_callback_set(
265 localfile_exceeded_callback,
269 auto wrt_message_callback = [](void* user_data,
272 WebView* self = static_cast<WebView*>(user_data);
273 Ewk_IPC_Wrt_Message_Data* msg =
274 static_cast<Ewk_IPC_Wrt_Message_Data*>(event_info);
276 self->listener_->OnReceivedWrtMessage(self, msg);
278 evas_object_smart_callback_add(ewk_view_,
280 wrt_message_callback,
283 // Orientation lock callback
284 auto orientation_lock_callback = [](Evas_Object* o,
287 void* user_data) -> Eina_Bool {
288 WebView* self = static_cast<WebView*>(user_data);
289 if (self->listener_) {
290 self->listener_->OnOrientationLock(self,
292 ToNativeRotation(orientation));
296 ewk_view_orientation_lock_callback_set(ewk_view_,
297 orientation_lock_callback,
301 ewk_view_orientation_send(ewk_view_, ToWebRotation(window_->rotation()));
302 rotation_handler_id_ = window_->AddRotationHandler(
303 std::bind(&WebView::OnRotation,
305 std::placeholders::_1));
307 evas_object_show(ewk_view_);
310 std::string WebView::GetUrl() {
311 return std::string(ewk_view_url_get(ewk_view_));
314 Evas_Object* WebView::evas_object() const {
318 void WebView::OnRotation(int degree) {
319 ewk_view_orientation_send(ewk_view_, ToWebRotation(degree));
322 void WebView::OnKeyEvent(Ea_Callback_Type key_type) {
324 if (key_type == EA_CALLBACK_BACK) {
325 if (EINA_TRUE == ewk_view_text_selection_clear(ewk_view_)) {
328 keyname = kKeyNameBack;
329 } else if (key_type == EA_CALLBACK_MORE) {
330 keyname = kKeyNameMenu;
336 listener_->OnHardwareKey(this, keyname);