[Release] wrt_0.8.166 for tizen_2.1 branch
[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 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     char *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
94     if (proxy && strlen(proxy) && strcmp(proxy, "0.0.0.0")) {
95         LogInfo("proxy address: " << proxy);
96         ewk_context_proxy_uri_set(m_ewkContext,  proxy);
97     } else {
98         LogInfo("proxy address is empty");
99         ewk_context_proxy_uri_set(m_ewkContext, NULL);
100     }
101
102     if (proxy) {
103         free(proxy);
104     }
105
106     ewk_context_certificate_file_set(m_ewkContext, caCertPath.c_str());
107
108     // set local stroage database path
109     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
110     ewk_context_web_storage_path_set(
111             m_ewkContext,
112             dao.getPrivateLocalStoragePath().c_str());
113
114     // memory saving mode
115     int result;
116     vconf_get_int(
117             WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
118                 DPL::FromUTF8String(m_appId)).c_str(), &result);
119
120     ewk_context_memory_saving_mode_set(
121             m_ewkContext,
122             static_cast<WrtDB::SettingsType>(result) ==
123             WrtDB::SETTINGS_TYPE_ON ? EINA_TRUE : EINA_FALSE);
124
125     setAutoFullscreenMode();
126     setBackgroundSupport();
127
128     // ewk storage_path set
129     ewk_context_storage_path_reset(m_ewkContext);
130
131     m_initialized = true;
132
133     return true;
134 }
135
136 void EwkContextManager::destroy()
137 {
138     // only in the following case, webkit context should be deleted
139     if (m_initialized && m_isInternalContext) {
140         ewk_context_delete(m_ewkContext);
141     }
142 }
143
144 void EwkContextManager::setCallbacks()
145 {
146     if (!m_initialized) {
147         return;
148     }
149
150     ewk_context_message_from_injected_bundle_callback_set(
151             m_ewkContext,
152             messageFromInjectedBundleCallback,
153             this);
154
155     ewk_context_did_start_download_callback_set(
156             m_ewkContext,
157             didStartDownloadCallback,
158             this);
159
160     ewk_context_vibration_client_callbacks_set(
161             m_ewkContext,
162             vibrationClientStartCallback,
163             vibrationClientStopCallback,
164             this);
165 }
166
167 void EwkContextManager::setAutoFullscreenMode()
168 {
169     using namespace WrtDB;
170     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
171     if(!m_ewkContext) {
172         return;
173     }
174     std::list<DPL::String> widgetPrivilege = dao.getWidgetPrivilege();
175     bool fullscreen = false;
176
177     FOREACH(it, widgetPrivilege) {
178         std::map<std::string, Feature>::const_iterator result =
179         g_W3CPrivilegeTextMap.find(DPL::ToUTF8String(*it));
180             if (result != g_W3CPrivilegeTextMap.end()) {
181                 if (result->second == FEATURE_FULLSCREEN_MODE) {
182                     fullscreen = true;
183                     break;
184             }
185         }
186     }
187     ewk_context_tizen_extensible_api_set(m_ewkContext,
188         EWK_EXTENSIBLE_API_FULL_SCREEN,
189         fullscreen);
190 }
191
192 void EwkContextManager::setBackgroundSupport()
193 {
194     WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_appId));
195     if(!m_ewkContext) {
196         return;
197     }
198
199     WidgetSettings widgetSettings;
200     dao.getWidgetSettings(widgetSettings);
201     WidgetSettingList settings(widgetSettings);
202     bool backgroundSupport = false;
203     if(settings.getBackgroundSupport() == BackgroundSupport_Enable) {
204         backgroundSupport = true;
205     }
206     ewk_context_tizen_extensible_api_set(
207         m_ewkContext,
208         EWK_EXTENSIBLE_API_BACKGROUND_MUSIC,
209         backgroundSupport);
210 }
211
212 void EwkContextManager::unsetCallbacks()
213 {
214     if (!m_initialized) {
215         return;
216     }
217
218     ewk_context_message_from_injected_bundle_callback_set(
219             m_ewkContext, NULL, NULL);
220     ewk_context_did_start_download_callback_set(
221             m_ewkContext, NULL, NULL);
222     ewk_context_vibration_client_callbacks_set(
223             m_ewkContext, NULL, NULL, NULL);
224 }
225
226
227 void EwkContextManager::messageFromInjectedBundleCallback(
228         const char* name,
229         const char* body,
230         char** returnData,
231         void* clientInfo)
232 {
233     LogDebug("enter");
234     LogDebug("did recive message: " << name);
235
236     EwkContextManager* This = static_cast<EwkContextManager*>(clientInfo);
237     This->m_view->checkSyncMessageFromBundle(name, body, returnData);
238 }
239
240 void EwkContextManager::didStartDownloadCallback(const char* downloadUrl, void* data)
241 {
242     LogDebug("enter");
243     LogDebug("download url: " << downloadUrl);
244
245     EwkContextManager* This = static_cast<EwkContextManager*>(data);
246     This->m_view->downloadData(downloadUrl);
247 }
248
249 void EwkContextManager::vibrationClientStartCallback(uint64_t time, void* data)
250 {
251     LogDebug("enter");
252
253     EwkContextManager* This = static_cast<EwkContextManager*>(data);
254     This->m_view->activateVibration(true, static_cast<long>(time));
255 }
256
257 void EwkContextManager::vibrationClientStopCallback(void* data)
258 {
259     LogDebug("enter");
260
261     EwkContextManager* This = static_cast<EwkContextManager*>(data);
262     This->m_view->activateVibration(false, 0);
263 }
264
265 void EwkContextManager::handleLowMemory()
266 {
267     if (!m_ewkContext) {
268         return;
269     }
270
271     //ewk_context_cache_clear(m_ewkContext);
272     //ewk_context_notify_low_memory(m_ewkContext);
273 }
274
275 ContextManagerPtr createEwkContextManager(
276         std::string& tizenAppId,
277         Ewk_Context* ewkContext,
278         ViewModule::IViewModulePtr viewModule)
279 {
280     return ContextManagerPtr(new EwkContextManager(tizenAppId, ewkContext, viewModule));
281 }
282
283 }
284