1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <mutex>
20eace7efcSopenharmony_ci#include <memory>
21eace7efcSopenharmony_ci#include <map>
22eace7efcSopenharmony_ci#include <vector>
23eace7efcSopenharmony_ci#include <string>
24eace7efcSopenharmony_ci#include "cpp/mutex.h"
25eace7efcSopenharmony_ci
26eace7efcSopenharmony_ci#include "ability_manager_errors.h"
27eace7efcSopenharmony_ci#include "ability_record.h"
28eace7efcSopenharmony_ci#include "common_event.h"
29eace7efcSopenharmony_ci#include "nocopyable.h"
30eace7efcSopenharmony_ci#include "pending_want_key.h"
31eace7efcSopenharmony_ci#include "pending_want_record.h"
32eace7efcSopenharmony_ci#include "pending_want_common_event.h"
33eace7efcSopenharmony_ci#include "sender_info.h"
34eace7efcSopenharmony_ci#include "want_sender_info.h"
35eace7efcSopenharmony_ci
36eace7efcSopenharmony_cinamespace OHOS {
37eace7efcSopenharmony_cinamespace AAFwk {
38eace7efcSopenharmony_cienum class OperationType {
39eace7efcSopenharmony_ci    /**
40eace7efcSopenharmony_ci     * Unknown operation.
41eace7efcSopenharmony_ci     */
42eace7efcSopenharmony_ci    UNKNOWN_TYPE,
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ci    /**
45eace7efcSopenharmony_ci     * Starts an ability with a UI.
46eace7efcSopenharmony_ci     */
47eace7efcSopenharmony_ci    START_ABILITY,
48eace7efcSopenharmony_ci
49eace7efcSopenharmony_ci    /**
50eace7efcSopenharmony_ci     * Starts multiple abilities.
51eace7efcSopenharmony_ci     */
52eace7efcSopenharmony_ci    START_ABILITIES,
53eace7efcSopenharmony_ci
54eace7efcSopenharmony_ci    /**
55eace7efcSopenharmony_ci     * Starts an ability without a UI.
56eace7efcSopenharmony_ci     */
57eace7efcSopenharmony_ci    START_SERVICE,
58eace7efcSopenharmony_ci
59eace7efcSopenharmony_ci    /**
60eace7efcSopenharmony_ci     * Sends a common event.
61eace7efcSopenharmony_ci     */
62eace7efcSopenharmony_ci    SEND_COMMON_EVENT,
63eace7efcSopenharmony_ci
64eace7efcSopenharmony_ci    /**
65eace7efcSopenharmony_ci     * Starts a foreground ability without a UI.
66eace7efcSopenharmony_ci     */
67eace7efcSopenharmony_ci    START_FOREGROUND_SERVICE,
68eace7efcSopenharmony_ci
69eace7efcSopenharmony_ci    /**
70eace7efcSopenharmony_ci     * Starts a service extension.
71eace7efcSopenharmony_ci     */
72eace7efcSopenharmony_ci    START_SERVICE_EXTENSION
73eace7efcSopenharmony_ci};
74eace7efcSopenharmony_ci
75eace7efcSopenharmony_cienum class Flags {
76eace7efcSopenharmony_ci    /**
77eace7efcSopenharmony_ci     * Indicates that the {@link WantAgent} can be used only once.
78eace7efcSopenharmony_ci     */
79eace7efcSopenharmony_ci    ONE_TIME_FLAG = 1 << 30,
80eace7efcSopenharmony_ci
81eace7efcSopenharmony_ci    /**
82eace7efcSopenharmony_ci     * Indicates that {@code null} is returned if the {@link WantAgent} does not exist.
83eace7efcSopenharmony_ci     */
84eace7efcSopenharmony_ci    NO_BUILD_FLAG = 1 << 29,
85eace7efcSopenharmony_ci
86eace7efcSopenharmony_ci    /**
87eace7efcSopenharmony_ci     * Indicates that the existing {@link WantAgent} should be canceled before the new object is generated.
88eace7efcSopenharmony_ci     */
89eace7efcSopenharmony_ci    CANCEL_PRESENT_FLAG = 1 << 28,
90eace7efcSopenharmony_ci
91eace7efcSopenharmony_ci    /**
92eace7efcSopenharmony_ci     * Indicates that the system only replaces the extra data of the existing {@link WantAgent}
93eace7efcSopenharmony_ci     * with that of the new object.
94eace7efcSopenharmony_ci     */
95eace7efcSopenharmony_ci    UPDATE_PRESENT_FLAG = 1 << 27,
96eace7efcSopenharmony_ci
97eace7efcSopenharmony_ci    /**
98eace7efcSopenharmony_ci     * Indicates that the created {@link WantAgent} should be immutable.
99eace7efcSopenharmony_ci     */
100eace7efcSopenharmony_ci    CONSTANT_FLAG = 1 << 26,
101eace7efcSopenharmony_ci
102eace7efcSopenharmony_ci    /**
103eace7efcSopenharmony_ci     * Indicates that the current value of {@code element} can be replaced
104eace7efcSopenharmony_ci     * when the {@link WantAgent} is triggered.
105eace7efcSopenharmony_ci     */
106eace7efcSopenharmony_ci    REPLACE_ELEMENT,
107eace7efcSopenharmony_ci
108eace7efcSopenharmony_ci    /**
109eace7efcSopenharmony_ci     * Indicates that the current value of {@code action} can be replaced
110eace7efcSopenharmony_ci     * when the {@link WantAgent} is triggered.
111eace7efcSopenharmony_ci     */
112eace7efcSopenharmony_ci    REPLACE_ACTION,
113eace7efcSopenharmony_ci
114eace7efcSopenharmony_ci    /**
115eace7efcSopenharmony_ci     * Indicates that the current value of {@code uri} can be replaced when the {@link WantAgent} is triggered.
116eace7efcSopenharmony_ci     */
117eace7efcSopenharmony_ci    REPLACE_URI,
118eace7efcSopenharmony_ci
119eace7efcSopenharmony_ci    /**
120eace7efcSopenharmony_ci     * Indicates that the current value of {@code entities} can be replaced
121eace7efcSopenharmony_ci     * when the {@link WantAgent} is triggered.
122eace7efcSopenharmony_ci     */
123eace7efcSopenharmony_ci    REPLACE_ENTITIES,
124eace7efcSopenharmony_ci
125eace7efcSopenharmony_ci    /**
126eace7efcSopenharmony_ci     * Indicates that the current value of {@code bundleName} can be replaced
127eace7efcSopenharmony_ci     * when the {@link WantAgent} is triggered.
128eace7efcSopenharmony_ci     */
129eace7efcSopenharmony_ci    REPLACE_BUNDLE
130eace7efcSopenharmony_ci};
131eace7efcSopenharmony_ci
132eace7efcSopenharmony_ciclass PendingWantManager : public std::enable_shared_from_this<PendingWantManager>, public NoCopyable {
133eace7efcSopenharmony_cipublic:
134eace7efcSopenharmony_ci    PendingWantManager();
135eace7efcSopenharmony_ci    explicit PendingWantManager(const std::shared_ptr<PendingWantManager> &manager) {};
136eace7efcSopenharmony_ci    virtual ~PendingWantManager();
137eace7efcSopenharmony_ci
138eace7efcSopenharmony_cipublic:
139eace7efcSopenharmony_ci    sptr<IWantSender> GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp,
140eace7efcSopenharmony_ci        const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
141eace7efcSopenharmony_ci    int32_t SendWantSender(sptr<IWantSender> target, const SenderInfo &senderInfo);
142eace7efcSopenharmony_ci    void CancelWantSender(const bool isSystemApp, const sptr<IWantSender> &sender);
143eace7efcSopenharmony_ci
144eace7efcSopenharmony_ci    int32_t GetPendingWantUid(const sptr<IWantSender> &target);
145eace7efcSopenharmony_ci    int32_t GetPendingWantUserId(const sptr<IWantSender> &target);
146eace7efcSopenharmony_ci    std::string GetPendingWantBundleName(const sptr<IWantSender> &target);
147eace7efcSopenharmony_ci    int32_t GetPendingWantCode(const sptr<IWantSender> &target);
148eace7efcSopenharmony_ci    int32_t GetPendingWantType(const sptr<IWantSender> &target);
149eace7efcSopenharmony_ci    void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier);
150eace7efcSopenharmony_ci    void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier);
151eace7efcSopenharmony_ci    int32_t GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want);
152eace7efcSopenharmony_ci    int32_t GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info);
153eace7efcSopenharmony_ci
154eace7efcSopenharmony_ci    void CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility);
155eace7efcSopenharmony_ci    int32_t PendingWantStartAbility(const Want &want, const sptr<StartOptions> &startOptions,
156eace7efcSopenharmony_ci        const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
157eace7efcSopenharmony_ci    int32_t PendingWantStartServiceExtension(Want &want, const sptr<IRemoteObject> &callerToken);
158eace7efcSopenharmony_ci    int32_t PendingWantStartAbilitys(const std::vector<WantsInfo> &wantsInfo, const sptr<StartOptions> &startOptions,
159eace7efcSopenharmony_ci        const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
160eace7efcSopenharmony_ci    int32_t DeviceIdDetermine(const Want &want, const sptr<StartOptions> &startOptions,
161eace7efcSopenharmony_ci        const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId);
162eace7efcSopenharmony_ci    int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t callerUid,
163eace7efcSopenharmony_ci        int32_t callerTokenId);
164eace7efcSopenharmony_ci    void ClearPendingWantRecord(const std::string &bundleName, int32_t uid);
165eace7efcSopenharmony_ci
166eace7efcSopenharmony_ci    void Dump(std::vector<std::string> &info);
167eace7efcSopenharmony_ci    void DumpByRecordId(std::vector<std::string> &info, const std::string &args);
168eace7efcSopenharmony_ci    int32_t GetAllRunningInstanceKeysByBundleName(const std::string &bundleName, std::vector<std::string> &appKey);
169eace7efcSopenharmony_ci
170eace7efcSopenharmony_ciprivate:
171eace7efcSopenharmony_ci    sptr<IWantSender> GetWantSenderLocked(const int32_t callingUid, const int32_t uid, const int32_t userId,
172eace7efcSopenharmony_ci        WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
173eace7efcSopenharmony_ci    void MakeWantSenderCanceledLocked(PendingWantRecord &record);
174eace7efcSopenharmony_ci
175eace7efcSopenharmony_ci    sptr<PendingWantRecord> GetPendingWantRecordByKey(const std::shared_ptr<PendingWantKey> &key);
176eace7efcSopenharmony_ci    bool CheckPendingWantRecordByKey(
177eace7efcSopenharmony_ci        const std::shared_ptr<PendingWantKey> &inputKey, const std::shared_ptr<PendingWantKey> &key);
178eace7efcSopenharmony_ci
179eace7efcSopenharmony_ci    sptr<PendingWantRecord> GetPendingWantRecordByCode(int32_t code);
180eace7efcSopenharmony_ci    static int32_t PendingRecordIdCreate();
181eace7efcSopenharmony_ci    void ClearPendingWantRecordTask(const std::string &bundleName, int32_t uid);
182eace7efcSopenharmony_ci
183eace7efcSopenharmony_ci    bool CheckCallerPermission();
184eace7efcSopenharmony_ci
185eace7efcSopenharmony_ci    bool CheckWindowState(int32_t pid);
186eace7efcSopenharmony_ci
187eace7efcSopenharmony_ci    void EraseBundleRecord(const std::vector<WantsInfo> &wantsInfos, std::shared_ptr<PendingWantKey> key);
188eace7efcSopenharmony_ci
189eace7efcSopenharmony_ci    void InsertBundleRecord(const std::vector<WantsInfo> &wantsInfos, std::shared_ptr<PendingWantKey> key);
190eace7efcSopenharmony_ci
191eace7efcSopenharmony_ci    bool QueryRecordByBundle(const std::string &bundleName);
192eace7efcSopenharmony_ci
193eace7efcSopenharmony_ciprivate:
194eace7efcSopenharmony_ci    std::map<std::shared_ptr<PendingWantKey>, sptr<PendingWantRecord>> wantRecords_;
195eace7efcSopenharmony_ci    std::map<std::string, std::vector<std::shared_ptr<PendingWantKey>>> bundleRecords_;
196eace7efcSopenharmony_ci    ffrt::mutex mutex_;
197eace7efcSopenharmony_ci    ffrt::mutex bundleRecordsMutex_;
198eace7efcSopenharmony_ci};
199eace7efcSopenharmony_ci}  // namespace AAFwk
200eace7efcSopenharmony_ci}  // namespace OHOS
201eace7efcSopenharmony_ci
202eace7efcSopenharmony_ci#endif  // OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H
203