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