[Release] wrt_0.8.191
[platform/framework/web/wrt.git] / src / api_new / ewk_context_manager.cpp
1 /*
2  * Copyright (c) 2013 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  * @file    ewk_context_manager.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  * @version 0.1
20  * @brief   Implementation of EwkContextManager class.
21  *          This file handles operation regarding Ewk_Context
22  */
23
24 #include <string>
25 #include <vconf.h>
26 #include <ewk_context.h>
27 #include <dpl/log/log.h>
28 #include <dpl/exception.h>
29 #include <profiling_util.h>
30 #include <i_view_module.h>
31 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
32 #include <dpl/wrt-dao-ro/widget_dao_types.h>
33 #include <dpl/wrt-dao-ro/vconf_config.h>
34 #include <dpl/utils/wrt_global_settings.h>
35 #include "ewk_context_manager.h"
36
37 namespace WRT {
38
39 static const std::string bundlePath("/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so");
40 static const std::string caCertPath("/opt/usr/share/certs/ca-certificate.crt");
41
42 #ifdef __arm__
43 const std::string plugins_arch = "plugins/arm";
44 #else
45 const std::string plugins_arch = "plugins/x86";
46 #endif
47
48 EwkContextManager::EwkContextManager(
49         const std::string& tizenAppId,
50         Ewk_Context* ewkContext,
51         ViewModule::IViewModulePtr viewModule)
52     : AbstractContextManager(tizenAppId, ewkContext, viewModule),
53       m_initialized(false), m_isInternalContext(false)
54 {
55     if (!initialize()) {
56         ThrowMsg(DPL::Exception, "Fail to intialize EwkContextManager");
57     }
58     // set ewk context callbacks
59     setCallbacks();
60 }
61
62 EwkContextManager::~EwkContextManager()
63 {
64     // unset registered ewk context callbacks
65     unsetCallbacks();
66     destroy();
67 }
68
69 Ewk_Context * EwkContextManager::getEwkContext() const
70 {
71     return m_ewkContext;
72 }
73
74 bool EwkContextManager::initialize()
75 {
76     if (!m_ewkContext) {
77         m_ewkContext = ewk_context_new_with_injected_bundle_path(bundlePath.c_str());
78
79         if (!m_ewkContext) {
80             return false;
81         }
82
83         m_isInternalContext = true;
84     }
85
86     // cache model setting
87     ewk_context_cache_model_set(
88             m_ewkContext,
89             EWK_CACHE_MODEL_DOCUMENT_BROWSER);
90     ADD_PROFILING_POINT("WebProcess fork", "start");
91     // To fork a Webprocess as soon as possible,
92     // the following ewk_api is called explicitly.
93     ewk_cookie_manager_accept_policy_set(
94             ewk_context_cookie_manager_get(m_ewkContext),
95             EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
96     ADD_PROFILING_POINT("WebProcess fork", "stop");
97
98     // proxy server setting
99     char *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
100     if (proxy && strlen(proxy) && strcmp(proxy, "0.0.0.0")) {
101         LogInfo("proxy address: " << proxy);
102         ewk_context_proxy_uri_set(m_ewkContext,  proxy);
103     } else {
104         LogInfo("proxy address is empty");
105         ewk_context_proxy_uri_set(m_ewkContext, NULL);
106     }
107
108     if (proxy) {
109         free(proxy);
110     }
111
112     // temp - this api was moved to wrt-client for performance
113     //ewk_context_certificate_file_set(m_ewkContext, caCertPath.c_str());
114
115     // set local stroage database path
116     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
117     ewk_context_web_storage_path_set(
118             m_ewkContext,
119             dao.getPrivateLocalStoragePath().c_str());
120
121     // memory saving mode
122     int result;
123     vconf_get_int(
124             WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
125                 DPL::FromUTF8String(m_appId)).c_str(), &result);
126
127     ewk_context_memory_saving_mode_set(
128             m_ewkContext,
129             static_cast<WrtDB::SettingsType>(result) ==
130             WrtDB::SETTINGS_TYPE_ON ? EINA_TRUE : EINA_FALSE);
131
132     ewk_context_tizen_extensible_api_set(
133         m_ewkContext,
134         EWK_EXTENSIBLE_API_MEDIA_STREAM_RECORD,
135         EINA_TRUE);
136     ewk_context_tizen_extensible_api_set(
137        m_ewkContext,
138        EWK_EXTENSIBLE_API_ROTATE_CAMERA_VIEW,
139        EINA_FALSE);
140     ewk_context_tizen_extensible_api_set(
141        m_ewkContext,
142        EWK_EXTENSIBLE_API_MEDIA_VOLUME_CONTROL,
143        EINA_TRUE);
144
145     setAutoFullscreenMode();
146     setBackgroundSupport();
147 #ifdef CSP_ENABLED
148     if (dao.getSecurityModelVersion() ==
149         WrtDB::WidgetSecurityModelVersion::WIDGET_SECURITY_MODEL_V2)
150     {
151         setCSPSupport();
152     }
153 #endif
154
155     // ewk storage_path set
156     ewk_context_storage_path_reset(m_ewkContext);
157
158     // npruntime plugins path set
159     std::ostringstream plugins_path;
160     plugins_path << dao.getPath() << plugins_arch;
161     LogInfo("ewk_context_additional_plugin_path_set() : " << plugins_path.str());
162
163     ewk_context_additional_plugin_path_set(m_ewkContext, plugins_path.str().c_str());
164
165     m_initialized = true;
166
167     return true;
168 }
169
170 void EwkContextManager::destroy()
171 {
172     // only in the following case, webkit context should be deleted
173     if (m_initialized && m_isInternalContext) {
174         ewk_context_delete(m_ewkContext);
175     }
176 }
177
178 void EwkContextManager::setCallbacks()
179 {
180     if (!m_initialized) {
181         return;
182     }
183
184     ewk_context_message_from_injected_bundle_callback_set(
185             m_ewkContext,
186             messageFromInjectedBundleCallback,
187             this);
188
189     ewk_context_did_start_download_callback_set(
190             m_ewkContext,
191             didStartDownloadCallback,
192             this);
193
194     ewk_context_vibration_client_callbacks_set(
195             m_ewkContext,
196             vibrationClientStartCallback,
197             vibrationClientStopCallback,
198             this);
199 }
200
201 void EwkContextManager::setAutoFullscreenMode()
202 {
203     using namespace WrtDB;
204     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
205     if(!m_ewkContext) {
206         return;
207     }
208     std::list<DPL::String> widgetPrivilege = dao.getWidgetPrivilege();
209     bool fullscreen = false;
210
211     FOREACH(it, widgetPrivilege) {
212         std::map<std::string, Feature>::const_iterator result =
213         g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(*it));
214             if (result != g_W3CPrivilegeTextMap.end()) {
215                 if (result->second == FEATURE_FULLSCREEN_MODE) {
216                     fullscreen = true;
217                     break;
218             }
219         }
220     }
221     ewk_context_tizen_extensible_api_set(m_ewkContext,
222         EWK_EXTENSIBLE_API_FULL_SCREEN,
223         fullscreen);
224 }
225
226 void EwkContextManager::setBackgroundSupport()
227 {
228     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
229     if(!m_ewkContext) {
230         return;
231     }
232
233     WrtDB::WidgetSettings widgetSettings;
234     dao.getWidgetSettings(widgetSettings);
235     WidgetSettingList settings(widgetSettings);
236     bool backgroundSupport = false;
237     if(settings.getBackgroundSupport() == BackgroundSupport_Enable) {
238         backgroundSupport = true;
239     }
240     ewk_context_tizen_extensible_api_set(
241         m_ewkContext,
242         EWK_EXTENSIBLE_API_BACKGROUND_MUSIC,
243         backgroundSupport);
244 }
245
246 void EwkContextManager::setCSPSupport()
247 {
248     if(!m_ewkContext) {
249         return;
250     }
251
252     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
253     LogInfo("Setting CSP default policy");
254     ewk_context_tizen_extensible_api_set(m_ewkContext,
255                                          EWK_EXTENSIBLE_API_CSP,
256                                          true);
257 }
258
259 void EwkContextManager::unsetCallbacks()
260 {
261     if (!m_initialized) {
262         return;
263     }
264
265     ewk_context_message_from_injected_bundle_callback_set(
266             m_ewkContext, NULL, NULL);
267     ewk_context_did_start_download_callback_set(
268             m_ewkContext, NULL, NULL);
269     ewk_context_vibration_client_callbacks_set(
270             m_ewkContext, NULL, NULL, NULL);
271 }
272
273
274 void EwkContextManager::messageFromInjectedBundleCallback(
275         const char* name,
276         const char* body,
277         char** returnData,
278         void* clientInfo)
279 {
280     LogDebug("enter");
281     LogDebug("did recive message: " << name);
282
283     EwkContextManager* This = static_cast<EwkContextManager*>(clientInfo);
284     This->m_view->checkSyncMessageFromBundle(name, body, returnData);
285 }
286
287 void EwkContextManager::didStartDownloadCallback(const char* downloadUrl, void* data)
288 {
289     LogDebug("enter");
290     LogDebug("download url: " << downloadUrl);
291
292     EwkContextManager* This = static_cast<EwkContextManager*>(data);
293     This->m_view->downloadData(downloadUrl);
294 }
295
296 void EwkContextManager::vibrationClientStartCallback(uint64_t time, void* data)
297 {
298     LogDebug("enter");
299
300     EwkContextManager* This = static_cast<EwkContextManager*>(data);
301     This->m_view->activateVibration(true, static_cast<long>(time));
302 }
303
304 void EwkContextManager::vibrationClientStopCallback(void* data)
305 {
306     LogDebug("enter");
307
308     EwkContextManager* This = static_cast<EwkContextManager*>(data);
309     This->m_view->activateVibration(false, 0);
310 }
311
312 void EwkContextManager::handleLowMemory()
313 {
314     if (!m_ewkContext) {
315         return;
316     }
317
318     //ewk_context_cache_clear(m_ewkContext);
319     //ewk_context_notify_low_memory(m_ewkContext);
320 }
321
322 ContextManagerPtr createEwkContextManager(
323         std::string& tizenAppId,
324         Ewk_Context* ewkContext,
325         ViewModule::IViewModulePtr viewModule)
326 {
327     return ContextManagerPtr(new EwkContextManager(tizenAppId, ewkContext, viewModule));
328 }
329
330 }
331