31fe832b19b64de4f6be69690298f36fc26ba12d
[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
59     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
60     {
61         Assert(m_model);
62         if (!m_securityOriginDAO) {
63             LogDebug("initialize securityOriginDAO");
64             m_securityOriginDAO =
65                 SecurityOriginDB::SecurityOriginDAOPtr(
66                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.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
111 SecurityOriginSupport::~SecurityOriginSupport()
112 {
113 }
114
115 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::getSecurityOriginDAO(void)
116 {
117     return m_impl->getSecurityOriginDAO();
118 }
119
120 WrtDB::SettingsType SecurityOriginSupport::isNeedPermissionCheck(
121     SecurityOriginDB::Feature feature)
122 {
123     return m_impl->isNeedPermissionCheck(feature);
124 }
125
126 Evas_Object* SecurityOriginSupportUtil::createPopup(Evas_Object* window,
127                          const char* bodyText,
128                          const char* checkText,
129                          Evas_Smart_Cb buttonCallback,
130                          void* data)
131 {
132     LogDebug("createPopup");
133     Evas_Object* popup = elm_popup_add(window);
134
135     Evas_Object* label = elm_label_add(popup);
136     elm_object_style_set(label, "popup/default");
137     elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
138     elm_object_text_set(label, bodyText);
139     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
140     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
141     evas_object_show(label);
142
143     Evas_Object* layout = elm_layout_add(popup);
144     elm_layout_file_set(layout, DAEMON_EDJ_PATH, "popupWithCheck");
145     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
146
147     Evas_Object* check = elm_check_add(popup);
148     evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
149     evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
150     evas_object_show(check);
151
152     elm_object_part_text_set(layout, "elm.text", checkText);
153     elm_object_part_content_set(layout, "elm.swallow.content", label);
154     elm_object_part_content_set(layout, "elm.swallow.end", check);
155
156     evas_object_show(layout);
157     elm_object_content_set(popup, layout);
158     Evas_Object* btn1 = elm_button_add(popup);
159     elm_object_text_set(btn1, "YES");
160     elm_object_part_content_set(popup, "button1", btn1);
161     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
162     Evas_Object* btn2 = elm_button_add(popup);
163     elm_object_text_set(btn2, "NO");
164     elm_object_part_content_set(popup, "button2", btn2);
165     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
166     return popup;
167 }
168
169 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
170 {
171     Assert(button);
172
173     Evas_Object* popup = button;
174     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
175         popup = elm_object_parent_widget_get(popup);
176         if (!popup) {
177             return NULL;
178         }
179     }
180     return popup;
181 }
182
183 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
184 {
185     Assert(popup);
186     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
187         return NULL;
188     }
189     Evas_Object* check = elm_object_part_content_get(
190         elm_object_content_get(popup),
191         "elm.swallow.end");
192     return check;
193 }
194
195 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(Evas_Object* button)
196 {
197     using namespace SecurityOriginDB;
198
199     Assert(button);
200     // get popup evas_object
201     Evas_Object* popup = getPopup(button);
202     if (popup == NULL) {
203         return RESULT_UNKNOWN;
204     }
205     bool allow = !strcmp("YES", elm_object_text_get(button));
206
207     // get check evas_object
208     Evas_Object* check = getCheck(popup);
209     if (check == NULL) {
210         return RESULT_UNKNOWN;
211     }
212     if (allow) {
213         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS : RESULT_ALLOW_ONCE;
214     } else {
215         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS : RESULT_DENY_ONCE;
216     }
217 }
218 } // namespace ViewModule