• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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) {
38         HILOG_ERROR("remote object is nullptr");
39         return;
40     }
41 
42     remoteObject_ = call;
43 
44     if (!callRecipient_) {
45         callRecipient_ =
46             new CallRecipient(std::bind(&LocalCallRecord::OnCallStubDied, this, std::placeholders::_1));
47     }
48 
49     remoteObject_->AddDeathRecipient(callRecipient_);
50     HILOG_DEBUG("SetRemoteObject complete.");
51 }
52 
SetRemoteObject(const sptr<IRemoteObject> & call,sptr<IRemoteObject::DeathRecipient> callRecipient)53 void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject> &call,
54     sptr<IRemoteObject::DeathRecipient> callRecipient)
55 {
56     if (!call) {
57         HILOG_ERROR("remote object is nullptr");
58         return;
59     }
60 
61     remoteObject_ = call;
62     callRecipient_ = callRecipient;
63 
64     remoteObject_->AddDeathRecipient(callRecipient_);
65     HILOG_DEBUG("SetRemoteObject2 complete.");
66 }
67 
AddCaller(const std::shared_ptr<CallerCallBack> & callback)68 void LocalCallRecord::AddCaller(const std::shared_ptr<CallerCallBack> &callback)
69 {
70     callers_.emplace_back(callback);
71 }
72 
RemoveCaller(const std::shared_ptr<CallerCallBack> & callback)73 bool LocalCallRecord::RemoveCaller(const std::shared_ptr<CallerCallBack> &callback)
74 {
75     if (callers_.empty()) {
76         HILOG_ERROR("this caller vector is empty.");
77         return false;
78     }
79 
80     auto iter = std::find(callers_.begin(), callers_.end(), callback);
81     if (iter != callers_.end()) {
82         callback->InvokeOnRelease(ON_RELEASE);
83         callers_.erase(iter);
84         return true;
85     }
86 
87     HILOG_ERROR("this caller callback can't find.");
88     return false;
89 }
90 
OnCallStubDied(const wptr<IRemoteObject> & remote)91 void LocalCallRecord::OnCallStubDied(const wptr<IRemoteObject> &remote)
92 {
93     HILOG_DEBUG("OnCallStubDied.");
94     for (auto &callBack:callers_) {
95         if (callBack) {
96             HILOG_ERROR("invoke caller's OnRelease.");
97             callBack->InvokeOnRelease(ON_DIED);
98         }
99     }
100 }
101 
InvokeCallBack() const102 void LocalCallRecord::InvokeCallBack() const
103 {
104     if (!remoteObject_) {
105         HILOG_ERROR("remote object is nullptr, can't callback.");
106         return;
107     }
108 
109     for (auto &callBack:callers_) {
110         if (callBack && !callBack->IsCallBack()) {
111             callBack->InvokeCallBack(remoteObject_);
112         }
113     }
114     HILOG_DEBUG("finish callback with remote object.");
115 }
116 
GetRemoteObject() const117 sptr<IRemoteObject> LocalCallRecord::GetRemoteObject() const
118 {
119     return remoteObject_;
120 }
121 
GetElementName() const122 AppExecFwk::ElementName LocalCallRecord::GetElementName() const
123 {
124     return elementName_;
125 }
126 
IsExistCallBack() const127 bool LocalCallRecord::IsExistCallBack() const
128 {
129     return (callers_.size() > 0);
130 }
131 
GetRecordId()132 int LocalCallRecord::GetRecordId()
133 {
134     return recordId_;
135 }
136 
GetCallers()137 std::vector<std::shared_ptr<CallerCallBack>> LocalCallRecord::GetCallers()
138 {
139     return callers_;
140 }
141 
IsSameObject(const sptr<IRemoteObject> & remote)142 bool LocalCallRecord::IsSameObject(const sptr<IRemoteObject> &remote)
143 {
144     if (remote == nullptr) {
145         HILOG_ERROR("input remote object is nullptr");
146         return false;
147     }
148 
149     bool retval = remoteObject_ == remote;
150     HILOG_DEBUG("LocalCallRecord::%{public}s the input object same as local object is %{public}s.",
151         __func__, retval ? "true" : "false");
152     return retval;
153 }
154 }  // namespace AbilityRuntime
155 }  // namespace OHOS