• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "common_event_permission_manager.h"
17 
18 #include <algorithm>
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include "common_event_support.h"
25 #include "event_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace EventFwk {
29 constexpr size_t REVERSE = 3;
30 
31 static const std::unordered_map<std::string, std::pair<PermissionState, std::vector<std::string>>> COMMON_EVENT_MAP {
32     {CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED,
33         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
34     },
35     {CommonEventSupport::COMMON_EVENT_LOCKED_BOOT_COMPLETED,
36         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
37     },
38     {CommonEventSupport::COMMON_EVENT_USER_SWITCHED,
39         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
40     },
41     {CommonEventSupport::COMMON_EVENT_USER_STARTING,
42         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
43     },
44     {CommonEventSupport::COMMON_EVENT_USER_STOPPING,
45         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
46     },
47     {CommonEventSupport::COMMON_EVENT_WIFI_SCAN_FINISHED,
48         {PermissionState::DEFAULT, {"ohos.permission.LOCATION"}}
49     },
50     {CommonEventSupport::COMMON_EVENT_WIFI_RSSI_VALUE,
51         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
52     },
53     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_JOIN,
54         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
55     },
56     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_LEAVE,
57         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
58     },
59     {CommonEventSupport::COMMON_EVENT_WIFI_MPLINK_STATE_CHANGE,
60         {PermissionState::DEFAULT, {"ohos.permission.MPLINK_CHANGE_STATE"}}
61     },
62     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CONN_STATE,
63         {PermissionState::AND, {"ohos.permission.GET_WIFI_INFO", "ohos.permission.LOCATION"}}
64     },
65     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED,
66         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
67     },
68     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED,
69         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
70     },
71     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED,
72         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
73     },
74     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED,
75         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
76     },
77     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED,
78         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
79     },
80     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CONNECT_STATE_UPDATE,
81         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
82     },
83     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CURRENT_DEVICE_UPDATE,
84         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
85     },
86     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_AUDIO_STATE_UPDATE,
87         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
88     },
89     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CONNECT_STATE_UPDATE,
90         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
91     },
92     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CURRENT_DEVICE_UPDATE,
93         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
94     },
95     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_PLAYING_STATE_UPDATE,
96         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
97     },
98     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CODEC_VALUE_UPDATE,
99         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
100     },
101     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_DISCOVERED,
102         {PermissionState::AND, {"ohos.permission.USE_BLUETOOTH", "ohos.permission.LOCATION"}}
103     },
104     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CLASS_VALUE_UPDATE,
105         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
106     },
107     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_CONNECTED,
108         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
109     },
110     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_DISCONNECTED,
111         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
112     },
113     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_NAME_UPDATE,
114         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
115     },
116     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIR_STATE,
117         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
118     },
119     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_BATTERY_VALUE_UPDATE,
120         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
121     },
122     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_UUID_VALUE,
123         {PermissionState::DEFAULT, {"ohos.permission.DISCOVER_BLUETOOTH"}}
124     },
125     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIRING_REQ,
126         {PermissionState::DEFAULT, {"ohos.permission.DISCOVER_BLUETOOTH"}}
127     },
128     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE,
129         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
130     },
131     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_REQ_ENABLE,
132         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
133     },
134     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_REQ_DISABLE,
135         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
136     },
137     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_SCAN_MODE_UPDATE,
138         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
139     },
140     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_STARTED,
141         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
142     },
143     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_FINISHED,
144         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
145     },
146     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_NAME_UPDATE,
147         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
148     },
149     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_CONNECT_STATE_UPDATE,
150         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
151     },
152     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_PLAYING_STATE_UPDATE,
153         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
154     },
155     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_AUDIO_STATE_UPDATE,
156         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
157     },
158     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED,
159         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
160     },
161     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED,
162         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
163     },
164     {CommonEventSupport::COMMON_EVENT_USER_ADDED,
165         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
166     },
167     {CommonEventSupport::COMMON_EVENT_USER_REMOVED,
168         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
169     },
170     {CommonEventSupport::COMMON_EVENT_ABILITY_ADDED,
171         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
172     },
173     {CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED,
174         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
175     },
176     {CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED,
177         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
178     },
179     {CommonEventSupport::COMMON_EVENT_DISK_REMOVED,
180         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
181     },
182     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTED,
183         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
184     },
185     {CommonEventSupport::COMMON_EVENT_DISK_MOUNTED,
186         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
187     },
188     {CommonEventSupport::COMMON_EVENT_DISK_BAD_REMOVAL,
189         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
190     },
191     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTABLE,
192         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
193     },
194     {CommonEventSupport::COMMON_EVENT_DISK_EJECT,
195         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
196     },
197     {CommonEventSupport::COMMON_EVENT_VOLUME_REMOVED,
198         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
199     },
200     {CommonEventSupport::COMMON_EVENT_VOLUME_UNMOUNTED,
201         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
202     },
203     {CommonEventSupport::COMMON_EVENT_VOLUME_MOUNTED,
204         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
205     },
206     {CommonEventSupport::COMMON_EVENT_VOLUME_BAD_REMOVAL,
207         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
208     },
209     {CommonEventSupport::COMMON_EVENT_VOLUME_EJECT,
210         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
211     },
212     {CommonEventSupport::COMMON_EVENT_VISIBLE_ACCOUNTS_UPDATED,
213             {PermissionState::DEFAULT, {"ohos.permission.GET_APP_ACCOUNTS"}}
214     },
215     {CommonEventSupport::COMMON_EVENT_ACCOUNT_DELETED,
216         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
217     },
218     {CommonEventSupport::COMMON_EVENT_FOUNDATION_READY,
219         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
220     },
221     {CommonEventSupport::COMMON_EVENT_SLOT_CHANGE,
222         {PermissionState::DEFAULT, {"ohos.permission.NOTIFICATION_CONTROLLER"}}
223     },
224     {CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED,
225         {PermissionState::DEFAULT, {"ohos.permission.RECEIVE_SMS"}}
226     }
227 };
228 
229 static const std::unordered_set<std::string> SYSTEM_API_COMMON_EVENTS {
230     CommonEventSupport::COMMON_EVENT_DOMAIN_ACCOUNT_STATUS_CHANGED,
231     CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_EXEMPTION_LIST_UPDATED,
232     CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED
233 };
234 
235 static const std::vector<std::string> SENSITIVE_COMMON_EVENTS {
236     CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED
237 };
238 
CommonEventPermissionManager()239 CommonEventPermissionManager::CommonEventPermissionManager()
240 {
241     Init();
242 }
243 
Init()244 void CommonEventPermissionManager::Init()
245 {
246     EVENT_LOGD("enter");
247     Permission per;
248     per.names.reserve(REVERSE);
249 
250     for (auto &[eventName, permissions] : COMMON_EVENT_MAP) {
251         per.state = permissions.first;
252         for (auto &permissionName : permissions.second) {
253             per.names.emplace_back(permissionName);
254         }
255         if (IsSensitiveEvent(eventName)) {
256             per.isSensitive = true;
257         }
258         eventMap_.insert(std::make_pair(eventName, per));
259         per.names.clear();
260     }
261 }
262 
GetEventPermission(const std::string & event)263 Permission CommonEventPermissionManager::GetEventPermission(const std::string &event)
264 {
265     EVENT_LOGD("enter");
266     if (eventMap_.find(event) != eventMap_.end()) {
267         return eventMap_.find(event)->second;
268     }
269     Permission per;
270     return per;
271 }
272 
IsSensitiveEvent(const std::string & event)273 bool CommonEventPermissionManager::IsSensitiveEvent(const std::string &event)
274 {
275     auto it = find(SENSITIVE_COMMON_EVENTS.begin(), SENSITIVE_COMMON_EVENTS.end(), event);
276     return it != SENSITIVE_COMMON_EVENTS.end();
277 }
278 
IsSystemAPIEvent(const std::string & event)279 bool CommonEventPermissionManager::IsSystemAPIEvent(const std::string &event)
280 {
281     return SYSTEM_API_COMMON_EVENTS.find(event) != SYSTEM_API_COMMON_EVENTS.end();
282 }
283 }  // namespace EventFwk
284 }  // namespace OHOS
285