• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use crate::config::Action;
15 use crate::utils::check_permission;
16 
17 static INTERNET_PERMISSION: &str = "ohos.permission.INTERNET";
18 static MANAGER_DOWNLOAD: &str = "ohos.permission.DOWNLOAD_SESSION_MANAGER";
19 static MANAGER_UPLOAD: &str = "ohos.permission.UPLOAD_SESSION_MANAGER";
20 
21 pub(crate) struct PermissionChecker;
22 
23 impl PermissionChecker {
check_internet() -> bool24     pub(crate) fn check_internet() -> bool {
25         check_permission(INTERNET_PERMISSION)
26     }
27 
check_down_permission() -> bool28     pub(crate) fn check_down_permission() -> bool {
29         check_permission(MANAGER_DOWNLOAD)
30     }
31 
check_manager() -> ManagerPermission32     pub(crate) fn check_manager() -> ManagerPermission {
33         debug!("Checks MANAGER permission");
34 
35         let manager_download = check_permission(MANAGER_DOWNLOAD);
36         let manager_upload = check_permission(MANAGER_UPLOAD);
37         info!(
38             "Checks manager_download permission is {}, manager_upload permission is {}",
39             manager_download, manager_upload
40         );
41 
42         match (manager_download, manager_upload) {
43             (true, true) => ManagerPermission::ManagerAll,
44             (true, false) => ManagerPermission::ManagerDownLoad,
45             (false, true) => ManagerPermission::ManagerUpload,
46             (false, false) => ManagerPermission::NoPermission,
47         }
48     }
49 }
50 
51 #[derive(Clone, Copy)]
52 pub(crate) enum ManagerPermission {
53     NoPermission = 0,
54     ManagerDownLoad,
55     ManagerUpload,
56     ManagerAll,
57 }
58 
59 impl ManagerPermission {
get_action(&self) -> Option<Action>60     pub(crate) fn get_action(&self) -> Option<Action> {
61         match self {
62             ManagerPermission::NoPermission => None,
63             ManagerPermission::ManagerDownLoad => Some(Action::Download),
64             ManagerPermission::ManagerUpload => Some(Action::Upload),
65             ManagerPermission::ManagerAll => Some(Action::Any),
66         }
67     }
68 
check_action(caller_action: Action, task_action: Action) -> bool69     pub(crate) fn check_action(caller_action: Action, task_action: Action) -> bool {
70         caller_action == task_action || caller_action == Action::Any
71     }
72 }
73