[Release] wrt_0.8.227
[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/widget_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/libwrt-injected-bundle.so");
40 static const std::string caCertPath("/opt/usr/share/certs/ca-certificate.crt");
41
42 EwkContextManager::EwkContextManager(
43         const std::string& tizenAppId,
44         Ewk_Context* ewkContext,
45         ViewModule::IViewModulePtr viewModule)
46     : AbstractContextManager(tizenAppId, ewkContext, viewModule),
47       m_initialized(false), m_isInternalContext(false)
48 {
49     if (!initialize()) {
50         ThrowMsg(DPL::Exception, "Fail to intialize EwkContextManager");
51     }
52     // set ewk context callbacks
53     setCallbacks();
54 }
55
56 EwkContextManager::~EwkContextManager()
57 {
58     // unset registered ewk context callbacks
59     unsetCallbacks();
60     destroy();
61 }
62
63 Ewk_Context * EwkContextManager::getEwkContext() const
64 {
65     return m_ewkContext;
66 }
67
68 bool EwkContextManager::initialize()
69 {
70     if (!m_ewkContext) {
71         m_ewkContext = ewk_context_new_with_injected_bundle_path(bundlePath.c_str());
72
73         if (!m_ewkContext) {
74             return false;
75         }
76
77         m_isInternalContext = true;
78     }
79
80     // cache model setting
81     ewk_context_cache_model_set(
82             m_ewkContext,
83             EWK_CACHE_MODEL_DOCUMENT_BROWSER);
84     ADD_PROFILING_POINT("WebProcess fork", "start");
85     // To fork a Webprocess as soon as possible,
86     // the following ewk_api is called explicitly.
87     ewk_cookie_manager_accept_policy_set(
88             ewk_context_cookie_manager_get(m_ewkContext),
89             EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
90     ADD_PROFILING_POINT("WebProcess fork", "stop");
91
92     // proxy server setting
93     setNetworkProxy();
94
95     LogInfo("ewk_context_certificate_file_set() was called.");
96     ewk_context_certificate_file_set(m_ewkContext, caCertPath.c_str());
97
98     // set local stroage database path
99     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
100     ewk_context_web_storage_path_set(
101             m_ewkContext,
102             dao.getPrivateLocalStoragePath().c_str());
103
104     // memory saving mode
105     //ewk_context_memory_saving_mode_set(
106     //       m_ewkContext,
107     //       static_cast<WrtDB::SettingsType>(result) ==
108     //        WrtDB::SETTINGS_TYPE_ON ? EINA_TRUE : EINA_FALSE);
109
110     ewk_context_tizen_extensible_api_set(
111         m_ewkContext,
112         EWK_EXTENSIBLE_API_MEDIA_STREAM_RECORD,
113         EINA_TRUE);
114     ewk_context_tizen_extensible_api_set(
115        m_ewkContext,
116        EWK_EXTENSIBLE_API_ROTATE_CAMERA_VIEW,
117        EINA_FALSE);
118     ewk_context_tizen_extensible_api_set(
119        m_ewkContext,
120        EWK_EXTENSIBLE_API_MEDIA_VOLUME_CONTROL,
121        EINA_TRUE);
122
123     setAutoFullscreenMode();
124     setBackgroundSupport();
125 #ifdef CSP_ENABLED
126     if (dao.getSecurityModelVersion() ==
127         WrtDB::WidgetSecurityModelVersion::WIDGET_SECURITY_MODEL_V2)
128     {
129         setCSPSupport();
130     }
131 #endif
132
133     // ewk storage_path set
134     ewk_context_storage_path_reset(m_ewkContext);
135
136     std::string pluginsPath =
137             WrtDB::WidgetConfig::GetWidgetNPRuntimePluginsPath(
138                     dao.getTizenPkgId());
139
140     // npruntime plugins path set
141     LogInfo("ewk_context_additional_plugin_path_set() : " << pluginsPath);
142
143     ewk_context_additional_plugin_path_set(m_ewkContext, pluginsPath.c_str());
144
145     m_initialized = true;
146
147     return true;
148 }
149
150 void EwkContextManager::destroy()
151 {
152     // only in the following case, webkit context should be deleted
153     if (m_initialized && m_isInternalContext) {
154         ewk_context_delete(m_ewkContext);
155     }
156 }
157
158 void EwkContextManager::setCallbacks()
159 {
160     if (!m_initialized) {
161         return;
162     }
163
164     ewk_context_message_from_injected_bundle_callback_set(
165             m_ewkContext,
166             messageFromInjectedBundleCallback,
167             this);
168
169     ewk_context_did_start_download_callback_set(
170             m_ewkContext,
171             didStartDownloadCallback,
172             this);
173
174     ewk_context_vibration_client_callbacks_set(
175             m_ewkContext,
176             vibrationClientStartCallback,
177             vibrationClientStopCallback,
178             this);
179
180     vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
181                              vconfChangedCallback,
182                              this);
183 }
184
185 void EwkContextManager::unsetCallbacks()
186 {
187     if (!m_initialized) {
188         return;
189     }
190
191     ewk_context_message_from_injected_bundle_callback_set(
192             m_ewkContext, NULL, NULL);
193     ewk_context_did_start_download_callback_set(
194             m_ewkContext, NULL, NULL);
195     ewk_context_vibration_client_callbacks_set(
196             m_ewkContext, NULL, NULL, NULL);
197
198     vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
199                              vconfChangedCallback);
200 }
201
202 void EwkContextManager::setAutoFullscreenMode()
203 {
204     using namespace WrtDB;
205     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
206     if(!m_ewkContext) {
207         return;
208     }
209     std::list<DPL::String> widgetPrivilege = dao.getWidgetPrivilege();
210     bool fullscreen = false;
211
212     FOREACH(it, widgetPrivilege) {
213         std::map<std::string, Feature>::const_iterator result =
214         g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(*it));
215             if (result != g_W3CPrivilegeTextMap.end()) {
216                 if (result->second == FEATURE_FULLSCREEN_MODE) {
217                     fullscreen = true;
218                     break;
219             }
220         }
221     }
222     ewk_context_tizen_extensible_api_set(m_ewkContext,
223         EWK_EXTENSIBLE_API_FULL_SCREEN,
224         fullscreen);
225 }
226
227 void EwkContextManager::setBackgroundSupport()
228 {
229     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
230     if(!m_ewkContext) {
231         return;
232     }
233
234     WrtDB::WidgetSettings widgetSettings;
235     dao.getWidgetSettings(widgetSettings);
236     WidgetSettingList settings(widgetSettings);
237     bool backgroundSupport = false;
238     if(settings.getBackgroundSupport() == BackgroundSupport_Enable) {
239         backgroundSupport = true;
240     }
241     ewk_context_tizen_extensible_api_set(
242         m_ewkContext,
243         EWK_EXTENSIBLE_API_BACKGROUND_MUSIC,
244         backgroundSupport);
245 }
246
247 void EwkContextManager::setCSPSupport()
248 {
249     if(!m_ewkContext) {
250         return;
251     }
252
253     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
254     LogInfo("Setting CSP default policy");
255     ewk_context_tizen_extensible_api_set(m_ewkContext,
256                                          EWK_EXTENSIBLE_API_CSP,
257                                          true);
258 }
259
260 void EwkContextManager::setNetworkProxy()
261 {
262     Assert(m_ewkContext);
263
264     int networkState;
265     if (vconf_get_int(VCONFKEY_NETWORK_STATUS,
266                       &networkState) != 0)
267     {
268         LogError("Fail to get network state");
269         return;
270     }
271     if (networkState == VCONFKEY_NETWORK_OFF) {
272         return;
273     }
274
275     char* proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
276     if (proxy && strlen(proxy) && strcmp(proxy, "0.0.0.0")) {
277         LogInfo("proxy address: " << proxy);
278         ewk_context_proxy_uri_set(m_ewkContext, proxy);
279     } else {
280         LogInfo("proxy address is empty");
281         ewk_context_proxy_uri_set(m_ewkContext, NULL);
282     }
283 }
284
285 void EwkContextManager::messageFromInjectedBundleCallback(
286         const char* name,
287         const char* body,
288         char** returnData,
289         void* clientInfo)
290 {
291     LogDebug("enter");
292     LogDebug("did recive message: " << name);
293
294     EwkContextManager* This = static_cast<EwkContextManager*>(clientInfo);
295     if (returnData) {
296         This->m_view->checkSyncMessageFromBundle(name, body, returnData);
297     } else {
298         This->m_view->checkAsyncMessageFromBundle(name, body);
299     }
300 }
301
302 void EwkContextManager::didStartDownloadCallback(const char* downloadUrl, void* data)
303 {
304     LogDebug("enter");
305     LogDebug("download url: " << downloadUrl);
306
307     EwkContextManager* This = static_cast<EwkContextManager*>(data);
308     This->m_view->downloadData(downloadUrl);
309 }
310
311 void EwkContextManager::vibrationClientStartCallback(uint64_t time, void* data)
312 {
313     LogDebug("enter");
314
315     EwkContextManager* This = static_cast<EwkContextManager*>(data);
316     This->m_view->activateVibration(true, static_cast<long>(time));
317 }
318
319 void EwkContextManager::vibrationClientStopCallback(void* data)
320 {
321     LogDebug("enter");
322
323     EwkContextManager* This = static_cast<EwkContextManager*>(data);
324     This->m_view->activateVibration(false, 0);
325 }
326
327 void EwkContextManager::vconfChangedCallback(keynode_t* keynode, void* data)
328 {
329     LogDebug("enter");
330     char* key = vconf_keynode_get_name(keynode);
331
332     if (NULL == key) {
333         LogError("key is null");
334         return;
335     }
336     std::string keyString = key;
337     Assert(data);
338     EwkContextManager* This = static_cast<EwkContextManager*>(data);
339     if (keyString == VCONFKEY_NETWORK_PROXY) {
340         This->setNetworkProxy();
341     }
342 }
343
344 void EwkContextManager::handleLowMemory()
345 {
346     if (!m_ewkContext) {
347         return;
348     }
349
350     //ewk_context_cache_clear(m_ewkContext);
351     //ewk_context_notify_low_memory(m_ewkContext);
352 }
353
354 ContextManagerPtr createEwkContextManager(
355         std::string& tizenAppId,
356         Ewk_Context* ewkContext,
357         ViewModule::IViewModulePtr viewModule)
358 {
359     return ContextManagerPtr(new EwkContextManager(tizenAppId, ewkContext, viewModule));
360 }
361
362 }
363