• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "local_call_record.h"
16 
17 #include "hilog_wrapper.h"
18 
19 namespace OHOS {
20 namespace AbilityRuntime {
21 namespace {
22 constexpr int32_t FOREGROUND = 2;
23 constexpr int32_t BACKGROUND = 4;
24 }
25 int64_t LocalCallRecord::callRecordId = 0;
LocalCallRecord(const AppExecFwk::ElementName & elementName)26 LocalCallRecord::LocalCallRecord(const AppExecFwk::ElementName& elementName)
27 {
28     recordId_ = callRecordId++;
29     elementName_ = elementName;
30 }
31 
~LocalCallRecord()32 LocalCallRecord::~LocalCallRecord()
33 {
34     if (remoteObject_ && callRecipient_) {
35         remoteObject_->RemoveDeathRecipient(callRecipient_);
36     }
37 }
38 
SetRemoteObject(const sptr<IRemoteObject> & call)39 void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject>& call)
40 {
41     if (call == nullptr) {
42         HILOG_ERROR("remote object is nullptr");
43         return;
44     }
45 
46     remoteObject_ = call;
47     if (callRecipient_ == nullptr) {
48         auto self(weak_from_this());
49         auto diedTask = [self](const wptr<IRemoteObject>& remote) {
50             auto record = self.lock();
51             if (record == nullptr) {
52                 HILOG_ERROR("LocalCallRecord is null, OnCallStubDied failed.");
53                 return;
54             }
55             record->OnCallStubDied(remote);
56         };
57         callRecipient_ = new CallRecipient(diedTask);
58     }
59     remoteObject_->AddDeathRecipient(callRecipient_);
60     HILOG_DEBUG("SetRemoteObject complete.");
61 }
62 
SetRemoteObject(const sptr<IRemoteObject> & call,sptr<IRemoteObject::DeathRecipient> callRecipient)63 void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject>& call,
64     sptr<IRemoteObject::DeathRecipient> callRecipient)
65 {
66     if (call == nullptr) {
67         HILOG_ERROR("remote object is nullptr");
68         return;
69     }
70 
71     remoteObject_ = call;
72     callRecipient_ = callRecipient;
73 
74     remoteObject_->AddDeathRecipient(callRecipient_);
75     HILOG_DEBUG("SetRemoteObject2 complete.");
76 }
77 
AddCaller(const std::shared_ptr<CallerCallBack> & callback)78 void LocalCallRecord::AddCaller(const std::shared_ptr<CallerCallBack>& callback)
79 {
80     if (callback == nullptr) {
81         HILOG_ERROR("input param is nullptr");
82         return;
83     }
84 
85     callback->SetRecord(weak_from_this());
86     callers_.emplace_back(callback);
87 }
88 
RemoveCaller(const std::shared_ptr<CallerCallBack> & callback)89 bool LocalCallRecord::RemoveCaller(const std::shared_ptr<CallerCallBack>& callback)
90 {
91     if (callers_.empty()) {
92         HILOG_ERROR("this caller vector is empty.");
93         return false;
94     }
95 
96     auto iter = std::find(callers_.begin(), callers_.end(), callback);
97     if (iter != callers_.end()) {
98         callback->InvokeOnRelease(ON_RELEASE);
99         callers_.erase(iter);
100         return true;
101     }
102 
103     HILOG_ERROR("this caller callback can't find.");
104     return false;
105 }
106 
OnCallStubDied(const wptr<IRemoteObject> & remote)107 void LocalCallRecord::OnCallStubDied(const wptr<IRemoteObject>& remote)
108 {
109     HILOG_DEBUG("OnCallStubDied.");
110     for (auto& callBack : callers_) {
111         if (callBack != nullptr) {
112             HILOG_ERROR("invoke caller's OnRelease.");
113             callBack->InvokeOnRelease(ON_DIED);
114         }
115     }
116 }
117 
InvokeCallBack() const118 void LocalCallRecord::InvokeCallBack() const
119 {
120     if (remoteObject_ == nullptr) {
121         HILOG_ERROR("remote object is nullptr, can't callback.");
122         return;
123     }
124 
125     for (auto& callBack : callers_) {
126         if (callBack != nullptr && !callBack->IsCallBack()) {
127             callBack->InvokeCallBack(remoteObject_);
128         }
129     }
130     HILOG_DEBUG("finish callback with remote object.");
131 }
132 
NotifyRemoteStateChanged(int32_t abilityState)133 void LocalCallRecord::NotifyRemoteStateChanged(int32_t abilityState)
134 {
135     if (remoteObject_ == nullptr) {
136         HILOG_ERROR("remote object is nullptr, can't notify.");
137         return;
138     }
139     std::string state = "";
140     if (abilityState == FOREGROUND) {
141         state = "foreground";
142     } else if (abilityState == BACKGROUND) {
143         state = "background";
144     }
145     HILOG_DEBUG("NotifyRemoteStateChanged, state = %{public}s.", state.c_str());
146 
147     for (auto& callBack : callers_) {
148         if (callBack != nullptr && callBack->IsCallBack()) {
149             HILOG_INFO("callback is not nullptr, and is callbcak ");
150             callBack->InvokeOnNotify(state);
151         }
152     }
153     HILOG_DEBUG("finish notify remote state changed.");
154 }
155 
GetRemoteObject() const156 sptr<IRemoteObject> LocalCallRecord::GetRemoteObject() const
157 {
158     return remoteObject_;
159 }
160 
GetElementName() const161 AppExecFwk::ElementName LocalCallRecord::GetElementName() const
162 {
163     return elementName_;
164 }
165 
IsExistCallBack() const166 bool LocalCallRecord::IsExistCallBack() const
167 {
168     return !callers_.empty();
169 }
170 
GetRecordId() const171 int LocalCallRecord::GetRecordId() const
172 {
173     return recordId_;
174 }
175 
GetCallers() const176 std::vector<std::shared_ptr<CallerCallBack>> LocalCallRecord::GetCallers() const
177 {
178     return callers_;
179 }
180 
IsSameObject(const sptr<IRemoteObject> & remote) const181 bool LocalCallRecord::IsSameObject(const sptr<IRemoteObject>& remote) const
182 {
183     if (remote == nullptr) {
184         HILOG_ERROR("input remote object is nullptr");
185         return false;
186     }
187 
188     bool retVal = (remoteObject_ == remote);
189     HILOG_DEBUG("LocalCallRecord::%{public}s the input object same as local object is %{public}s.",
190         __func__, retVal ? "true" : "false");
191     return retVal;
192 }
193 
SetIsSingleton(bool flag)194 void LocalCallRecord::SetIsSingleton(bool flag)
195 {
196     isSingleton_ = flag;
197 }
198 
IsSingletonRemote()199 bool LocalCallRecord::IsSingletonRemote()
200 {
201     return isSingleton_;
202 }
203 
SetConnection(const sptr<IRemoteObject> & connect)204 void LocalCallRecord::SetConnection(const sptr<IRemoteObject> &connect)
205 {
206     connection_ = connect;
207 }
208 
GetConnection()209 sptr<IRemoteObject> LocalCallRecord::GetConnection()
210 {
211     return connection_.promote();
212 }
213 
SetUserId(int32_t userId)214 void LocalCallRecord::SetUserId(int32_t userId)
215 {
216     userId_ = userId;
217 }
218 
GetUserId() const219 int32_t LocalCallRecord::GetUserId() const
220 {
221     return userId_;
222 }
223 } // namespace AbilityRuntime
224 } // namespace OHOS
225