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