1 /*
2 * Copyright (c) 2022 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 "hilog_wrapper.h"
19 #include "ability_util.h"
20 #include "ability_event_handler.h"
21 #include "ability_manager_service.h"
22 #include "ability_record.h"
23 #include "element_name.h"
24
25 namespace OHOS {
26 namespace AAFwk {
27 int64_t CallRecord::callRecordId = 0;
28
CallRecord(const int32_t callerUid,const std::shared_ptr<AbilityRecord> & targetService,const sptr<IAbilityConnection> & connCallback,const sptr<IRemoteObject> & callToken)29 CallRecord::CallRecord(const int32_t callerUid, const std::shared_ptr<AbilityRecord> &targetService,
30 const sptr<IAbilityConnection> &connCallback, const sptr<IRemoteObject> &callToken)
31 : callerUid_(callerUid),
32 state_(CallState::INIT),
33 service_(targetService),
34 connCallback_(connCallback),
35 callerToken_(callToken)
36 {
37 recordId_ = callRecordId++;
38 startTime_ = AbilityUtil::SystemTimeMillis();
39 }
40
~CallRecord()41 CallRecord::~CallRecord()
42 {
43 if (callRemoteObject_ && callDeathRecipient_) {
44 callRemoteObject_->RemoveDeathRecipient(callDeathRecipient_);
45 }
46 }
47
CreateCallRecord(const int32_t callerUid,const std::shared_ptr<AbilityRecord> & targetService,const sptr<IAbilityConnection> & connCallback,const sptr<IRemoteObject> & callToken)48 std::shared_ptr<CallRecord> CallRecord::CreateCallRecord(const int32_t callerUid,
49 const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback,
50 const sptr<IRemoteObject> &callToken)
51 {
52 auto callRecord = std::make_shared<CallRecord>(callerUid, targetService, connCallback, callToken);
53 CHECK_POINTER_AND_RETURN(callRecord, nullptr);
54 callRecord->SetCallState(CallState::INIT);
55 return callRecord;
56 }
57
SetCallStub(const sptr<IRemoteObject> & call)58 void CallRecord::SetCallStub(const sptr<IRemoteObject> &call)
59 {
60 CHECK_POINTER(call);
61 if (callRemoteObject_) {
62 // Already got callRemoteObject, just return
63 return;
64 }
65 callRemoteObject_ = call;
66
67 HILOG_DEBUG("SetCallStub complete.");
68
69 if (callDeathRecipient_ == nullptr) {
70 std::weak_ptr<CallRecord> callRecord = shared_from_this();
71 auto callStubDied = [wptr = callRecord] (const wptr<IRemoteObject> &remote) {
72 auto call = wptr.lock();
73 if (call == nullptr) {
74 HILOG_ERROR("callRecord is nullptr, can't call stub died.");
75 return;
76 }
77
78 call->OnCallStubDied(remote);
79 };
80 callDeathRecipient_ =
81 new AbilityCallRecipient(callStubDied);
82 }
83
84 callRemoteObject_->AddDeathRecipient(callDeathRecipient_);
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 HILOG_DEBUG("Scheduler Connect Done by callback. id:%{public}d", recordId_);
122 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
123 if (!callRemoteObject_ || !connCallback_ || !tmpService) {
124 HILOG_ERROR("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 connCallback_->OnAbilityConnectDone(element, callRemoteObject_, ERR_OK);
132 state_ = CallState::REQUESTED;
133
134 HILOG_DEBUG("element: %{public}s, result: %{public}d. connectstate:%{public}d.", element.GetURI().c_str(),
135 ERR_OK, state_);
136 return true;
137 }
138
SchedulerDisconnectDone()139 bool CallRecord::SchedulerDisconnectDone()
140 {
141 HILOG_DEBUG("Scheduler disconnect Done by callback. id:%{public}d", recordId_);
142 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
143 if (!connCallback_ || !tmpService) {
144 HILOG_ERROR("callstub or connCallback is nullptr, can't scheduler connect done.");
145 return false;
146 }
147
148 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
149 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
150 abilityInfo.name, abilityInfo.moduleName);
151 connCallback_->OnAbilityDisconnectDone(element, ERR_OK);
152
153 return true;
154 }
155
OnCallStubDied(const wptr<IRemoteObject> & remote)156 void CallRecord::OnCallStubDied(const wptr<IRemoteObject> &remote)
157 {
158 HILOG_DEBUG("callstub is died. id:%{public}d begin", recordId_);
159
160 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
161 CHECK_POINTER(abilityManagerService);
162 auto handler = abilityManagerService->GetEventHandler();
163 CHECK_POINTER(handler);
164 auto task = [abilityManagerService, callRecord = shared_from_this()]() {
165 abilityManagerService->OnCallConnectDied(callRecord);
166 };
167 handler->PostTask(task);
168 HILOG_DEBUG("callstub is died. id:%{public}d, end", recordId_);
169 }
170
Dump(std::vector<std::string> & info) const171 void CallRecord::Dump(std::vector<std::string> &info) const
172 {
173 HILOG_DEBUG("CallRecord::Dump is called");
174
175 std::string tempstr = " CallRecord";
176 tempstr += " ID #" + std::to_string (recordId_) + "\n";
177 tempstr += " caller";
178 auto abilityRecord = Token::GetAbilityRecordByToken(callerToken_);
179 if (abilityRecord) {
180 AppExecFwk::ElementName element(
181 abilityRecord->GetAbilityInfo().deviceId, abilityRecord->GetAbilityInfo().bundleName,
182 abilityRecord->GetAbilityInfo().name, abilityRecord->GetAbilityInfo().moduleName);
183 tempstr += " uri [" + element.GetURI() + "]" + "\n";
184 }
185
186 std::string state = (state_ == CallState::INIT ? "INIT" :
187 state_ == CallState::REQUESTING ? "REQUESTING" : "REQUESTED");
188 tempstr += " state #" + state;
189 tempstr += " start time [" + std::to_string (startTime_) + "]";
190 info.emplace_back(tempstr);
191 HILOG_DEBUG("CallRecord::Dump is called1");
192 }
193
GetCallerUid() const194 int32_t CallRecord::GetCallerUid() const
195 {
196 return callerUid_;
197 }
198
IsCallState(const CallState & state) const199 bool CallRecord::IsCallState(const CallState &state) const
200 {
201 return (state_ == state);
202 }
203
SetCallState(const CallState & state)204 void CallRecord::SetCallState(const CallState &state)
205 {
206 state_ = state;
207 }
208
GetCallRecordId() const209 int CallRecord::GetCallRecordId() const
210 {
211 return recordId_;
212 }
213 } // namespace AAFwk
214 } // namespace OHOS
215