• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import { Permission, ButtonStatus, PermissionOption } from '../model/definition';
17import { PermissionGroupConfig, CallerAppInfo, PermissionWithOption } from '../model/typedef';
18import { Log } from '../utils/utils';
19import common from '@ohos.app.ability.common';
20
21export abstract class BasePermissionStrategy {
22  /**
23   * 获取权限组配置
24   * return 权限组配置
25   */
26  public abstract getPermissionGroupConfig(): PermissionGroupConfig;
27
28  /**
29   * 获取权限组标题
30   * @param appName 应用名
31   * @param locationFlag 位置权限状态
32   * return 标题
33   */
34  public abstract getGroupTitle(appName: string, locationFlag: number): ResourceStr;
35
36  /**
37   * 获取读写信息
38   * @param permissions 授权权限
39   * return 按钮列表
40   */
41  public getReadAndWrite(permissions: Set<Permission>): ResourceStr {
42    return '';
43  }
44
45  /**
46   * 根据待申请的权限获取授权理由
47   * @param groupToPermissions 权限组内所有权限
48   * @param permissions 授权权限
49   * @param context 上下文信息
50   * @param callerAppInfo 调用方信息
51   * @param pasteBoardName 剪贴板信息
52   * return reason
53   */
54  public getReasonByPermission(
55    groupToPermissions: Permission[],
56    permissions: Set<Permission>,
57    context: common.ServiceExtensionContext,
58    callerAppInfo: CallerAppInfo,
59    pasteBoardName: string
60  ): ResourceStr {
61    let reason: string = '';
62    groupToPermissions.forEach(perm => {
63      if (!permissions.has(perm)) {
64        return;
65      }
66      let permissionDetail = callerAppInfo.reqPermsDetails.find(item => item.name === perm);
67      if (permissionDetail === undefined) {
68        return;
69      }
70      try {
71        let moduleContext: Context = context.createModuleContext(callerAppInfo.bundleName, permissionDetail.moduleName);
72        let value = moduleContext.resourceManager.getStringSync(permissionDetail.reasonId);
73        if (value && reason === '') {
74          reason = value;
75          return;
76        }
77      } catch (error) {
78        Log.error(`get reason faild, code: ${error.code}, message: ${error.message}.`);
79      }
80    })
81    return reason || '';
82  }
83
84  /**
85   * 获取权限组按钮列表
86   * return 按钮列表
87   */
88  public getButtonList(): ButtonStatus[] {
89    return [ButtonStatus.DENY, ButtonStatus.ALLOW];
90  }
91
92  /**
93   * 区分设备是否支持
94   * @param permission 权限
95   * return
96   */
97  public isSupport(permission: Permission): boolean {
98    return true;
99  }
100
101  /**
102   * 授权操作
103   * @param permission 权限
104   * @param callerAppInfo 调用方信息
105   * @param locationFlag 位置权限组flag
106   * @param buttonStatus
107   * return
108   */
109  public grantHandle(
110    permission: Permission, callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus
111  ): PermissionOption {
112    return PermissionOption.GRANT;
113  }
114
115  /**
116   * 禁止操作
117   * @param permission 权限
118   * @param callerAppInfo 调用方信息
119   * @param locationFlag 位置权限组flag
120   * @param buttonStatus
121   * return
122   */
123  public denyHandle(
124    permission: Permission, callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus
125  ): PermissionOption {
126    return PermissionOption.REVOKE;
127  }
128
129  /**
130   * 判断当前权限是否为申请权限
131   * @param permission 权限
132   * @param callerAppInfo 调用方信息
133   * return
134   */
135  public isPermissionPendingGrant(permission: Permission, callerAppInfo: CallerAppInfo): boolean {
136    let isPendingPermission: boolean = false;
137    if (callerAppInfo.reqPerms.includes(permission)) {
138      isPendingPermission = true;
139    }
140    return isPendingPermission;
141  }
142
143  /**
144   * 预处理权限
145   * @param permission 权限
146   * @param tokenId 应用token
147   * return
148   */
149  public async preProcessingPermission(permission: Permission, tokenId: number): Promise<PermissionWithOption[]> {
150    return [];
151  }
152
153}