• 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     if (!callRemoteObject_ || !connCallback_ || !tmpService) {
125         HILOG_ERROR("callstub or connCallback is nullptr, can't scheduler connect done.");
126         return false;
127     }
128 
129     const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
130     AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
131         abilityInfo.name, abilityInfo.moduleName);
132     connCallback_->OnAbilityConnectDone(element, callRemoteObject_, static_cast<int32_t>(abilityInfo.launchMode));
133     state_ = CallState::REQUESTED;
134 
135     HILOG_DEBUG("element: %{public}s, mode: %{public}d. connectstate:%{public}d.", element.GetURI().c_str(),
136         static_cast<int32_t>(abilityInfo.launchMode), state_);
137     return true;
138 }
139 
SchedulerDisconnectDone()140 bool CallRecord::SchedulerDisconnectDone()
141 {
142     HILOG_DEBUG("Scheduler disconnect Done by callback. id:%{public}d", recordId_);
143     std::shared_ptr<AbilityRecord> tmpService = service_.lock();
144     if (!connCallback_ || !tmpService) {
145         HILOG_ERROR("callstub or connCallback is nullptr, can't scheduler connect done.");
146         return false;
147     }
148 
149     const AppExecFwk::AbilityInfo &abilityInfo = tmpService->GetAbilityInfo();
150     AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName,
151         abilityInfo.name, abilityInfo.moduleName);
152     connCallback_->OnAbilityDisconnectDone(element,  ERR_OK);
153 
154     return true;
155 }
156 
OnCallStubDied(const wptr<IRemoteObject> & remote)157 void CallRecord::OnCallStubDied(const wptr<IRemoteObject> &remote)
158 {
159     HILOG_DEBUG("callstub is died. id:%{public}d begin", recordId_);
160 
161     auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
162     CHECK_POINTER(abilityManagerService);
163     auto handler = abilityManagerService->GetTaskHandler();
164     CHECK_POINTER(handler);
165     auto task = [abilityManagerService, callRecord = shared_from_this()]() {
166         abilityManagerService->OnCallConnectDied(callRecord);
167     };
168     handler->SubmitTask(task);
169     HILOG_DEBUG("callstub is died. id:%{public}d, end", recordId_);
170 }
171 
Dump(std::vector<std::string> & info) const172 void CallRecord::Dump(std::vector<std::string> &info) const
173 {
174     HILOG_DEBUG("CallRecord::Dump is called");
175 
176     std::string tempstr = "            CallRecord";
177     tempstr += " ID #" + std::to_string (recordId_) + "\n";
178     tempstr += "              caller";
179     auto abilityRecord = Token::GetAbilityRecordByToken(callerToken_);
180     if (abilityRecord) {
181         AppExecFwk::ElementName element(
182             abilityRecord->GetAbilityInfo().deviceId, abilityRecord->GetAbilityInfo().bundleName,
183             abilityRecord->GetAbilityInfo().name, abilityRecord->GetAbilityInfo().moduleName);
184         tempstr += " uri [" + element.GetURI() + "]" + "\n";
185     }
186 
187     std::string state = (state_ == CallState::INIT ? "INIT" :
188                         state_ == CallState::REQUESTING ? "REQUESTING" : "REQUESTED");
189     tempstr += "              state #" + state;
190     tempstr += " start time [" + std::to_string (startTime_) + "]";
191     info.emplace_back(tempstr);
192     HILOG_DEBUG("CallRecord::Dump is called1");
193 }
194 
GetCallerUid() const195 int32_t CallRecord::GetCallerUid() const
196 {
197     return callerUid_;
198 }
199 
IsCallState(const CallState & state) const200 bool CallRecord::IsCallState(const CallState &state) const
201 {
202     return (state_ == state);
203 }
204 
SetCallState(const CallState & state)205 void CallRecord::SetCallState(const CallState &state)
206 {
207     state_ = state;
208 }
209 
GetCallRecordId() const210 int CallRecord::GetCallRecordId() const
211 {
212     return recordId_;
213 }
214 }  // namespace AAFwk
215 }  // namespace OHOS
216