Merge "fix: use EINA_* booleans instread of TRUE/FALSE" into tizen
[platform/framework/web/wrt.git] / src / view / common / view_logic_security_origin_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    view_logic_security_origin_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @version 1.0
20  * @brief   Support security origin dao
21  */
22
23 #include "view_logic_security_origin_support.h"
24
25 #include <memory>
26 #include <Evas.h>
27 #include <Elementary.h>
28 #include <efl_assist.h>
29 #include <dpl/assert.h>
30 #include <dpl/log/log.h>
31 #include <dpl/log/secure_log.h>
32 #include <dpl/availability.h>
33 #include <dpl/wrt-dao-ro/common_dao_types.h>
34 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
35 #include <widget_model.h>
36 #include <widget_string.h>
37 #include <common/view_logic_get_parent_window_util.h>
38
39
40 #if (ELM_VERSION_MAJOR == 1 && ELM_VERSION_MINOR == 7)
41 #define POPUP_CLASSNAME "popup"
42 #else
43 //#if ELM_VERSION_MAJOR == 1 && ELM_VERSION_MINOR == 8)
44 #define POPUP_CLASSNAME "elm_popup"
45 #endif
46 namespace ViewModule {
47 namespace {
48 const double MAX_POPUP_HEIGHT = 0.80;
49 const double MAX_SCROLLER_HEIGHT = 0.5;
50
51 struct CallbackData {
52     Evas_Smart_Cb eaKeyCallback;
53 };
54
55 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
56 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
57
58 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
59 {
60     _D("called");
61
62     DPL_UNUSED_PARAM(e);
63     DPL_UNUSED_PARAM(eventInfo);
64
65     CallbackData* callbackData = static_cast<CallbackData*>(data);
66     Assert(obj);
67     if (callbackData) {
68         ea_object_event_callback_del(obj, EA_CALLBACK_BACK, callbackData->eaKeyCallback);
69         delete callbackData;
70     }
71     evas_object_event_callback_del(obj, EVAS_CALLBACK_RESIZE, resizeCallback);
72 }
73 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
74 {
75     _D("called");
76
77     DPL_UNUSED_PARAM(data);
78     DPL_UNUSED_PARAM(e);
79     DPL_UNUSED_PARAM(eventInfo);
80
81     Assert(obj);
82     Evas_Object* popup = obj;
83     int popupH;
84     evas_object_geometry_get(popup, 0, 0, 0, &popupH);
85
86     Evas_Object* parent = PopupUtil::getParentWindow(popup);
87     int parentW, parentH;
88     evas_object_geometry_get(parent, 0, 0, &parentW, &parentH);
89
90     // compare current popup height with screen height.
91     // To avoid popup is filled full screen, used magic number to be filled 80% of screen height.
92     // TODO: Automatically add scroller feature should implement in the elementary
93     double threshold = parentH * MAX_POPUP_HEIGHT;
94     double currentH = popupH;
95     if (threshold < currentH) {
96         _D("text is overflow popup height. add scroller");
97         Evas_Object* layout = elm_object_content_get(obj);
98         Evas_Object* label = elm_object_part_content_get(layout, "elm.swallow.label");
99
100         Evas_Object* scroller = elm_scroller_add(layout);
101         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_ON, ELM_SCROLLER_POLICY_AUTO);
102         evas_object_size_hint_weight_set(scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
103         evas_object_size_hint_align_set(scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
104         elm_scroller_content_min_limit(scroller, EINA_TRUE, EINA_TRUE);
105         int scrollerHeight = parentW > parentH ? parentH : parentW;
106         evas_object_size_hint_max_set(scroller, -1, scrollerHeight * MAX_SCROLLER_HEIGHT);
107
108         elm_object_part_content_unset(layout, "elm.swallow.label");
109         elm_object_content_set(scroller, label);
110         elm_object_part_content_set(layout, "elm.swallow.label", scroller);
111         evas_object_show(layout);
112     }
113 }
114 }
115
116 class SecurityOriginSupportImplementation
117 {
118   private:
119     WidgetModel* m_model;
120     SecurityOriginDB::SecurityOriginDAOPtr m_securityOriginDAO;
121
122   public:
123     SecurityOriginSupportImplementation(WidgetModel* widgetModel) :
124         m_model(NULL)
125     {
126         Assert(widgetModel);
127         m_model = widgetModel;
128     }
129
130     ~SecurityOriginSupportImplementation()
131     {}
132
133     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
134     {
135         Assert(m_model);
136         if (!m_securityOriginDAO) {
137             LogDebug("initialize securityOriginDAO");
138             m_securityOriginDAO =
139                 SecurityOriginDB::SecurityOriginDAOPtr(
140                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.
141                                                                 Get()));
142             // initialize security result data. Remove allow, deny for
143             m_securityOriginDAO->removeSecurityOriginData(
144                 SecurityOriginDB::RESULT_ALLOW_ONCE);
145             m_securityOriginDAO->removeSecurityOriginData(
146                 SecurityOriginDB::RESULT_DENY_ONCE);
147         }
148         return m_securityOriginDAO.get();
149     }
150 };
151
152 SecurityOriginSupport::SecurityOriginSupport(WidgetModel* widgetModel) :
153     m_impl(new SecurityOriginSupportImplementation(widgetModel))
154 {}
155
156 SecurityOriginSupport::~SecurityOriginSupport()
157 {}
158
159 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::
160     getSecurityOriginDAO(void)
161 {
162     return m_impl->getSecurityOriginDAO();
163 }
164
165 Evas_Object* SecurityOriginSupportUtil::createPopup(
166     Evas_Object* window,
167     const char* bodyText,
168     const char* checkText,
169     Evas_Smart_Cb buttonCallback,
170     Evas_Smart_Cb keyCallback,
171     void* data)
172 {
173     LogDebug("createPopup");
174     Evas_Object* parentWindow = PopupUtil::getParentWindow(window);
175     Evas_Object* popup = elm_popup_add(parentWindow);
176
177     CallbackData* callbackData = NULL;
178     if (keyCallback) {
179         callbackData = new CallbackData;
180         callbackData->eaKeyCallback = keyCallback;
181         ea_object_event_callback_add(popup, EA_CALLBACK_BACK, keyCallback, data);
182     }
183     evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, deleteCallback, static_cast<void*>(callbackData));
184     evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, resizeCallback, NULL);
185
186     elm_object_style_set(popup, "popup/default");
187     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
188     evas_object_size_hint_align_set(popup, EVAS_HINT_FILL, EVAS_HINT_FILL);
189
190     Evas_Object* layout = elm_layout_add(popup);
191     elm_layout_file_set(layout, WRT_EDJ_PATH, "popupWithCheck");
192     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
193     evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
194
195     Evas_Object* label = elm_label_add(popup);
196     elm_label_line_wrap_set(label, ELM_WRAP_WORD);
197     elm_object_text_set(label, bodyText);
198     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
199     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
200
201     Evas_Object* check = elm_check_add(layout);
202     elm_object_text_set(check, checkText);
203
204     elm_object_part_content_set(layout, "elm.swallow.label", label);
205     elm_object_part_content_set(layout, "elm.swallow.checkbox", check);
206     elm_object_content_set(popup, layout);
207
208     Evas_Object* btn1 = elm_button_add(popup);
209     elm_object_text_set(btn1, WRT_SK_YES);
210     elm_object_part_content_set(popup, "button1", btn1);
211     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
212     Evas_Object* btn2 = elm_button_add(popup);
213     elm_object_text_set(btn2, WRT_SK_NO);
214     elm_object_part_content_set(popup, "button2", btn2);
215     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
216     return popup;
217 }
218
219 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
220 {
221     Assert(button);
222
223     Evas_Object* popup = button;
224     while (strcmp(elm_object_widget_type_get(popup), POPUP_CLASSNAME )) {
225         popup = elm_object_parent_widget_get(popup);
226         if (!popup) {
227             return NULL;
228         }
229     }
230     return popup;
231 }
232
233 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
234 {
235     Assert(popup);
236     if (strcmp(elm_object_widget_type_get(popup), POPUP_CLASSNAME )) {
237         return NULL;
238     }
239     Evas_Object* check = elm_object_part_content_get(
240             elm_object_content_get(popup),
241             "elm.swallow.checkbox");
242     return check;
243 }
244
245 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(
246     Evas_Object* button)
247 {
248     using namespace SecurityOriginDB;
249
250     Assert(button);
251     // get popup evas_object
252     Evas_Object* popup = getPopup(button);
253     if (popup == NULL) {
254         return RESULT_UNKNOWN;
255     }
256     bool allow = !strcmp(WRT_SK_YES, elm_object_text_get(button));
257
258     // get check evas_object
259     Evas_Object* check = getCheck(popup);
260     if (check == NULL) {
261         return RESULT_UNKNOWN;
262     }
263     if (allow) {
264         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
265                RESULT_ALLOW_ONCE;
266     } else {
267         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
268                RESULT_DENY_ONCE;
269     }
270 }
271 } // namespace ViewModule