• 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 #include "local_call_record.h"
16 
17 #include "hilog_wrapper.h"
18 
19 namespace OHOS {
20 namespace AbilityRuntime {
21 int64_t LocalCallRecord::callRecordId = 0;
LocalCallRecord(const AppExecFwk::ElementName & elementName)22 LocalCallRecord::LocalCallRecord(const AppExecFwk::ElementName &elementName)
23 {
24     recordId_ = callRecordId++;
25     elementName_ = elementName;
26 }
27 
~LocalCallRecord()28 LocalCallRecord::~LocalCallRecord()
29 {
30     if (remoteObject_ && callRecipient_) {
31         remoteObject_->RemoveDeathRecipient(callRecipient_);
32     }
33 }
34 
SetRemoteObject(const sptr<IRemoteObject> & call)35 void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject> &call)
36 {
37     if (call == nullptr) {
38         HILOG_ERROR("remote object is nullptr");
39         return;
40     }
41 
42     remoteObject_ = call;
43     if (callRecipient_ == nullptr) {
44         auto self(weak_from_this());
45         auto diedTask = [self](const wptr<IRemoteObject> &remote) {
46             auto record = self.lock();
47             if (record == nullptr) {
48                 HILOG_ERROR("LocalCallRecord is null, OnCallStubDied failed.");
49                 return;
50             }
51             record->OnCallStubDied(remote);
52         };
53         callRecipient_ = new CallRecipient(diedTask);
54     }
55     remoteObject_->AddDeathRecipient(callRecipient_);
56     HILOG_DEBUG("SetRemoteObject complete.");
57 }
58 
SetRemoteObject(const sptr<IRemoteObject> & call,sptr<IRemoteObject::DeathRecipient> callRecipient)59 void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject> &call,
60     sptr<IRemoteObject::DeathRecipient> callRecipient)
61 {
62     if (call == nullptr) {
63         HILOG_ERROR("remote object is nullptr");
64         return;
65     }
66 
67     remoteObject_ = call;
68     callRecipient_ = callRecipient;
69 
70     remoteObject_->AddDeathRecipient(callRecipient_);
71     HILOG_DEBUG("SetRemoteObject2 complete.");
72 }
73 
AddCaller(const std::shared_ptr<CallerCallBack> & callback)74 void LocalCallRecord::AddCaller(const std::shared_ptr<CallerCallBack> &callback)
75 {
76     callers_.emplace_back(callback);
77 }
78 
RemoveCaller(const std::shared_ptr<CallerCallBack> & callback)79 bool LocalCallRecord::RemoveCaller(const std::shared_ptr<CallerCallBack> &callback)
80 {
81     if (callers_.empty()) {
82         HILOG_ERROR("this caller vector is empty.");
83         return false;
84     }
85 
86     auto iter = std::find(callers_.begin(), callers_.end(), callback);
87     if (iter != callers_.end()) {
88         callback->InvokeOnRelease(ON_RELEASE);
89         callers_.erase(iter);
90         return true;
91     }
92 
93     HILOG_ERROR("this caller callback can't find.");
94     return false;
95 }
96 
OnCallStubDied(const wptr<IRemoteObject> & remote)97 void LocalCallRecord::OnCallStubDied(const wptr<IRemoteObject> &remote)
98 {
99     HILOG_DEBUG("OnCallStubDied.");
100     for (auto &callBack : callers_) {
101         if (callBack != nullptr) {
102             HILOG_ERROR("invoke caller's OnRelease.");
103             callBack->InvokeOnRelease(ON_DIED);
104         }
105     }
106 }
107 
InvokeCallBack() const108 void LocalCallRecord::InvokeCallBack() const
109 {
110     if (remoteObject_ == nullptr) {
111         HILOG_ERROR("remote object is nullptr, can't callback.");
112         return;
113     }
114 
115     for (auto &callBack : callers_) {
116         if (callBack != nullptr && !callBack->IsCallBack()) {
117             callBack->InvokeCallBack(remoteObject_);
118         }
119     }
120     HILOG_DEBUG("finish callback with remote object.");
121 }
122 
GetRemoteObject() const123 sptr<IRemoteObject> LocalCallRecord::GetRemoteObject() const
124 {
125     return remoteObject_;
126 }
127 
GetElementName() const128 AppExecFwk::ElementName LocalCallRecord::GetElementName() const
129 {
130     return elementName_;
131 }
132 
IsExistCallBack() const133 bool LocalCallRecord::IsExistCallBack() const
134 {
135     return !callers_.empty();
136 }
137 
GetRecordId() const138 int LocalCallRecord::GetRecordId() const
139 {
140     return recordId_;
141 }
142 
GetCallers() const143 std::vector<std::shared_ptr<CallerCallBack>> LocalCallRecord::GetCallers() const
144 {
145     return callers_;
146 }
147 
IsSameObject(const sptr<IRemoteObject> & remote) const148 bool LocalCallRecord::IsSameObject(const sptr<IRemoteObject> &remote) const
149 {
150     if (remote == nullptr) {
151         HILOG_ERROR("input remote object is nullptr");
152         return false;
153     }
154 
155     bool retVal = (remoteObject_ == remote);
156     HILOG_DEBUG("LocalCallRecord::%{public}s the input object same as local object is %{public}s.",
157         __func__, retVal ? "true" : "false");
158     return retVal;
159 }
160 } // namespace AbilityRuntime
161 } // namespace OHOS
162