1 /* 2 * Copyright (c) 2024-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 16 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_PERMISSION_UTIL_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_PERMISSION_UTIL_H 18 19 #include <memory> 20 21 #include "ability_record.h" 22 #include "ffrt.h" 23 #include "iremote_object.h" 24 #include "nocopyable.h" 25 #include "permission_verification.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 struct RunningProcessInfo; 30 } 31 namespace AAFwk { 32 struct AbilityRequest; 33 34 /** 35 * @class Want 36 * the struct to open abilities. 37 */ 38 class Want; 39 40 class StartSelfUIAbilityRecordGuard { 41 public: 42 StartSelfUIAbilityRecordGuard() = delete; 43 44 StartSelfUIAbilityRecordGuard(pid_t pid, int32_t tokenId); 45 46 ~StartSelfUIAbilityRecordGuard(); 47 48 private: 49 pid_t pid_; 50 }; 51 52 /** 53 * @class AbilityPermissionUtil 54 * provides ability permission utilities. 55 */ 56 class AbilityPermissionUtil { 57 public: 58 /** 59 * GetInstance, get an instance of AbilityPermissionUtil. 60 * 61 * @return An instance of AbilityPermissionUtil. 62 */ 63 static AbilityPermissionUtil &GetInstance(); 64 65 /** 66 * IsDelegatorCall, check caller is delegator. 67 * 68 * @param processInfo The process information. 69 * @param abilityRequest The ability request. 70 * @return Whether the caller is delegator. 71 */ 72 bool IsDelegatorCall(const AppExecFwk::RunningProcessInfo &processInfo, const AbilityRequest &abilityRequest) const; 73 74 /** 75 * IsDominateScreen, check dominate screen. 76 * 77 * @param want The want. 78 * @param isPendingWantCaller Flag of whether it is the pending want caller. 79 * @return Whether it is dominate screen. 80 */ 81 bool IsDominateScreen(const Want &want, bool isPendingWantCaller); 82 83 /** 84 * CheckMultiInstanceAndAppClone, check if the app is either multi-instance or app-clone. 85 * 86 * @param want The want. 87 * @param userId The user id. 88 * @param appIndex The app index. 89 * @param callerToken The caller token. 90 * @return Whether the app is either multi-instance or app-clone. 91 */ 92 int32_t CheckMultiInstanceAndAppClone(Want &want, int32_t userId, int32_t appIndex, 93 sptr<IRemoteObject> callerToken); 94 95 /** 96 * CheckMultiInstanceKeyForExtension, check multi-instance key for extension. 97 * 98 * @param abilityRequest The ability request. 99 * @return Whether the key is multi-instance key. 100 */ 101 int32_t CheckMultiInstanceKeyForExtension(const AbilityRequest &abilityRequest); 102 103 int32_t CheckStartRecentAbility(const Want &want, AbilityRequest &request); 104 105 /** 106 * Check HasFloatingWindow. 107 * @param callerToken The caller token. 108 * @return Whether the caller has floatingWindow. 109 */ 110 int32_t CheckStartCallHasFloatingWindow(const sptr<IRemoteObject> &callerToken); 111 112 bool IsStartSelfUIAbility(); 113 114 int32_t CheckPrepareTerminateEnable(const std::shared_ptr<AbilityRecord> &abilityRecord); 115 116 private: 117 /** 118 * AbilityPermissionUtil, the private constructor. 119 * 120 */ 121 AbilityPermissionUtil() = default; 122 123 /** 124 * AbilityPermissionUtil, the private destructor. 125 * 126 */ 127 ~AbilityPermissionUtil() = default; 128 129 /** 130 * CheckMultiInstance, check multi-instance. 131 * 132 * @param want The want. 133 * @param callerToken The caller token. 134 * @param isCreating Whether the app is being created. 135 * @param instanceKey The instance key. 136 * @param maxCount The max number of instances. 137 * @return Whether it is a valid multi-instance instance. 138 */ 139 int32_t CheckMultiInstance(Want &want, sptr<IRemoteObject> callerToken, bool isCreating, 140 const std::string &instanceKey, int32_t maxCount); 141 142 /** 143 * UpdateInstanceKey, update instance key. 144 * 145 * @param want The want. 146 * @param originInstanceKey The original instance key. 147 * @param instanceKeyArray Candidate instance keys. 148 * @param instanceKey The new instance key. 149 * @return Whether the update is successful. 150 */ 151 int32_t UpdateInstanceKey(Want &want, const std::string &originInstanceKey, 152 const std::vector<std::string> &instanceKeyArray, const std::string &instanceKey); 153 154 void AddStartSelfUIAbilityRecord(pid_t pid, int32_t tokenId); 155 156 void RemoveStartSelfUIAbilityRecord(pid_t pid); 157 158 int GetTokenIdByPid(pid_t pid); 159 160 std::vector<std::vector<int32_t>> startSelfUIAbilityRecords_; 161 ffrt::mutex startSelfUIAbilityRecordsMutex_; 162 163 friend class StartSelfUIAbilityRecordGuard; 164 165 DISALLOW_COPY_AND_MOVE(AbilityPermissionUtil); 166 }; 167 } // namespace AAFwk 168 } // namespace OHOS 169 #endif // OHOS_ABILITY_RUNTIME_ABILITY_PERMISSION_UTIL_H