1b51a8039f647e09a4348719598f6402ed7d8ccb
[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 <EWebKit2.h>
26 #include <net_connection.h>
27 #include <FAppApp.h>
28 #include <FGrpPoint.h>
29 #include <FGrpRectangle.h>
30 #include <FIoDbEnumerator.h>
31 #include <FSysVibrator.h>
32 #include <FUiAnimVisualElement.h>
33 #include <FApp_AppInfo.h>
34 #include <FBaseSysLog.h>
35 #include <FBase_StringConverter.h>
36 #include <FGrp_CoordinateSystem.h>
37 #include <FIo_DirectoryImpl.h>
38 #include <FIo_DatabaseImpl.h>
39 #include <FIo_FileImpl.h>
40 #include <FSecCert_CertService.h>
41 #include <FSys_VibratorImpl.h>
42 #include "FUiAnim_EflNode.h"
43 #include "FUiAnim_VisualElementImpl.h"
44 #include "FWebCtrl_EflWebkit.h"
45 #include "FWebCtrl_Utility.h"
46 #include "FWebCtrl_WebImpl.h"
47
48
49 using namespace Tizen::Base;
50 using namespace Tizen::Graphics;
51 using namespace Tizen::Io;
52 using namespace Tizen::Security::Cert;
53 using namespace Tizen::Ui;
54 using namespace Tizen::Ui::Animations;
55
56
57 namespace Tizen { namespace Web { namespace Controls
58 {
59
60
61 static const char PLUGIN_DIRECTORY_PATH[] =  "/usr/lib/osp/browser-plugin/";
62
63
64 extern const wchar_t CUSTOM_DB_DIRECTORY_PATH[] = L"data/.webkit/customDatabase/";
65 extern const wchar_t USER_CONFIRM_DB_NAME[] = L"userConfirm.db";
66 extern const wchar_t GEOLOCATION_TABLE_NAME[] = L"geolocationPermission";
67 extern const wchar_t CUSTOM_PROTOCOL_TABLE_NAME[] = L"customProtocol";
68 extern const wchar_t CUSTOM_CONTENT_TABLE_NAME[] = L"customContent";
69
70
71 static const int CUSTOM_DB_TABLE_COUNT= 3;
72
73
74 _EflWebkit::_EflWebkit(void)
75         : __pWebFrame(null)
76 {
77 }
78
79
80 _EflWebkit::~_EflWebkit(void)
81 {
82 //      evas_object_smart_member_del(__pWebFrame);
83         evas_object_del(__pWebFrame);
84         __pWebFrame = null;
85 }
86
87
88 result
89 _EflWebkit::Construct(const Rectangle& rect, VisualElement& containerVisualElement)
90 {
91         result r = E_SUCCESS;
92
93         const _VisualElementImpl* pWebVisualElementImpl = _VisualElementImpl::GetInstance(containerVisualElement);
94         SysAssertf(pWebVisualElementImpl, "Failed to get VisualElement of Web control.");
95
96         _EflNode* pEflNode = dynamic_cast< _EflNode* >(pWebVisualElementImpl->GetNativeNode());
97         SysAssertf(pEflNode, "Failed to get native node.");
98
99         Evas* pEvas = pEflNode->GetEvas();
100         SysAssertf(pEvas, "Failed to get Evas.");
101
102         __pWebFrame = ewk_view_add(pEvas);
103         SysTryReturnResult(NID_WEB_CTRL, __pWebFrame, E_SYSTEM, "A system error has been occurred. Failed to create webkit instance.");
104
105         evas_object_pass_events_set(__pWebFrame, EINA_TRUE);
106
107         r = SetWebConfiguration();
108         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         _ICoordinateSystemTransformer* pXformer = _CoordinateSystem::GetInstance()->GetTransformer();
111         SysAssertf(pXformer, "Failed to get coordinate transformer.");
112
113         evas_object_resize(__pWebFrame, pXformer->TransformHorizontal(rect.width), pXformer->TransformVertical(rect.height));
114         evas_object_move(__pWebFrame, pXformer->TransformHorizontal(rect.x), pXformer->TransformVertical(rect.y));
115
116         pEflNode->AddNativeSmartObject(containerVisualElement, __pWebFrame);
117
118         return E_SUCCESS;
119 }
120
121
122 result
123 _EflWebkit::SetWebConfiguration(void) const
124 {
125         result r = E_SUCCESS;
126
127         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
128         SysAssertf(pContext, "Failed to request.");
129
130         Ewk_Settings* pSettings = ewk_view_settings_get(__pWebFrame);
131         SysAssertf(pSettings, "Failed to get webkit instance.");
132
133         ewk_context_cache_model_set(pContext, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
134
135         ewk_settings_text_selection_enabled_set(pSettings, EINA_FALSE);
136
137         ewk_context_additional_plugin_path_set(pContext, PLUGIN_DIRECTORY_PATH);
138
139         String certPath(_CertService::GetCertificateCrtFilePath());
140         std::unique_ptr<char[]> pcertPath(_StringConverter::CopyToCharArrayN(certPath));
141         SysTryReturn(NID_WEB_CTRL, pcertPath.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
142
143         ewk_context_certificate_file_set(pContext, pcertPath.get());
144
145         r = CreateResourceDirectory();
146         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
147
148         r = InitializeCustomDb();
149         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150
151         r = SetProxyAddress();
152         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
153
154         return E_SUCCESS;
155 }
156
157
158 result
159 _EflWebkit::CreateResourceDirectory(void) const
160 {
161         String html5FeaturesPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH);
162
163         if (!_FileImpl::IsFileExist(html5FeaturesPath))
164         {
165                 result r = E_SUCCESS;
166
167                 r = _DirectoryImpl::Create(html5FeaturesPath, true);
168                 SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. create html5 features directory.");
169         }
170
171         return E_SUCCESS;
172 }
173
174
175 result
176 _EflWebkit::InitializeCustomDb(void) const
177 {
178         result r = E_SUCCESS;
179
180         _DatabaseImpl db;
181         String path(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
182         String geolocationTable(GEOLOCATION_TABLE_NAME);
183         String protocolTable(CUSTOM_PROTOCOL_TABLE_NAME);
184         String contentTable(CUSTOM_CONTENT_TABLE_NAME);
185
186         r = db.Construct(path, "a+", null);
187         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         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"')"));
190         if (pEnum.get())
191         {
192                 int count = 0;
193
194                 r = pEnum->MoveNext();
195                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
196
197                 r = pEnum->GetIntAt(0, count);
198                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
199
200                 if (count == CUSTOM_DB_TABLE_COUNT)
201                 {
202                         return E_SUCCESS;
203                 }
204         }
205
206         pEnum.reset();
207         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + geolocationTable + L"'"));
208         if (!pEnum.get())
209         {
210                 r = db.ExecuteSql(
211                         L"CREATE TABLE IF NOT EXISTS " + geolocationTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, origin TEXT, permission INTEGER)",
212                         true);
213                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
214         }
215
216         pEnum.reset();
217         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + protocolTable + L"'"));
218         if (!pEnum.get())
219         {
220                 r = db.ExecuteSql(
221                         L"CREATE TABLE IF NOT EXISTS " + protocolTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, url TEXT, mime TEXT, allow INTEGER)",
222                         true);
223                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
224         }
225
226         pEnum.reset();
227         pEnum = std::unique_ptr<DbEnumerator>(db.QueryN(L"Select name from sqlite_master Where type='table' And name = '" + contentTable + L"'"));
228         if (!pEnum.get())
229         {
230                 r = db.ExecuteSql(
231                         L"CREATE TABLE IF NOT EXISTS " + contentTable + L"(id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, url TEXT, mime TEXT, allow INTEGER)",
232                         true);
233                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
234         }
235
236         return E_SUCCESS;
237 }
238
239
240 result
241 _EflWebkit::SetProxyAddress(void) const
242 {
243         int ret = -1;
244         connection_h handle = null;
245
246         ret = connection_create(&handle);
247         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to create connection.");
248
249         char* pProxy = null;
250         connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
251
252         ret = connection_get_proxy(handle, family, &pProxy);
253         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to get proxy address.");
254
255         ret = connection_destroy(handle);
256         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to destroy connection.");
257
258         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
259         SysAssertf(pContext, "Failed to get context.");
260
261         if (!pProxy || !strlen(pProxy))
262         {
263                 ewk_context_proxy_uri_set(pContext, null);
264         }
265         else
266         {
267                 ewk_context_proxy_uri_set(pContext, pProxy);
268         }
269
270         SysLog(NID_WEB_CTRL, "The current value of proxy is %s", pProxy);
271
272         if (pProxy)
273         {
274                 free(pProxy);
275         }
276
277         return E_SUCCESS;
278 }
279
280
281 Evas_Object*
282 _EflWebkit::GetWebEvasObject(void) const
283 {
284         return __pWebFrame;
285 }
286
287
288 }}} // Tizen::Web::Controls