[Release] wrt_0.8.213
[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 <dpl/log/log.h>
29 #include <dpl/assert.h>
30 #include <dpl/wrt-dao-ro/common_dao_types.h>
31 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
32 #include <widget_model.h>
33
34 namespace ViewModule {
35 namespace {
36 const char* const DAEMON_EDJ_PATH = "/usr/share/edje/wrt/Daemon.edj";
37 }
38
39 class SecurityOriginSupportImplementation
40 {
41   private:
42     WidgetModel* m_model;
43     SecurityOriginDB::SecurityOriginDAOPtr m_securityOriginDAO;
44
45   public:
46     SecurityOriginSupportImplementation(WidgetModel* widgetModel) :
47         m_model(NULL)
48     {
49         Assert(widgetModel);
50         m_model = widgetModel;
51     }
52
53     ~SecurityOriginSupportImplementation()
54     {}
55
56     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
57     {
58         Assert(m_model);
59         if (!m_securityOriginDAO) {
60             LogDebug("initialize securityOriginDAO");
61             m_securityOriginDAO =
62                 SecurityOriginDB::SecurityOriginDAOPtr(
63                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.
64                                                                 Get()));
65             // initialize security result data. Remove allow, deny for
66             m_securityOriginDAO->removeSecurityOriginData(
67                 SecurityOriginDB::RESULT_ALLOW_ONCE);
68             m_securityOriginDAO->removeSecurityOriginData(
69                 SecurityOriginDB::RESULT_DENY_ONCE);
70         }
71         return m_securityOriginDAO.get();
72     }
73 };
74
75 SecurityOriginSupport::SecurityOriginSupport(WidgetModel* widgetModel) :
76     m_impl(new SecurityOriginSupportImplementation(widgetModel))
77 {}
78
79 SecurityOriginSupport::~SecurityOriginSupport()
80 {}
81
82 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::
83     getSecurityOriginDAO(void)
84 {
85     return m_impl->getSecurityOriginDAO();
86 }
87
88 Evas_Object* SecurityOriginSupportUtil::createPopup(
89     Evas_Object* window,
90     const char* bodyText,
91     const char* checkText,
92     Evas_Smart_Cb
93     buttonCallback,
94     void* data)
95 {
96     LogDebug("createPopup");
97     Evas_Object* popup = elm_popup_add(window);
98
99     Evas_Object* label = elm_label_add(popup);
100     elm_object_style_set(label, "popup/default");
101     elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
102     elm_object_text_set(label, bodyText);
103     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
104     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
105     evas_object_show(label);
106
107     Evas_Object* layout = elm_layout_add(popup);
108     elm_layout_file_set(layout, DAEMON_EDJ_PATH, "popupWithCheck");
109     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
110
111     Evas_Object* check = elm_check_add(popup);
112     evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
113     evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
114     evas_object_show(check);
115
116     elm_object_part_text_set(layout, "elm.text", checkText);
117     elm_object_part_content_set(layout, "elm.swallow.content", label);
118     elm_object_part_content_set(layout, "elm.swallow.end", check);
119
120     evas_object_show(layout);
121     elm_object_content_set(popup, layout);
122     Evas_Object* btn1 = elm_button_add(popup);
123     elm_object_text_set(btn1, "Allow");
124     elm_object_part_content_set(popup, "button1", btn1);
125     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
126     Evas_Object* btn2 = elm_button_add(popup);
127     elm_object_text_set(btn2, "Deny");
128     elm_object_part_content_set(popup, "button2", btn2);
129     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
130     return popup;
131 }
132
133 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
134 {
135     Assert(button);
136
137     Evas_Object* popup = button;
138     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
139         popup = elm_object_parent_widget_get(popup);
140         if (!popup) {
141             return NULL;
142         }
143     }
144     return popup;
145 }
146
147 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
148 {
149     Assert(popup);
150     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
151         return NULL;
152     }
153     Evas_Object* check = elm_object_part_content_get(
154             elm_object_content_get(popup),
155             "elm.swallow.end");
156     return check;
157 }
158
159 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(
160     Evas_Object* button)
161 {
162     using namespace SecurityOriginDB;
163
164     Assert(button);
165     // get popup evas_object
166     Evas_Object* popup = getPopup(button);
167     if (popup == NULL) {
168         return RESULT_UNKNOWN;
169     }
170     bool allow = !strcmp("Allow", elm_object_text_get(button));
171
172     // get check evas_object
173     Evas_Object* check = getCheck(popup);
174     if (check == NULL) {
175         return RESULT_UNKNOWN;
176     }
177     if (allow) {
178         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
179                RESULT_ALLOW_ONCE;
180     } else {
181         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
182                RESULT_DENY_ONCE;
183     }
184 }
185 } // namespace ViewModule