1fc0b0055Sopenharmony_ci/*
2fc0b0055Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at
6fc0b0055Sopenharmony_ci *
7fc0b0055Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc0b0055Sopenharmony_ci *
9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and
13fc0b0055Sopenharmony_ci * limitations under the License.
14fc0b0055Sopenharmony_ci */
15fc0b0055Sopenharmony_ci
16fc0b0055Sopenharmony_ci#include "constant.h"
17fc0b0055Sopenharmony_ci
18fc0b0055Sopenharmony_cinamespace OHOS {
19fc0b0055Sopenharmony_cinamespace Security {
20fc0b0055Sopenharmony_cinamespace AccessToken {
21fc0b0055Sopenharmony_ciconst std::map<std::string, int32_t> Constant::PERMISSION_OPCODE_MAP = {
22fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.ANSWER_CALL", Constant::OP_ANSWER_CALL),
23fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_CALENDAR", Constant::OP_READ_CALENDAR),
24fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_CALL_LOG", Constant::OP_READ_CALL_LOG),
25fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_CELL_MESSAGES", Constant::OP_READ_CELL_MESSAGES),
26fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_CONTACTS", Constant::OP_READ_CONTACTS),
27fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_MESSAGES", Constant::OP_READ_MESSAGES),
28fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.RECEIVE_MMS", Constant::OP_RECEIVE_MMS),
29fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.RECEIVE_SMS", Constant::OP_RECEIVE_SMS),
30fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
31fc0b0055Sopenharmony_ci        "ohos.permission.RECEIVE_WAP_MESSAGES", Constant::OP_RECEIVE_WAP_MESSAGES),
32fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.MICROPHONE", Constant::OP_MICROPHONE),
33fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.SEND_MESSAGES", Constant::OP_SEND_MESSAGES),
34fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CALENDAR", Constant::OP_WRITE_CALENDAR),
35fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CALL_LOG", Constant::OP_WRITE_CALL_LOG),
36fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CONTACTS", Constant::OP_WRITE_CONTACTS),
37fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
38fc0b0055Sopenharmony_ci        "ohos.permission.DISTRIBUTED_DATASYNC", Constant::OP_DISTRIBUTED_DATASYNC),
39fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.MANAGE_VOICEMAIL", Constant::OP_MANAGE_VOICEMAIL),
40fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
41fc0b0055Sopenharmony_ci        "ohos.permission.LOCATION_IN_BACKGROUND", Constant::OP_LOCATION_IN_BACKGROUND),
42fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.LOCATION", Constant::OP_LOCATION),
43fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
44fc0b0055Sopenharmony_ci        "ohos.permission.APPROXIMATELY_LOCATION", Constant::OP_APPROXIMATELY_LOCATION),
45fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.MEDIA_LOCATION", Constant::OP_MEDIA_LOCATION),
46fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.CAMERA", Constant::OP_CAMERA),
47fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_MEDIA", Constant::OP_READ_MEDIA),
48fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_MEDIA", Constant::OP_WRITE_MEDIA),
49fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.ACTIVITY_MOTION", Constant::OP_ACTIVITY_MOTION),
50fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_HEALTH_DATA", Constant::OP_READ_HEALTH_DATA),
51fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_IMAGEVIDEO", Constant::OP_READ_IMAGEVIDEO),
52fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_AUDIO", Constant::OP_READ_AUDIO),
53fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_DOCUMENT", Constant::OP_READ_DOCUMENT),
54fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_IMAGEVIDEO", Constant::OP_WRITE_IMAGEVIDEO),
55fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_AUDIO", Constant::OP_WRITE_AUDIO),
56fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_DOCUMENT", Constant::OP_WRITE_DOCUMENT),
57fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_WHOLE_CALENDAR", Constant::OP_READ_WHOLE_CALENDAR),
58fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
59fc0b0055Sopenharmony_ci        "ohos.permission.WRITE_WHOLE_CALENDAR", Constant::OP_WRITE_WHOLE_CALENDAR),
60fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
61fc0b0055Sopenharmony_ci        "ohos.permission.APP_TRACKING_CONSENT", Constant::OP_APP_TRACKING_CONSENT),
62fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
63fc0b0055Sopenharmony_ci        "ohos.permission.GET_INSTALLED_BUNDLE_LIST", Constant::OP_GET_INSTALLED_BUNDLE_LIST),
64fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.ACCESS_BLUETOOTH", Constant::OP_ACCESS_BLUETOOTH),
65fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type("ohos.permission.READ_PASTEBOARD", Constant::OP_READ_PASTEBOARD),
66fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
67fc0b0055Sopenharmony_ci        "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY", Constant::OP_READ_WRITE_DOWNLOAD_DIRECTORY),
68fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
69fc0b0055Sopenharmony_ci        "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY", Constant::OP_READ_WRITE_DOCUMENTS_DIRECTORY),
70fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
71fc0b0055Sopenharmony_ci        "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY", Constant::OP_READ_WRITE_DESKTOP_DIRECTORY),
72fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
73fc0b0055Sopenharmony_ci        "ohos.permission.ACCESS_NEARLINK", Constant::OP_ACCESS_NEARLINK),
74fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
75fc0b0055Sopenharmony_ci        "ohos.permission.CAPTURE_SCREEN", Constant::OP_CAPTURE_SCREEN),
76fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
77fc0b0055Sopenharmony_ci        "ohos.permission.SHORT_TERM_WRITE_IMAGEVIDEO", Constant::SHORT_TERM_WRITE_IMAGEVIDEO),
78fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
79fc0b0055Sopenharmony_ci        "ohos.permission.CAMERA_BACKGROUND", Constant::CAMERA_BACKGROUND),
80fc0b0055Sopenharmony_ci    std::map<std::string, int32_t>::value_type(
81fc0b0055Sopenharmony_ci        "ohos.permission.CUSTOM_SCREEN_CAPTURE", Constant::OP_CUSTOM_SCREEN_CAPTURE),
82fc0b0055Sopenharmony_ci};
83fc0b0055Sopenharmony_ci
84fc0b0055Sopenharmony_cibool Constant::TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode)
85fc0b0055Sopenharmony_ci{
86fc0b0055Sopenharmony_ci    if (PERMISSION_OPCODE_MAP.count(permissionName) == 0) {
87fc0b0055Sopenharmony_ci        return false;
88fc0b0055Sopenharmony_ci    }
89fc0b0055Sopenharmony_ci    opCode = PERMISSION_OPCODE_MAP.at(permissionName);
90fc0b0055Sopenharmony_ci    return true;
91fc0b0055Sopenharmony_ci}
92fc0b0055Sopenharmony_ci
93fc0b0055Sopenharmony_cibool Constant::TransferOpcodeToPermission(int32_t opCode, std::string& permissionName)
94fc0b0055Sopenharmony_ci{
95fc0b0055Sopenharmony_ci    auto iter = std::find_if(PERMISSION_OPCODE_MAP.begin(), PERMISSION_OPCODE_MAP.end(),
96fc0b0055Sopenharmony_ci        [opCode](const std::map<std::string, int32_t>::value_type item) {
97fc0b0055Sopenharmony_ci            return item.second == opCode;
98fc0b0055Sopenharmony_ci        });
99fc0b0055Sopenharmony_ci    if (iter == PERMISSION_OPCODE_MAP.end()) {
100fc0b0055Sopenharmony_ci        return false;
101fc0b0055Sopenharmony_ci    }
102fc0b0055Sopenharmony_ci    permissionName = iter->first;
103fc0b0055Sopenharmony_ci    return true;
104fc0b0055Sopenharmony_ci}
105fc0b0055Sopenharmony_ci} // namespace AccessToken
106fc0b0055Sopenharmony_ci} // namespace Security
107fc0b0055Sopenharmony_ci} // namespace OHOS