代码优化

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/continuation_manager:continuation_manager",
"//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"
],
"service_group": [
@ -111,6 +110,7 @@
}
],
"test": [
"//foundation/ability/dmsfwk/interfaces/innerkits/tests:dms_sdk_demo",
"//foundation/ability/dmsfwk/services/dtbschedmgr:unittest",
"//foundation/ability/dmsfwk/services/dtbabilitymgr:unittest",
"//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"
sanitize = {
cfi = true

View File

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

View File

@ -84,6 +84,7 @@ private:
bool EnforceInterfaceToken(MessageParcel& data);
bool CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data);
void SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo);
void InitExtendedLocalFuncsInner();
void InitLocalFuncsInner();
void InitRemoteFuncsInner();
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)
{
if (continuationHandler_ == nullptr) {
HILOGE("not initialized!");
return false;
}
HILOGI("DSchedContinuation PushCallback start!");
if (callback == nullptr) {
HILOGE("callback null!");
return false;
}
if (continuationHandler_ == nullptr) {
HILOGE("not initialized!");
return false;
}
std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iterSession = continuationCallbackMap_.find(type);
if (iterSession != continuationCallbackMap_.end()) {
HILOGE("type:%{public}s exist!", type.c_str());
return false;
std::vector<sptr<IRemoteObject>> vecCallback = continuationCallbackMap_[type];
for (auto ele = vecCallback.begin(); ele != vecCallback.end(); ++ele) {
if ((*ele) == callback) {
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;
}
@ -203,29 +204,30 @@ bool DSchedContinuation::PushCallback(int32_t missionId, const sptr<IRemoteObjec
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_);
auto iter = continuationCallbackMap_.find(type);
if (iter == continuationCallbackMap_.end()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str());
return nullptr;
}
sptr<IRemoteObject> callback = iter->second;
return callback;
return continuationCallbackMap_[type];
}
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_);
auto iter = continuationCallbackMap_.find(type);
if (iter == continuationCallbackMap_.end()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str());
return nullptr;
std::vector<sptr<IRemoteObject>> vecCallback = continuationCallbackMap_[type];
if (vecCallback.empty()) {
HILOGE("PopCallback not found, type:%{public}s", type.c_str());
return false;
}
sptr<IRemoteObject> callback = iter->second;
continuationCallbackMap_.erase(iter);
return callback;
for (auto ele = vecCallback.begin(); ele != vecCallback.end(); ++ele) {
if ((*ele) == 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)
@ -257,26 +259,29 @@ sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId)
int32_t DSchedContinuation::NotifyDSchedEventResult(const std::string& type, int32_t resultCode)
{
HILOGI("GetCallback IDSchedEventListener");
sptr<IRemoteObject> callback = GetCallback(type);
if (callback == nullptr) {
std::vector<sptr<IRemoteObject>> vecCallback = GetCallback(type);
if (vecCallback.empty()) {
HILOGE("NotifyMissionCenterResult IDSchedEventListener is null");
return INVALID_PARAMETERS_ERR;
}
MessageParcel data;
if (!data.WriteInterfaceToken(DSCHED_EVENT_TOKEN)) {
HILOGE("NotifyMissionCenterResult write token failed");
return INVALID_PARAMETERS_ERR;
int32_t error = -1;
for (auto callback = vecCallback.begin(); callback != vecCallback.end(); ++callback) {
MessageParcel data;
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;
}

View File

@ -451,10 +451,17 @@ int32_t DistributedSchedService::ContinueRemoteMission(const std::string& srcDev
sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId);
if (remoteDms == nullptr) {
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;
}
int32_t result = remoteDms->ContinueMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
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;
}
@ -2053,8 +2060,8 @@ int32_t DistributedSchedService::RegisterDSchedEventListener(const std::string&
int32_t DistributedSchedService::UnRegisterDSchedEventListener(const std::string& type,
const sptr<IRemoteObject>& callback)
{
sptr<IRemoteObject> result = dschedContinuation_->CleanupCallback(type);
if (result == nullptr) {
bool result = dschedContinuation_->CleanupCallback(type, callback);
if (!result) {
HILOGI("The callback does not exist.");
} else {
HILOGI("Clearing the callback succeeded.");

View File

@ -60,6 +60,7 @@ const int DEFAULT_REQUEST_CODE = -1;
DistributedSchedStub::DistributedSchedStub()
{
InitExtendedLocalFuncsInner();
InitLocalFuncsInner();
InitRemoteFuncsInner();
}
@ -70,6 +71,17 @@ DistributedSchedStub::~DistributedSchedStub()
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()
{
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY)] =
@ -92,10 +104,6 @@ void DistributedSchedStub::InitLocalFuncsInner()
&DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER)] =
&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)] =
&DistributedSchedStub::RegisterOnListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER)] =