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 #include "pending_want_record.h"
17
18 #include "hilog_wrapper.h"
19 #include "iremote_object.h"
20 #include "pending_want_manager.h"
21
22 namespace OHOS {
23 namespace AAFwk {
PendingWantRecord()24 PendingWantRecord::PendingWantRecord()
25 {}
26
PendingWantRecord(const std::shared_ptr<PendingWantManager> & pendingWantManager,int32_t uid,int32_t callerTokenId,const sptr<IRemoteObject> & callerToken,std::shared_ptr<PendingWantKey> key)27 PendingWantRecord::PendingWantRecord(const std::shared_ptr<PendingWantManager> &pendingWantManager, int32_t uid,
28 int32_t callerTokenId, const sptr<IRemoteObject> &callerToken, std::shared_ptr<PendingWantKey> key)
29 : pendingWantManager_(pendingWantManager), uid_(uid), callerTokenId_(callerTokenId),
30 callerToken_(callerToken), key_(key)
31 {}
32
~PendingWantRecord()33 PendingWantRecord::~PendingWantRecord()
34 {}
35
Send(SenderInfo & senderInfo)36 void PendingWantRecord::Send(SenderInfo &senderInfo)
37 {
38 SenderInner(senderInfo);
39 }
40
RegisterCancelListener(const sptr<IWantReceiver> & receiver)41 void PendingWantRecord::RegisterCancelListener(const sptr<IWantReceiver> &receiver)
42 {
43 if (receiver == nullptr) {
44 return;
45 }
46 mCancelCallbacks_.emplace_back(receiver);
47 }
48
UnregisterCancelListener(const sptr<IWantReceiver> & receiver)49 void PendingWantRecord::UnregisterCancelListener(const sptr<IWantReceiver> &receiver)
50 {
51 if (receiver == nullptr) {
52 return;
53 }
54 if (mCancelCallbacks_.size()) {
55 auto it = std::find(mCancelCallbacks_.cbegin(), mCancelCallbacks_.cend(), receiver);
56 if (it != mCancelCallbacks_.cend()) {
57 mCancelCallbacks_.erase(it);
58 }
59 }
60 }
61
SenderInner(SenderInfo & senderInfo)62 int32_t PendingWantRecord::SenderInner(SenderInfo &senderInfo)
63 {
64 HILOG_INFO("%{public}s:begin.", __func__);
65 std::lock_guard<std::recursive_mutex> locker(lock_);
66 if (canceled_) {
67 return START_CANCELED;
68 }
69
70 auto pendingWantManager = pendingWantManager_.lock();
71 if (pendingWantManager == nullptr) {
72 HILOG_ERROR("%{public}s:pendingWantManager is nullptr.", __func__);
73 return ERR_INVALID_VALUE;
74 }
75
76 if (((uint32_t)key_->GetFlags() & (uint32_t)Flags::ONE_TIME_FLAG) != 0) {
77 pendingWantManager->CancelWantSenderLocked(*this, true);
78 }
79
80 Want want;
81 if (key_->GetAllWantsInfos().size() != 0) {
82 want = key_->GetRequestWant();
83 }
84 bool immutable = ((uint32_t)key_->GetFlags() & (uint32_t)Flags::CONSTANT_FLAG) != 0;
85 senderInfo.resolvedType = key_->GetRequestResolvedType();
86 if (!immutable) {
87 want.AddFlags(key_->GetFlags());
88 }
89
90 bool sendFinish = (senderInfo.finishedReceiver != nullptr);
91 int res = NO_ERROR;
92 switch (key_->GetType()) {
93 case static_cast<int32_t>(OperationType::START_ABILITY):
94 res = pendingWantManager->PendingWantStartAbility(want, callerToken_, -1, callerUid_);
95 break;
96 case static_cast<int32_t>(OperationType::START_ABILITIES): {
97 std::vector<WantsInfo> allWantsInfos = key_->GetAllWantsInfos();
98 allWantsInfos.back().want = want;
99 res = pendingWantManager->PendingWantStartAbilitys(allWantsInfos, callerToken_, -1, callerUid_);
100 break;
101 }
102 case static_cast<int32_t>(OperationType::START_SERVICE):
103 case static_cast<int32_t>(OperationType::START_FOREGROUND_SERVICE):
104 res = pendingWantManager->PendingWantStartAbility(want, callerToken_, -1, callerUid_);
105 break;
106 case static_cast<int32_t>(OperationType::SEND_COMMON_EVENT):
107 res = pendingWantManager->PendingWantPublishCommonEvent(want, senderInfo, callerUid_, callerTokenId_);
108 (res == ERR_OK) ? (sendFinish = false) : (sendFinish = (senderInfo.finishedReceiver != nullptr));
109 break;
110 default:
111 break;
112 }
113
114 if (sendFinish && res != START_CANCELED) {
115 WantParams wantParams = {};
116 senderInfo.finishedReceiver->PerformReceive(want, senderInfo.code, "", wantParams, false, false, 0);
117 }
118
119 return res;
120 }
121
GetKey()122 std::shared_ptr<PendingWantKey> PendingWantRecord::GetKey()
123 {
124 return key_;
125 }
126
GetUid() const127 int32_t PendingWantRecord::GetUid() const
128 {
129 return uid_;
130 }
131
SetCanceled()132 void PendingWantRecord::SetCanceled()
133 {
134 canceled_ = true;
135 }
GetCanceled()136 bool PendingWantRecord::GetCanceled()
137 {
138 return canceled_;
139 }
140
SetCallerUid(const int32_t callerUid)141 void PendingWantRecord::SetCallerUid(const int32_t callerUid)
142 {
143 callerUid_ = callerUid;
144 }
145
GetCancelCallbacks()146 std::list<sptr<IWantReceiver>> PendingWantRecord::GetCancelCallbacks()
147 {
148 return mCancelCallbacks_;
149 }
150 } // namespace AAFwk
151 } // namespace OHOS
152