• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * @param isScbCall The scb call.
91      * @return Whether the app is either multi-instance or app-clone.
92      */
93     int32_t CheckMultiInstanceAndAppClone(Want &want, int32_t userId, int32_t appIndex,
94         sptr<IRemoteObject> callerToken, bool isScbCall);
95 
96     /**
97      * CheckMultiInstanceKeyForExtension, check multi-instance key for extension.
98      *
99      * @param abilityRequest The ability request.
100      * @return Whether the key is multi-instance key.
101      */
102     int32_t CheckMultiInstanceKeyForExtension(const AbilityRequest &abilityRequest);
103 
104     int32_t CheckStartRecentAbility(const Want &want, AbilityRequest &request);
105 
106     /**
107      * Check HasFloatingWindow.
108      * @param callerToken The caller token.
109      * @return Whether the caller has floatingWindow.
110      */
111     int32_t CheckStartCallHasFloatingWindow(const sptr<IRemoteObject> &callerToken);
112 
113     bool IsStartSelfUIAbility();
114 
115     int32_t CheckPrepareTerminateEnable(std::shared_ptr<AbilityRecord> abilityRecord);
116 
117 private:
118     /**
119      * AbilityPermissionUtil, the private constructor.
120      *
121      */
122     AbilityPermissionUtil() = default;
123 
124     /**
125      * AbilityPermissionUtil, the private destructor.
126      *
127      */
128     ~AbilityPermissionUtil() = default;
129 
130     /**
131      * CheckMultiInstance, check multi-instance.
132      *
133      * @param want The want.
134      * @param callerToken The caller token.
135      * @param maxCount The max number of instances.
136      * @param isScbCall The scb call.
137      * @return Whether it is a valid multi-instance instance.
138      */
139     int32_t CheckMultiInstance(Want &want, sptr<IRemoteObject> callerToken, int32_t maxCount, bool isScbCall);
140 
141     /**
142      * UpdateInstanceKey, update instance key.
143      *
144      * @param want The want.
145      * @param originInstanceKey The original instance key.
146      * @param instanceKeyArray Candidate instance keys.
147      * @param instanceKey The new instance key.
148      * @return Whether the update is successful.
149      */
150     int32_t UpdateInstanceKey(Want &want, const std::string &originInstanceKey,
151         const std::vector<std::string> &instanceKeyArray, const std::string &instanceKey);
152 
153     void AddStartSelfUIAbilityRecord(pid_t pid, int32_t tokenId);
154 
155     void RemoveStartSelfUIAbilityRecord(pid_t pid);
156 
157     int GetTokenIdByPid(pid_t pid);
158 
159     std::vector<std::vector<int32_t>> startSelfUIAbilityRecords_;
160     ffrt::mutex startSelfUIAbilityRecordsMutex_;
161 
162     friend class StartSelfUIAbilityRecordGuard;
163 
164     DISALLOW_COPY_AND_MOVE(AbilityPermissionUtil);
165 };
166 } // namespace AAFwk
167 } // namespace OHOS
168 #endif // OHOS_ABILITY_RUNTIME_ABILITY_PERMISSION_UTIL_H