apply font setting
[framework/osp/web.git] / src / controls / FWebCtrl_EflWebkit.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_EflWebkit.cpp
20  * @brief               The file contains the definition of _EflWebkit class.
21  *
22  * The file contains the definition of _EflWebkit class.
23  */
24 #include <unique_ptr.h>
25 #include <vconf.h>
26 #include <EWebKit2.h>
27 #include <net_connection.h>
28 #include <FAppApp.h>
29 #include <FGrpPoint.h>
30 #include <FGrpRectangle.h>
31 #include <FIoDbEnumerator.h>
32 #include <FSysVibrator.h>
33 #include <FUiAnimVisualElement.h>
34 #include <FApp_AppInfo.h>
35 #include <FBaseSysLog.h>
36 #include <FBase_StringConverter.h>
37 #include <FGrp_CoordinateSystem.h>
38 #include <FIo_DirectoryImpl.h>
39 #include <FIo_DatabaseImpl.h>
40 #include <FIo_FileImpl.h>
41 #include <FSecCert_CertService.h>
42 #include <FSys_VibratorImpl.h>
43 #include <FUi_Control.h>
44 #include <FUi_Window.h>
45 #include "FUiAnim_EflNode.h"
46 #include "FUiAnim_VisualElementImpl.h"
47 #include "FUiAnim_VisualElementSurfaceImpl.h"
48 #include "FWebCtrl_EflWebkit.h"
49 #include "FWebCtrl_Utility.h"
50 #include "FWebCtrl_WebImpl.h"
51
52
53 using namespace Tizen::Base;
54 using namespace Tizen::Graphics;
55 using namespace Tizen::Io;
56 using namespace Tizen::Security::Cert;
57 using namespace Tizen::Ui;
58 using namespace Tizen::Ui::Animations;
59
60
61 namespace Tizen { namespace Web { namespace Controls
62 {
63
64
65 static const char PLUGIN_DIRECTORY_PATH[] =  "/usr/lib/osp/browser-plugin/";
66
67
68 extern const wchar_t CUSTOM_DB_DIRECTORY_PATH[] = L"data/.webkit/customDatabase/";
69 extern const wchar_t USER_CONFIRM_DB_NAME[] = L"userConfirm.db";
70 extern const wchar_t GEOLOCATION_TABLE_NAME[] = L"geolocationPermission";
71 extern const wchar_t CUSTOM_PROTOCOL_TABLE_NAME[] = L"customProtocol";
72 extern const wchar_t CUSTOM_CONTENT_TABLE_NAME[] = L"customContent";
73 extern const wchar_t CERTIFICATE_TABLE_NAME[] = L"certificate";
74
75
76 static const wchar_t ICON_DB_DIRECTORY_PATH[] = L"data/.webkit/iconDatabase/";
77 static const wchar_t ICON_DB_FILE_NAME[] = L"icon.db";
78
79
80 static const int CUSTOM_DB_TABLE_COUNT= 4;
81
82
83 _EflWebkit::_EflWebkit(void)
84         : __pWebFrame(null)
85         , __pContainerVisualElement(null)
86         , __pSurface(null)
87 {
88 }
89
90
91 _EflWebkit::~_EflWebkit(void)
92 {
93         if (__pContainerVisualElement)
94         {
95                 __pContainerVisualElement->SetSurface(null);
96         }
97         delete __pSurface;
98 //      evas_object_smart_member_del(__pWebFrame);
99         evas_object_del(__pWebFrame);
100         __pWebFrame = null;
101 }
102
103
104 result
105 _EflWebkit::Construct(const Rectangle& rect, VisualElement& containerVisualElement, _Control* pControl)
106 {
107         result r = E_SUCCESS;
108
109         const _VisualElementImpl* pWebVisualElementImpl = _VisualElementImpl::GetInstance(containerVisualElement);
110         SysAssertf(pWebVisualElementImpl, "Failed to get VisualElement of Web control.");
111
112         _EflNode* pEflNode = dynamic_cast< _EflNode* >(pWebVisualElementImpl->GetNativeNode());
113         SysAssertf(pEflNode, "Failed to get native node.");
114
115         Evas* pEvas = pEflNode->GetEvas();
116         SysAssertf(pEvas, "Failed to get Evas.");
117
118         __pWebFrame = ewk_view_add(pEvas);
119         SysTryReturnResult(NID_WEB_CTRL, __pWebFrame, E_SYSTEM, "A system error has been occurred. Failed to create webkit instance.");
120
121         evas_object_pass_events_set(__pWebFrame, EINA_TRUE);
122
123         r = SetWebConfiguration();
124         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
125
126         _ICoordinateSystemTransformer* pXformer = _CoordinateSystem::GetInstance()->GetTransformer();
127         SysAssertf(pXformer, "Failed to get coordinate transformer.");
128
129         evas_object_resize(__pWebFrame, pXformer->TransformHorizontal(rect.width), pXformer->TransformVertical(rect.height));
130         evas_object_move(__pWebFrame, pXformer->TransformHorizontal(rect.x), pXformer->TransformVertical(rect.y));
131
132 #if 0
133         pEflNode->AddNativeSmartObject(containerVisualElement, __pWebFrame);
134 #else
135         __pContainerVisualElement = &containerVisualElement;
136
137         DisplayContext* pDisplayContext = pControl->GetRootWindow()->GetDisplayContext();
138         SysTryReturn(NID_WEB_CTRL, pDisplayContext, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
139
140         __pSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pDisplayContext, (Handle)__pWebFrame, Dimension(rect.width, rect.height));
141         SysTryReturn(NID_WEB_CTRL, __pSurface, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
142
143         containerVisualElement.SetSurface(__pSurface);
144 #endif
145
146         return E_SUCCESS;
147 }
148
149
150 result
151 _EflWebkit::SetWebConfiguration(void) const
152 {
153         result r = E_SUCCESS;
154
155         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
156         SysAssertf(pContext, "Failed to request.");
157
158         Ewk_Settings* pSettings = ewk_view_settings_get(__pWebFrame);
159         SysAssertf(pSettings, "Failed to get webkit instance.");
160
161         ewk_context_cache_model_set(pContext, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
162
163         ewk_settings_text_selection_enabled_set(pSettings, EINA_FALSE);
164         ewk_settings_uses_keypad_without_user_action_set(pSettings, EINA_FALSE);
165
166         ewk_context_additional_plugin_path_set(pContext, PLUGIN_DIRECTORY_PATH);
167
168         String certPath(_CertService::GetCertificateCrtFilePath());
169         std::unique_ptr<char[]> pcertPath(_StringConverter::CopyToCharArrayN(certPath));
170         SysTryReturn(NID_WEB_CTRL, pcertPath.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
171
172         ewk_context_certificate_file_set(pContext, pcertPath.get());
173
174         String iconPath(App::App::GetInstance()->GetAppRootPath() + ICON_DB_DIRECTORY_PATH + ICON_DB_FILE_NAME);
175         std::unique_ptr<char[]> pIconPath(_StringConverter::CopyToCharArrayN(iconPath));
176         SysTryReturn(NID_WEB_CTRL, pIconPath.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
177
178         ewk_context_icon_database_path_set(pContext, pIconPath.get());
179
180         r = CreateResourceDirectory();
181         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
182
183         r = InitializeCustomDb();
184         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
185
186         InitializeCustomHeader();
187
188         return E_SUCCESS;
189 }
190
191
192 result
193 _EflWebkit::CreateResourceDirectory(void) const
194 {
195         String html5FeaturesPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH);
196
197         if (!_FileImpl::IsFileExist(html5FeaturesPath))
198         {
199                 result r = E_SUCCESS;
200
201                 r = _DirectoryImpl::Create(html5FeaturesPath, true);
202                 SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. create html5 features directory.");
203         }
204
205         return E_SUCCESS;
206 }
207
208
209 result
210 _EflWebkit::InitializeCustomDb(void) const
211 {
212         result r = E_SUCCESS;
213
214         _DatabaseImpl db;
215         String path(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
216         String geolocationTable(GEOLOCATION_TABLE_NAME);
217         String protocolTable(CUSTOM_PROTOCOL_TABLE_NAME);
218         String contentTable(CUSTOM_CONTENT_TABLE_NAME);
219         String certificateTable(CERTIFICATE_TABLE_NAME);
220
221         r = db.Construct(path, "a+", null);
222         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
223
224         std::unique_ptr<DbEnumerator>   pEnum(db.QueryN(L"Select count(name) from sqlite_master Where type='table' And name in ('" + geolocationTable + L"', '" + protocolTable + L"', '" + contentTable + L"', '" + certificateTable + L"')"));
225         if (pEnum.get())
226         {
227                 int count = 0;
228
229                 r = pEnum->MoveNext();
230                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
231
232                 r = pEnum->GetIntAt(0, count);
233                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
234
235                 if (count == CUSTOM_DB_TABLE_COUNT)
236                 {
237                         return E_SUCCESS;
238                 }
239         }
240
241         pEnum.reset();
242         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + geolocationTable + L"'"));
243         if (!pEnum.get())
244         {
245                 r = db.ExecuteSql(
246                         L"CREATE TABLE IF NOT EXISTS " + geolocationTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, origin TEXT, permission INTEGER)",
247                         true);
248                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
249         }
250
251         pEnum.reset();
252         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + protocolTable + L"'"));
253         if (!pEnum.get())
254         {
255                 r = db.ExecuteSql(
256                         L"CREATE TABLE IF NOT EXISTS " + protocolTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, url TEXT, mime TEXT, allow INTEGER)",
257                         true);
258                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
259         }
260
261         pEnum.reset();
262         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + contentTable + L"'"));
263         if (!pEnum.get())
264         {
265                 r = db.ExecuteSql(
266                         L"CREATE TABLE IF NOT EXISTS " + contentTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, url TEXT, mime TEXT, allow INTEGER)",
267                         true);
268                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
269         }
270
271         pEnum.reset();
272         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + certificateTable + L"'"));
273         if (!pEnum.get())
274         {
275                 r = db.ExecuteSql(
276                         L"CREATE TABLE IF NOT EXISTS " + certificateTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, pem TEXT, allow INTEGER)",
277                         true);
278                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
279         }
280
281         return E_SUCCESS;
282 }
283
284
285 result
286 _EflWebkit::SetProxyAddress(char* pProxy) const
287 {
288         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
289         SysAssertf(pContext, "Failed to get context.");
290
291         if (!pProxy || !strlen(pProxy))
292         {
293                 ewk_context_proxy_uri_set(pContext, null);
294         }
295         else
296         {
297                 ewk_context_proxy_uri_set(pContext, pProxy);
298         }
299
300         SysLog(NID_WEB_CTRL, "The current value of proxy is %s", pProxy);
301
302         return E_SUCCESS;
303 }
304
305
306 void
307 _EflWebkit::InitializeCustomHeader(void) const
308 {
309         char pAcceptLang[6] = {0, };
310         char* pLangSet = vconf_get_str(VCONFKEY_LANGSET);
311         const char name[] = "Accept-Language";
312
313         if (pLangSet && strlen(pLangSet))
314         {
315                 strncpy(pAcceptLang, pLangSet, 2);
316
317                 char *pRawData = strchr(pLangSet, '_');
318                 if (pRawData && strlen(pLangSet) >= 5)
319                 {
320                         strcat(pAcceptLang, "-");
321                         strncat(pAcceptLang, pRawData + 1, 2);
322                 }
323         }
324         else
325         {
326                 strcat(pAcceptLang, "en");
327         }
328         free(pLangSet);
329
330         ewk_view_custom_header_add(__pWebFrame, name, pAcceptLang);
331 }
332
333
334 Evas_Object*
335 _EflWebkit::GetWebEvasObject(void) const
336 {
337         return __pWebFrame;
338 }
339
340
341 }}} // Tizen::Web::Controls