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