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