diff --git a/interfaces/inner_api/wantagent/include/want_agent_client.h b/interfaces/inner_api/wantagent/include/want_agent_client.h index eef0eb808e9a81ec35e66a2de0657b478443a7a9..9f6de84fc58a07ddc6ef1ba35cd74a7149465263 100644 --- a/interfaces/inner_api/wantagent/include/want_agent_client.h +++ b/interfaces/inner_api/wantagent/include/want_agent_client.h @@ -58,6 +58,8 @@ public: ErrCode GetPendingRequestWant(const sptr &target, std::shared_ptr &want); ErrCode GetWantSenderInfo(const sptr &target, std::shared_ptr &info); + + void RemoveDeathRecipient(); private: class WantAgentDeathRecipient : public IRemoteObject::DeathRecipient { public: diff --git a/interfaces/inner_api/wantagent/src/want_agent_client.cpp b/interfaces/inner_api/wantagent/src/want_agent_client.cpp index a81962456c4b484b5c5ebf38277ea99944570d07..26a73ffe0c8ace683ee13b55c1f278f901e5965c 100644 --- a/interfaces/inner_api/wantagent/src/want_agent_client.cpp +++ b/interfaces/inner_api/wantagent/src/want_agent_client.cpp @@ -36,7 +36,10 @@ WantAgentClient &WantAgentClient::GetInstance() WantAgentClient::WantAgentClient() {} -WantAgentClient::~WantAgentClient() {} +WantAgentClient::~WantAgentClient() +{ + RemoveDeathRecipient(); +} ErrCode WantAgentClient::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken, sptr &wantSender, @@ -402,6 +405,27 @@ sptr WantAgentClient::GetAbilityManager() return proxy_; } +void WantAgentClient::RemoveDeathRecipient() +{ + TAG_LOGI(AAFwkTag::WANTAGENT, "remove death recipient"); + std::lock_guard lock(mutex_); + if (proxy_ == nullptr) { + TAG_LOGI(AAFwkTag::WANTAGENT, "null proxy_"); + return; + } + + if (deathRecipient_ == nullptr) { + TAG_LOGI(AAFwkTag::WANTAGENT, "null deathRecipient_"); + return; + } + + if (proxy_->RemoveDeathRecipient(deathRecipient_)) { + proxy_ = nullptr; + deathRecipient_ = nullptr; + TAG_LOGI(AAFwkTag::WANTAGENT, "remove success"); + } +} + void WantAgentClient::WantAgentDeathRecipient::OnRemoteDied(const wptr& remote) { TAG_LOGI(AAFwkTag::WANTAGENT, "call"); diff --git a/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp b/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp index cfbd49c427708d90c86dda79fa3ff0c234b23843..40c518321cba76064b14c143e8e8be96384be620 100644 --- a/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp +++ b/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp @@ -36,6 +36,7 @@ #define private public #define protected public #include "want_agent_helper.h" +#include "want_agent_client.h" #include "want_agent_info.h" #undef private #undef protected @@ -1235,4 +1236,37 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_5700, Function | MediumTest | Leve type = WantAgentHelper::GetType(wantAgent); EXPECT_EQ(type, WantAgentConstant::OperationType::UNKNOWN_TYPE); } + +/* + * @tc.number : WantAgentHelper_5800 + * @tc.name : WantAgentHelper Cancel success after remove death recipient + * @tc.desc : RemoveDeathRecipient normal test + */ +HWTEST_F(WantAgentHelperTest, WantAgentHelper_5800, Function | MediumTest | Level1) +{ + std::shared_ptr wantAgentHelper = std::make_shared(); + std::shared_ptr want = std::make_shared(); + ElementName element("device", "bundleName", "abilityName"); + want->SetElement(element); + WantAgentInfo wantAgentInfo; + wantAgentInfo.wants_.emplace_back(want); + wantAgentInfo.operationType_ = WantAgentConstant::OperationType::START_ABILITY; + wantAgentInfo.requestCode_ = 10; + bool value = true; + std::string key = "key"; + std::shared_ptr wParams = std::make_shared(); + wParams->SetParam(key, Boolean::Box(value)); + wantAgentInfo.extraInfo_ = wParams; + auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo); + EXPECT_NE(wantAgent, nullptr); + auto type = wantAgentHelper->GetType(wantAgent); + EXPECT_EQ(type, WantAgentConstant::OperationType::START_ABILITY); + + // remove death recipient + WantAgentClient::GetInstance().RemoveDeathRecipient(); + + WantAgentHelper::Cancel(wantAgent); + type = WantAgentHelper::GetType(wantAgent); + EXPECT_EQ(type, WantAgentConstant::OperationType::UNKNOWN_TYPE); +} } // namespace OHOS::AbilityRuntime::WantAgent