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_application.h"
7 #include <cert-service.h>
9 #include <ewk_chromium.h>
13 #include "runtime/native_window.h"
14 #include "runtime/command_line.h"
15 #include "runtime/web_view.h"
16 #include "runtime/vibration_manager.h"
20 // TODO(sngn.lee) : It should be declare in common header
21 const char* kKeyNameBack = "back";
23 const char* kAppControlEventScript = \
24 "var __event = document.createEvent(\"CustomEvent\");\n"
25 "__event.initCustomEvent(\"appcontrol\", true, true);\n"
26 "document.dispatchEvent(__event);\n"
28 "for (var i=0; i < window.frames.length; i++)\n"
29 "{ window.frames[i].document.dispatchEvent(__event); }";
30 const char* kBackKeyEventScript = \
31 "var __event = document.createEvent(\"CustomEvent\");\n"
32 "__event.initCustomEvent(\"tizenhwkey\", true, true);\n"
33 "__event.keyName = \"back\";\n"
34 "document.dispatchEvent(__event);\n"
36 "for (var i=0; i < window.frames.length; i++)\n"
37 "{ window.frames[i].document.dispatchEvent(__event); }";
43 WebApplication::WebApplication(const std::string& appid)
44 : initialized_(false),
45 appid_(appid), ewk_context_(ewk_context_new()) {
46 std::unique_ptr<char, decltype(std::free)*>
47 path {app_get_data_path(), std::free};
48 app_data_path_ = path.get();
51 WebApplication::~WebApplication() {
52 ewk_context_delete(ewk_context_);
55 bool WebApplication::Initialize(NativeWindow* window) {
59 ewk_context_cache_model_set(ewk_context_, EWK_CACHE_MODEL_DOCUMENT_BROWSER);
62 auto cookie_manager = ewk_context_cookie_manager_get(ewk_context_);
63 ewk_cookie_manager_accept_policy_set(cookie_manager,
64 EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
66 // set persistent storage path
67 std::string cookie_path = data_path() + ".cookie";
68 ewk_cookie_manager_persistent_storage_set(
69 cookie_manager, cookie_path.c_str(),
70 EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
73 auto vibration_start_callback = [](uint64_t ms, void*) {
74 platform::VibrationManager::GetInstance()->Start(static_cast<int>(ms));
76 auto vibration_stop_callback = [](void* /*user_data*/) {
77 platform::VibrationManager::GetInstance()->Stop();
79 ewk_context_vibration_client_callbacks_set(ewk_context_,
80 vibration_start_callback,
81 vibration_stop_callback,
84 // Set certificate path
85 char* cert_path = cert_svc_get_certificate_crt_file_path();
86 if (cert_path != NULL) {
87 ewk_context_certificate_file_set(ewk_context_, cert_path);
90 // TODO(sngn.lee): find the proxy url
91 // ewk_context_proxy_uri_set(ewk_context_, ... );
95 void WebApplication::Launch() {
97 WebView* view = new WebView(window_, ewk_context_);
99 // TODO(sngn.lee): Get the start file
100 view->LoadUrl("index.html");
101 view_stack_.push_front(view);
102 window_->SetContent(view->evas_object());
104 // TODO(sngn.lee): below code only debug code
105 auto callback = [](void* data, Evas* e, Evas_Object* obj, void* eventInfo) -> void {
107 evas_object_geometry_get(obj, &x,&y,&w,&h);
108 fprintf(stderr,"resize ! (%d, %d, %d, %d)\n", x,y,w,h);
110 evas_object_event_callback_add(view->evas_object(), EVAS_CALLBACK_RESIZE, callback, NULL );
112 // TODO(sngn.lee): check the below code location.
113 // in Wearable, webkit can render contents before show window
114 // but Mobile, webkit can't render contents before show window
118 void WebApplication::AppControl() {
119 // TODO(sngn.lee): find the app control url and the reset options
121 // TODO(sngn.lee): Set the injected bundle into extension process
127 WebView* view = new WebView(window_, ewk_context_);
128 view->LoadUrl("file:///index.html");
129 view_stack_.push_front(view);
130 window_->SetContent(view->evas_object());
133 SendAppControlEvent();
137 void WebApplication::SendAppControlEvent() {
138 auto it = view_stack_.begin();
139 while (it != view_stack_.end()) {
140 (*it)->EvalJavascript(kAppControlEventScript);
144 void WebApplication::ClearViewStack() {
145 window_->SetContent(NULL);
146 auto it = view_stack_.begin();
147 while (it != view_stack_.end()) {
154 void WebApplication::Resume() {
155 if (view_stack_.size() > 0 && view_stack_.front() != NULL)
156 view_stack_.front()->SetVisibility(true);
158 // TODO(sngn.lee) : should be check the background support option
159 // if background suuport options was on, skip below code
161 auto it = view_stack_.begin();
162 for ( ; it != view_stack_.end(); ++it) {
167 void WebApplication::Suspend() {
168 if (view_stack_.size() > 0 && view_stack_.front() != NULL)
169 view_stack_.front()->SetVisibility(false);
171 // TODO(sngn.lee) : should be check the background support option
172 // if background suuport options was on, skip below code
173 auto it = view_stack_.begin();
174 for ( ; it != view_stack_.end(); ++it) {
179 void WebApplication::OnCreatedNewWebView(WebView* view, WebView* new_view) {
180 if (view_stack_.size() > 0 && view_stack_.front() != NULL)
181 view_stack_.front()->SetVisibility(false);
183 view_stack_.push_front(new_view);
184 window_->SetContent(new_view->evas_object());
187 void WebApplication::OnClosedWebView(WebView * view) {
188 if (view_stack_.size() == 0)
191 WebView* current = view_stack_.front();
192 if (current == view) {
193 view_stack_.pop_front();
195 auto found = std::find(view_stack_.begin(), view_stack_.end(), view);
196 if (found != view_stack_.end()) {
197 view_stack_.erase(found);
201 if (view_stack_.size() == 0) {
202 // TODO(sngn.lee): terminate the webapp
203 } else if (current != view_stack_.front()) {
204 view_stack_.front()->SetVisibility(true);
205 window_->SetContent(view_stack_.front()->evas_object());
211 void WebApplication::OnRendered(WebView* view) {
215 void WebApplication::OnReceivedWrtMessage(
217 const Ewk_IPC_Wrt_Message_Data& message) {
218 // TODO(wy80.choi): To be implemented
221 void WebApplication::OnOrientationLock(WebView* view,
223 int preferred_rotation) {
224 if (view_stack_.size() == 0)
227 // Only top-most view can set the orientation relate operation
228 if (view_stack_.front() != view)
231 // TODO(sngn.lee): check the orientaion setting
232 // if allow the auto orientation
233 // if (is not allow orientation) {
237 window_->SetRotationLock(preferred_rotation);
239 window_->SetAutoRotation();
243 void WebApplication::OnHardwareKey(WebView* view, const std::string& keyname) {
244 // TODO(sngn.lee): Check the hw key event was enabled
245 if (true && kKeyNameBack == keyname) {
246 view->EvalJavascript(kBackKeyEventScript);