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