• 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 <vector>
22 
23 #include "common_event_support.h"
24 #include "event_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace EventFwk {
28 constexpr size_t REVERSE = 3;
29 
30 static const std::unordered_map<std::string, std::pair<PermissionState, std::vector<std::string>>> COMMON_EVENT_MAP {
31     {CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED,
32         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
33     },
34     {CommonEventSupport::COMMON_EVENT_LOCKED_BOOT_COMPLETED,
35         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
36     },
37     {CommonEventSupport::COMMON_EVENT_USER_SWITCHED,
38         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
39     },
40     {CommonEventSupport::COMMON_EVENT_USER_STARTING,
41         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
42     },
43     {CommonEventSupport::COMMON_EVENT_USER_STOPPING,
44         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
45     },
46     {CommonEventSupport::COMMON_EVENT_WIFI_SCAN_FINISHED,
47         {PermissionState::DEFAULT, {"ohos.permission.LOCATION"}}
48     },
49     {CommonEventSupport::COMMON_EVENT_WIFI_RSSI_VALUE,
50         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
51     },
52     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_JOIN,
53         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
54     },
55     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_LEAVE,
56         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
57     },
58     {CommonEventSupport::COMMON_EVENT_WIFI_MPLINK_STATE_CHANGE,
59         {PermissionState::DEFAULT, {"ohos.permission.MPLINK_CHANGE_STATE"}}
60     },
61     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CONN_STATE,
62         {PermissionState::AND, {"ohos.permission.GET_WIFI_INFO", "ohos.permission.LOCATION"}}
63     },
64     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED,
65         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
66     },
67     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED,
68         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
69     },
70     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED,
71         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
72     },
73     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED,
74         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
75     },
76     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED,
77         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
78     },
79     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CONNECT_STATE_UPDATE,
80         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
81     },
82     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CURRENT_DEVICE_UPDATE,
83         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
84     },
85     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_AUDIO_STATE_UPDATE,
86         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
87     },
88     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CONNECT_STATE_UPDATE,
89         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
90     },
91     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CURRENT_DEVICE_UPDATE,
92         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
93     },
94     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_PLAYING_STATE_UPDATE,
95         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
96     },
97     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CODEC_VALUE_UPDATE,
98         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
99     },
100     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_DISCOVERED,
101         {PermissionState::AND, {"ohos.permission.USE_BLUETOOTH", "ohos.permission.LOCATION"}}
102     },
103     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CLASS_VALUE_UPDATE,
104         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
105     },
106     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_CONNECTED,
107         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
108     },
109     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_DISCONNECTED,
110         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
111     },
112     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_NAME_UPDATE,
113         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
114     },
115     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIR_STATE,
116         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
117     },
118     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_BATTERY_VALUE_UPDATE,
119         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
120     },
121     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_UUID_VALUE,
122         {PermissionState::DEFAULT, {"ohos.permission.DISCOVER_BLUETOOTH"}}
123     },
124     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIRING_REQ,
125         {PermissionState::DEFAULT, {"ohos.permission.DISCOVER_BLUETOOTH"}}
126     },
127     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE,
128         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
129     },
130     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_REQ_ENABLE,
131         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
132     },
133     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_REQ_DISABLE,
134         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
135     },
136     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_SCAN_MODE_UPDATE,
137         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
138     },
139     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_STARTED,
140         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
141     },
142     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_FINISHED,
143         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
144     },
145     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_NAME_UPDATE,
146         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
147     },
148     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_CONNECT_STATE_UPDATE,
149         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
150     },
151     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_PLAYING_STATE_UPDATE,
152         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
153     },
154     {CommonEventSupport::COMMON_EVENT_BLUETOOTH_A2DPSINK_AUDIO_STATE_UPDATE,
155         {PermissionState::DEFAULT, {"ohos.permission.USE_BLUETOOTH"}}
156     },
157     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED,
158         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
159     },
160     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED,
161         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
162     },
163     {CommonEventSupport::COMMON_EVENT_USER_ADDED,
164         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
165     },
166     {CommonEventSupport::COMMON_EVENT_USER_REMOVED,
167         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
168     },
169     {CommonEventSupport::COMMON_EVENT_ABILITY_ADDED,
170         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
171     },
172     {CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED,
173         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
174     },
175     {CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED,
176         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
177     },
178     {CommonEventSupport::COMMON_EVENT_DISK_REMOVED,
179         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
180     },
181     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTED,
182         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
183     },
184     {CommonEventSupport::COMMON_EVENT_DISK_MOUNTED,
185         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
186     },
187     {CommonEventSupport::COMMON_EVENT_DISK_BAD_REMOVAL,
188         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
189     },
190     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTABLE,
191         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
192     },
193     {CommonEventSupport::COMMON_EVENT_DISK_EJECT,
194         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
195     },
196     {CommonEventSupport::COMMON_EVENT_VOLUME_REMOVED,
197         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
198     },
199     {CommonEventSupport::COMMON_EVENT_VOLUME_UNMOUNTED,
200         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
201     },
202     {CommonEventSupport::COMMON_EVENT_VOLUME_MOUNTED,
203         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
204     },
205     {CommonEventSupport::COMMON_EVENT_VOLUME_BAD_REMOVAL,
206         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
207     },
208     {CommonEventSupport::COMMON_EVENT_VOLUME_EJECT,
209         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
210     },
211     {CommonEventSupport::COMMON_EVENT_VISIBLE_ACCOUNTS_UPDATED,
212             {PermissionState::DEFAULT, {"ohos.permission.GET_APP_ACCOUNTS"}}
213     },
214     {CommonEventSupport::COMMON_EVENT_ACCOUNT_DELETED,
215         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
216     },
217     {CommonEventSupport::COMMON_EVENT_FOUNDATION_READY,
218         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
219     },
220     {CommonEventSupport::COMMON_EVENT_SLOT_CHANGE,
221         {PermissionState::DEFAULT, {"ohos.permission.NOTIFICATION_CONTROLLER"}}
222     },
223     {CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED,
224         {PermissionState::DEFAULT, {"ohos.permission.RECEIVE_SMS"}}
225     }
226 };
227 
228 static const std::vector<std::string> SENSITIVE_COMMON_EVENTS {
229     CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED
230 };
231 
CommonEventPermissionManager()232 CommonEventPermissionManager::CommonEventPermissionManager()
233 {
234     Init();
235 }
236 
Init()237 void CommonEventPermissionManager::Init()
238 {
239     EVENT_LOGI("enter");
240     Permission per;
241     per.names.reserve(REVERSE);
242 
243     for (auto &[eventName, permissions] : COMMON_EVENT_MAP) {
244         per.state = permissions.first;
245         for (auto &permissionName : permissions.second) {
246             per.names.emplace_back(permissionName);
247         }
248         if (IsSensitiveEvent(eventName)) {
249             per.isSensitive = true;
250         }
251         eventMap_.insert(std::make_pair(eventName, per));
252         per.names.clear();
253     }
254 }
255 
GetEventPermission(const std::string & event)256 Permission CommonEventPermissionManager::GetEventPermission(const std::string &event)
257 {
258     EVENT_LOGI("enter");
259     if (eventMap_.find(event) != eventMap_.end()) {
260         return eventMap_.find(event)->second;
261     }
262     Permission per;
263     return per;
264 }
265 
IsSensitiveEvent(const std::string & event)266 bool CommonEventPermissionManager::IsSensitiveEvent(const std::string &event)
267 {
268     auto it = find(SENSITIVE_COMMON_EVENTS.begin(), SENSITIVE_COMMON_EVENTS.end(), event);
269     return it != SENSITIVE_COMMON_EVENTS.end();
270 }
271 }  // namespace EventFwk
272 }  // namespace OHOS
273