代码优化

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
m30043719 2023-12-02 14:16:56 +08:00
parent 76d22d4ba6
commit 2482050fa3
7 changed files with 75 additions and 54 deletions

View File

@ -72,7 +72,6 @@
"//foundation/ability/dmsfwk/interfaces/innerkits/common:common_sdk", "//foundation/ability/dmsfwk/interfaces/innerkits/common:common_sdk",
"//foundation/ability/dmsfwk/interfaces/innerkits/continuation_manager:continuation_manager", "//foundation/ability/dmsfwk/interfaces/innerkits/continuation_manager:continuation_manager",
"//foundation/ability/dmsfwk/interfaces/innerkits/distributed_event:distributed_sdk", "//foundation/ability/dmsfwk/interfaces/innerkits/distributed_event:distributed_sdk",
"//foundation/ability/dmsfwk/interfaces/innerkits/tests:dms_sdk_demo",
"//foundation/ability/dmsfwk/interfaces/kits/napi:napi_packages" "//foundation/ability/dmsfwk/interfaces/kits/napi:napi_packages"
], ],
"service_group": [ "service_group": [
@ -111,6 +110,7 @@
} }
], ],
"test": [ "test": [
"//foundation/ability/dmsfwk/interfaces/innerkits/tests:dms_sdk_demo",
"//foundation/ability/dmsfwk/services/dtbschedmgr:unittest", "//foundation/ability/dmsfwk/services/dtbschedmgr:unittest",
"//foundation/ability/dmsfwk/services/dtbabilitymgr:unittest", "//foundation/ability/dmsfwk/services/dtbabilitymgr:unittest",
"//foundation/ability/dmsfwk/test/fuzztest/continuationmanager_fuzzer:fuzztest" "//foundation/ability/dmsfwk/test/fuzztest/continuationmanager_fuzzer:fuzztest"

View File

@ -29,7 +29,7 @@ config("tests_public_config") {
} }
} }
ohos_shared_library("dms_sdk_demo") { ohos_executable("dms_sdk_demo") {
branch_protector_ret = "pac_ret" branch_protector_ret = "pac_ret"
sanitize = { sanitize = {
cfi = true cfi = true

View File

@ -47,8 +47,8 @@ public:
bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback, bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback,
std::string deviceId, bool isFreeInstall); std::string deviceId, bool isFreeInstall);
bool PushCallback(const std::string& type, const sptr<IRemoteObject>& callback); bool PushCallback(const std::string& type, const sptr<IRemoteObject>& callback);
sptr<IRemoteObject> CleanupCallback(const std::string& type); bool CleanupCallback(const std::string& type, const sptr<IRemoteObject>& callback);
sptr<IRemoteObject> GetCallback(const std::string& type); std::vector<sptr<IRemoteObject>> GetCallback(const std::string& type);
sptr<IRemoteObject> PopCallback(int32_t missionId); sptr<IRemoteObject> PopCallback(int32_t missionId);
int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode); int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode);
int32_t NotifyDSchedEventResult(const std::string& type, int32_t resultCode); int32_t NotifyDSchedEventResult(const std::string& type, int32_t resultCode);
@ -77,7 +77,7 @@ private:
std::mutex continuationLock_; std::mutex continuationLock_;
int32_t currSessionId_ = 1; int32_t currSessionId_ = 1;
std::map<int32_t, sptr<IRemoteObject>> continuationMap_; std::map<int32_t, sptr<IRemoteObject>> continuationMap_;
std::map<std::string, sptr<IRemoteObject>> continuationCallbackMap_; std::map<std::string, std::vector<sptr<IRemoteObject>>> continuationCallbackMap_;
std::map<int32_t, sptr<IRemoteObject>> callbackMap_; std::map<int32_t, sptr<IRemoteObject>> callbackMap_;
std::map<int32_t, bool> freeInstall_; std::map<int32_t, bool> freeInstall_;
std::map<int32_t, bool> cleanMission_; std::map<int32_t, bool> cleanMission_;

View File

@ -84,6 +84,7 @@ private:
bool EnforceInterfaceToken(MessageParcel& data); bool EnforceInterfaceToken(MessageParcel& data);
bool CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data); bool CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data);
void SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo); void SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo);
void InitExtendedLocalFuncsInner();
void InitLocalFuncsInner(); void InitLocalFuncsInner();
void InitRemoteFuncsInner(); void InitRemoteFuncsInner();
std::shared_ptr<AAFwk::Want> ReadDistributedWant(MessageParcel& data); std::shared_ptr<AAFwk::Want> ReadDistributedWant(MessageParcel& data);

View File

@ -154,24 +154,25 @@ std::string DSchedContinuation::GetTargetDevice(int32_t missionId)
bool DSchedContinuation::PushCallback(const std::string& type, const sptr<IRemoteObject>& callback) bool DSchedContinuation::PushCallback(const std::string& type, const sptr<IRemoteObject>& callback)
{ {
if (continuationHandler_ == nullptr) {
HILOGE("not initialized!");
return false;
}
HILOGI("DSchedContinuation PushCallback start!"); HILOGI("DSchedContinuation PushCallback start!");
if (callback == nullptr) { if (callback == nullptr) {
HILOGE("callback null!"); HILOGE("callback null!");
return false; return false;
} }
if (continuationHandler_ == nullptr) {
HILOGE("not initialized!");
return false;
}
std::lock_guard<std::mutex> autoLock(continuationLock_); std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iterSession = continuationCallbackMap_.find(type); std::vector<sptr<IRemoteObject>> vecCallback = continuationCallbackMap_[type];
if (iterSession != continuationCallbackMap_.end()) { for (auto ele = vecCallback.begin(); ele != vecCallback.end(); ++ele) {
HILOGE("type:%{public}s exist!", type.c_str()); if ((*ele) == callback) {
return false; HILOGE("type:%{public}s, callback is exists!", type.c_str());
return false;
}
} }
(void)continuationCallbackMap_.emplace(type, callback); vecCallback.push_back(callback);
continuationCallbackMap_[type] = vecCallback;
return true; return true;
} }
@ -203,29 +204,30 @@ bool DSchedContinuation::PushCallback(int32_t missionId, const sptr<IRemoteObjec
return true; return true;
} }
sptr<IRemoteObject> DSchedContinuation::GetCallback(const std::string& type) std::vector<sptr<IRemoteObject>> DSchedContinuation::GetCallback(const std::string& type)
{ {
std::lock_guard<std::mutex> autoLock(continuationLock_); std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iter = continuationCallbackMap_.find(type); return continuationCallbackMap_[type];
if (iter == continuationCallbackMap_.end()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str());
return nullptr;
}
sptr<IRemoteObject> callback = iter->second;
return callback;
} }
sptr<IRemoteObject> DSchedContinuation::CleanupCallback(const std::string& type) bool DSchedContinuation::CleanupCallback(const std::string& type, const sptr<IRemoteObject>& callback)
{ {
std::lock_guard<std::mutex> autoLock(continuationLock_); std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iter = continuationCallbackMap_.find(type); std::vector<sptr<IRemoteObject>> vecCallback = continuationCallbackMap_[type];
if (iter == continuationCallbackMap_.end()) { if (vecCallback.empty()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str()); HILOGE("PopCallback not found, type:%{public}s", type.c_str());
return nullptr; return false;
} }
sptr<IRemoteObject> callback = iter->second; for (auto ele = vecCallback.begin(); ele != vecCallback.end(); ++ele) {
continuationCallbackMap_.erase(iter); if ((*ele) == callback) {
return callback; vecCallback.erase(ele);
continuationCallbackMap_[type] = vecCallback;
HILOGI("type:%{public}s, callback is exists, cleared successfully.", type.c_str());
return true;
}
}
HILOGI("type:%{public}s, callback is not exists!", type.c_str());
return false;
} }
sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId) sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId)
@ -257,26 +259,29 @@ sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId)
int32_t DSchedContinuation::NotifyDSchedEventResult(const std::string& type, int32_t resultCode) int32_t DSchedContinuation::NotifyDSchedEventResult(const std::string& type, int32_t resultCode)
{ {
HILOGI("GetCallback IDSchedEventListener"); HILOGI("GetCallback IDSchedEventListener");
sptr<IRemoteObject> callback = GetCallback(type); std::vector<sptr<IRemoteObject>> vecCallback = GetCallback(type);
if (callback == nullptr) { if (vecCallback.empty()) {
HILOGE("NotifyMissionCenterResult IDSchedEventListener is null"); HILOGE("NotifyMissionCenterResult IDSchedEventListener is null");
return INVALID_PARAMETERS_ERR; return INVALID_PARAMETERS_ERR;
} }
MessageParcel data; int32_t error = -1;
if (!data.WriteInterfaceToken(DSCHED_EVENT_TOKEN)) { for (auto callback = vecCallback.begin(); callback != vecCallback.end(); ++callback) {
HILOGE("NotifyMissionCenterResult write token failed"); MessageParcel data;
return INVALID_PARAMETERS_ERR; if (!data.WriteInterfaceToken(DSCHED_EVENT_TOKEN)) {
HILOGE("NotifyMissionCenterResult write token failed");
return INVALID_PARAMETERS_ERR;
}
PARCEL_WRITE_HELPER_RET(data, Int32, resultCode, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.srcNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.dstNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.bundleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.moduleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.abilityName, false);
MessageParcel reply;
MessageOption option;
int32_t error = (*callback)->SendRequest(DSCHED_EVENT_CALLBACK, data, reply, option);
HILOGI("NotifyDSchedEventListenerResult transact result: %{public}d", error);
} }
PARCEL_WRITE_HELPER_RET(data, Int32, resultCode, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.srcNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.dstNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.bundleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.moduleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.abilityName, false);
MessageParcel reply;
MessageOption option;
int32_t error = callback->SendRequest(DSCHED_EVENT_CALLBACK, data, reply, option);
HILOGI("NotifyDSchedEventListenerResult transact result: %{public}d", error);
return error; return error;
} }

View File

@ -451,10 +451,17 @@ int32_t DistributedSchedService::ContinueRemoteMission(const std::string& srcDev
sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId); sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId);
if (remoteDms == nullptr) { if (remoteDms == nullptr) {
HILOGE("get remote dms null!"); HILOGE("get remote dms null!");
int32_t dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(DSCHED_EVENT_KEY,
INVALID_REMOTE_PARAMETERS_ERR);
HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
return INVALID_REMOTE_PARAMETERS_ERR; return INVALID_REMOTE_PARAMETERS_ERR;
} }
int32_t result = remoteDms->ContinueMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams); int32_t result = remoteDms->ContinueMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
HILOGI("ContinueRemoteMission result: %{public}d!", result); HILOGI("ContinueRemoteMission result: %{public}d!", result);
if (result != ERR_OK) {
int32_t dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(DSCHED_EVENT_KEY, result);
HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
}
return result; return result;
} }
@ -2053,8 +2060,8 @@ int32_t DistributedSchedService::RegisterDSchedEventListener(const std::string&
int32_t DistributedSchedService::UnRegisterDSchedEventListener(const std::string& type, int32_t DistributedSchedService::UnRegisterDSchedEventListener(const std::string& type,
const sptr<IRemoteObject>& callback) const sptr<IRemoteObject>& callback)
{ {
sptr<IRemoteObject> result = dschedContinuation_->CleanupCallback(type); bool result = dschedContinuation_->CleanupCallback(type, callback);
if (result == nullptr) { if (!result) {
HILOGI("The callback does not exist."); HILOGI("The callback does not exist.");
} else { } else {
HILOGI("Clearing the callback succeeded."); HILOGI("Clearing the callback succeeded.");

View File

@ -60,6 +60,7 @@ const int DEFAULT_REQUEST_CODE = -1;
DistributedSchedStub::DistributedSchedStub() DistributedSchedStub::DistributedSchedStub()
{ {
InitExtendedLocalFuncsInner();
InitLocalFuncsInner(); InitLocalFuncsInner();
InitRemoteFuncsInner(); InitRemoteFuncsInner();
} }
@ -70,6 +71,17 @@ DistributedSchedStub::~DistributedSchedStub()
localFuncsMap_.clear(); localFuncsMap_.clear();
} }
void DistributedSchedStub::InitExtendedLocalFuncsInner()
{
// request codes for mission manager
#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::RegisterDSchedEventListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::UnRegisterDSchedEventListenerInner;
#endif
}
void DistributedSchedStub::InitLocalFuncsInner() void DistributedSchedStub::InitLocalFuncsInner()
{ {
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY)] = localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY)] =
@ -92,10 +104,6 @@ void DistributedSchedStub::InitLocalFuncsInner()
&DistributedSchedStub::GetRemoteMissionSnapshotInfoInner; &DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER)] = localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER)] =
&DistributedSchedStub::RegisterMissionListenerInner; &DistributedSchedStub::RegisterMissionListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::RegisterDSchedEventListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::UnRegisterDSchedEventListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_ON_LISTENER)] = localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_ON_LISTENER)] =
&DistributedSchedStub::RegisterOnListenerInner; &DistributedSchedStub::RegisterOnListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER)] = localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER)] =