[Release] livebox.web-provider-1.14
[platform/framework/web/web-provider.git] / src / Core / View / WebView.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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  * @file    WebView.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <string>
21 #include <fstream>
22 #include <streambuf>
23 #include <Evas.h>
24 #include <Eina.h>
25 #include <ewk_context.h>
26 #include <ewk_view.h>
27 #include <Core/Util/Log.h>
28 #include "WebView.h"
29
30 // injection javascript file regarding creating js object used by box and pd
31 static const std::string injectionFile("/usr/share/web-provider/injection.js");
32
33 std::map<const std::string, const Evas_Smart_Cb> WebView::m_smartCallbacksMap =
34 {
35     {"load,started", WebView::loadStartedCallback},
36     {"load,finished", WebView::loadFinishedCallback},
37     {"title,changed", WebView::titleChangedCallback},
38     {"uri,changed", WebView::uriChangedCallback},
39     {"load,progress", WebView::loadProgressCallback},
40     {"load,progress,finished", WebView::loadProgressFinishedCallback},
41     {"process,crashed", WebView::processCrashedCallback},
42     {"create,window", WebView::createWindowCallback},
43     {"close,window", WebView::closeWindowCallback},
44     {"policy,navigation,decide", WebView::policyNavigationDecideCallback},
45     {"policy,newwindow,decide", WebView::policyNewWindowDecideCallback},
46     {"policy,response,decide", WebView::pageResponseDecideCallback},
47     {"contextmenu,customize", WebView::contextmenuCustomizeCallback},
48     {"form,submit", WebView::formSubmitCallback},
49     {"request,geolocation,permission", WebView::geolocationPermissionRequestCallback},
50     {"notification,show", WebView::notificationShowCallback},
51     {"notification,cancel", WebView::notificationCancelCallback},
52     {"notification,permission,request", WebView::notificationPermissionRequestCallback},
53     {"database,quota,exceeded", WebView::databaseUsagePermissionRequestCallback},
54     {"filesystem,permission,request", WebView::fileSystemPermissionRequestCallback},
55     {"fullscreen,enterfullscreen", WebView::enterFullscreenCallback},
56     {"fullscreen,exitfullscreen", WebView::exitFullscreenCallback},
57     {"inputmethod,changed", WebView::imeChangedCallback},
58     {"editorclient,ime,opened", WebView::imeOpenedCallback},
59     {"editorclient,ime,closed", WebView::imeClosedCallback},
60     {"usermedia,permission,request", WebView::usermediaPermissionRequestCallback},
61     {"protocolhandler,registration,requested", WebView::protocolHandlerRegistrationCallback},
62     {"protocolhandler,isregistered", WebView::protocolHandlerIsRegisteredCallback},
63     {"protocolhandler,unregistration,requested", WebView::protocolHandlerUnregistrationCallback},
64     {"contenthandler,registration,requested", WebView::contentHandlerRegistrationCallback},
65     {"contenthandler,isregistered", WebView::contentHandlerIsRegisteredCallback},
66     {"contenthandler,unregistration,requested", WebView::contentHandlerUnregistrationCallback},
67     {"request,certificate,confirm", WebView::certificateConfirmRequestCallback}
68 };
69
70 WebView::WebView(Evas_Object* win, Ewk_Context* ewkContext)
71     : m_topWebview(NULL)
72     , m_win(win)
73     , m_ewkContext(ewkContext)
74 {
75 }
76
77 WebView::~WebView() 
78 {
79 }
80
81 bool WebView::show(
82         std::string& startUrl, int width, int height,
83         WebViewCallback didCreateBaseWebView, void* data)
84 {
85     LogD("enter");
86     if (!m_topWebview) {
87         m_topWebview = ewk_view_add_with_context(
88                         evas_object_evas_get(m_win), m_ewkContext);
89         if (!setBasicSetting(m_topWebview)) {
90             return false;
91         }
92     }
93
94     // inform base webview to caller
95     if (didCreateBaseWebView) {
96         didCreateBaseWebView(data, static_cast<void*>(m_topWebview));
97     }
98
99     // check scheme of url
100     std::string url = validateUrl(startUrl);
101     LogD("load: %s", url.c_str());
102     evas_object_resize(m_topWebview, width, height);
103     evas_object_show(m_topWebview);
104     ewk_view_url_set(m_topWebview, url.c_str());
105
106     return true;
107 }
108
109 bool WebView::hide()
110 {
111     LogD("enter");
112
113     // TODO If created webviews are managed by WebView Class,
114     //  add code regarding created all webviews
115     if (!unsetBasicSetting(m_topWebview)) {
116         return false;
117     }
118     evas_object_del(m_topWebview);
119     m_topWebview = NULL;
120
121     return true;
122 }
123
124 bool WebView::suspend()
125 {
126     LogD("enter");
127     ewk_view_page_visibility_state_set(
128             m_topWebview, EWK_PAGE_VISIBILITY_STATE_HIDDEN, EINA_FALSE);
129     ewk_view_visibility_set(m_topWebview, EINA_FALSE);
130     ewk_view_suspend(m_topWebview);
131     return true;
132 }
133
134 bool WebView::resume()
135 {
136     LogD("enter");
137     ewk_view_resume(m_topWebview);
138     ewk_view_page_visibility_state_set(
139             m_topWebview, EWK_PAGE_VISIBILITY_STATE_VISIBLE, EINA_FALSE);
140     ewk_view_visibility_set(m_topWebview, EINA_TRUE);
141     return true;
142 }
143
144 bool WebView::setBasicSetting(Evas_Object* webview)
145 {
146     LogD("enter");
147
148     if (!webview) {
149         return false;
150     }
151
152     for (auto it = m_smartCallbacksMap.begin(); 
153             it != m_smartCallbacksMap.end(); it++) {
154         evas_object_smart_callback_add(
155             webview, it->first.c_str(), it->second, this);
156     }
157
158     // set specific features 
159     Ewk_Settings* setting = ewk_view_settings_get(webview);
160     // set user agent like WRT, or Browser?
161     // ewk_view_user_agent_set(webview, "some_ua_string");
162     ewk_settings_plugins_enabled_set(setting, EINA_TRUE);
163     ewk_settings_javascript_enabled_set(setting, EINA_TRUE);
164     ewk_settings_loads_images_automatically_set(setting, EINA_TRUE);
165     ewk_settings_auto_fitting_set(setting, EINA_FALSE);
166
167     // set visibility
168     ewk_view_page_visibility_state_set(
169             webview, EWK_PAGE_VISIBILITY_STATE_VISIBLE, EINA_TRUE);
170
171     // set orientation
172     ewk_view_orientation_lock_callback_set(
173             webview, orientationLockCallback, this);
174
175     evas_object_color_set(webview, 0, 0, 0, 0);
176     
177     return true;
178 }
179
180 bool WebView::unsetBasicSetting(Evas_Object* webview)
181 {
182     LogD("enter");
183
184     if (!webview) {
185         return false;
186     }
187
188     for (auto it = m_smartCallbacksMap.begin(); 
189             it != m_smartCallbacksMap.end(); it++) {
190         evas_object_smart_callback_del(
191             m_topWebview, it->first.c_str(), it->second);
192     }
193
194     ewk_view_orientation_lock_callback_set(m_topWebview, NULL, NULL);
195     return true;
196 }
197
198 std::string WebView::validateUrl(std::string& url)
199 {
200     LogD("enter");
201
202     if (url.empty()) {
203         return std::string();
204     }
205
206     if((!url.compare(0, 4, "http")) || 
207             (!url.compare(0, 5, "https")) ||
208             (!url.compare(0, 4, "file"))) 
209     {
210         return url;
211     }
212
213     std::string newUrl("file://");
214     newUrl += url;
215     return newUrl;
216 }
217
218 void WebView::loadStartedCallback(
219         void* data, Evas_Object* obj, void* eventInfo)
220 {
221     LogD("enter");
222
223     WebView* This = static_cast<WebView*>(data);
224
225     std::ifstream jsFile(injectionFile);
226     std::string script((std::istreambuf_iterator<char>(jsFile)),
227                         std::istreambuf_iterator<char>());
228     LogD("injected js code: %s", script.c_str());
229
230     ewk_view_script_execute(obj, script.c_str(), executeScriptCallback, This);
231     This->didLoadStarted(obj);
232 }
233
234 void WebView::loadFinishedCallback(
235         void* data, Evas_Object* obj, void* eventInfo)
236 {
237     LogD("enter");
238
239     WebView* This = static_cast<WebView*>(data);
240     This->didLoadFinished(obj);
241 }
242
243 void WebView::titleChangedCallback(
244         void* data, Evas_Object* obj, void* eventInfo)
245 {
246     LogD("enter");
247
248     WebView* This = static_cast<WebView*>(data);
249     This->didTitleChanged(obj, eventInfo);
250 }
251
252 void WebView::uriChangedCallback(
253         void* data, Evas_Object* obj, void* eventInfo)
254 {
255     LogD("enter");
256
257     WebView* This = static_cast<WebView*>(data);
258     This->didUriChanged(obj, eventInfo);
259 }
260
261 void WebView::loadProgressCallback(
262         void* data, Evas_Object* obj, void* eventInfo)
263 {
264     LogD("enter");
265
266     WebView* This = static_cast<WebView*>(data);
267     This->didLoadProgress(obj, eventInfo);
268 }
269
270 void WebView::loadProgressFinishedCallback(
271         void* data, Evas_Object* obj, void* eventInfo)
272 {
273     LogD("enter");
274
275     WebView* This = static_cast<WebView*>(data);
276     This->didLoadProgressFinished(obj);
277 }
278
279 void WebView::processCrashedCallback(
280         void* data, Evas_Object* obj, void* eventInfo)
281 {
282     LogD("enter");
283
284     WebView* This = static_cast<WebView*>(data);
285     This->didProcessCrashed(obj);
286 }
287
288 void WebView::createWindowCallback(
289         void* data, Evas_Object* obj, void* eventInfo)
290 {
291     LogD("enter");
292
293     WebView* This = static_cast<WebView*>(data);
294     This->didCreateWindow(obj, eventInfo);
295 }
296
297 void WebView::closeWindowCallback(
298         void* data, Evas_Object* obj, void* eventInfo)
299 {
300     LogD("enter");
301
302     WebView* This = static_cast<WebView*>(data);
303     This->didCloseWindow(obj);
304 }
305
306 void WebView::policyNavigationDecideCallback(
307         void* data, Evas_Object* obj, void* eventInfo)
308 {
309     LogD("enter");
310
311     WebView* This = static_cast<WebView*>(data);
312     This->didPolicyNavigationDecide(obj, eventInfo);
313 }
314
315 void WebView::policyNewWindowDecideCallback(
316         void* data, Evas_Object* obj, void* eventInfo)
317 {
318     LogD("enter");
319
320     WebView* This = static_cast<WebView*>(data);
321     This->didPolicyNewWindowDecide(obj, eventInfo);
322 }
323
324 void WebView::pageResponseDecideCallback(
325         void* data, Evas_Object* obj, void* eventInfo)
326 {
327     LogD("enter");
328
329     WebView* This = static_cast<WebView*>(data);
330     This->didPageResponseDecide(obj);
331 }
332
333 void WebView::contextmenuCustomizeCallback(
334         void* data, Evas_Object* obj, void* eventInfo)
335 {
336     LogD("enter");
337
338     WebView* This = static_cast<WebView*>(data);
339     This->didContextmenuCustomize(obj, eventInfo);
340 }
341
342 void WebView::formSubmitCallback(
343         void* data, Evas_Object *obj, void* eventInfo)
344 {
345     LogD("enter");
346
347     WebView* This = static_cast<WebView*>(data);
348     This->didFormSubmit(obj);
349 }
350
351 void WebView::geolocationPermissionRequestCallback(
352         void* data, Evas_Object* obj, void* eventInfo)
353 {
354     LogD("enter");
355
356     WebView* This = static_cast<WebView*>(data);
357     This->didGeolocationPermissionRequest(obj, eventInfo);
358 }
359
360 void WebView::notificationShowCallback(
361         void* data, Evas_Object* obj, void* eventInfo)
362 {
363     LogD("enter");
364
365     WebView* This = static_cast<WebView*>(data);
366     This->didNotificationShow(obj, eventInfo);
367 }
368
369 void WebView::notificationCancelCallback(
370         void* data, Evas_Object* obj, void* eventInfo)
371 {
372     LogD("enter");
373
374     WebView* This = static_cast<WebView*>(data);
375     This->didNotificationCancel(obj, eventInfo);
376 }
377
378 void WebView::notificationPermissionRequestCallback(
379         void* data, Evas_Object* obj, void* eventInfo)
380 {
381     LogD("enter");
382
383     WebView* This = static_cast<WebView*>(data);
384     This->didNotificationPermissionRequest(obj, eventInfo);
385 }
386
387 void WebView::databaseUsagePermissionRequestCallback(
388         void* data, Evas_Object* obj, void* eventInfo)
389 {
390     LogD("enter");
391
392     WebView* This = static_cast<WebView*>(data);
393     This->didDatabaseUsagePermissionRequest(obj, eventInfo);
394 }
395
396 void WebView::fileSystemPermissionRequestCallback(
397         void* data, Evas_Object* obj, void* eventInfo)
398 {
399     LogD("enter");
400
401     WebView* This = static_cast<WebView*>(data);
402     This->didFilesystemPermissionRequest(obj, eventInfo);
403 }
404
405 void WebView::enterFullscreenCallback(
406         void* data, Evas_Object* obj, void* eventInfo)
407 {
408     LogD("enter");
409
410     WebView* This = static_cast<WebView*>(data);
411     This->didEnterFullscreen(obj);
412 }
413
414 void WebView::exitFullscreenCallback(
415         void* data, Evas_Object* obj, void* eventInfo)
416 {
417     LogD("enter");
418
419     WebView* This = static_cast<WebView*>(data);
420     This->didExitFullscreen(obj);
421 }
422
423 void WebView::imeChangedCallback(
424         void* data, Evas_Object* obj, void* eventInfo)
425 {
426     LogD("enter");
427
428     WebView* This = static_cast<WebView*>(data);
429     This->didImeChanged(obj, eventInfo);
430 }
431
432 void WebView::imeOpenedCallback(
433         void* data, Evas_Object* obj, void* eventInfo)
434 {
435     LogD("enter");
436
437     WebView* This = static_cast<WebView*>(data);
438     This->didImeOpened(obj);
439 }
440
441 void WebView::imeClosedCallback(
442         void* data, Evas_Object* obj, void* eventInfo)
443 {
444     LogD("enter");
445
446     WebView* This = static_cast<WebView*>(data);
447     This->didImeClosed(obj);
448 }
449
450 void WebView::usermediaPermissionRequestCallback(
451         void* data, Evas_Object* obj, void* eventInfo)
452 {
453     LogD("enter");
454
455     WebView* This = static_cast<WebView*>(data);
456     This->didUsermediaPermissionRequest(obj, eventInfo);
457 }
458
459 void WebView::protocolHandlerRegistrationCallback(
460         void* data, Evas_Object* obj, void* eventInfo)
461 {
462     LogD("enter");
463
464     WebView* This = static_cast<WebView*>(data);
465     This->didProtocolHandlerRegistration(obj, eventInfo);
466 }
467
468 void WebView::protocolHandlerIsRegisteredCallback(
469         void* data, Evas_Object* obj, void* eventInfo)
470 {
471     LogD("enter");
472
473     WebView* This = static_cast<WebView*>(data);
474     This->didProtocolHandlerIsRegistered(obj, eventInfo);
475 }
476
477 void WebView::protocolHandlerUnregistrationCallback(
478         void* data, Evas_Object* obj, void* eventInfo)
479 {
480     LogD("enter");
481
482     WebView* This = static_cast<WebView*>(data);
483     This->didProtocolHandlerUnregistration(obj, eventInfo);
484 }
485
486 void WebView::contentHandlerRegistrationCallback(
487         void* data, Evas_Object* obj, void* eventInfo)
488 {
489     LogD("enter");
490
491     WebView* This = static_cast<WebView*>(data);
492     This->didContentHandlerRegistration(obj, eventInfo);
493 }
494
495 void WebView::contentHandlerIsRegisteredCallback(
496         void* data, Evas_Object* obj, void* eventInfo)
497 {
498     LogD("enter");
499
500     WebView* This = static_cast<WebView*>(data);
501     This->didContentHandlerIsRegistered(obj, eventInfo);
502 }
503
504 void WebView::contentHandlerUnregistrationCallback(
505         void* data, Evas_Object* obj, void* eventInfo)
506 {
507     LogD("enter");
508
509     WebView* This = static_cast<WebView*>(data);
510     This->didContentHandlerUnregistration(obj, eventInfo);
511 }
512
513 void WebView::certificateConfirmRequestCallback(
514     void* data, Evas_Object* obj, void* eventInfo)
515 {
516     LogD("enter");
517
518     WebView* This = static_cast<WebView*>(data);
519     This->didCertificateConfirmRequest(obj, eventInfo);
520 }
521
522 void WebView::didLoadStarted(Evas_Object* obj)
523 {
524     LogD("enter");
525     // This Will be implemented by derived class
526 }
527
528 void WebView::didLoadFinished(Evas_Object* obj)
529 {
530     LogD("enter");
531     // This Will be implemented by derived class
532 }
533
534 void WebView::didTitleChanged(Evas_Object* obj, void* eventInfo)
535 {
536     LogD("enter");
537     // This Will be implemented by derived class
538 }
539
540 void WebView::didUriChanged(Evas_Object* obj, void* eventInfo)
541 {
542     LogD("enter");
543     // This Will be implemented by derived class
544 }
545
546 void WebView::didLoadProgress(Evas_Object* obj, void* eventInfo)
547 {
548     LogD("enter");
549     // This Will be implemented by derived class
550 }
551
552 void WebView::didLoadProgressFinished(Evas_Object* obj)
553 {
554     LogD("enter");
555     // This Will be implemented by derived class
556 }
557
558 void WebView::didProcessCrashed(Evas_Object* obj)
559 {
560     LogD("enter");
561     // This Will be implemented by derived class
562 }
563  
564 void WebView::didCreateWindow(Evas_Object* obj, void* eventInfo)
565 {
566     LogD("enter");
567     // This Will be implemented by derived class
568 }
569
570 void WebView::didCloseWindow(Evas_Object* obj)
571 {
572     LogD("enter");
573     // This Will be implemented by derived class
574 }
575
576 void WebView::didPolicyNavigationDecide(Evas_Object* obj, void* eventInfo)
577 {
578     LogD("enter");
579     // This Will be implemented by derived class
580 }
581
582 void WebView::didPolicyNewWindowDecide(Evas_Object* obj, void* eventInfo)
583 {
584     LogD("enter");
585     // This Will be implemented by derived class
586 }
587
588 void WebView::didPageResponseDecide(Evas_Object* obj)
589 {
590     LogD("enter");
591     // This Will be implemented by derived class
592 }
593  
594 void WebView::didContextmenuCustomize(Evas_Object* obj, void* eventInfo)
595 {
596     LogD("enter");
597     // This Will be implemented by derived class
598 }
599
600 void WebView::didFormSubmit(Evas_Object* obj)
601 {
602     LogD("enter");
603     // This Will be implemented by derived class
604 }
605
606 void WebView::didGeolocationPermissionRequest(Evas_Object* obj, void* eventInfo)
607 {
608     LogD("enter");
609     // This Will be implemented by derived class
610 }
611
612 void WebView::didNotificationShow(Evas_Object* obj, void* eventInfo)
613 {
614     LogD("enter");
615     // This Will be implemented by derived class
616 }
617
618 void WebView::didNotificationCancel(Evas_Object* obj, void* eventInfo)
619 {
620     LogD("enter");
621     // This Will be implemented by derived class
622 }
623
624 void WebView::didNotificationPermissionRequest(Evas_Object* obj, void* eventInfo)
625 {
626     LogD("enter");
627     // This Will be implemented by derived class
628 }
629  
630 void WebView::didDatabaseUsagePermissionRequest(Evas_Object* obj, void* eventInfo)
631 {
632     LogD("enter");
633     // This Will be implemented by derived class
634 }
635
636 void WebView::didFilesystemPermissionRequest(Evas_Object* obj, void* eventInfo)
637 {
638     LogD("enter");
639     // This Will be implemented by derived class
640 }
641
642 void WebView::didEnterFullscreen(Evas_Object* obj)
643 {
644     LogD("enter");
645     // This Will be implemented by derived class
646 }
647
648 void WebView::didExitFullscreen(Evas_Object* obj)
649 {
650     LogD("enter");
651     // This Will be implemented by derived class
652 }
653
654 void WebView::didImeChanged(Evas_Object* obj, void* eventInfo)
655 {
656     LogD("enter");
657     // This Will be implemented by derived class
658 }
659
660 void WebView::didImeOpened(Evas_Object* obj)
661 {
662     LogD("enter");
663     // This Will be implemented by derived class
664 }
665
666 void WebView::didImeClosed(Evas_Object* obj)
667 {
668     LogD("enter");
669     // This Will be implemented by derived class
670 }
671
672 void WebView::didUsermediaPermissionRequest(Evas_Object* obj, void* eventInfo)
673 {
674     LogD("enter");
675     // This Will be implemented by derived class
676 }
677
678 void WebView::didProtocolHandlerRegistration(Evas_Object* obj, void* eventInfo)
679 {
680     LogD("enter");
681     // This Will be implemented by derived class
682 }
683
684 void WebView::didProtocolHandlerIsRegistered(Evas_Object* obj, void* eventInfo)
685 {
686     LogD("enter");
687     // This Will be implemented by derived class
688 }
689
690 void WebView::didProtocolHandlerUnregistration(Evas_Object* obj, void* eventInfo)
691 {
692     LogD("enter");
693     // This Will be implemented by derived class
694 }
695
696 void WebView::didContentHandlerRegistration(Evas_Object* obj, void* eventInfo)
697 {
698     LogD("enter");
699     // This Will be implemented by derived class
700 }
701
702 void WebView::didContentHandlerIsRegistered(Evas_Object* obj, void* eventInfo)
703 {
704     LogD("enter");
705     // This Will be implemented by derived class
706 }
707
708 void WebView::didContentHandlerUnregistration(Evas_Object* obj, void* eventInfo)
709 {
710     LogD("enter");
711     // This Will be implemented by derived class
712 }
713
714 void WebView::didCertificateConfirmRequest(Evas_Object* obj, void* eventInfo)
715 {
716     LogD("enter");
717     // This Will be implemented by derived class
718 }
719
720 Eina_Bool WebView::orientationLockCallback(
721         Evas_Object* obj, Eina_Bool needLock, int orientation, void* data)
722 {
723     LogD("enter");
724     return EINA_TRUE;
725 }
726
727 void WebView::executeScriptCallback(
728                 Evas_Object* obj, const char* result, void* data)
729 {
730     LogD("enter");
731 }
732