Release remote object manually

Signed-off-by: wangzhen <wangzhen346@huawei.com>
Change-Id: Id0daa1cc0c3f4c732e613adfd1df7d8a3a298ec7
This commit is contained in:
wangzhen 2024-08-04 08:52:08 +08:00
parent 76efb160f1
commit 2b270bb4aa
5 changed files with 57 additions and 3 deletions

View File

@ -113,6 +113,8 @@ int LocalCallContainer::ReleaseCall(const std::shared_ptr<CallerCallBack>& callb
}
connections_.erase(connect);
connect->ClearCallRecord();
localCallRecord->ClearData();
if (abilityClient->ReleaseCall(connect, localCallRecord->GetElementName()) != ERR_OK) {
TAG_LOGE(AAFwkTag::LOCAL_CALL, "ReleaseCall failed");
return ERR_INVALID_VALUE;
@ -330,6 +332,11 @@ void LocalCallContainer::SetMultipleCallLocalRecord(
iter->second.emplace(localCallRecord);
}
void CallerConnection::ClearCallRecord()
{
localCallRecord_.reset();
}
void CallerConnection::SetRecordAndContainer(const std::shared_ptr<LocalCallRecord> &localCallRecord,
const std::weak_ptr<LocalCallContainer> &container)
{

View File

@ -31,9 +31,22 @@ LocalCallRecord::LocalCallRecord(const AppExecFwk::ElementName& elementName)
LocalCallRecord::~LocalCallRecord()
{
if (remoteObject_ && callRecipient_) {
remoteObject_->RemoveDeathRecipient(callRecipient_);
ClearData();
}
void LocalCallRecord::ClearData()
{
if (remoteObject_ == nullptr) {
return;
}
if (callRecipient_) {
remoteObject_->RemoveDeathRecipient(callRecipient_);
callRecipient_ = nullptr;
}
callers_.clear();
remoteObject_ = nullptr;
}
void LocalCallRecord::SetRemoteObject(const sptr<IRemoteObject>& call)

View File

@ -263,6 +263,30 @@ public:
currentState_ = OBJSTATE::OBJ_NORMAL;
}
void SetJsRemoteObj(napi_env env, napi_value value)
{
if (env == nullptr || value == nullptr) {
TAG_LOGE(AAFwkTag::DEFAULT, "parameter error");
return;
}
napi_ref ref = nullptr;
napi_create_reference(env, value, 1, &ref);
jsRemoteObj_.reset(reinterpret_cast<NativeReference*>(ref));
jsRemoteObjEnv_ = env;
}
void ReleaseJsRemoteObj()
{
if (jsRemoteObjEnv_ == nullptr || jsRemoteObj_ == nullptr) {
return;
}
TAG_LOGI(AAFwkTag::DEFAULT, "before release call");
napi_value value = jsRemoteObj_->GetNapiValue();
NAPI_ohos_rpc_ClearNativeRemoteProxy(jsRemoteObjEnv_, value);
jsRemoteObj_.reset();
jsRemoteObjEnv_ = nullptr;
}
private:
void OnReleaseNotify(const std::string &str)
@ -350,6 +374,9 @@ private:
TAG_LOGE(AAFwkTag::DEFAULT, "JsCallerComplex::%{public}s, releaseFunc is nullptr", __func__);
ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
}
callee_ = nullptr;
callerCallBackObj_->SetCallBack(nullptr);
ReleaseJsRemoteObj();
int32_t innerErrorCode = releaseCallFunc_(callerCallBackObj_);
if (innerErrorCode != ERR_OK) {
TAG_LOGE(AAFwkTag::DEFAULT, "JsCallerComplex::%{public}s, ReleaseAbility failed %{public}d",
@ -450,9 +477,11 @@ private:
sptr<IRemoteObject> callee_;
napi_env releaseCallBackEngine_;
napi_env remoteStateChanegdEngine_;
napi_env jsRemoteObjEnv_ = nullptr;
std::shared_ptr<CallerCallBack> callerCallBackObj_;
std::unique_ptr<NativeReference> jsReleaseCallBackObj_;
std::unique_ptr<NativeReference> jsRemoteStateChangedObj_;
std::unique_ptr<NativeReference> jsRemoteObj_;
std::shared_ptr<AppExecFwk::EventHandler> handler_;
std::mutex stateMechanismMutex_;
OBJSTATE currentState_;
@ -486,8 +515,10 @@ napi_value CreateJsCallerComplex(
return CreateJsUndefined(env);
}
auto jsRemoteObj = CreateJsCalleeRemoteObject(env, remoteObj);
jsCaller->SetJsRemoteObj(env, jsRemoteObj);
napi_wrap(env, object, jsCaller.release(), JsCallerComplex::Finalizer, nullptr, nullptr);
napi_set_named_property(env, object, "callee", CreateJsCalleeRemoteObject(env, remoteObj));
napi_set_named_property(env, object, "callee", jsRemoteObj);
const char *moduleName = "JsCallerComplex";
BindNativeFunction(env, object, "release", moduleName, JsCallerComplex::JsReleaseCall);
BindNativeFunction(env, object, "onRelease", moduleName, JsCallerComplex::JsSetOnReleaseCallBack);

View File

@ -76,6 +76,8 @@ public:
CallerConnection() = default;
virtual ~CallerConnection() = default;
void ClearCallRecord();
void SetRecordAndContainer(const std::shared_ptr<LocalCallRecord> &localCallRecord,
const std::weak_ptr<LocalCallContainer> &container);

View File

@ -31,6 +31,7 @@ public:
explicit LocalCallRecord(const AppExecFwk::ElementName &elementName);
virtual ~LocalCallRecord();
void ClearData();
void SetRemoteObject(const sptr<IRemoteObject> &call);
void SetRemoteObject(const sptr<IRemoteObject> &call, sptr<IRemoteObject::DeathRecipient> callRecipient);
void AddCaller(const std::shared_ptr<CallerCallBack> &callback);