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