1 // Copyright 2014 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ewk_view_private.h"
10 #include "tizen_webview/public/tw_webview_delegate.h"
11 #include "private/ewk_context_private.h"
12 #include "private/webview_delegate_ewk.h"
15 using tizen_webview::WebView;
16 using tizen_webview::WebViewDelegate;
18 // -------- EwkViewImpl
20 explicit EwkViewImpl(tizen_webview::WebView* wv) : wv_(wv) {}
21 ~EwkViewImpl() { tizen_webview::WebView::Delete(wv_); }
22 tizen_webview::WebView* GetTizenWebView() { return wv_; }
23 const tizen_webview::WebView* GetTizenWebView() const { return wv_; }
25 tizen_webview::WebView* wv_;
26 DISALLOW_COPY_AND_ASSIGN(EwkViewImpl);
29 Evas_Smart_Class parent_smart_class_ = EVAS_SMART_CLASS_INIT_NULL;
33 Evas_Smart* DefaultSmartClassInstance()
35 static Ewk_View_Smart_Class api = EWK_VIEW_SMART_CLASS_INIT_NAME_VERSION(EwkViewSmartClassName);
36 static Evas_Smart* smart = 0;
38 InitSmartClassInterface(api);
39 smart = evas_smart_class_new(&api.sc);
45 void SmartDataChanged(Ewk_View_Smart_Data* d)
50 d->changed.any = true;
51 evas_object_smart_changed(d->self);
54 // Evas_Smart_Class callback interface:
55 void handleEvasObjectAdd(Evas_Object* evas_object)
57 const Evas_Smart* smart = evas_object_smart_smart_get(evas_object);
58 const Evas_Smart_Class* smart_class = evas_smart_class_get(smart);
59 const Ewk_View_Smart_Class* api = reinterpret_cast<const Ewk_View_Smart_Class*>(smart_class);
62 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(evas_object);
65 // Allocating with 'calloc' as the API contract is that it should be deleted with 'free()'.
66 d = static_cast<Ewk_View_Smart_Data*>(calloc(1, sizeof(Ewk_View_Smart_Data)));
67 evas_object_smart_data_set(evas_object, d);
69 d->self = evas_object;
72 parent_smart_class_.add(evas_object);
74 d->priv = 0; // Will be initialized later.
77 // Ewk_View_Smart_Class callback interface:
78 void handleEvasObjectDelete(Evas_Object* evas_object)
80 Ewk_View_Smart_Data* smart_data = GetEwkViewSmartDataFromEvasObject(evas_object);
82 delete smart_data->priv;
83 smart_data->priv = NULL;
85 parent_smart_class_.del(evas_object);
88 void handleEvasObjectShow(Evas_Object* o)
90 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(o);
91 WebView* wv = GetWebViewFromSmartData(d);
95 // WebKit bails here if widget accelerated compositing is used.
96 // TODO: consider this when we will have AC support.
97 if (evas_object_clipees_get(d->base.clipper))
98 evas_object_show(d->base.clipper);
102 void handleEvasObjectHide(Evas_Object* o)
104 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(o);
105 WebView* wv = GetWebViewFromSmartData(d);
109 evas_object_hide(d->base.clipper);
110 // Deleting view by app results in calling hide method.
111 // We assert that, RWHV is null only when renderer has crashed.
112 /*if (!ToEWebView(d)->rwhv()) {
113 DLOG_ASSERT is used because it is controlled by NDEBUG
114 DLOG_ASSERT(ToEWebView(d)->renderer_crashed_);
120 void handleEvasObjectMove(Evas_Object* o, Evas_Coord x, Evas_Coord y)
122 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(o);
123 WebView* wv = GetWebViewFromSmartData(d);
127 wv->HandleMove(x, y);
131 void handleEvasObjectResize(Evas_Object* o, Evas_Coord width, Evas_Coord height)
133 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(o);
134 WebView* wv = GetWebViewFromSmartData(d);
140 wv->HandleResize(width, height);
144 void handleEvasObjectCalculate(Evas_Object* o)
146 Ewk_View_Smart_Data* d = GetEwkViewSmartDataFromEvasObject(o);
147 WebView* wv = GetWebViewFromSmartData(d);
151 Evas_Coord x, y, width, height;
152 evas_object_geometry_get(o, &x, &y, &width, &height);
159 void handleEvasObjectColorSet(Evas_Object*, int red, int green, int blue, int alpha)
164 Eina_Bool handleTextSelectionDown(Ewk_View_Smart_Data* d, int x, int y)
166 WebView* wv = GetWebViewFromSmartData(d);
169 return wv->HandleTextSelectionDown(x, y);
172 Eina_Bool handleTextSelectionUp(Ewk_View_Smart_Data* d, int x, int y)
174 WebView* wv = GetWebViewFromSmartData(d);
177 return wv->HandleTextSelectionUp(x, y);
180 unsigned long long handleExceededDatabaseQuota(Ewk_View_Smart_Data *sd, const char *databaseName, const char *displayName, unsigned long long currentQuota, unsigned long long currentOriginUsage, unsigned long long currentDatabaseUsage, unsigned long long expectedUsage)
182 // Chromium does not support quota per origin right now, this API can't be implemented
190 // --------- public API --------------
192 bool IsWebViewObject(const Evas_Object* evas_object)
198 const char* object_type = evas_object_type_get(evas_object);
199 const Evas_Smart* evas_smart = evas_object_smart_smart_get(evas_object);
201 EINA_LOG_CRIT("%p (%s) is not a smart object!", evas_object,
202 object_type ? object_type : "(null)");
206 const Evas_Smart_Class* smart_class = evas_smart_class_get(evas_smart);
208 EINA_LOG_CRIT("%p (%s) is not a smart class object!", evas_object,
209 object_type ? object_type : "(null)");
213 if (smart_class->data != EwkViewSmartClassName) {
214 EINA_LOG_CRIT("%p (%s) is not of an ewk_view (need %p, got %p)!",
215 evas_object, object_type ? object_type : "(null)",
216 EwkViewSmartClassName, smart_class->data);
222 Ewk_View_Smart_Data* GetEwkViewSmartDataFromEvasObject(const Evas_Object* evas_object)
225 assert(IsWebViewObject(evas_object));
226 return static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(evas_object));
229 Ewk_View_Smart_Data* GetEwkViewSmartDataFromWebView(tizen_webview::WebView* wv) {
231 return GetEwkViewSmartDataFromEvasObject(wv->AsEvasObject());
234 WebView* GetWebViewFromSmartData(const Ewk_View_Smart_Data* smartData)
236 if (smartData && smartData->priv) {
237 return smartData->priv->GetTizenWebView();
242 WebView* GetWebViewFromEvasObject(const Evas_Object* eo) {
243 if (!IsWebViewObject(eo)) {
246 Ewk_View_Smart_Data* sd = GetEwkViewSmartDataFromEvasObject(eo);
247 return GetWebViewFromSmartData(sd);
250 Evas_Object* CreateWebViewAsEvasObject(Ewk_Context* context,
252 Evas_Smart* smart /*= 0*/) {
253 if(!WebViewDelegate::IsDelegateRegistered()) {
254 WebViewDelegate::RegisterDelegate(
255 const_cast<WebViewDelegateEwk*>(&WebViewDelegateEwk::GetInstance()));
258 smart = smart ? smart : DefaultSmartClassInstance();
259 Evas_Object* wv_evas_object = evas_object_smart_add(canvas, smart);
260 WebView* view = WebView::Create(context, wv_evas_object);
264 Ewk_View_Smart_Data* sd = GetEwkViewSmartDataFromEvasObject(wv_evas_object);
266 WebView::Delete(view);
269 // attach webview as a member of smart data
270 sd->priv = new EwkViewImpl(view);
272 return wv_evas_object;
276 bool InitSmartClassInterface(Ewk_View_Smart_Class& api)
278 if (api.version != EWK_VIEW_SMART_CLASS_VERSION) {
279 EINA_LOG_CRIT("Ewk_View_Smart_Class %p is version %lu while %lu was expected.",
280 &api, api.version, EWK_VIEW_SMART_CLASS_VERSION);
284 if (!parent_smart_class_.add)
285 evas_object_smart_clipped_smart_set(&parent_smart_class_);
287 evas_object_smart_clipped_smart_set(&api.sc);
289 // Set Evas_Smart_Class callbacks.
290 api.sc.add = &handleEvasObjectAdd;
291 api.sc.del = &handleEvasObjectDelete;
292 api.sc.move = &handleEvasObjectMove;
293 api.sc.resize = &handleEvasObjectResize;
294 api.sc.show = &handleEvasObjectShow;
295 api.sc.hide = &handleEvasObjectHide;
296 api.sc.calculate = &handleEvasObjectCalculate;
297 api.sc.color_set = &handleEvasObjectColorSet;
299 // Set Ewk_View_Smart_Class callbacks.
300 api.text_selection_down = &handleTextSelectionDown;
301 api.text_selection_up = &handleTextSelectionUp;
303 api.exceeded_database_quota = &handleExceededDatabaseQuota;
306 api.sc.data = EwkViewSmartClassName;