dcebefc976e6c9aeccd96282dc85d068c6b5740c
[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/unused.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 namespace ViewModule {
40 namespace {
41 const double MAX_POPUP_HEIGHT = 0.80;
42 const double MAX_SCROLLER_HEIGHT = 0.5;
43
44 struct CallbackData {
45     Evas_Smart_Cb eaKeyCallback;
46 };
47
48 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
49 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
50
51 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
52 {
53     _D("called");
54
55     DPL_UNUSED_PARAM(e);
56     DPL_UNUSED_PARAM(eventInfo);
57
58     CallbackData* callbackData = static_cast<CallbackData*>(data);
59     Assert(obj);
60     if (callbackData) {
61         ea_object_event_callback_del(obj, EA_CALLBACK_BACK, callbackData->eaKeyCallback);
62         delete callbackData;
63     }
64     evas_object_event_callback_del(obj, EVAS_CALLBACK_RESIZE, resizeCallback);
65 }
66 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
67 {
68     _D("called");
69
70     DPL_UNUSED_PARAM(data);
71     DPL_UNUSED_PARAM(e);
72     DPL_UNUSED_PARAM(eventInfo);
73
74     Assert(obj);
75     Evas_Object* popup = obj;
76     int popupH;
77     evas_object_geometry_get(popup, 0, 0, 0, &popupH);
78
79     Evas_Object* parent = PopupUtil::getParentWindow(popup);
80     int parentW, parentH;
81     evas_object_geometry_get(parent, 0, 0, &parentW, &parentH);
82
83     // compare current popup height with screen height.
84     // To avoid popup is filled full screen, used magic number to be filled 80% of screen height.
85     // TODO: Automatically add scroller feature should implement in the elementary
86     double threshold = parentH * MAX_POPUP_HEIGHT;
87     double currentH = popupH;
88     if (threshold < currentH) {
89         _D("text is overflow popup height. add scroller");
90         Evas_Object* layout = elm_object_content_get(obj);
91         Evas_Object* label = elm_object_part_content_get(layout, "elm.swallow.label");
92
93         Evas_Object* scroller = elm_scroller_add(layout);
94         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_ON, ELM_SCROLLER_POLICY_AUTO);
95         evas_object_size_hint_weight_set(scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
96         evas_object_size_hint_align_set(scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
97         elm_scroller_content_min_limit(scroller, EINA_TRUE, EINA_TRUE);
98         int scrollerHeight = parentW > parentH ? parentH : parentW;
99         evas_object_size_hint_max_set(scroller, -1, scrollerHeight * MAX_SCROLLER_HEIGHT);
100
101         elm_object_part_content_unset(layout, "elm.swallow.label");
102         elm_object_content_set(scroller, label);
103         elm_object_part_content_set(layout, "elm.swallow.label", scroller);
104         evas_object_show(layout);
105     }
106 }
107 }
108
109 class SecurityOriginSupportImplementation
110 {
111   private:
112     WidgetModel* m_model;
113     SecurityOriginDB::SecurityOriginDAOPtr m_securityOriginDAO;
114
115   public:
116     SecurityOriginSupportImplementation(WidgetModel* widgetModel) :
117         m_model(NULL)
118     {
119         Assert(widgetModel);
120         m_model = widgetModel;
121     }
122
123     ~SecurityOriginSupportImplementation()
124     {}
125
126     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
127     {
128         Assert(m_model);
129         if (!m_securityOriginDAO) {
130             LogDebug("initialize securityOriginDAO");
131             m_securityOriginDAO =
132                 SecurityOriginDB::SecurityOriginDAOPtr(
133                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.
134                                                                 Get()));
135             // initialize security result data. Remove allow, deny for
136             m_securityOriginDAO->removeSecurityOriginData(
137                 SecurityOriginDB::RESULT_ALLOW_ONCE);
138             m_securityOriginDAO->removeSecurityOriginData(
139                 SecurityOriginDB::RESULT_DENY_ONCE);
140         }
141         return m_securityOriginDAO.get();
142     }
143 };
144
145 SecurityOriginSupport::SecurityOriginSupport(WidgetModel* widgetModel) :
146     m_impl(new SecurityOriginSupportImplementation(widgetModel))
147 {}
148
149 SecurityOriginSupport::~SecurityOriginSupport()
150 {}
151
152 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::
153     getSecurityOriginDAO(void)
154 {
155     return m_impl->getSecurityOriginDAO();
156 }
157
158 Evas_Object* SecurityOriginSupportUtil::createPopup(
159     Evas_Object* window,
160     const char* bodyText,
161     const char* checkText,
162     Evas_Smart_Cb buttonCallback,
163     Evas_Smart_Cb keyCallback,
164     void* data)
165 {
166     LogDebug("createPopup");
167     Evas_Object* parentWindow = PopupUtil::getParentWindow(window);
168     Evas_Object* popup = elm_popup_add(parentWindow);
169
170     CallbackData* callbackData = NULL;
171     if (keyCallback) {
172         CallbackData* callbackData = new CallbackData;
173         callbackData->eaKeyCallback = keyCallback;
174         ea_object_event_callback_add(popup, EA_CALLBACK_BACK, keyCallback, data);
175     }
176     evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, deleteCallback, static_cast<void*>(callbackData));
177     evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, resizeCallback, NULL);
178
179     elm_object_style_set(popup, "popup/default");
180     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
181     evas_object_size_hint_align_set(popup, EVAS_HINT_FILL, EVAS_HINT_FILL);
182
183     Evas_Object* layout = elm_layout_add(popup);
184     elm_layout_file_set(layout, WRT_EDJ_PATH, "popupWithCheck");
185     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186     evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
187
188     Evas_Object* label = elm_label_add(popup);
189     elm_label_line_wrap_set(label, ELM_WRAP_WORD);
190     elm_object_text_set(label, bodyText);
191     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
192     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
193
194     Evas_Object* check = elm_check_add(layout);
195     elm_object_text_set(check, checkText);
196
197     elm_object_part_content_set(layout, "elm.swallow.label", label);
198     elm_object_part_content_set(layout, "elm.swallow.checkbox", check);
199     elm_object_content_set(popup, layout);
200
201     Evas_Object* btn1 = elm_button_add(popup);
202     elm_object_text_set(btn1, WRT_SK_YES);
203     elm_object_part_content_set(popup, "button1", btn1);
204     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
205     Evas_Object* btn2 = elm_button_add(popup);
206     elm_object_text_set(btn2, WRT_SK_NO);
207     elm_object_part_content_set(popup, "button2", btn2);
208     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
209     return popup;
210 }
211
212 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
213 {
214     Assert(button);
215
216     Evas_Object* popup = button;
217     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
218         popup = elm_object_parent_widget_get(popup);
219         if (!popup) {
220             return NULL;
221         }
222     }
223     return popup;
224 }
225
226 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
227 {
228     Assert(popup);
229     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
230         return NULL;
231     }
232     Evas_Object* check = elm_object_part_content_get(
233             elm_object_content_get(popup),
234             "elm.swallow.checkbox");
235     return check;
236 }
237
238 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(
239     Evas_Object* button)
240 {
241     using namespace SecurityOriginDB;
242
243     Assert(button);
244     // get popup evas_object
245     Evas_Object* popup = getPopup(button);
246     if (popup == NULL) {
247         return RESULT_UNKNOWN;
248     }
249     bool allow = !strcmp(WRT_SK_YES, elm_object_text_get(button));
250
251     // get check evas_object
252     Evas_Object* check = getCheck(popup);
253     if (check == NULL) {
254         return RESULT_UNKNOWN;
255     }
256     if (allow) {
257         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
258                RESULT_ALLOW_ONCE;
259     } else {
260         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
261                RESULT_DENY_ONCE;
262     }
263 }
264 } // namespace ViewModule