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