1 /*
2 * Copyright (c) 2022-2024 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 "call_record.h"
17
18 #include "ability_util.h"
19 #include "ability_manager_service.h"
20
21 namespace OHOS {
22 namespace AAFwk {
23 int64_t CallRecord::callRecordId = 0;
24
CallRecord(const int32_t callerUid,const std::shared_ptr<AbilityRecord> & targetService,const sptr<IAbilityConnection> & connCallback,const sptr<IRemoteObject> & callToken)25 CallRecord::CallRecord(const int32_t callerUid, const std::shared_ptr<AbilityRecord> &targetService,
26 const sptr<IAbilityConnection> &connCallback, const sptr<IRemoteObject> &callToken)
27 : callerUid_(callerUid),
28 state_(CallState::INIT),
29 service_(targetService),
30 connCallback_(connCallback),
31 callerToken_(callToken)
32 {
33 recordId_ = callRecordId++;
34 startTime_ = AbilityUtil::SystemTimeMillis();
35 }
36
~CallRecord()37 CallRecord::~CallRecord()
38 {
39 if (callRemoteObject_ && callDeathRecipient_) {
40 callRemoteObject_->RemoveDeathRecipient(callDeathRecipient_);
41 }
42 }
43
CreateCallRecord(const int32_t callerUid,const std::shared_ptr<AbilityRecord> & targetService,const sptr<IAbilityConnection> & connCallback,const sptr<IRemoteObject> & callToken)44 std::shared_ptr<CallRecord> CallRecord::CreateCallRecord(const int32_t callerUid,
45 const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback,
46 const sptr<IRemoteObject> &callToken)
47 {
48 auto callRecord = std::make_shared<CallRecord>(callerUid, targetService, connCallback, callToken);
49 CHECK_POINTER_AND_RETURN(callRecord, nullptr);
50 callRecord->SetCallState(CallState::INIT);
51 return callRecord;
52 }
53
SetCallStub(const sptr<IRemoteObject> & call)54 void CallRecord::SetCallStub(const sptr<IRemoteObject> &call)
55 {
56 CHECK_POINTER(call);
57 if (callRemoteObject_) {
58 // Already got callRemoteObject, just return
59 return;
60 }
61 callRemoteObject_ = call;
62
63 TAG_LOGD(AAFwkTag::ABILITYMGR, "SetCallStub complete.");
64
65 if (callDeathRecipient_ == nullptr) {
66 std::weak_ptr<CallRecord> callRecord = shared_from_this();
67 auto callStubDied = [wptr = callRecord] (const wptr<IRemoteObject> &remote) {
68 auto call = wptr.lock();
69 if (call == nullptr) {
70 TAG_LOGE(AAFwkTag::ABILITYMGR, "callRecord is nullptr, can't call stub died.");
71 return;
72 }
73
74 call->OnCallStubDied(remote);
75 };
76 callDeathRecipient_ =
77 new AbilityCallRecipient(callStubDied);
78 }
79
80 if (!callRemoteObject_->AddDeathRecipient(callDeathRecipient_)) {
81 TAG_LOGE(AAFwkTag::ABILITYMGR, "AddDeathRecipient failed.");
82 }
83 }
84
GetCallStub()85 sptr<IRemoteObject> CallRecord::GetCallStub()
86 {
87 return callRemoteObject_;
88 }
89
SetConCallBack(const sptr<IAbilityConnection> & connCallback)90 void CallRecord::SetConCallBack(const sptr<IAbilityConnection> &connCallback)
91 {
92 connCallback_ = connCallback;
93 }
94
GetConCallBack() const95 sptr<IAbilityConnection> CallRecord::GetConCallBack() const
96 {
97 return connCallback_;
98 }
99
GetTargetServiceName() const100 AppExecFwk::ElementName CallRecord::GetTargetServiceName() const
101 {
102 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
103 if (tmpService) {
104 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
105 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
106 abilityInfo.name, abilityInfo.moduleName);
107 return element;
108 }
109 return AppExecFwk::ElementName();
110 }
111
GetCallerToken() const112 sptr<IRemoteObject> CallRecord::GetCallerToken() const
113 {
114 return callerToken_;
115 }
116
SchedulerConnectDone()117 bool CallRecord::SchedulerConnectDone()
118 {
119 TAG_LOGD(AAFwkTag::ABILITYMGR, "Scheduler Connect Done by callback. id:%{public}d", recordId_);
120 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
121 auto remoteObject = callRemoteObject_;
122 auto callback = connCallback_;
123 if (!remoteObject || !callback || !tmpService) {
124 TAG_LOGE(AAFwkTag::ABILITYMGR, "callstub or connCallback is nullptr, can't scheduler connect done.");
125 return false;
126 }
127
128 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
129 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
130 abilityInfo.name, abilityInfo.moduleName);
131 auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
132 CHECK_POINTER_AND_RETURN(handler, false);
133 handler->SubmitTask([callback, remoteObject, launchMode = abilityInfo.launchMode, element]() {
134 callback->OnAbilityConnectDone(element,
135 remoteObject, static_cast<int32_t>(launchMode));
136 });
137 state_ = CallState::REQUESTED;
138
139 TAG_LOGD(AAFwkTag::ABILITYMGR, "element: %{public}s, mode: %{public}d. connectstate:%{public}d.",
140 element.GetURI().c_str(), static_cast<int32_t>(abilityInfo.launchMode), state_);
141 return true;
142 }
143
SchedulerDisconnectDone()144 bool CallRecord::SchedulerDisconnectDone()
145 {
146 TAG_LOGD(AAFwkTag::ABILITYMGR, "Scheduler disconnect Done by callback. id:%{public}d", recordId_);
147 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
148 auto callback = connCallback_;
149 if (!callback || !tmpService) {
150 TAG_LOGE(AAFwkTag::ABILITYMGR, "callstub or connCallback is nullptr, can't scheduler connect done.");
151 return false;
152 }
153
154 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
155 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
156 abilityInfo.name, abilityInfo.moduleName);
157 auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
158 CHECK_POINTER_AND_RETURN(handler, false);
159 handler->SubmitTask([callback, element]() {
160 callback->OnAbilityDisconnectDone(element, ERR_OK);
161 });
162
163 return true;
164 }
165
OnCallStubDied(const wptr<IRemoteObject> & remote)166 void CallRecord::OnCallStubDied(const wptr<IRemoteObject> &remote)
167 {
168 TAG_LOGD(AAFwkTag::ABILITYMGR, "callstub is died. id:%{public}d begin", recordId_);
169
170 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
171 CHECK_POINTER(abilityManagerService);
172 auto handler = abilityManagerService->GetTaskHandler();
173 CHECK_POINTER(handler);
174 auto task = [abilityManagerService, callRecord = shared_from_this()]() {
175 abilityManagerService->OnCallConnectDied(callRecord);
176 };
177 handler->SubmitTask(task);
178 TAG_LOGD(AAFwkTag::ABILITYMGR, "callstub is died. id:%{public}d, end", recordId_);
179 }
180
Dump(std::vector<std::string> & info) const181 void CallRecord::Dump(std::vector<std::string> &info) const
182 {
183 TAG_LOGD(AAFwkTag::ABILITYMGR, "CallRecord::Dump is called");
184
185 std::string tempstr = " CallRecord";
186 tempstr += " ID #" + std::to_string (recordId_) + "\n";
187 tempstr += " caller";
188 auto abilityRecord = Token::GetAbilityRecordByToken(callerToken_);
189 if (abilityRecord) {
190 AppExecFwk::ElementName element(
191 abilityRecord->GetAbilityInfo().deviceId, abilityRecord->GetAbilityInfo().bundleName,
192 abilityRecord->GetAbilityInfo().name, abilityRecord->GetAbilityInfo().moduleName);
193 tempstr += " uri [" + element.GetURI() + "]" + "\n";
194 }
195
196 std::string state = (state_ == CallState::INIT ? "INIT" :
197 state_ == CallState::REQUESTING ? "REQUESTING" : "REQUESTED");
198 tempstr += " state #" + state;
199 tempstr += " start time [" + std::to_string (startTime_) + "]";
200 info.emplace_back(tempstr);
201 TAG_LOGD(AAFwkTag::ABILITYMGR, "CallRecord::Dump is called1");
202 }
203
GetCallerUid() const204 int32_t CallRecord::GetCallerUid() const
205 {
206 return callerUid_;
207 }
208
IsCallState(const CallState & state) const209 bool CallRecord::IsCallState(const CallState &state) const
210 {
211 return (state_ == state);
212 }
213
SetCallState(const CallState & state)214 void CallRecord::SetCallState(const CallState &state)
215 {
216 state_ = state;
217 }
218
GetCallRecordId() const219 int CallRecord::GetCallRecordId() const
220 {
221 return recordId_;
222 }
223 } // namespace AAFwk
224 } // namespace OHOS
225