Init Tizen 2.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 <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 <FSysVibrator.h>
32 #include <FUiAnimVisualElement.h>
33 #include <FBaseSysLog.h>
34 #include <FBase_StringConverter.h>
35 #include <FGrp_CoordinateSystem.h>
36 #include <FSecCert_CertService.h>
37 #include <FUi_Control.h>
38 #include <FUi_Window.h>
39 #include "FUiAnim_EflNode.h"
40 #include "FUiAnim_VisualElementImpl.h"
41 #include "FUiAnim_VisualElementSurfaceImpl.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 static const wchar_t ICON_DB_DIRECTORY_PATH[] = L"data/.webkit/iconDatabase/";
62 static const wchar_t ICON_DB_FILE_NAME[] = L"icon.db";
63
64
65 _EflWebkit::_EflWebkit(void)
66         : __pWebFrame(null)
67         , __pContainerVisualElement(null)
68         , __pSurface(null)
69 {
70 }
71
72
73 _EflWebkit::~_EflWebkit(void)
74 {
75         if (__pContainerVisualElement)
76         {
77                 __pContainerVisualElement->SetSurface(null);
78         }
79
80         delete __pSurface;
81         __pSurface = null;
82
83         if (__pWebFrame)
84         {
85                 evas_object_del(__pWebFrame);
86                 __pWebFrame = null;
87         }
88 }
89
90
91 result
92 _EflWebkit::Construct(const Rectangle& rect, VisualElement& containerVisualElement, _Control* pControl)
93 {
94         result r = E_SUCCESS;
95
96         const _VisualElementImpl* pWebVisualElementImpl = _VisualElementImpl::GetInstance(containerVisualElement);
97         SysAssertf(pWebVisualElementImpl, "Failed to get VisualElement of Web control.");
98
99         _EflNode* pEflNode = dynamic_cast< _EflNode* >(pWebVisualElementImpl->GetNativeNode());
100         SysAssertf(pEflNode, "Failed to get native node.");
101
102         Evas* pEvas = pEflNode->GetEvas();
103         SysAssertf(pEvas, "Failed to get Evas.");
104
105         __pWebFrame = ewk_view_add(pEvas);
106         SysTryReturnResult(NID_WEB_CTRL, __pWebFrame, E_SYSTEM, "A system error has been occurred. Failed to create webkit instance.");
107
108         evas_object_pass_events_set(__pWebFrame, EINA_TRUE);
109
110         r = SetWebConfiguration();
111         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
112
113         _ICoordinateSystemTransformer* pXformer = _CoordinateSystem::GetInstance()->GetTransformer();
114         SysAssertf(pXformer, "Failed to get coordinate transformer.");
115
116         evas_object_resize(__pWebFrame, pXformer->TransformHorizontal(rect.width), pXformer->TransformVertical(rect.height));
117         evas_object_move(__pWebFrame, pXformer->TransformHorizontal(rect.x), pXformer->TransformVertical(rect.y));
118
119 #if 0
120         pEflNode->AddNativeSmartObject(containerVisualElement, __pWebFrame);
121 #else
122         __pContainerVisualElement = &containerVisualElement;
123
124         DisplayContext* pDisplayContext = pControl->GetRootWindow()->GetDisplayContext();
125         SysTryReturn(NID_WEB_CTRL, pDisplayContext, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
126
127         __pSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pDisplayContext, (Handle)__pWebFrame, Dimension(rect.width, rect.height));
128         SysTryReturn(NID_WEB_CTRL, __pSurface, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
129
130         containerVisualElement.SetSurface(__pSurface);
131 #endif
132
133         return E_SUCCESS;
134 }
135
136
137 result
138 _EflWebkit::SetWebConfiguration(void) const
139 {
140         result r = E_SUCCESS;
141
142         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
143         SysAssertf(pContext, "Failed to request.");
144
145         Ewk_Settings* pSettings = ewk_view_settings_get(__pWebFrame);
146         SysAssertf(pSettings, "Failed to get webkit instance.");
147
148         ewk_context_cache_model_set(pContext, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
149
150         ewk_settings_text_selection_enabled_set(pSettings, EINA_FALSE);
151         ewk_settings_uses_keypad_without_user_action_set(pSettings, EINA_FALSE);
152
153         ewk_context_additional_plugin_path_set(pContext, PLUGIN_DIRECTORY_PATH);
154
155         String certPath(_CertService::GetCertificateCrtFilePath());
156         std::unique_ptr<char[]> pCertPath(_StringConverter::CopyToCharArrayN(certPath));
157         SysTryReturn(NID_WEB_CTRL, pCertPath.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
158
159         ewk_context_certificate_file_set(pContext, pCertPath.get());
160
161         String iconPath(App::App::GetInstance()->GetAppRootPath() + ICON_DB_DIRECTORY_PATH + ICON_DB_FILE_NAME);
162         std::unique_ptr<char[]> pIconPath(_StringConverter::CopyToCharArrayN(iconPath));
163         SysTryReturn(NID_WEB_CTRL, pIconPath.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164
165         ewk_context_icon_database_path_set(pContext, pIconPath.get());
166
167         InitializeCustomHeader();
168
169         return E_SUCCESS;
170 }
171
172
173 result
174 _EflWebkit::SetProxyAddress(char* pProxy) const
175 {
176         Ewk_Context* pContext = ewk_view_context_get(__pWebFrame);
177         SysAssertf(pContext, "Failed to get context.");
178
179         if (!pProxy || !strlen(pProxy))
180         {
181                 ewk_context_proxy_uri_set(pContext, null);
182         }
183         else
184         {
185                 ewk_context_proxy_uri_set(pContext, pProxy);
186         }
187
188         SysLog(NID_WEB_CTRL, "The current value of proxy is %s", pProxy);
189
190         return E_SUCCESS;
191 }
192
193
194 void
195 _EflWebkit::InitializeCustomHeader(void) const
196 {
197         char pAcceptLang[6] = {0, };
198         char* pLangSet = vconf_get_str(VCONFKEY_LANGSET);
199         const char name[] = "Accept-Language";
200
201         if (pLangSet && strlen(pLangSet))
202         {
203                 strncpy(pAcceptLang, pLangSet, 2);
204
205                 char *pRawData = strchr(pLangSet, '_');
206                 if (pRawData && strlen(pLangSet) >= 5)
207                 {
208                         strcat(pAcceptLang, "-");
209                         strncat(pAcceptLang, pRawData + 1, 2);
210                 }
211         }
212         else
213         {
214                 strcat(pAcceptLang, "en");
215         }
216         free(pLangSet);
217
218         ewk_view_custom_header_add(__pWebFrame, name, pAcceptLang);
219 }
220
221
222 Evas_Object*
223 _EflWebkit::GetWebEvasObject(void) const
224 {
225         return __pWebFrame;
226 }
227
228
229 }}} // Tizen::Web::Controls