fix review code suggestion

Signed-off-by: zhangmingxiang <zhangmingxiang@huawei.com>
Change-Id: I1ef664ef86f8825bc17282456452e9176458a136
Signed-off-by: zhangmingxiang <zhangmingxiang@huawei.com>
This commit is contained in:
zhangmingxiang 2022-07-16 19:22:00 +08:00
parent 6654f28d95
commit 240b2c284b
10 changed files with 190 additions and 170 deletions

View File

@ -24,8 +24,8 @@
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string EVENT_CONNECT = "deviceConnect";
const std::string EVENT_DISCONNECT = "deviceDisconnect";
constexpr const char* EVENT_CONNECT = "deviceConnect";
constexpr const char* EVENT_DISCONNECT = "deviceDisconnect";
}
class IDeviceSelectionNotifier : public IRemoteBroker {

View File

@ -16,7 +16,7 @@
#include "native_engine/native_engine.h"
#include "js_continuation_manager.h"
extern "C" __attribute__((constructor)) void NAPI_application_continuationmanager_AutoRegister()
__attribute__((constructor)) static void NAPI_application_continuationmanager_AutoRegister()
{
auto moduleManager = NativeModuleManager::GetInstance();
NativeModule newModuleInfo = {

View File

@ -38,7 +38,11 @@ constexpr int32_t ARG_COUNT_THREE = 3;
void JsContinuationManager::Finalizer(NativeEngine* engine, void* data, void* hint)
{
HILOGI("JsContinuationManager::Finalizer is called");
std::unique_ptr<JsContinuationManager>(static_cast<JsContinuationManager*>(data));
JsContinuationManager* jsContinuationManager = static_cast<JsContinuationManager*>(data);
if (jsContinuationManager != nullptr) {
delete jsContinuationManager;
jsContinuationManager = nullptr;
}
}
NativeValue* JsContinuationManager::Register(NativeEngine* engine, NativeCallbackInfo* info)
@ -179,32 +183,32 @@ NativeValue* JsContinuationManager::OnRegisterDeviceSelectionCallback(NativeEngi
return engine.CreateUndefined();
}
NativeValue* jsListenerObj = info.argv[ARG_COUNT_TWO];
if (!IfCallbackValid(jsListenerObj)) {
if (!IsCallbackValid(jsListenerObj)) {
return engine.CreateUndefined();
}
if (IfCallbackRegistered(token, cbType)) {
return engine.CreateUndefined();
}
std::unique_ptr<NativeReference> callbackRef;
callbackRef.reset(engine.CreateReference(jsListenerObj, 1));
sptr<JsDeviceSelectionListener> deviceSelectionListener = new JsDeviceSelectionListener(&engine);
if (deviceSelectionListener == nullptr) {
HILOGE("deviceSelectionListener is nullptr");
return engine.CreateUndefined();
}
int32_t ret = DistributedAbilityManagerClient::GetInstance().RegisterDeviceSelectionCallback(
token, cbType, deviceSelectionListener);
if (ret == ERR_OK) {
deviceSelectionListener->AddCallback(cbType, jsListenerObj);
CallbackPair callbackPair = std::make_pair(std::move(callbackRef), deviceSelectionListener);
{
std::lock_guard<std::mutex> jsCbMapLock(jsCbMapMutex_);
jsCbMap_[token][cbType] = std::move(callbackPair); // move assignment
{
std::lock_guard<std::mutex> jsCbMapLock(jsCbMapMutex_);
if (IsCallbackRegistered(token, cbType)) {
return engine.CreateUndefined();
}
std::unique_ptr<NativeReference> callbackRef;
callbackRef.reset(engine.CreateReference(jsListenerObj, 1));
sptr<JsDeviceSelectionListener> deviceSelectionListener = new JsDeviceSelectionListener(&engine);
if (deviceSelectionListener == nullptr) {
HILOGE("deviceSelectionListener is nullptr");
return engine.CreateUndefined();
}
int32_t ret = DistributedAbilityManagerClient::GetInstance().RegisterDeviceSelectionCallback(
token, cbType, deviceSelectionListener);
if (ret == ERR_OK) {
deviceSelectionListener->AddCallback(cbType, jsListenerObj);
CallbackPair callbackPair = std::make_pair(std::move(callbackRef), deviceSelectionListener);
jsCbMap_[token][cbType] = std::move(callbackPair); // move assignment
HILOGI("RegisterDeviceSelectionListener success");
} else {
deviceSelectionListener = nullptr;
HILOGE("RegisterDeviceSelectionListener failed");
}
HILOGI("RegisterDeviceSelectionListener success");
} else {
deviceSelectionListener = nullptr;
HILOGE("RegisterDeviceSelectionListener failed");
}
return engine.CreateUndefined();
}
@ -230,11 +234,11 @@ NativeValue* JsContinuationManager::OnUnregisterDeviceSelectionCallback(NativeEn
HILOGE("Parse token failed");
return engine.CreateUndefined();
}
if (!IfCallbackRegistered(token, cbType)) {
return engine.CreateUndefined();
}
{
std::lock_guard<std::mutex> jsCbMapLock(jsCbMapMutex_);
if (!IsCallbackRegistered(token, cbType)) {
return engine.CreateUndefined();
}
CallbackPair& callbackPair = jsCbMap_[token][cbType];
auto& listener = callbackPair.second;
int32_t ret = DistributedAbilityManagerClient::GetInstance().UnregisterDeviceSelectionCallback(token, cbType);
@ -390,7 +394,7 @@ napi_status JsContinuationManager::SetEnumItem(const napi_env& env, napi_value o
return napi_ok;
}
bool JsContinuationManager::IfCallbackValid(NativeValue* listenerObj)
bool JsContinuationManager::IsCallbackValid(NativeValue* listenerObj)
{
if (listenerObj == nullptr) {
HILOGE("listenerObj is nullptr");
@ -403,9 +407,8 @@ bool JsContinuationManager::IfCallbackValid(NativeValue* listenerObj)
return true;
}
bool JsContinuationManager::IfCallbackRegistered(int32_t token, const std::string& cbType)
bool JsContinuationManager::IsCallbackRegistered(int32_t token, const std::string& cbType)
{
std::lock_guard<std::mutex> jsCbMapLock(jsCbMapMutex_);
if (jsCbMap_.empty() || jsCbMap_.find(token) == jsCbMap_.end()) {
HILOGE("token %{public}d not registered callback!", token);
return false;
@ -426,36 +429,36 @@ bool JsContinuationManager::UnWrapContinuationExtraParams(const napi_env& env, c
HILOGE("options is invalid.");
return false;
}
std::vector<std::string> deviceTypeStringList;
if (UnwrapStringArrayByPropertyName(env, options, "deviceType", deviceTypeStringList)) {
continuationExtraParams->SetDeviceType(deviceTypeStringList);
std::vector<std::string> deviceTypes;
if (UnwrapStringArrayByPropertyName(env, options, "deviceType", deviceTypes)) {
continuationExtraParams->SetDeviceType(deviceTypes);
}
std::string targetBundleString("");
if (UnwrapStringByPropertyName(env, options, "targetBundle", targetBundleString)) {
continuationExtraParams->SetTargetBundle(targetBundleString);
std::string targetBundle("");
if (UnwrapStringByPropertyName(env, options, "targetBundle", targetBundle)) {
continuationExtraParams->SetTargetBundle(targetBundle);
}
std::string descriptionString("");
if (UnwrapStringByPropertyName(env, options, "description", descriptionString)) {
continuationExtraParams->SetDescription(descriptionString);
std::string description("");
if (UnwrapStringByPropertyName(env, options, "description", description)) {
continuationExtraParams->SetDescription(description);
}
nlohmann::json filterJson;
if (!UnwrapJsonByPropertyName(env, options, "filter", filterJson)) {
nlohmann::json filter;
if (!UnwrapJsonByPropertyName(env, options, "filter", filter)) {
return false;
}
continuationExtraParams->SetFilter(filterJson.dump());
continuationExtraParams->SetFilter(filter.dump());
int32_t continuationMode = 0;
if (UnwrapInt32ByPropertyName(env, options, "continuationMode", continuationMode)) {
continuationExtraParams->SetContinuationMode(static_cast<ContinuationMode>(continuationMode));
}
nlohmann::json authInfoJson;
if (UnwrapJsonByPropertyName(env, options, "authInfo", authInfoJson)) {
continuationExtraParams->SetAuthInfo(authInfoJson.dump());
nlohmann::json authInfo;
if (UnwrapJsonByPropertyName(env, options, "authInfo", authInfo)) {
continuationExtraParams->SetAuthInfo(authInfo.dump());
}
return true;
}
bool JsContinuationManager::UnwrapJsonByPropertyName(const napi_env& env, const napi_value& param,
const std::string& fieldStr, nlohmann::json& jsonObj)
const std::string& field, nlohmann::json& jsonObject)
{
HILOGD("called.");
if (!IsTypeForNapiValue(env, param, napi_object)) {
@ -463,18 +466,18 @@ bool JsContinuationManager::UnwrapJsonByPropertyName(const napi_env& env, const
return false;
}
napi_value jsonField = nullptr;
napi_get_named_property(env, param, fieldStr.c_str(), &jsonField);
napi_get_named_property(env, param, field.c_str(), &jsonField);
napi_valuetype jsonFieldType = napi_undefined;
napi_typeof(env, jsonField, &jsonFieldType);
if (jsonFieldType != napi_object && jsonFieldType != napi_undefined) {
HILOGE("field: %{public}s is invalid json.", fieldStr.c_str());
HILOGE("field: %{public}s is invalid json.", field.c_str());
return false;
}
napi_value jsProNameList = nullptr;
uint32_t jsProCount = 0;
napi_get_property_names(env, jsonField, &jsProNameList);
napi_get_array_length(env, jsProNameList, &jsProCount);
if (!PraseJson(env, jsonField, jsProNameList, jsProCount, jsonObj)) {
if (!PraseJson(env, jsonField, jsProNameList, jsProCount, jsonObject)) {
HILOGE("PraseJson failed.");
return false;
}
@ -482,7 +485,7 @@ bool JsContinuationManager::UnwrapJsonByPropertyName(const napi_env& env, const
}
bool JsContinuationManager::PraseJson(const napi_env& env, const napi_value& jsonField,
const napi_value& jsProNameList, uint32_t jsProCount, nlohmann::json& jsonObj)
const napi_value& jsProNameList, uint32_t jsProCount, nlohmann::json& jsonObject)
{
napi_value jsProName = nullptr;
napi_value jsProValue = nullptr;
@ -496,14 +499,14 @@ bool JsContinuationManager::PraseJson(const napi_env& env, const napi_value& jso
case napi_string: {
std::string elementValue = UnwrapStringFromJS(env, jsProValue);
HILOGI("Property name=%{public}s, string, value=%{public}s", strProName.c_str(), elementValue.c_str());
jsonObj[strProName] = elementValue;
jsonObject[strProName] = elementValue;
break;
}
case napi_boolean: {
bool elementValue = false;
napi_get_value_bool(env, jsProValue, &elementValue);
HILOGI("Property name=%{public}s, boolean, value=%{public}d.", strProName.c_str(), elementValue);
jsonObj[strProName] = elementValue;
jsonObject[strProName] = elementValue;
break;
}
case napi_number: {
@ -512,7 +515,7 @@ bool JsContinuationManager::PraseJson(const napi_env& env, const napi_value& jso
HILOGE("Property name=%{public}s, Property int32_t parse error", strProName.c_str());
} else {
HILOGI("Property name=%{public}s, number, value=%{public}d.", strProName.c_str(), elementValue);
jsonObj[strProName] = elementValue;
jsonObject[strProName] = elementValue;
}
break;
}
@ -539,8 +542,8 @@ NativeValue* JsContinuationManagerInit(NativeEngine* engine, NativeValue* export
return nullptr;
}
std::unique_ptr<JsContinuationManager> jsContinuationManager = std::make_unique<JsContinuationManager>();
object->SetNativePointer(jsContinuationManager.release(), JsContinuationManager::Finalizer, nullptr);
JsContinuationManager* jsContinuationManager = new JsContinuationManager();
object->SetNativePointer(jsContinuationManager, JsContinuationManager::Finalizer, nullptr);
BindNativeFunction(*engine, *object, "register", JsContinuationManager::Register);
BindNativeFunction(*engine, *object, "unregister", JsContinuationManager::Unregister);

View File

@ -54,8 +54,8 @@ private:
NativeValue* OnInitContinuationModeObject(NativeEngine &engine, NativeCallbackInfo &info);
napi_status SetEnumItem(const napi_env& env, napi_value object, const char* name, int32_t value);
bool IfCallbackValid(NativeValue* listenerObj);
bool IfCallbackRegistered(int32_t token, const std::string& cbType);
bool IsCallbackValid(NativeValue* listenerObj);
bool IsCallbackRegistered(int32_t token, const std::string& cbType);
bool UnWrapContinuationExtraParams(const napi_env& env, const napi_value& options,
std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
bool UnwrapJsonByPropertyName(const napi_env& env, const napi_value& param,

View File

@ -24,6 +24,7 @@
#include "connect_status_info.h"
#include "iremote_object.h"
#include "notifier_death_recipient.h"
#include "refbase.h"
namespace OHOS {
@ -34,9 +35,13 @@ public:
~NotifierInfo() = default;
using NotifierMap = std::map<std::string, sptr<IRemoteObject>>;
NotifierMap& GetNotifierMap();
sptr<IRemoteObject> GetNotifier(const std::string& cbType);
sptr<IRemoteObject> GetNotifier(const std::string& cbType) const;
void SetNotifier(const std::string& cbType, const sptr<IRemoteObject>& notifier);
void DeleteNotifier(const std::string& cbType);
bool QueryNotifier(const sptr<IRemoteObject>& notifier) const;
bool IsNotifierMapEmpty();
void RemoveDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& notifierDeathRecipient,
const std::string& cbType = std::string());
std::shared_ptr<ConnectStatusInfo> GetConnectStatusInfo() const;
void SetConnectStatusInfo(const std::shared_ptr<ConnectStatusInfo>& connectStatusInfo);
private:

View File

@ -61,8 +61,8 @@ private:
bool IsContinuationModeValid(ContinuationMode continuationMode);
bool IsConnectStatusValid(DeviceConnectStatus deviceConnectStatus);
bool IsTokenRegistered(uint32_t accessToken, int32_t token);
bool IfNotifierRegistered(int32_t token);
bool IfNotifierRegistered(int32_t token, const std::string& cbType);
bool IsNotifierRegistered(int32_t token);
bool IsNotifierRegisteredLocked(int32_t token, const std::string& cbType);
bool QueryTokenByNotifier(const sptr<IRemoteObject>& notifier, int32_t& token);
bool HandleDeviceConnect(const sptr<IRemoteObject>& notifier,
const std::vector<ContinuationResult>& continuationResults);

View File

@ -127,10 +127,7 @@ bool ContinuationResult::WriteContinuationResultsToParcel(Parcel& parcel,
{
size_t size = continuationResults.size();
if (size == 0) {
if (!parcel.WriteInt32(VALUE_NULL)) {
return false;
}
return true;
return parcel.WriteInt32(VALUE_NULL);
}
if (!parcel.WriteInt32(VALUE_OBJECT)) {
return false;

View File

@ -17,18 +17,13 @@
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string TAG = "NotifierInfo";
}
NotifierInfo::NotifierMap& NotifierInfo::GetNotifierMap()
sptr<IRemoteObject> NotifierInfo::GetNotifier(const std::string& cbType) const
{
return notifierMap_;
}
sptr<IRemoteObject> NotifierInfo::GetNotifier(const std::string& cbType)
{
return notifierMap_[cbType];
auto iter = notifierMap_.find(cbType);
if (iter == notifierMap_.end()) {
return nullptr;
}
return iter->second;
}
void NotifierInfo::SetNotifier(const std::string& cbType, const sptr<IRemoteObject>& notifier)
@ -36,6 +31,47 @@ void NotifierInfo::SetNotifier(const std::string& cbType, const sptr<IRemoteObje
notifierMap_[cbType] = notifier;
}
void NotifierInfo::DeleteNotifier(const std::string& cbType)
{
auto iter = notifierMap_.find(cbType);
if (iter != notifierMap_.end()) {
notifierMap_.erase(iter);
}
}
bool NotifierInfo::QueryNotifier(const sptr<IRemoteObject>& notifier) const
{
for (auto iter = notifierMap_.begin(); iter != notifierMap_.end(); iter++) {
if (iter->second == notifier) {
return true;
}
}
return false;
}
bool NotifierInfo::IsNotifierMapEmpty()
{
if (notifierMap_.empty()) {
return true;
}
return false;
}
void NotifierInfo::RemoveDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& notifierDeathRecipient,
const std::string& cbType)
{
if (cbType.empty()) {
for (auto iter = notifierMap_.begin(); iter != notifierMap_.end(); iter++) {
iter->second->RemoveDeathRecipient(notifierDeathRecipient);
}
return;
}
auto it = notifierMap_.find(cbType);
if (it != notifierMap_.end()) {
it->second->RemoveDeathRecipient(notifierDeathRecipient);
}
}
std::shared_ptr<ConnectStatusInfo> NotifierInfo::GetConnectStatusInfo() const
{
return connectStatusInfo_;

View File

@ -220,11 +220,8 @@ int32_t DistributedAbilityManagerService::Unregister(int32_t token)
// remove death recipient and update callbackMap_ by token
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
if (IfNotifierRegistered(token)) {
auto& notifierMap = callbackMap_[token]->GetNotifierMap();
for (auto iter = notifierMap.begin(); iter != notifierMap.end(); iter++) {
iter->second->RemoveDeathRecipient(notifierDeathRecipient_);
}
if (IsNotifierRegistered(token)) {
callbackMap_[token]->RemoveDeathRecipient(notifierDeathRecipient_);
callbackMap_.erase(token);
}
}
@ -259,7 +256,7 @@ int32_t DistributedAbilityManagerService::RegisterDeviceSelectionCallback(
if (!IsTokenRegistered(accessToken, token)) {
return TOKEN_HAS_NOT_REGISTERED;
}
if (IfNotifierRegistered(token, cbType)) {
if (IsNotifierRegisteredLocked(token, cbType)) {
return CALLBACK_HAS_REGISTERED;
}
{
@ -292,21 +289,17 @@ int32_t DistributedAbilityManagerService::UnregisterDeviceSelectionCallback(int3
if (!IsTokenRegistered(accessToken, token)) {
return TOKEN_HAS_NOT_REGISTERED;
}
if (!IfNotifierRegistered(token, cbType)) {
if (!IsNotifierRegisteredLocked(token, cbType)) {
return CALLBACK_HAS_NOT_REGISTERED;
}
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
auto iter = callbackMap_.find(token);
if (iter != callbackMap_.end()) {
auto& notifierMap = iter->second->GetNotifierMap();
auto it = notifierMap.find(cbType);
if (it != notifierMap.end()) {
it->second->RemoveDeathRecipient(notifierDeathRecipient_);
notifierMap.erase(it);
if (notifierMap.empty()) {
callbackMap_.erase(iter);
}
iter->second->RemoveDeathRecipient(notifierDeathRecipient_, cbType);
iter->second->DeleteNotifier(cbType);
if (iter->second->IsNotifierMapEmpty()) {
callbackMap_.erase(iter);
}
}
}
@ -329,7 +322,7 @@ int32_t DistributedAbilityManagerService::UpdateConnectStatus(int32_t token, con
if (!IsTokenRegistered(accessToken, token)) {
return TOKEN_HAS_NOT_REGISTERED;
}
if (!IfNotifierRegistered(token)) {
if (!IsNotifierRegistered(token)) {
return CALLBACK_HAS_NOT_REGISTERED;
}
{
@ -362,7 +355,7 @@ int32_t DistributedAbilityManagerService::StartDeviceManager(
if (!IsTokenRegistered(accessToken, token)) {
return TOKEN_HAS_NOT_REGISTERED;
}
if (!IfNotifierRegistered(token)) {
if (!IsNotifierRegistered(token)) {
return CALLBACK_HAS_NOT_REGISTERED;
}
// 1. connect to app and get the app proxy if appProxy_ is null, otherwise start device manager directly.
@ -389,17 +382,14 @@ int32_t DistributedAbilityManagerService::OnDeviceConnect(int32_t token,
if (!HandleDisconnectAbility()) {
return DISCONNECT_ABILITY_FAILED;
}
if (!IfNotifierRegistered(token, EVENT_CONNECT)) {
if (!IsNotifierRegisteredLocked(token, EVENT_CONNECT)) {
return CALLBACK_HAS_NOT_REGISTERED;
}
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
auto& notifierMap = callbackMap_[token]->GetNotifierMap();
auto it = notifierMap.find(EVENT_CONNECT);
if (it != notifierMap.end()) {
if (!HandleDeviceConnect(it->second, continuationResults)) {
return INVALID_PARAMETERS_ERR;
}
auto notifier = callbackMap_[token]->GetNotifier(EVENT_CONNECT);
if (!HandleDeviceConnect(notifier, continuationResults)) {
return INVALID_PARAMETERS_ERR;
}
}
return ERR_OK;
@ -411,17 +401,14 @@ int32_t DistributedAbilityManagerService::OnDeviceDisconnect(int32_t token, cons
if (!HandleDisconnectAbility()) {
return DISCONNECT_ABILITY_FAILED;
}
if (!IfNotifierRegistered(token, EVENT_DISCONNECT)) {
if (!IsNotifierRegisteredLocked(token, EVENT_DISCONNECT)) {
return CALLBACK_HAS_NOT_REGISTERED;
}
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
auto& notifierMap = callbackMap_[token]->GetNotifierMap();
auto it = notifierMap.find(EVENT_DISCONNECT);
if (it != notifierMap.end()) {
if (!HandleDeviceDisconnect(it->second, deviceIds)) {
return INVALID_PARAMETERS_ERR;
}
auto notifier = callbackMap_[token]->GetNotifier(EVENT_DISCONNECT);
if (!HandleDeviceDisconnect(notifier, deviceIds)) {
return INVALID_PARAMETERS_ERR;
}
}
return ERR_OK;
@ -504,7 +491,7 @@ bool DistributedAbilityManagerService::IsTokenRegistered(uint32_t accessToken, i
return false;
}
bool DistributedAbilityManagerService::IfNotifierRegistered(int32_t token)
bool DistributedAbilityManagerService::IsNotifierRegistered(int32_t token)
{
// must be in callbackMapLock scope
auto iter = callbackMap_.find(token);
@ -519,15 +506,13 @@ bool DistributedAbilityManagerService::IfNotifierRegistered(int32_t token)
return true;
}
bool DistributedAbilityManagerService::IfNotifierRegistered(int32_t token, const std::string& cbType)
bool DistributedAbilityManagerService::IsNotifierRegisteredLocked(int32_t token, const std::string& cbType)
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
if (!IfNotifierRegistered(token)) {
if (!IsNotifierRegistered(token)) {
return false;
}
auto& notifierMap = callbackMap_[token]->GetNotifierMap();
auto it = notifierMap.find(cbType);
if (it != notifierMap.end()) {
if (callbackMap_[token]->GetNotifier(cbType) != nullptr) {
HILOGD("token: %{public}d cbType:%{public}s has already registered", token, cbType.c_str());
return true;
}
@ -611,7 +596,7 @@ void DistributedAbilityManagerService::HandleStartDeviceManager(int32_t token,
// query whether the connect status needs to be send
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
if (IfNotifierRegistered(token)) {
if (IsNotifierRegistered(token)) {
std::shared_ptr<ConnectStatusInfo> connectStatusInfo = callbackMap_[token]->GetConnectStatusInfo();
if (connectStatusInfo == nullptr) {
PARCEL_WRITE_HELPER_NORET(data, Int32, VALUE_NULL);
@ -673,12 +658,9 @@ bool DistributedAbilityManagerService::QueryTokenByNotifier(const sptr<IRemoteOb
if (iter->second == nullptr) {
return false;
}
auto& notifierMap = iter->second->GetNotifierMap();
for (auto it = notifierMap.begin(); it != notifierMap.end(); it++) {
if (it->second == notifier) {
token = iter->first;
return true;
}
if (iter->second->QueryNotifier(notifier)) {
token = iter->first;
return true;
}
}
return false;
@ -710,11 +692,8 @@ void DistributedAbilityManagerService::HandleNotifierDied(const sptr<IRemoteObje
// remove death recipient and update callbackMap_ by token
{
std::lock_guard<std::mutex> callbackMapLock(callbackMapMutex_);
if (IfNotifierRegistered(token)) {
auto& notifierMap = callbackMap_[token]->GetNotifierMap();
for (auto iter = notifierMap.begin(); iter != notifierMap.end(); iter++) {
iter->second->RemoveDeathRecipient(notifierDeathRecipient_);
}
if (IsNotifierRegistered(token)) {
callbackMap_[token]->RemoveDeathRecipient(notifierDeathRecipient_);
callbackMap_.erase(token);
}
}

View File

@ -931,71 +931,71 @@ HWTEST_F(ContinuationManagerTest, IsTokenRegisteredTest_002, TestSize.Level1)
}
/**
* @tc.name: IfNotifierRegisteredTest_001
* @tc.desc: test IfNotifierRegistered function with invalid token.
* @tc.name: IsNotifierRegisteredTest_001
* @tc.desc: test IsNotifierRegistered function with invalid token.
* @tc.type: FUNC
* @tc.require: SR000H34KJ
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_001, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredTest_001, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_001 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredTest_001 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
}
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN);
bool result = dtbabilitymgrService_->IsNotifierRegistered(TEST_TOKEN);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(false, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_001 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredTest_001 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_002
* @tc.desc: test IfNotifierRegistered function with invalid token.
* @tc.name: IsNotifierRegisteredLockedTest_002
* @tc.desc: test IsNotifierRegisteredLocked function with invalid token.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_002, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredLockedTest_002, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_002 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_002 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
}
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN, CALLBACK_TYPE1);
bool result = dtbabilitymgrService_->IsNotifierRegisteredLocked(TEST_TOKEN, CALLBACK_TYPE1);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(false, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_002 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_002 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_003
* @tc.desc: test IfNotifierRegistered function with invalid callback type.
* @tc.name: IsNotifierRegisteredLockedTest_003
* @tc.desc: test IsNotifierRegisteredLocked function with invalid callback type.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_003, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredLockedTest_003, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_003 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_003 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
}
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN, INVALID_CALLBACK_TYPE);
bool result = dtbabilitymgrService_->IsNotifierRegisteredLocked(TEST_TOKEN, INVALID_CALLBACK_TYPE);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(false, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_003 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_003 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_004
* @tc.desc: test IfNotifierRegistered function with correct token.
* @tc.name: IsNotifierRegisteredTest_004
* @tc.desc: test IsNotifierRegistered function with correct token.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_004, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredTest_004, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_004 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredTest_004 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
@ -1004,21 +1004,21 @@ HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_004, TestSize.Level1)
sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
notifierInfo->SetNotifier(CALLBACK_TYPE1, notifier);
dtbabilitymgrService_->callbackMap_[TEST_TOKEN] = std::move(notifierInfo);
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN);
bool result = dtbabilitymgrService_->IsNotifierRegistered(TEST_TOKEN);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(true, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_004 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredTest_004 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_005
* @tc.desc: test IfNotifierRegistered function with incorrect callback type.
* @tc.name: IsNotifierRegisteredLockedTest_005
* @tc.desc: test IsNotifierRegisteredLocked function with incorrect callback type.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_005, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredLockedTest_005, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_005 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_005 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
@ -1027,21 +1027,21 @@ HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_005, TestSize.Level1)
sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
notifierInfo->SetNotifier(CALLBACK_TYPE1, notifier);
dtbabilitymgrService_->callbackMap_[TEST_TOKEN] = std::move(notifierInfo);
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN, CALLBACK_TYPE2);
bool result = dtbabilitymgrService_->IsNotifierRegisteredLocked(TEST_TOKEN, CALLBACK_TYPE2);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(false, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_005 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_005 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_006
* @tc.desc: test IfNotifierRegistered function with incorrect token.
* @tc.name: IsNotifierRegisteredLockedTest_006
* @tc.desc: test IsNotifierRegisteredLocked function with incorrect token.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_006, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredLockedTest_006, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_006 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_006 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
@ -1050,21 +1050,21 @@ HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_006, TestSize.Level1)
sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
notifierInfo->SetNotifier(CALLBACK_TYPE1, notifier);
dtbabilitymgrService_->callbackMap_[TEST_TOKEN] = std::move(notifierInfo);
bool result = dtbabilitymgrService_->IfNotifierRegistered(UNREGISTER_TOKEN, CALLBACK_TYPE1);
bool result = dtbabilitymgrService_->IsNotifierRegisteredLocked(UNREGISTER_TOKEN, CALLBACK_TYPE1);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(false, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_006 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_006 end" << std::endl;
}
/**
* @tc.name: IfNotifierRegisteredTest_007
* @tc.desc: test IfNotifierRegistered function with correct token and callback type.
* @tc.name: IsNotifierRegisteredLockedTest_007
* @tc.desc: test IsNotifierRegisteredLocked function with correct token and callback type.
* @tc.type: FUNC
* @tc.require: AR000H34KK
*/
HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_007, TestSize.Level1)
HWTEST_F(ContinuationManagerTest, IsNotifierRegisteredLockedTest_007, TestSize.Level1)
{
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_007 start" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_007 start" << std::endl;
if (dtbabilitymgrService_ == nullptr) {
DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
return;
@ -1073,10 +1073,10 @@ HWTEST_F(ContinuationManagerTest, IfNotifierRegisteredTest_007, TestSize.Level1)
sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
notifierInfo->SetNotifier(CALLBACK_TYPE1, notifier);
dtbabilitymgrService_->callbackMap_[TEST_TOKEN] = std::move(notifierInfo);
bool result = dtbabilitymgrService_->IfNotifierRegistered(TEST_TOKEN, CALLBACK_TYPE1);
bool result = dtbabilitymgrService_->IsNotifierRegisteredLocked(TEST_TOKEN, CALLBACK_TYPE1);
DTEST_LOG << "result:" << result << std::endl;
EXPECT_EQ(true, result);
DTEST_LOG << "ContinuationManagerTest IfNotifierRegisteredTest_007 end" << std::endl;
DTEST_LOG << "ContinuationManagerTest IsNotifierRegisteredLockedTest_007 end" << std::endl;
}
/**