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 "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
62 callRemoteObject_ = call;
63
64 HILOG_DEBUG("SetCallStub complete.");
65
66 if (callDeathRecipient_ == nullptr) {
67 std::weak_ptr<CallRecord> callRecord = shared_from_this();
68 auto callStubDied = [wptr = callRecord] (const wptr<IRemoteObject> & remote) {
69 auto call = wptr.lock();
70 if (call == nullptr) {
71 HILOG_ERROR("callRecord is nullptr, can't call stub died.");
72 return;
73 }
74
75 call->OnCallStubDied(remote);
76 };
77 callDeathRecipient_ =
78 new AbilityCallRecipient(callStubDied);
79 }
80
81 callRemoteObject_->AddDeathRecipient(callDeathRecipient_);
82 }
83
GetCallStub()84 sptr<IRemoteObject> CallRecord::GetCallStub()
85 {
86 return callRemoteObject_;
87 }
88
SetConCallBack(const sptr<IAbilityConnection> & connCallback)89 void CallRecord::SetConCallBack(const sptr<IAbilityConnection> &connCallback)
90 {
91 connCallback_ = connCallback;
92 }
93
GetConCallBack() const94 sptr<IAbilityConnection> CallRecord::GetConCallBack() const
95 {
96 return connCallback_;
97 }
98
GetTargetServiceName() const99 AppExecFwk::ElementName CallRecord::GetTargetServiceName() const
100 {
101 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
102 if (tmpService) {
103 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
104 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name);
105 return element;
106 }
107 return AppExecFwk::ElementName();
108 }
109
GetCallerToken() const110 sptr<IRemoteObject> CallRecord::GetCallerToken() const
111 {
112 return callerToken_;
113 }
114
SchedulerConnectDone()115 bool CallRecord::SchedulerConnectDone()
116 {
117 HILOG_DEBUG("Scheduler Connect Done by callback. id:%{public}d", recordId_);
118 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
119 if (!callRemoteObject_ || !connCallback_ || !tmpService) {
120 HILOG_ERROR("callstub or connCallback is nullptr, can't scheduler connect done.");
121 return false;
122 }
123
124 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
125 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name);
126 connCallback_->OnAbilityConnectDone(element, callRemoteObject_, ERR_OK);
127 state_ = CallState::REQUESTED;
128
129 HILOG_DEBUG("element: %{public}s, result: %{public}d. connectstate:%{public}d.", element.GetURI().c_str(),
130 ERR_OK, state_);
131 return true;
132 }
133
SchedulerDisConnectDone()134 bool CallRecord::SchedulerDisConnectDone()
135 {
136 HILOG_DEBUG("Scheduler disconnect Done by callback. id:%{public}d", recordId_);
137 std::shared_ptr<AbilityRecord> tmpService = service_.lock();
138 if (!connCallback_ || !tmpService) {
139 HILOG_ERROR("callstub or connCallback is nullptr, can't scheduler connect done.");
140 return false;
141 }
142
143 const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
144 AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name);
145 connCallback_->OnAbilityDisconnectDone(element, ERR_OK);
146
147 return true;
148 }
149
OnCallStubDied(const wptr<IRemoteObject> & remote)150 void CallRecord::OnCallStubDied(const wptr<IRemoteObject> & remote)
151 {
152 HILOG_DEBUG("callstub is died. id:%{public}d begin", recordId_);
153
154 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
155 CHECK_POINTER(abilityManagerService);
156 auto handler = abilityManagerService->GetEventHandler();
157 CHECK_POINTER(handler);
158 auto task = [abilityManagerService, callRecord = shared_from_this()]() {
159 abilityManagerService->OnCallConnectDied(callRecord);
160 };
161 handler->PostTask(task);
162 HILOG_DEBUG("callstub is died. id:%{public}d, end", recordId_);
163 }
164
Dump(std::vector<std::string> & info) const165 void CallRecord::Dump(std::vector<std::string> &info) const
166 {
167 HILOG_DEBUG("CallRecord::Dump is called");
168
169 std::string tempstr = " CallRecord";
170 tempstr += " ID #" + std::to_string (recordId_) + "\n";
171 tempstr += " caller";
172 auto abilityRecord = Token::GetAbilityRecordByToken(callerToken_);
173 if (abilityRecord) {
174 AppExecFwk::ElementName element(
175 abilityRecord->GetAbilityInfo().deviceId, abilityRecord->GetAbilityInfo().bundleName,
176 abilityRecord->GetAbilityInfo().name);
177 tempstr += " uri [" + element.GetURI() + "]" + "\n";
178 }
179
180 std::string state = (state_ == CallState::INIT ? "INIT" :
181 state_ == CallState::REQUESTING ? "REQUESTING" : "REQUESTED");
182 tempstr += " state #" + state;
183 tempstr += " start time [" + std::to_string (startTime_) + "]";
184 info.emplace_back(tempstr);
185 HILOG_DEBUG("CallRecord::Dump is called1");
186 }
187
GetCallerUid() const188 int32_t CallRecord::GetCallerUid() const
189 {
190 return callerUid_;
191 }
192
IsCallState(const CallState & state) const193 bool CallRecord::IsCallState(const CallState& state) const
194 {
195 return (state_ == state);
196 }
197
SetCallState(const CallState & state)198 void CallRecord::SetCallState(const CallState& state)
199 {
200 state_ = state;
201 }
202
GetCallRecordId() const203 int CallRecord::GetCallRecordId() const
204 {
205 return recordId_;
206 }
207 } // namespace AAFwk
208 } // namespace OHOS
209