Source code formating unification
[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 <vconf.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <dpl/wrt-dao-ro/common_dao_types.h>
32 #include <dpl/wrt-dao-ro/vconf_config.h>
33 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
34 #include <widget_model.h>
35
36 namespace ViewModule {
37 namespace {
38 const char* const DAEMON_EDJ_PATH = "/usr/share/edje/wrt/Daemon.edj";
39 }
40
41 class SecurityOriginSupportImplementation
42 {
43   private:
44     WidgetModel* m_model;
45     SecurityOriginDB::SecurityOriginDAOPtr m_securityOriginDAO;
46
47   public:
48     SecurityOriginSupportImplementation(WidgetModel* widgetModel) :
49         m_model(NULL)
50     {
51         Assert(widgetModel);
52         m_model = widgetModel;
53     }
54
55     ~SecurityOriginSupportImplementation()
56     {}
57
58     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
59     {
60         Assert(m_model);
61         if (!m_securityOriginDAO) {
62             LogDebug("initialize securityOriginDAO");
63             m_securityOriginDAO =
64                 SecurityOriginDB::SecurityOriginDAOPtr(
65                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.
66                                                                 Get()));
67             // initialize security result data. Remove allow, deny for
68             m_securityOriginDAO->removeSecurityOriginData(
69                 SecurityOriginDB::RESULT_ALLOW_ONCE);
70             m_securityOriginDAO->removeSecurityOriginData(
71                 SecurityOriginDB::RESULT_DENY_ONCE);
72         }
73         return m_securityOriginDAO.get();
74     }
75
76     WrtDB::SettingsType isNeedPermissionCheck(
77         SecurityOriginDB::Feature feature)
78     {
79         using namespace SecurityOriginDB;
80         std::string key;
81         if (feature == FEATURE_GEOLOCATION) {
82             key = WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
83                     m_model->TizenId);
84         } else if (feature == FEATURE_WEB_NOTIFICATION) {
85             key = WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
86                     m_model->TizenId);
87         } else if (feature == FEATURE_WEB_DATABASE) {
88             key = WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
89                     m_model->TizenId);
90         } else if (feature == FEATURE_FILE_SYSTEM_ACCESS) {
91             key = WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
92                     m_model->TizenId);
93         } else {
94             Assert("Wrong feature argument is input");
95         }
96
97         int value = 0;
98         vconf_get_int(key.c_str(), &value);
99         LogDebug(key << " = " << value);
100         WrtDB::SettingsType ret = static_cast<WrtDB::SettingsType>(value);
101
102         return ret;
103     }
104 };
105
106 SecurityOriginSupport::SecurityOriginSupport(WidgetModel* widgetModel) :
107     m_impl(new SecurityOriginSupportImplementation(widgetModel))
108 {}
109
110 SecurityOriginSupport::~SecurityOriginSupport()
111 {}
112
113 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::
114     getSecurityOriginDAO(void)
115 {
116     return m_impl->getSecurityOriginDAO();
117 }
118
119 WrtDB::SettingsType SecurityOriginSupport::isNeedPermissionCheck(
120     SecurityOriginDB::Feature feature)
121 {
122     return m_impl->isNeedPermissionCheck(feature);
123 }
124
125 Evas_Object* SecurityOriginSupportUtil::createPopup(
126     Evas_Object* window,
127     const char* bodyText,
128     const char* checkText,
129     Evas_Smart_Cb
130     buttonCallback,
131     void* data)
132 {
133     LogDebug("createPopup");
134     Evas_Object* popup = elm_popup_add(window);
135
136     Evas_Object* label = elm_label_add(popup);
137     elm_object_style_set(label, "popup/default");
138     elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
139     elm_object_text_set(label, bodyText);
140     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
141     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
142     evas_object_show(label);
143
144     Evas_Object* layout = elm_layout_add(popup);
145     elm_layout_file_set(layout, DAEMON_EDJ_PATH, "popupWithCheck");
146     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
147
148     Evas_Object* check = elm_check_add(popup);
149     evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
150     evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
151     evas_object_show(check);
152
153     elm_object_part_text_set(layout, "elm.text", checkText);
154     elm_object_part_content_set(layout, "elm.swallow.content", label);
155     elm_object_part_content_set(layout, "elm.swallow.end", check);
156
157     evas_object_show(layout);
158     elm_object_content_set(popup, layout);
159     Evas_Object* btn1 = elm_button_add(popup);
160     elm_object_text_set(btn1, "YES");
161     elm_object_part_content_set(popup, "button1", btn1);
162     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
163     Evas_Object* btn2 = elm_button_add(popup);
164     elm_object_text_set(btn2, "NO");
165     elm_object_part_content_set(popup, "button2", btn2);
166     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
167     return popup;
168 }
169
170 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
171 {
172     Assert(button);
173
174     Evas_Object* popup = button;
175     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
176         popup = elm_object_parent_widget_get(popup);
177         if (!popup) {
178             return NULL;
179         }
180     }
181     return popup;
182 }
183
184 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
185 {
186     Assert(popup);
187     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
188         return NULL;
189     }
190     Evas_Object* check = elm_object_part_content_get(
191             elm_object_content_get(popup),
192             "elm.swallow.end");
193     return check;
194 }
195
196 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(
197     Evas_Object* button)
198 {
199     using namespace SecurityOriginDB;
200
201     Assert(button);
202     // get popup evas_object
203     Evas_Object* popup = getPopup(button);
204     if (popup == NULL) {
205         return RESULT_UNKNOWN;
206     }
207     bool allow = !strcmp("YES", elm_object_text_get(button));
208
209     // get check evas_object
210     Evas_Object* check = getCheck(popup);
211     if (check == NULL) {
212         return RESULT_UNKNOWN;
213     }
214     if (allow) {
215         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
216                RESULT_ALLOW_ONCE;
217     } else {
218         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
219                RESULT_DENY_ONCE;
220     }
221 }
222 } // namespace ViewModule