1 /*
2  * Copyright (c) 2021-2022 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_PENDING_WANT_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H
18 
19 #include <mutex>
20 #include <memory>
21 #include <map>
22 #include <vector>
23 #include <string>
24 #include "cpp/mutex.h"
25 
26 #include "ability_manager_errors.h"
27 #include "ability_record.h"
28 #include "common_event.h"
29 #include "nocopyable.h"
30 #include "pending_want_key.h"
31 #include "pending_want_record.h"
32 #include "pending_want_common_event.h"
33 #include "sender_info.h"
34 #include "task_handler_wrap.h"
35 #include "want_sender_info.h"
36 
37 namespace OHOS {
38 namespace AAFwk {
39 enum class OperationType {
40     /**
41      * Unknown operation.
42      */
43     UNKNOWN_TYPE,
44 
45     /**
46      * Starts an ability with a UI.
47      */
48     START_ABILITY,
49 
50     /**
51      * Starts multiple abilities.
52      */
53     START_ABILITIES,
54 
55     /**
56      * Starts an ability without a UI.
57      */
58     START_SERVICE,
59 
60     /**
61      * Sends a common event.
62      */
63     SEND_COMMON_EVENT,
64 
65     /**
66      * Starts a foreground ability without a UI.
67      */
68     START_FOREGROUND_SERVICE,
69 
70     /**
71      * Starts a service extension.
72      */
73     START_SERVICE_EXTENSION
74 };
75 
76 enum class Flags {
77     /**
78      * Indicates that the {@link WantAgent} can be used only once.
79      */
80     ONE_TIME_FLAG = 1 << 30,
81 
82     /**
83      * Indicates that {@code null} is returned if the {@link WantAgent} does not exist.
84      */
85     NO_BUILD_FLAG = 1 << 29,
86 
87     /**
88      * Indicates that the existing {@link WantAgent} should be canceled before the new object is generated.
89      */
90     CANCEL_PRESENT_FLAG = 1 << 28,
91 
92     /**
93      * Indicates that the system only replaces the extra data of the existing {@link WantAgent}
94      * with that of the new object.
95      */
96     UPDATE_PRESENT_FLAG = 1 << 27,
97 
98     /**
99      * Indicates that the created {@link WantAgent} should be immutable.
100      */
101     CONSTANT_FLAG = 1 << 26,
102 
103     /**
104      * Indicates that the current value of {@code element} can be replaced
105      * when the {@link WantAgent} is triggered.
106      */
107     REPLACE_ELEMENT,
108 
109     /**
110      * Indicates that the current value of {@code action} can be replaced
111      * when the {@link WantAgent} is triggered.
112      */
113     REPLACE_ACTION,
114 
115     /**
116      * Indicates that the current value of {@code uri} can be replaced when the {@link WantAgent} is triggered.
117      */
118     REPLACE_URI,
119 
120     /**
121      * Indicates that the current value of {@code entities} can be replaced
122      * when the {@link WantAgent} is triggered.
123      */
124     REPLACE_ENTITIES,
125 
126     /**
127      * Indicates that the current value of {@code bundleName} can be replaced
128      * when the {@link WantAgent} is triggered.
129      */
130     REPLACE_BUNDLE,
131 
132     /**
133      * Indicates that the existing {@link WantAgent} allow canceled by user.
134      */
135     ALLOW_CANCEL_FLAG = 1 << 20
136 };
137 
138 class PendingWantManager : public std::enable_shared_from_this<PendingWantManager>, public NoCopyable {
139 public:
140     PendingWantManager();
PendingWantManager(const std::shared_ptr<PendingWantManager> & manager)141     explicit PendingWantManager(const std::shared_ptr<PendingWantManager> &manager) {};
142     virtual ~PendingWantManager();
143 
144 public:
145     sptr<IWantSender> GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp,
146         const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
147     int32_t SendWantSender(sptr<IWantSender> target, const SenderInfo &senderInfo);
148     void CancelWantSender(const bool isSystemAppCall, const sptr<IWantSender> &sender);
149 
150     int32_t GetPendingWantUid(const sptr<IWantSender> &target);
151     int32_t GetPendingWantUserId(const sptr<IWantSender> &target);
152     std::string GetPendingWantBundleName(const sptr<IWantSender> &target);
153     int32_t GetPendingWantCode(const sptr<IWantSender> &target);
154     int32_t GetPendingWantType(const sptr<IWantSender> &target);
155     void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier);
156     void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier);
157     int32_t GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want);
158     int32_t GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info);
159 
160     void CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility);
161     int32_t PendingWantStartAbility(const Want &want, const sptr<StartOptions> &startOptions,
162         const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
163     int32_t PendingWantStartServiceExtension(Want &want, const sptr<IRemoteObject> &callerToken);
164     int32_t PendingWantStartAbilitys(const std::vector<WantsInfo> &wantsInfo, const sptr<StartOptions> &startOptions,
165         const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
166     int32_t DeviceIdDetermine(const Want &want, const sptr<StartOptions> &startOptions,
167         const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
168     int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t callerUid,
169         int32_t callerTokenId);
170     void ClearPendingWantRecord(const std::string &bundleName, int32_t uid);
171 
172     void Dump(std::vector<std::string> &info);
173     void DumpByRecordId(std::vector<std::string> &info, const std::string &args);
174 
175 private:
176     sptr<IWantSender> GetWantSenderLocked(const int32_t callingUid, const int32_t uid, const int32_t userId,
177         WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
178     void MakeWantSenderCanceledLocked(PendingWantRecord &record);
179 
180     sptr<PendingWantRecord> GetPendingWantRecordByKey(const std::shared_ptr<PendingWantKey> &key);
181     bool CheckPendingWantRecordByKey(
182         const std::shared_ptr<PendingWantKey> &inputKey, const std::shared_ptr<PendingWantKey> &key);
183 
184     sptr<PendingWantRecord> GetPendingWantRecordByCode(int32_t code);
185     static int32_t PendingRecordIdCreate();
186     void ClearPendingWantRecordTask(const std::string &bundleName, int32_t uid);
187 
188     bool CheckCallerPermission();
189 
190     bool CheckWindowState(int32_t pid);
191 
192 private:
193     std::shared_ptr<TaskHandlerWrap> taskHandler_;
194     std::map<std::shared_ptr<PendingWantKey>, sptr<PendingWantRecord>> wantRecords_;
195     ffrt::mutex mutex_;
196 };
197 }  // namespace AAFwk
198 }  // namespace OHOS
199 
200 #endif  // OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H
201