1 /*
2  * Copyright (c) 2024 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 "iremote_object.h"
22 #include "nocopyable.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 struct RunningProcessInfo;
27 }
28 namespace AAFwk {
29 struct AbilityRequest;
30 
31 /**
32  * @class Want
33  * the struct to open abilities.
34  */
35 class Want;
36 
37 /**
38  * @class AbilityPermissionUtil
39  * provides ability permission utilities.
40  */
41 class AbilityPermissionUtil {
42 public:
43     /**
44      * GetInstance, get an instance of AbilityPermissionUtil.
45      *
46      * @return An instance of AbilityPermissionUtil.
47      */
48     static AbilityPermissionUtil &GetInstance();
49 
50     /**
51      * IsDelegatorCall, check caller is delegator.
52      *
53      * @param processInfo The process information.
54      * @param abilityRequest The ability request.
55      * @return Whether the caller is delegator.
56      */
57     bool IsDelegatorCall(const AppExecFwk::RunningProcessInfo &processInfo, const AbilityRequest &abilityRequest) const;
58 
59     /**
60      * IsDominateScreen, check dominate screen.
61      *
62      * @param want The want.
63      * @param isPendingWantCaller Flag of whether it is the pending want caller.
64      * @return Whether it is dominate screen.
65      */
66     bool IsDominateScreen(const Want &want, bool isPendingWantCaller);
67 
68     /**
69      * CheckMultiInstanceAndAppClone, check if the app is either multi-instance or app-clone.
70      *
71      * @param want The want.
72      * @param userId The user id.
73      * @param appIndex The app index.
74      * @param callerToken The caller token.
75      * @return Whether the app is either multi-instance or app-clone.
76      */
77     int32_t CheckMultiInstanceAndAppClone(Want &want, int32_t userId, int32_t appIndex,
78         sptr<IRemoteObject> callerToken);
79 
80     /**
81      * CheckMultiInstanceKeyForExtension, check multi-instance key for extension.
82      *
83      * @param abilityRequest The ability request.
84      * @return Whether the key is multi-instance key.
85      */
86     int32_t CheckMultiInstanceKeyForExtension(const AbilityRequest &abilityRequest);
87 
88 private:
89     /**
90      * AbilityPermissionUtil, the private constructor.
91      *
92      */
93     AbilityPermissionUtil() = default;
94 
95     /**
96      * AbilityPermissionUtil, the private destructor.
97      *
98      */
99     ~AbilityPermissionUtil() = default;
100 
101     /**
102      * CheckMultiInstance, check multi-instance.
103      *
104      * @param want The want.
105      * @param callerToken The caller token.
106      * @param isCreating Whether the app is being created.
107      * @param instanceKey The instance key.
108      * @param maxCount The max number of instances.
109      * @return Whether it is a valid multi-instance instance.
110      */
111     int32_t CheckMultiInstance(Want &want, sptr<IRemoteObject> callerToken, bool isCreating,
112         const std::string &instanceKey, int32_t maxCount);
113 
114     /**
115      * UpdateInstanceKey, update instance key.
116      *
117      * @param want The want.
118      * @param originInstanceKey The original instance key.
119      * @param instanceKeyArray Candidate instance keys.
120      * @param instanceKey The new instance key.
121      * @return Whether the update is successful.
122      */
123     int32_t UpdateInstanceKey(Want &want, const std::string &originInstanceKey,
124         const std::vector<std::string> &instanceKeyArray, const std::string &instanceKey);
125 
126     DISALLOW_COPY_AND_MOVE(AbilityPermissionUtil);
127 };
128 } // namespace AAFwk
129 } // namespace OHOS
130 #endif // OHOS_ABILITY_RUNTIME_ABILITY_PERMISSION_UTIL_H