b3a7253b4843b1c9fea91a27ee433c46884eeb97
[platform/core/api/maps-service.git] / src / api / maps_condition.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
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 #include <glib.h>
18 #include <unistd.h>
19 #include <system_info.h>                /* system_info_get_platform_bool */
20 #include <maps_util.h>                  /* log */
21 #include <maps_error.h>
22 #include <maps_condition.h>
23 #if (TIZEN_VER < VERSION(3, 0, 0))
24 #include <privacy_checker_client.h>
25 #include <sys/smack.h>
26
27 #define MAPSERVICE_PRIVILEGE    "privilege::tizen::mapservice"
28 #define MAPSERVICE_PERMISSION   "rw"
29 #endif
30 #define MAPS_FEATURE                    "http://tizen.org/feature/maps"
31 #define INTERNET_FEATURE                "http://tizen.org/feature/network.internet"
32
33
34 void *_maps_view_get_maps_service_ptr(maps_view_h view);
35
36
37 #if (TIZEN_VER < VERSION(3, 0, 0))
38 static int __maps_condition_check_privilege(const char *privilege_name, const char *permission)
39 {
40         if (!privilege_name || !permission)
41                 return MAPS_ERROR_INVALID_PARAMETER;
42
43         int ret;
44         char *label = NULL;
45
46         if (smack_new_label_from_self(&label) == -1) {
47                 MAPS_LOGE("smack_new_label_from_self error");
48                 return MAPS_ERROR_INVALID_OPERATION;
49         }
50         ret = smack_have_access(label, privilege_name, permission);
51         if (ret == 1) {
52                 ret = MAPS_ERROR_NONE;
53         } else if (ret == 0) {
54                 ret = MAPS_ERROR_PERMISSION_DENIED;
55         } else {
56                 ret = MAPS_ERROR_INVALID_OPERATION;
57         }
58         g_free(label);
59         return ret;
60 }
61 #endif
62
63 bool maps_condition_check_privilege(void)
64 {
65         static bool is_permitted = true;
66         static bool is_read = false;
67
68         if (!is_read) {
69 #if (TIZEN_VER < VERSION(3, 0, 0))
70                 int ret = __maps_condition_check_privilege(MAPSERVICE_PRIVILEGE, MAPSERVICE_PERMISSION);
71                 is_permitted = (ret == MAPS_ERROR_NONE);
72 #else
73                 /* to check for Tizen 3.x privilege */
74                 is_permitted = (access(MAPS_PLUGINS_PATH_PREFIX, F_OK) != 0) || /* not exist */
75                                (access(MAPS_PLUGINS_PATH_PREFIX, R_OK) == 0);   /* readable */
76 #endif
77                 is_read = true;
78                 MAPS_LOGD("mapservice privilege is%sconsented", (is_permitted ? " " : " not "));
79         }
80         return is_permitted;
81 }
82
83 bool maps_condition_check_maps_feature(void)
84 {
85         static bool is_supported = false;
86         static bool is_read = false;
87
88         if (!is_read) {
89                 system_info_get_platform_bool(MAPS_FEATURE, &is_supported);
90                 MAPS_LOGD("maps feature is%ssupported", (is_supported ? " " : " not "));
91                 is_read = true;
92         }
93         return is_supported;
94 }
95
96 bool maps_condition_check_internet_feature(void)
97 {
98         static bool is_supported = false;
99         static bool is_read = false;
100
101         if (!is_read) {
102                 system_info_get_platform_bool(INTERNET_FEATURE, &is_supported);
103                 MAPS_LOGD("internet feature is%ssupported", (is_supported ? " " : " not "));
104                 is_read = true;
105         }
106         return is_supported;
107 }
108
109 bool maps_condition_check_service_supported(maps_service_h maps, maps_service_e service)
110 {
111         if (!maps)
112                 return false;
113         bool supported = false;
114         if (maps_service_provider_is_service_supported(maps, service, &supported) != MAPS_ERROR_NONE)
115                 return false;
116         return supported;
117 }
118
119 bool maps_condition_check_data_supported(maps_service_h maps, maps_service_data_e data)
120 {
121         if (!maps)
122                 return false;
123         bool supported = false;
124         if (maps_service_provider_is_data_supported(maps, data, &supported) != MAPS_ERROR_NONE)
125                 return false;
126         return supported;
127 }
128
129 bool maps_condition_check_view_service_supported(maps_view_h view, maps_service_e service)
130 {
131         if (!view)
132                 return false;
133         maps_service_h maps = _maps_view_get_maps_service_ptr(view);
134         bool supported = false;
135         if (maps_service_provider_is_service_supported(maps, service, &supported) != MAPS_ERROR_NONE)
136                 return false;
137         return supported;
138 }
139
140 bool maps_condition_check_view_data_supported(maps_view_h view, maps_service_data_e data)
141 {
142         if (!view)
143                 return false;
144         maps_service_h maps = _maps_view_get_maps_service_ptr(view);
145         bool supported = false;
146         if (maps_service_provider_is_data_supported(maps, data, &supported) != MAPS_ERROR_NONE)
147                 return false;
148         return supported;
149 }