1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 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#include "pending_want_manager.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "ability_manager_service.h"
19eace7efcSopenharmony_ci#include "ability_util.h"
20eace7efcSopenharmony_ci#include "distributed_client.h"
21eace7efcSopenharmony_ci#include "hitrace_meter.h"
22eace7efcSopenharmony_ci#include "permission_constants.h"
23eace7efcSopenharmony_ci#include "session_manager_lite.h"
24eace7efcSopenharmony_ci#include "utils/app_mgr_util.h"
25eace7efcSopenharmony_ci#include "wm_common.h"
26eace7efcSopenharmony_ci
27eace7efcSopenharmony_cinamespace OHOS {
28eace7efcSopenharmony_cinamespace AAFwk {
29eace7efcSopenharmony_ciusing namespace OHOS::EventFwk;
30eace7efcSopenharmony_ciusing namespace std::chrono;
31eace7efcSopenharmony_ciusing namespace std::placeholders;
32eace7efcSopenharmony_ci
33eace7efcSopenharmony_ciPendingWantManager::PendingWantManager()
34eace7efcSopenharmony_ci{
35eace7efcSopenharmony_ci}
36eace7efcSopenharmony_ci
37eace7efcSopenharmony_ciPendingWantManager::~PendingWantManager()
38eace7efcSopenharmony_ci{
39eace7efcSopenharmony_ci}
40eace7efcSopenharmony_ci
41eace7efcSopenharmony_cisptr<IWantSender> PendingWantManager::GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp,
42eace7efcSopenharmony_ci    const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex)
43eace7efcSopenharmony_ci{
44eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
45eace7efcSopenharmony_ci    if (wantSenderInfo.type != static_cast<int32_t>(OperationType::SEND_COMMON_EVENT)) {
46eace7efcSopenharmony_ci        if (callingUid != uid &&
47eace7efcSopenharmony_ci            !isSystemApp &&
48eace7efcSopenharmony_ci            !AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
49eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::WANTAGENT, "cannot send");
50eace7efcSopenharmony_ci            return nullptr;
51eace7efcSopenharmony_ci        }
52eace7efcSopenharmony_ci    }
53eace7efcSopenharmony_ci
54eace7efcSopenharmony_ci    if (wantSenderInfo.type == static_cast<int32_t>(OperationType::START_SERVICE_EXTENSION) && !isSystemApp &&
55eace7efcSopenharmony_ci        !AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
56eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "non-system app called");
57eace7efcSopenharmony_ci        return nullptr;
58eace7efcSopenharmony_ci    }
59eace7efcSopenharmony_ci
60eace7efcSopenharmony_ci    WantSenderInfo info = wantSenderInfo;
61eace7efcSopenharmony_ci
62eace7efcSopenharmony_ci    if (wantSenderInfo.type != static_cast<int32_t>(OperationType::SEND_COMMON_EVENT) &&
63eace7efcSopenharmony_ci        !isSystemApp && !AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
64eace7efcSopenharmony_ci        for (auto it = info.allWants.begin(); it != info.allWants.end(); ++it) {
65eace7efcSopenharmony_ci            if (info.bundleName != it->want.GetBundle()) {
66eace7efcSopenharmony_ci                info.allWants.erase(it);
67eace7efcSopenharmony_ci            } else {
68eace7efcSopenharmony_ci                it->want.RemoveParam("ohos.extra.param.key.appCloneIndex");
69eace7efcSopenharmony_ci            }
70eace7efcSopenharmony_ci        }
71eace7efcSopenharmony_ci    }
72eace7efcSopenharmony_ci
73eace7efcSopenharmony_ci    return GetWantSenderLocked(callingUid, uid, wantSenderInfo.userId, info, callerToken, appIndex);
74eace7efcSopenharmony_ci}
75eace7efcSopenharmony_ci
76eace7efcSopenharmony_cisptr<IWantSender> PendingWantManager::GetWantSenderLocked(const int32_t callingUid, const int32_t uid,
77eace7efcSopenharmony_ci    const int32_t userId, WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex)
78eace7efcSopenharmony_ci{
79eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
80eace7efcSopenharmony_ci
81eace7efcSopenharmony_ci    bool needCreate = (static_cast<uint32_t>(wantSenderInfo.flags) &
82eace7efcSopenharmony_ci        static_cast<uint32_t>(Flags::NO_BUILD_FLAG)) == 0;
83eace7efcSopenharmony_ci    bool needCancel = (static_cast<uint32_t>(wantSenderInfo.flags) &
84eace7efcSopenharmony_ci        static_cast<uint32_t>(Flags::CANCEL_PRESENT_FLAG)) != 0;
85eace7efcSopenharmony_ci    bool needUpdate = (static_cast<uint32_t>(wantSenderInfo.flags) &
86eace7efcSopenharmony_ci        static_cast<uint32_t>(Flags::UPDATE_PRESENT_FLAG)) != 0;
87eace7efcSopenharmony_ci
88eace7efcSopenharmony_ci    std::shared_ptr<PendingWantKey> pendingKey = std::make_shared<PendingWantKey>();
89eace7efcSopenharmony_ci    pendingKey->SetBundleName(wantSenderInfo.bundleName);
90eace7efcSopenharmony_ci    pendingKey->SetRequestWho(wantSenderInfo.resultWho);
91eace7efcSopenharmony_ci    pendingKey->SetRequestCode(wantSenderInfo.requestCode);
92eace7efcSopenharmony_ci    pendingKey->SetFlags(wantSenderInfo.flags);
93eace7efcSopenharmony_ci    pendingKey->SetUserId(wantSenderInfo.userId);
94eace7efcSopenharmony_ci    pendingKey->SetType(wantSenderInfo.type);
95eace7efcSopenharmony_ci    pendingKey->SetAppIndex(appIndex);
96eace7efcSopenharmony_ci    if (wantSenderInfo.allWants.size() > 0) {
97eace7efcSopenharmony_ci        pendingKey->SetRequestWant(wantSenderInfo.allWants.back().want);
98eace7efcSopenharmony_ci        pendingKey->SetRequestResolvedType(wantSenderInfo.allWants.back().resolvedTypes);
99eace7efcSopenharmony_ci        pendingKey->SetAllWantsInfos(wantSenderInfo.allWants);
100eace7efcSopenharmony_ci    }
101eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
102eace7efcSopenharmony_ci    auto ref = GetPendingWantRecordByKey(pendingKey);
103eace7efcSopenharmony_ci    if (ref != nullptr) {
104eace7efcSopenharmony_ci        if (!needCancel) {
105eace7efcSopenharmony_ci            if (needUpdate && wantSenderInfo.allWants.size() > 0) {
106eace7efcSopenharmony_ci                ref->GetKey()->SetRequestWant(wantSenderInfo.allWants.back().want);
107eace7efcSopenharmony_ci                ref->GetKey()->SetRequestResolvedType(wantSenderInfo.allWants.back().resolvedTypes);
108eace7efcSopenharmony_ci                wantSenderInfo.allWants.back().want = ref->GetKey()->GetRequestWant();
109eace7efcSopenharmony_ci                wantSenderInfo.allWants.back().resolvedTypes = ref->GetKey()->GetRequestResolvedType();
110eace7efcSopenharmony_ci                ref->GetKey()->SetAllWantsInfos(wantSenderInfo.allWants);
111eace7efcSopenharmony_ci                ref->SetCallerUid(callingUid);
112eace7efcSopenharmony_ci            }
113eace7efcSopenharmony_ci            return ref;
114eace7efcSopenharmony_ci        }
115eace7efcSopenharmony_ci        MakeWantSenderCanceledLocked(*ref);
116eace7efcSopenharmony_ci        wantRecords_.erase(ref->GetKey());
117eace7efcSopenharmony_ci        EraseBundleRecord(wantSenderInfo.allWants, ref->GetKey());
118eace7efcSopenharmony_ci    }
119eace7efcSopenharmony_ci
120eace7efcSopenharmony_ci    if (!needCreate) {
121eace7efcSopenharmony_ci        return (ref != nullptr) ? ref : nullptr;
122eace7efcSopenharmony_ci    }
123eace7efcSopenharmony_ci
124eace7efcSopenharmony_ci    sptr<PendingWantRecord> rec =
125eace7efcSopenharmony_ci        new (std::nothrow) PendingWantRecord(shared_from_this(), uid, IPCSkeleton::GetCallingTokenID(),
126eace7efcSopenharmony_ci        callerToken, pendingKey);
127eace7efcSopenharmony_ci    if (rec != nullptr) {
128eace7efcSopenharmony_ci        rec->SetCallerUid(callingUid);
129eace7efcSopenharmony_ci        pendingKey->SetCode(PendingRecordIdCreate());
130eace7efcSopenharmony_ci        wantRecords_.insert(std::make_pair(pendingKey, rec));
131eace7efcSopenharmony_ci        InsertBundleRecord(wantSenderInfo.allWants, pendingKey);
132eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::WANTAGENT, "wantRecords_ size %{public}zu", wantRecords_.size());
133eace7efcSopenharmony_ci        return rec;
134eace7efcSopenharmony_ci    }
135eace7efcSopenharmony_ci    return nullptr;
136eace7efcSopenharmony_ci}
137eace7efcSopenharmony_ci
138eace7efcSopenharmony_civoid PendingWantManager::MakeWantSenderCanceledLocked(PendingWantRecord &record)
139eace7efcSopenharmony_ci{
140eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
141eace7efcSopenharmony_ci
142eace7efcSopenharmony_ci    record.SetCanceled();
143eace7efcSopenharmony_ci    for (auto &callback : record.GetCancelCallbacks()) {
144eace7efcSopenharmony_ci        callback->Send(record.GetKey()->GetRequestCode());
145eace7efcSopenharmony_ci    }
146eace7efcSopenharmony_ci}
147eace7efcSopenharmony_ci
148eace7efcSopenharmony_cisptr<PendingWantRecord> PendingWantManager::GetPendingWantRecordByKey(const std::shared_ptr<PendingWantKey> &key)
149eace7efcSopenharmony_ci{
150eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
151eace7efcSopenharmony_ci    for (const auto &item : wantRecords_) {
152eace7efcSopenharmony_ci        const auto pendingKey = item.first;
153eace7efcSopenharmony_ci        const auto pendingRecord = item.second;
154eace7efcSopenharmony_ci        if ((pendingRecord != nullptr) && CheckPendingWantRecordByKey(pendingKey, key)) {
155eace7efcSopenharmony_ci            return pendingRecord;
156eace7efcSopenharmony_ci        }
157eace7efcSopenharmony_ci    }
158eace7efcSopenharmony_ci    return nullptr;
159eace7efcSopenharmony_ci}
160eace7efcSopenharmony_ci
161eace7efcSopenharmony_cibool PendingWantManager::CheckPendingWantRecordByKey(
162eace7efcSopenharmony_ci    const std::shared_ptr<PendingWantKey> &inputKey, const std::shared_ptr<PendingWantKey> &key)
163eace7efcSopenharmony_ci{
164eace7efcSopenharmony_ci    if (!inputKey || !key) {
165eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::WANTAGENT, "inputKey or key null");
166eace7efcSopenharmony_ci        return false;
167eace7efcSopenharmony_ci    }
168eace7efcSopenharmony_ci    if (inputKey->GetAppIndex() != key->GetAppIndex()) {
169eace7efcSopenharmony_ci        return false;
170eace7efcSopenharmony_ci    }
171eace7efcSopenharmony_ci    if (inputKey->GetBundleName().compare(key->GetBundleName()) != 0) {
172eace7efcSopenharmony_ci        return false;
173eace7efcSopenharmony_ci    }
174eace7efcSopenharmony_ci    if (inputKey->GetType() != key->GetType()) {
175eace7efcSopenharmony_ci        return false;
176eace7efcSopenharmony_ci    }
177eace7efcSopenharmony_ci    if (inputKey->GetRequestWho().compare(key->GetRequestWho()) != 0) {
178eace7efcSopenharmony_ci        return false;
179eace7efcSopenharmony_ci    }
180eace7efcSopenharmony_ci    if (inputKey->GetRequestCode() != key->GetRequestCode()) {
181eace7efcSopenharmony_ci        return false;
182eace7efcSopenharmony_ci    }
183eace7efcSopenharmony_ci
184eace7efcSopenharmony_ci    if (inputKey->GetRequestResolvedType().compare(key->GetRequestResolvedType()) != 0) {
185eace7efcSopenharmony_ci        return false;
186eace7efcSopenharmony_ci    }
187eace7efcSopenharmony_ci    if (inputKey->GetUserId() != key->GetUserId()) {
188eace7efcSopenharmony_ci        return false;
189eace7efcSopenharmony_ci    }
190eace7efcSopenharmony_ci
191eace7efcSopenharmony_ci    if (!inputKey->IsEqualsRequestWant(key->GetRequestWantRef())) {
192eace7efcSopenharmony_ci        return false;
193eace7efcSopenharmony_ci    }
194eace7efcSopenharmony_ci
195eace7efcSopenharmony_ci    return true;
196eace7efcSopenharmony_ci}
197eace7efcSopenharmony_ci
198eace7efcSopenharmony_ciint32_t PendingWantManager::SendWantSender(sptr<IWantSender> target, const SenderInfo &senderInfo)
199eace7efcSopenharmony_ci{
200eace7efcSopenharmony_ci    if (target == nullptr) {
201eace7efcSopenharmony_ci        if (senderInfo.finishedReceiver != nullptr) {
202eace7efcSopenharmony_ci            Want want;
203eace7efcSopenharmony_ci            WantParams wantParams = {};
204eace7efcSopenharmony_ci            senderInfo.finishedReceiver->PerformReceive(want, senderInfo.code, "canceled", wantParams, false, false, 0);
205eace7efcSopenharmony_ci        }
206eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null sender");
207eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
208eace7efcSopenharmony_ci    }
209eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
210eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
211eace7efcSopenharmony_ci        if (senderInfo.finishedReceiver != nullptr) {
212eace7efcSopenharmony_ci            Want want;
213eace7efcSopenharmony_ci            WantParams wantParams = {};
214eace7efcSopenharmony_ci            senderInfo.finishedReceiver->PerformReceive(want, senderInfo.code, "canceled", wantParams, false, false, 0);
215eace7efcSopenharmony_ci        }
216eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target object null or a proxy");
217eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
218eace7efcSopenharmony_ci    }
219eace7efcSopenharmony_ci    sptr<PendingWantRecord> record = iface_cast<PendingWantRecord>(obj);
220eace7efcSopenharmony_ci    SenderInfo info = senderInfo;
221eace7efcSopenharmony_ci    return record->SenderInner(info);
222eace7efcSopenharmony_ci}
223eace7efcSopenharmony_ci
224eace7efcSopenharmony_civoid PendingWantManager::CancelWantSender(const bool isSystemApp, const sptr<IWantSender> &sender)
225eace7efcSopenharmony_ci{
226eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
227eace7efcSopenharmony_ci
228eace7efcSopenharmony_ci    if (sender == nullptr) {
229eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null sender");
230eace7efcSopenharmony_ci        return;
231eace7efcSopenharmony_ci    }
232eace7efcSopenharmony_ci
233eace7efcSopenharmony_ci    auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
234eace7efcSopenharmony_ci    if (!isSaCall && !isSystemApp) {
235eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "cannot send");
236eace7efcSopenharmony_ci        return;
237eace7efcSopenharmony_ci    }
238eace7efcSopenharmony_ci
239eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = sender->AsObject();
240eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
241eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target object null or a proxy");
242eace7efcSopenharmony_ci        return;
243eace7efcSopenharmony_ci    }
244eace7efcSopenharmony_ci    sptr<PendingWantRecord> record = iface_cast<PendingWantRecord>(obj);
245eace7efcSopenharmony_ci    CancelWantSenderLocked(*record, true);
246eace7efcSopenharmony_ci}
247eace7efcSopenharmony_ci
248eace7efcSopenharmony_civoid PendingWantManager::CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility)
249eace7efcSopenharmony_ci{
250eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
251eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
252eace7efcSopenharmony_ci    MakeWantSenderCanceledLocked(record);
253eace7efcSopenharmony_ci    if (cleanAbility) {
254eace7efcSopenharmony_ci        wantRecords_.erase(record.GetKey());
255eace7efcSopenharmony_ci        if (record.GetKey() != nullptr) {
256eace7efcSopenharmony_ci            EraseBundleRecord(record.GetKey()->GetAllWantsInfos(), record.GetKey());
257eace7efcSopenharmony_ci        }
258eace7efcSopenharmony_ci    }
259eace7efcSopenharmony_ci}
260eace7efcSopenharmony_ci
261eace7efcSopenharmony_ciint32_t PendingWantManager::DeviceIdDetermine(const Want &want, const sptr<StartOptions> &startOptions,
262eace7efcSopenharmony_ci    const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId)
263eace7efcSopenharmony_ci{
264eace7efcSopenharmony_ci    int32_t result = ERR_OK;
265eace7efcSopenharmony_ci    std::string localDeviceId;
266eace7efcSopenharmony_ci    DelayedSingleton<AbilityManagerService>::GetInstance()->GetLocalDeviceId(localDeviceId);
267eace7efcSopenharmony_ci    if (want.GetElement().GetDeviceID() == "" || want.GetElement().GetDeviceID() == localDeviceId) {
268eace7efcSopenharmony_ci        if (!startOptions) {
269eace7efcSopenharmony_ci            result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbilityWithSpecifyTokenIdInner(
270eace7efcSopenharmony_ci                want, callerToken, callerTokenId, true, requestCode, callerUid);
271eace7efcSopenharmony_ci        } else {
272eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::WANTAGENT, "StartOptions windowMode:%{public}d displayId:%{public}d \
273eace7efcSopenharmony_ci                withAnimation:%{public}d windowLeft:%{public}d windowTop:%{public}d windowWidth:%{public}d \
274eace7efcSopenharmony_ci                windowHeight:%{public}d",
275eace7efcSopenharmony_ci                startOptions->GetWindowMode(), startOptions->GetDisplayID(), startOptions->GetWithAnimation(),
276eace7efcSopenharmony_ci                startOptions->GetWindowLeft(), startOptions->GetWindowTop(), startOptions->GetWindowWidth(),
277eace7efcSopenharmony_ci                startOptions->GetWindowHeight());
278eace7efcSopenharmony_ci            result = DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbilityWithSpecifyTokenIdInner(
279eace7efcSopenharmony_ci                want, *startOptions, callerToken, true, requestCode, callerUid, callerTokenId);
280eace7efcSopenharmony_ci        }
281eace7efcSopenharmony_ci
282eace7efcSopenharmony_ci        if (result != ERR_OK && result != START_ABILITY_WAITING) {
283eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::WANTAGENT, "startAbility failed");
284eace7efcSopenharmony_ci        }
285eace7efcSopenharmony_ci        return result;
286eace7efcSopenharmony_ci    }
287eace7efcSopenharmony_ci
288eace7efcSopenharmony_ci    sptr<IRemoteObject> remoteObject =
289eace7efcSopenharmony_ci        OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
290eace7efcSopenharmony_ci    if (remoteObject == nullptr) {
291eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "GetSystemAbility failed");
292eace7efcSopenharmony_ci        result = ERR_INVALID_VALUE;
293eace7efcSopenharmony_ci        return result;
294eace7efcSopenharmony_ci    }
295eace7efcSopenharmony_ci    DistributedClient dmsClient;
296eace7efcSopenharmony_ci    uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
297eace7efcSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
298eace7efcSopenharmony_ci    result = dmsClient.StartRemoteAbility(want, callingUid, requestCode, accessToken);
299eace7efcSopenharmony_ci    if (result != ERR_OK) {
300eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "StartRemoteAbility failed result = %{public}d", result);
301eace7efcSopenharmony_ci    }
302eace7efcSopenharmony_ci
303eace7efcSopenharmony_ci    return result;
304eace7efcSopenharmony_ci}
305eace7efcSopenharmony_ci
306eace7efcSopenharmony_ciint32_t PendingWantManager::PendingWantStartAbility(const Want &want, const sptr<StartOptions> &startOptions,
307eace7efcSopenharmony_ci    const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId)
308eace7efcSopenharmony_ci{
309eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::WANTAGENT, "begin");
310eace7efcSopenharmony_ci    if (!CheckCallerPermission()) {
311eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
312eace7efcSopenharmony_ci    }
313eace7efcSopenharmony_ci    int32_t result = DeviceIdDetermine(want, startOptions, callerToken, requestCode, callerUid, callerTokenId);
314eace7efcSopenharmony_ci    return result;
315eace7efcSopenharmony_ci}
316eace7efcSopenharmony_ci
317eace7efcSopenharmony_ciint32_t PendingWantManager::PendingWantStartServiceExtension(Want &want, const sptr<IRemoteObject> &callerToken)
318eace7efcSopenharmony_ci{
319eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::WANTAGENT, "called");
320eace7efcSopenharmony_ci    if (!PermissionVerification::GetInstance()->IsSystemAppCall()
321eace7efcSopenharmony_ci        && !PermissionVerification::GetInstance()->IsSACall()) {
322eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "non-system app called");
323eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
324eace7efcSopenharmony_ci    }
325eace7efcSopenharmony_ci    //reset flags
326eace7efcSopenharmony_ci    want.SetFlags(0);
327eace7efcSopenharmony_ci    return DelayedSingleton<AbilityManagerService>::GetInstance()->StartExtensionAbility(want, callerToken);
328eace7efcSopenharmony_ci}
329eace7efcSopenharmony_ci
330eace7efcSopenharmony_ciint32_t PendingWantManager::PendingWantStartAbilitys(const std::vector<WantsInfo> &wantsInfo,
331eace7efcSopenharmony_ci    const sptr<StartOptions> &startOptions, const sptr<IRemoteObject> &callerToken, int32_t requestCode,
332eace7efcSopenharmony_ci    const int32_t callerUid, int32_t callerTokenId)
333eace7efcSopenharmony_ci{
334eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::WANTAGENT, "begin");
335eace7efcSopenharmony_ci
336eace7efcSopenharmony_ci    if (!CheckCallerPermission()) {
337eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
338eace7efcSopenharmony_ci    }
339eace7efcSopenharmony_ci    int32_t result = ERR_OK;
340eace7efcSopenharmony_ci    for (const auto &item : wantsInfo) {
341eace7efcSopenharmony_ci        auto res = DeviceIdDetermine(item.want, startOptions, callerToken, requestCode, callerUid, callerTokenId);
342eace7efcSopenharmony_ci        if (res != ERR_OK && res != START_ABILITY_WAITING) {
343eace7efcSopenharmony_ci            result = res;
344eace7efcSopenharmony_ci        }
345eace7efcSopenharmony_ci    }
346eace7efcSopenharmony_ci    return result;
347eace7efcSopenharmony_ci}
348eace7efcSopenharmony_ci
349eace7efcSopenharmony_ciint32_t PendingWantManager::PendingWantPublishCommonEvent(
350eace7efcSopenharmony_ci    const Want &want, const SenderInfo &senderInfo, int32_t callerUid, int32_t callerTokenId)
351eace7efcSopenharmony_ci{
352eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::WANTAGENT, "begin");
353eace7efcSopenharmony_ci
354eace7efcSopenharmony_ci    CommonEventData eventData;
355eace7efcSopenharmony_ci    eventData.SetWant(want);
356eace7efcSopenharmony_ci    eventData.SetCode(senderInfo.code);
357eace7efcSopenharmony_ci
358eace7efcSopenharmony_ci    CommonEventPublishInfo eventPublishData;
359eace7efcSopenharmony_ci
360eace7efcSopenharmony_ci    if (!want.GetBundle().empty()) {
361eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::WANTAGENT, "eventPublishData set bundleName: %{public}s", want.GetBundle().c_str());
362eace7efcSopenharmony_ci        eventPublishData.SetBundleName(want.GetBundle());
363eace7efcSopenharmony_ci    }
364eace7efcSopenharmony_ci
365eace7efcSopenharmony_ci    if (!senderInfo.requiredPermission.empty()) {
366eace7efcSopenharmony_ci        std::vector<std::string> permissions;
367eace7efcSopenharmony_ci        permissions.emplace_back(senderInfo.requiredPermission);
368eace7efcSopenharmony_ci        eventPublishData.SetSubscriberPermissions(permissions);
369eace7efcSopenharmony_ci    }
370eace7efcSopenharmony_ci
371eace7efcSopenharmony_ci    bool result = IN_PROCESS_CALL(DelayedSingleton<EventFwk::CommonEvent>::GetInstance()->PublishCommonEvent(
372eace7efcSopenharmony_ci        eventData, eventPublishData, nullptr, callerUid, callerTokenId));
373eace7efcSopenharmony_ci    return ((result == true) ? ERR_OK : (-1));
374eace7efcSopenharmony_ci}
375eace7efcSopenharmony_ci
376eace7efcSopenharmony_ciint32_t PendingWantManager::PendingRecordIdCreate()
377eace7efcSopenharmony_ci{
378eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
379eace7efcSopenharmony_ci
380eace7efcSopenharmony_ci    static std::atomic_int id(0);
381eace7efcSopenharmony_ci    return ++id;
382eace7efcSopenharmony_ci}
383eace7efcSopenharmony_ci
384eace7efcSopenharmony_cisptr<PendingWantRecord> PendingWantManager::GetPendingWantRecordByCode(int32_t code)
385eace7efcSopenharmony_ci{
386eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
387eace7efcSopenharmony_ci
388eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
389eace7efcSopenharmony_ci    auto iter = std::find_if(wantRecords_.begin(), wantRecords_.end(), [&code](const auto &pair) {
390eace7efcSopenharmony_ci        return pair.second->GetKey()->GetCode() == code;
391eace7efcSopenharmony_ci    });
392eace7efcSopenharmony_ci    return ((iter == wantRecords_.end()) ? nullptr : iter->second);
393eace7efcSopenharmony_ci}
394eace7efcSopenharmony_ci
395eace7efcSopenharmony_ciint32_t PendingWantManager::GetPendingWantUid(const sptr<IWantSender> &target)
396eace7efcSopenharmony_ci{
397eace7efcSopenharmony_ci    if (target == nullptr) {
398eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
399eace7efcSopenharmony_ci        return -1;
400eace7efcSopenharmony_ci    }
401eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
402eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
403eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
404eace7efcSopenharmony_ci        return -1;
405eace7efcSopenharmony_ci    }
406eace7efcSopenharmony_ci
407eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
408eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
409eace7efcSopenharmony_ci    return ((record != nullptr) ? (record->GetUid()) : (-1));
410eace7efcSopenharmony_ci}
411eace7efcSopenharmony_ci
412eace7efcSopenharmony_ciint32_t PendingWantManager::GetPendingWantUserId(const sptr<IWantSender> &target)
413eace7efcSopenharmony_ci{
414eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
415eace7efcSopenharmony_ci
416eace7efcSopenharmony_ci    if (target == nullptr) {
417eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
418eace7efcSopenharmony_ci        return -1;
419eace7efcSopenharmony_ci    }
420eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
421eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
422eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
423eace7efcSopenharmony_ci        return -1;
424eace7efcSopenharmony_ci    }
425eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
426eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
427eace7efcSopenharmony_ci    return ((record != nullptr) ? (record->GetKey()->GetUserId()) : (-1));
428eace7efcSopenharmony_ci}
429eace7efcSopenharmony_ci
430eace7efcSopenharmony_cistd::string PendingWantManager::GetPendingWantBundleName(const sptr<IWantSender> &target)
431eace7efcSopenharmony_ci{
432eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
433eace7efcSopenharmony_ci
434eace7efcSopenharmony_ci    if (target == nullptr) {
435eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
436eace7efcSopenharmony_ci        return "";
437eace7efcSopenharmony_ci    }
438eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
439eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
440eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
441eace7efcSopenharmony_ci        return "";
442eace7efcSopenharmony_ci    }
443eace7efcSopenharmony_ci
444eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
445eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
446eace7efcSopenharmony_ci    if (record != nullptr) {
447eace7efcSopenharmony_ci        return record->GetKey()->GetBundleName();
448eace7efcSopenharmony_ci    }
449eace7efcSopenharmony_ci    return "";
450eace7efcSopenharmony_ci}
451eace7efcSopenharmony_ci
452eace7efcSopenharmony_ciint32_t PendingWantManager::GetPendingWantCode(const sptr<IWantSender> &target)
453eace7efcSopenharmony_ci{
454eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
455eace7efcSopenharmony_ci
456eace7efcSopenharmony_ci    if (target == nullptr) {
457eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
458eace7efcSopenharmony_ci        return -1;
459eace7efcSopenharmony_ci    }
460eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
461eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
462eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
463eace7efcSopenharmony_ci        return -1;
464eace7efcSopenharmony_ci    }
465eace7efcSopenharmony_ci
466eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
467eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
468eace7efcSopenharmony_ci    return ((record != nullptr) ? (record->GetKey()->GetCode()) : (-1));
469eace7efcSopenharmony_ci}
470eace7efcSopenharmony_ci
471eace7efcSopenharmony_ciint32_t PendingWantManager::GetPendingWantType(const sptr<IWantSender> &target)
472eace7efcSopenharmony_ci{
473eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
474eace7efcSopenharmony_ci
475eace7efcSopenharmony_ci    if (target == nullptr) {
476eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
477eace7efcSopenharmony_ci        return -1;
478eace7efcSopenharmony_ci    }
479eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
480eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
481eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
482eace7efcSopenharmony_ci        return -1;
483eace7efcSopenharmony_ci    }
484eace7efcSopenharmony_ci
485eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
486eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
487eace7efcSopenharmony_ci    return ((record != nullptr) ? (record->GetKey()->GetType()) : (-1));
488eace7efcSopenharmony_ci}
489eace7efcSopenharmony_ci
490eace7efcSopenharmony_civoid PendingWantManager::RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier)
491eace7efcSopenharmony_ci{
492eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::WANTAGENT, "begin");
493eace7efcSopenharmony_ci
494eace7efcSopenharmony_ci    if ((sender == nullptr) || (recevier == nullptr)) {
495eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "sender or recevier null");
496eace7efcSopenharmony_ci        return;
497eace7efcSopenharmony_ci    }
498eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = sender->AsObject();
499eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
500eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
501eace7efcSopenharmony_ci        return;
502eace7efcSopenharmony_ci    }
503eace7efcSopenharmony_ci
504eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
505eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
506eace7efcSopenharmony_ci    if (record == nullptr) {
507eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null record. code = %{public}d",
508eace7efcSopenharmony_ci            targetRecord->GetKey()->GetCode());
509eace7efcSopenharmony_ci        return;
510eace7efcSopenharmony_ci    }
511eace7efcSopenharmony_ci    bool cancel = record->GetCanceled();
512eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
513eace7efcSopenharmony_ci    if (!cancel) {
514eace7efcSopenharmony_ci        record->RegisterCancelListener(recevier);
515eace7efcSopenharmony_ci    }
516eace7efcSopenharmony_ci}
517eace7efcSopenharmony_ci
518eace7efcSopenharmony_civoid PendingWantManager::UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier)
519eace7efcSopenharmony_ci{
520eace7efcSopenharmony_ci    if (sender == nullptr || recevier == nullptr) {
521eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "sender or recevier null");
522eace7efcSopenharmony_ci        return;
523eace7efcSopenharmony_ci    }
524eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = sender->AsObject();
525eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
526eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
527eace7efcSopenharmony_ci        return;
528eace7efcSopenharmony_ci    }
529eace7efcSopenharmony_ci
530eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
531eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
532eace7efcSopenharmony_ci    if (record == nullptr) {
533eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null record");
534eace7efcSopenharmony_ci        return;
535eace7efcSopenharmony_ci    }
536eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
537eace7efcSopenharmony_ci    record->UnregisterCancelListener(recevier);
538eace7efcSopenharmony_ci}
539eace7efcSopenharmony_ci
540eace7efcSopenharmony_ciint32_t PendingWantManager::GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want)
541eace7efcSopenharmony_ci{
542eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
543eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
544eace7efcSopenharmony_ci    if (target == nullptr) {
545eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
546eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
547eace7efcSopenharmony_ci    }
548eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
549eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
550eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
551eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
552eace7efcSopenharmony_ci    }
553eace7efcSopenharmony_ci
554eace7efcSopenharmony_ci    if (want == nullptr) {
555eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null want");
556eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
557eace7efcSopenharmony_ci    }
558eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
559eace7efcSopenharmony_ci
560eace7efcSopenharmony_ci    if (targetRecord == nullptr) {
561eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null targetRecord");
562eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
563eace7efcSopenharmony_ci    }
564eace7efcSopenharmony_ci
565eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
566eace7efcSopenharmony_ci    if (record == nullptr) {
567eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null record");
568eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
569eace7efcSopenharmony_ci    }
570eace7efcSopenharmony_ci    want.reset(new (std::nothrow) Want(record->GetKey()->GetRequestWant()));
571eace7efcSopenharmony_ci    return NO_ERROR;
572eace7efcSopenharmony_ci}
573eace7efcSopenharmony_ci
574eace7efcSopenharmony_ciint32_t PendingWantManager::GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info)
575eace7efcSopenharmony_ci{
576eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
577eace7efcSopenharmony_ci    if (target == nullptr) {
578eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null target");
579eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
580eace7efcSopenharmony_ci    }
581eace7efcSopenharmony_ci    sptr<IRemoteObject> obj = target->AsObject();
582eace7efcSopenharmony_ci    if (obj == nullptr || obj->IsProxyObject()) {
583eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "target obj null or a proxy object");
584eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
585eace7efcSopenharmony_ci    }
586eace7efcSopenharmony_ci    if (info == nullptr) {
587eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null info");
588eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
589eace7efcSopenharmony_ci    }
590eace7efcSopenharmony_ci    sptr<PendingWantRecord> targetRecord = iface_cast<PendingWantRecord>(obj);
591eace7efcSopenharmony_ci    auto record = GetPendingWantRecordByCode(targetRecord->GetKey()->GetCode());
592eace7efcSopenharmony_ci    if (record == nullptr) {
593eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null record");
594eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
595eace7efcSopenharmony_ci    }
596eace7efcSopenharmony_ci    WantSenderInfo wantSenderInfo;
597eace7efcSopenharmony_ci    wantSenderInfo.requestCode = record->GetKey()->GetRequestCode();
598eace7efcSopenharmony_ci    wantSenderInfo.type = record->GetKey()->GetType();
599eace7efcSopenharmony_ci    wantSenderInfo.flags = (uint32_t)(record->GetKey()->GetFlags());
600eace7efcSopenharmony_ci    wantSenderInfo.allWants = record->GetKey()->GetAllWantsInfos();
601eace7efcSopenharmony_ci    info.reset(new (std::nothrow) WantSenderInfo(wantSenderInfo));
602eace7efcSopenharmony_ci    return NO_ERROR;
603eace7efcSopenharmony_ci}
604eace7efcSopenharmony_ci
605eace7efcSopenharmony_civoid PendingWantManager::ClearPendingWantRecord(const std::string &bundleName, int32_t uid)
606eace7efcSopenharmony_ci{
607eace7efcSopenharmony_ci    auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
608eace7efcSopenharmony_ci    CHECK_POINTER(abilityManagerService);
609eace7efcSopenharmony_ci    auto handler = abilityManagerService->GetTaskHandler();
610eace7efcSopenharmony_ci    CHECK_POINTER(handler);
611eace7efcSopenharmony_ci    auto task = [bundleName, uid, self = shared_from_this()]() { self->ClearPendingWantRecordTask(bundleName, uid); };
612eace7efcSopenharmony_ci    handler->SubmitTask(task);
613eace7efcSopenharmony_ci}
614eace7efcSopenharmony_ci
615eace7efcSopenharmony_civoid PendingWantManager::ClearPendingWantRecordTask(const std::string &bundleName, int32_t uid)
616eace7efcSopenharmony_ci{
617eace7efcSopenharmony_ci    if (!QueryRecordByBundle(bundleName)) {
618eace7efcSopenharmony_ci        return;
619eace7efcSopenharmony_ci    }
620eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(mutex_);
621eace7efcSopenharmony_ci    if (!QueryRecordByBundle(bundleName)) {
622eace7efcSopenharmony_ci        return;
623eace7efcSopenharmony_ci    }
624eace7efcSopenharmony_ci    auto iter = wantRecords_.begin();
625eace7efcSopenharmony_ci    while (iter != wantRecords_.end()) {
626eace7efcSopenharmony_ci        bool hasBundle = false;
627eace7efcSopenharmony_ci        const auto &pendingRecord = iter->second;
628eace7efcSopenharmony_ci        if ((pendingRecord != nullptr)) {
629eace7efcSopenharmony_ci            auto wantInfos = pendingRecord->GetKey()->GetAllWantsInfos();
630eace7efcSopenharmony_ci            for (const auto &wantInfo: wantInfos) {
631eace7efcSopenharmony_ci                if (wantInfo.want.GetBundle() == bundleName && uid == pendingRecord->GetUid()) {
632eace7efcSopenharmony_ci                    hasBundle = true;
633eace7efcSopenharmony_ci                    break;
634eace7efcSopenharmony_ci                }
635eace7efcSopenharmony_ci            }
636eace7efcSopenharmony_ci            if (hasBundle) {
637eace7efcSopenharmony_ci                EraseBundleRecord(wantInfos, pendingRecord->GetKey());
638eace7efcSopenharmony_ci                iter = wantRecords_.erase(iter);
639eace7efcSopenharmony_ci                TAG_LOGI(AAFwkTag::WANTAGENT, "wantRecords_ size %{public}zu", wantRecords_.size());
640eace7efcSopenharmony_ci            } else {
641eace7efcSopenharmony_ci                ++iter;
642eace7efcSopenharmony_ci            }
643eace7efcSopenharmony_ci        } else {
644eace7efcSopenharmony_ci            ++iter;
645eace7efcSopenharmony_ci        }
646eace7efcSopenharmony_ci    }
647eace7efcSopenharmony_ci}
648eace7efcSopenharmony_ci
649eace7efcSopenharmony_cibool PendingWantManager::CheckCallerPermission()
650eace7efcSopenharmony_ci{
651eace7efcSopenharmony_ci    auto callerPid = IPCSkeleton::GetCallingPid();
652eace7efcSopenharmony_ci    AppExecFwk::RunningProcessInfo processInfo;
653eace7efcSopenharmony_ci    DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo);
654eace7efcSopenharmony_ci    auto permission = DelayedSingleton<PermissionVerification>::GetInstance();
655eace7efcSopenharmony_ci    if (permission == nullptr) {
656eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null permission");
657eace7efcSopenharmony_ci        return false;
658eace7efcSopenharmony_ci    }
659eace7efcSopenharmony_ci    if ((!processInfo.isFocused && !processInfo.isAbilityForegrounding) ||
660eace7efcSopenharmony_ci        (!permission->IsSystemAppCall() && !CheckWindowState(callerPid))) {
661eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::WANTAGENT, "caller unfocused");
662eace7efcSopenharmony_ci        if (!permission->VerifyCallingPermission(PermissionConstants::PERMISSION_START_ABILITIES_FROM_BACKGROUND) &&
663eace7efcSopenharmony_ci            !permission->VerifyCallingPermission(PermissionConstants::PERMISSION_START_ABILIIES_FROM_BACKGROUND) &&
664eace7efcSopenharmony_ci            !permission->IsSACall()) {
665eace7efcSopenharmony_ci            TAG_LOGW(AAFwkTag::WANTAGENT, "caller PERMISSION_DENIED");
666eace7efcSopenharmony_ci            return false;
667eace7efcSopenharmony_ci        }
668eace7efcSopenharmony_ci    }
669eace7efcSopenharmony_ci    return true;
670eace7efcSopenharmony_ci}
671eace7efcSopenharmony_ci
672eace7efcSopenharmony_cibool PendingWantManager::CheckWindowState(int32_t pid)
673eace7efcSopenharmony_ci{
674eace7efcSopenharmony_ci    auto sceneSessionManager = Rosen::SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
675eace7efcSopenharmony_ci    if (sceneSessionManager == nullptr) {
676eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "null manager");
677eace7efcSopenharmony_ci        return false;
678eace7efcSopenharmony_ci    }
679eace7efcSopenharmony_ci    std::vector<Rosen::MainWindowState> windowStates;
680eace7efcSopenharmony_ci    Rosen::WSError ret = sceneSessionManager->GetMainWindowStatesByPid(pid, windowStates);
681eace7efcSopenharmony_ci    if (ret != Rosen::WSError::WS_OK) {
682eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "fail GetWindow");
683eace7efcSopenharmony_ci        return false;
684eace7efcSopenharmony_ci    }
685eace7efcSopenharmony_ci    for (auto &windowState : windowStates) {
686eace7efcSopenharmony_ci        if (!windowState.isPcOrPadEnableActivation_ && !windowState.isForegroundInteractive_) {
687eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::WANTAGENT, "window interactive");
688eace7efcSopenharmony_ci            return false;
689eace7efcSopenharmony_ci        }
690eace7efcSopenharmony_ci    }
691eace7efcSopenharmony_ci    return true;
692eace7efcSopenharmony_ci}
693eace7efcSopenharmony_ci
694eace7efcSopenharmony_civoid PendingWantManager::Dump(std::vector<std::string> &info)
695eace7efcSopenharmony_ci{
696eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "dump begin");
697eace7efcSopenharmony_ci    std::string dumpInfo = "    PendingWantRecords:";
698eace7efcSopenharmony_ci    info.push_back(dumpInfo);
699eace7efcSopenharmony_ci
700eace7efcSopenharmony_ci    for (const auto &item : wantRecords_) {
701eace7efcSopenharmony_ci        const auto &pendingKey = item.first;
702eace7efcSopenharmony_ci        dumpInfo = "        PendWantRecord ID #" + std::to_string(pendingKey->GetCode()) +
703eace7efcSopenharmony_ci            "  type #" + std::to_string(pendingKey->GetType());
704eace7efcSopenharmony_ci        info.push_back(dumpInfo);
705eace7efcSopenharmony_ci        dumpInfo = "        bundle name [" + pendingKey->GetBundleName() + "]";
706eace7efcSopenharmony_ci        info.push_back(dumpInfo);
707eace7efcSopenharmony_ci        dumpInfo = "        result who [" + pendingKey->GetRequestWho() + "]";
708eace7efcSopenharmony_ci        info.push_back(dumpInfo);
709eace7efcSopenharmony_ci        dumpInfo = "        request code #" + std::to_string(pendingKey->GetRequestCode()) +
710eace7efcSopenharmony_ci            "  flags #" + std::to_string(pendingKey->GetFlags());
711eace7efcSopenharmony_ci        info.push_back(dumpInfo);
712eace7efcSopenharmony_ci        dumpInfo = "        resolved type [" + pendingKey->GetRequestResolvedType() + "]";
713eace7efcSopenharmony_ci        info.push_back(dumpInfo);
714eace7efcSopenharmony_ci        dumpInfo = "        Wants:";
715eace7efcSopenharmony_ci        info.push_back(dumpInfo);
716eace7efcSopenharmony_ci        auto Wants = pendingKey->GetAllWantsInfos();
717eace7efcSopenharmony_ci        for (const auto &Want : Wants) {
718eace7efcSopenharmony_ci            dumpInfo = "          uri [" + Want.want.GetElement().GetDeviceID() + "//" +
719eace7efcSopenharmony_ci                Want.want.GetElement().GetBundleName() + "/" + Want.want.GetElement().GetAbilityName() + "]";
720eace7efcSopenharmony_ci            info.push_back(dumpInfo);
721eace7efcSopenharmony_ci            dumpInfo = "          resolved types [" + Want.resolvedTypes + "]";
722eace7efcSopenharmony_ci            info.push_back(dumpInfo);
723eace7efcSopenharmony_ci        }
724eace7efcSopenharmony_ci    }
725eace7efcSopenharmony_ci}
726eace7efcSopenharmony_civoid PendingWantManager::DumpByRecordId(std::vector<std::string> &info, const std::string &args)
727eace7efcSopenharmony_ci{
728eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::WANTAGENT, "dump by id begin");
729eace7efcSopenharmony_ci    std::string dumpInfo = "    PendingWantRecords:";
730eace7efcSopenharmony_ci    info.push_back(dumpInfo);
731eace7efcSopenharmony_ci
732eace7efcSopenharmony_ci    for (const auto &item : wantRecords_) {
733eace7efcSopenharmony_ci        const auto &pendingKey = item.first;
734eace7efcSopenharmony_ci        if (args == std::to_string(pendingKey->GetCode())) {
735eace7efcSopenharmony_ci            dumpInfo = "        PendWantRecord ID #" + std::to_string(pendingKey->GetCode()) +
736eace7efcSopenharmony_ci                "  type #" + std::to_string(pendingKey->GetType());
737eace7efcSopenharmony_ci                info.push_back(dumpInfo);
738eace7efcSopenharmony_ci                dumpInfo = "        bundle name [" + pendingKey->GetBundleName() + "]";
739eace7efcSopenharmony_ci                info.push_back(dumpInfo);
740eace7efcSopenharmony_ci                dumpInfo = "        result who [" + pendingKey->GetRequestWho() + "]";
741eace7efcSopenharmony_ci                info.push_back(dumpInfo);
742eace7efcSopenharmony_ci                dumpInfo = "        request code #" + std::to_string(pendingKey->GetRequestCode()) +
743eace7efcSopenharmony_ci                "  flags #" + std::to_string(pendingKey->GetFlags());
744eace7efcSopenharmony_ci                info.push_back(dumpInfo);
745eace7efcSopenharmony_ci            dumpInfo = "        resolved type [" + pendingKey->GetRequestResolvedType() + "]";
746eace7efcSopenharmony_ci            info.push_back(dumpInfo);
747eace7efcSopenharmony_ci            dumpInfo = "        Wants:";
748eace7efcSopenharmony_ci            info.push_back(dumpInfo);
749eace7efcSopenharmony_ci            auto Wants = pendingKey->GetAllWantsInfos();
750eace7efcSopenharmony_ci            for (const auto& Want : Wants) {
751eace7efcSopenharmony_ci                dumpInfo = "          uri [" + Want.want.GetElement().GetDeviceID() + "//" +
752eace7efcSopenharmony_ci                    Want.want.GetElement().GetBundleName() + "/" + Want.want.GetElement().GetAbilityName() + "]";
753eace7efcSopenharmony_ci                info.push_back(dumpInfo);
754eace7efcSopenharmony_ci                dumpInfo = "          resolved types [" + Want.resolvedTypes + "]";
755eace7efcSopenharmony_ci                info.push_back(dumpInfo);
756eace7efcSopenharmony_ci            }
757eace7efcSopenharmony_ci        }
758eace7efcSopenharmony_ci    }
759eace7efcSopenharmony_ci}
760eace7efcSopenharmony_ci
761eace7efcSopenharmony_ciint32_t PendingWantManager::GetAllRunningInstanceKeysByBundleName(
762eace7efcSopenharmony_ci    const std::string &bundleName, std::vector<std::string> &appKeyVec)
763eace7efcSopenharmony_ci{
764eace7efcSopenharmony_ci    if (bundleName.empty()) {
765eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "bundle name is empty");
766eace7efcSopenharmony_ci        return ERR_INVALID_VALUE;
767eace7efcSopenharmony_ci    }
768eace7efcSopenharmony_ci
769eace7efcSopenharmony_ci    auto appMgr = AppMgrUtil::GetAppMgr();
770eace7efcSopenharmony_ci    if (appMgr == nullptr) {
771eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "app mgr is null");
772eace7efcSopenharmony_ci        return OBJECT_NULL;
773eace7efcSopenharmony_ci    }
774eace7efcSopenharmony_ci
775eace7efcSopenharmony_ci    return IN_PROCESS_CALL(appMgr->GetAllRunningInstanceKeysByBundleName(bundleName, appKeyVec));
776eace7efcSopenharmony_ci}
777eace7efcSopenharmony_ci
778eace7efcSopenharmony_civoid PendingWantManager::EraseBundleRecord(
779eace7efcSopenharmony_ci    const std::vector<WantsInfo> &wantsInfos, std::shared_ptr<PendingWantKey> key)
780eace7efcSopenharmony_ci{
781eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(bundleRecordsMutex_);
782eace7efcSopenharmony_ci    for (const auto &wantInfo : wantsInfos) {
783eace7efcSopenharmony_ci        auto it = bundleRecords_.find(wantInfo.want.GetBundle());
784eace7efcSopenharmony_ci        if (it != bundleRecords_.end()) {
785eace7efcSopenharmony_ci            auto &recordVec = it->second;
786eace7efcSopenharmony_ci            recordVec.erase(std::remove_if(recordVec.begin(), recordVec.end(),
787eace7efcSopenharmony_ci                [key](std::shared_ptr<PendingWantKey> value) {
788eace7efcSopenharmony_ci                    return value == key;
789eace7efcSopenharmony_ci                }),
790eace7efcSopenharmony_ci                recordVec.end());
791eace7efcSopenharmony_ci            if (recordVec.empty()) {
792eace7efcSopenharmony_ci                bundleRecords_.erase(wantInfo.want.GetBundle());
793eace7efcSopenharmony_ci            }
794eace7efcSopenharmony_ci        }
795eace7efcSopenharmony_ci    }
796eace7efcSopenharmony_ci}
797eace7efcSopenharmony_ci
798eace7efcSopenharmony_civoid PendingWantManager::InsertBundleRecord(
799eace7efcSopenharmony_ci    const std::vector<WantsInfo> &wantsInfos, std::shared_ptr<PendingWantKey> key)
800eace7efcSopenharmony_ci{
801eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(bundleRecordsMutex_);
802eace7efcSopenharmony_ci    for (const auto &wantInfo : wantsInfos) {
803eace7efcSopenharmony_ci        auto it = bundleRecords_.find(wantInfo.want.GetBundle());
804eace7efcSopenharmony_ci        if (it != bundleRecords_.end()) {
805eace7efcSopenharmony_ci            auto &recordVec = it->second;
806eace7efcSopenharmony_ci            recordVec.emplace_back(key);
807eace7efcSopenharmony_ci        } else {
808eace7efcSopenharmony_ci            std::vector<std::shared_ptr<PendingWantKey>> pendingWantVector;
809eace7efcSopenharmony_ci            pendingWantVector.emplace_back(key);
810eace7efcSopenharmony_ci            bundleRecords_[wantInfo.want.GetBundle()] = pendingWantVector;
811eace7efcSopenharmony_ci        }
812eace7efcSopenharmony_ci    }
813eace7efcSopenharmony_ci}
814eace7efcSopenharmony_ci
815eace7efcSopenharmony_cibool PendingWantManager::QueryRecordByBundle(const std::string &bundleName)
816eace7efcSopenharmony_ci{
817eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> locker(bundleRecordsMutex_);
818eace7efcSopenharmony_ci    auto bundleIter = bundleRecords_.find(bundleName);
819eace7efcSopenharmony_ci    if (bundleIter != bundleRecords_.end()) {
820eace7efcSopenharmony_ci        return true;
821eace7efcSopenharmony_ci    }
822eace7efcSopenharmony_ci    return false;
823eace7efcSopenharmony_ci}
824eace7efcSopenharmony_ci}  // namespace AAFwk
825eace7efcSopenharmony_ci}  // namespace OHOS
826