diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index e73eb6d078632b9b73d56b6fc957aa62619bb27c..aa0dda32cba08c4a1ce15c8b4b6194ad438b56f4 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -25,6 +25,40 @@ namespace Storage { namespace DistributedFile { using namespace FileManagement::LibN; +std::vector GetJsPath(napi_env env, napi_value param) +{ + std::vector jsPath; + jsPath.clear(); + + bool isArray = false; + if (napi_is_array(env, param, &isArray) != napi_ok || isArray == false) { + LOGE("JS sendFile filename must be a string array."); + return jsPath; + } + + uint32_t arraySize = 0; + if (napi_get_array_length(env, param, &arraySize) != napi_ok) { + LOGE("JS sendFile get filename array size failed."); + return jsPath; + } + + for (uint32_t i = 0; i < arraySize; ++i) { + napi_value result = nullptr; + napi_status status = napi_get_element(env, param, i, &result); + if (status == napi_ok && result != nullptr) { + bool succ = false; + std::unique_ptr path; + std::tie(succ, path, std::ignore) = NVal(env, result).ToUTF8String(); + if (succ) { + jsPath.push_back(path.get()); + } else { + LOGE("JS sendFile: only string array were accepted as filename para."); + } + } + } + return jsPath; +} + napi_value JsSendFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -34,40 +68,24 @@ napi_value JsSendFile(napi_env env, napi_callback_info info) } bool succ = false; - auto resultCode = std::make_shared(); - std::unique_ptr deviceId; - std::vector sourPath; - std::vector destPath; + std::tie(succ, deviceId, std::ignore) = NVal(env, funcArg[DFS_ARG_POS::FIRST]).ToUTF8String(); + std::string deviceIdString = ""; + if (succ) { + deviceIdString = std::string(deviceId.get()); + } uint32_t fileCount = 0; - - std::tie(succ, deviceId, std::ignore) = NVal(env, funcArg[DFS_ARG_POS::FIRST]).ToUTF8String(); - napi_get_value_uint32(env, funcArg[DFS_ARG_POS::FOURTH], &fileCount); - if (fileCount != 1) { + napi_status status = napi_get_value_uint32(env, funcArg[DFS_ARG_POS::FOURTH], &fileCount); + if (status != napi_ok && fileCount != 1) { + LOGE("JS sendFile: param fileCounts only accecpt 1."); return nullptr; } - napi_value result; - napi_status status; - for (uint32_t i = 0; i < fileCount; ++i) { - status = napi_get_element(env, funcArg[DFS_ARG_POS::SECOND], i, &result); - if (napi_ok != status) { - return nullptr; - } - std::unique_ptr path; - std::tie(succ, path, std::ignore) = NVal(env, result).ToUTF8String(); - sourPath.push_back(path.get()); + std::vector sourPath = GetJsPath(env, funcArg[DFS_ARG_POS::SECOND]); + std::vector destPath = GetJsPath(env, funcArg[DFS_ARG_POS::THIRD]); - status = napi_get_element(env, funcArg[DFS_ARG_POS::THIRD], i, &result); - if (napi_ok != status) { - return nullptr; - } - std::tie(succ, path, std::ignore) = NVal(env, result).ToUTF8String(); - destPath.push_back(path.get()); - } - - std::string deviceIdString(deviceId.get()); + auto resultCode = std::make_shared(); auto cbExec = [deviceIdString, sourPath, destPath, fileCount, resultCode]() -> NError { *resultCode = SendFile::ExecSendFile(deviceIdString.c_str(), sourPath, destPath, fileCount); return NError(*resultCode); diff --git a/services/distributedfileservice/src/device/device_manager_agent.cpp b/services/distributedfileservice/src/device/device_manager_agent.cpp index aa418b9ca9009ad4856f91d2fde4836f522303b3..7e00effc1896be956c6f5c34e4084cf433831f9b 100644 --- a/services/distributedfileservice/src/device/device_manager_agent.cpp +++ b/services/distributedfileservice/src/device/device_manager_agent.cpp @@ -32,12 +32,24 @@ void DeviceManagerAgent::StartInstance() { // the time sequence can ensure there is no resource competition alreadyOnlineDev_.clear(); - RegisterToExternalDm(); + try { + RegisterToExternalDm(); + } catch (const DfsuException &e) { + LOGE("Register devicemagager callback failed."); + } catch (const std::exception &e) { + LOGE("Unexpected Low Level exception"); + } } void DeviceManagerAgent::StopInstance() { - UnregisterFromExternalDm(); + try { + UnregisterFromExternalDm(); + } catch (const DfsuException &e) { + LOGE("Unregister devicemagager callback failed."); + } catch (const std::exception &e) { + LOGE("Unexpected Low Level exception"); + } } void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) @@ -62,8 +74,14 @@ void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo void DeviceManagerAgent::OnRemoteDied() { LOGI("device manager service died"); - UnregisterFromExternalDm(); - RegisterToExternalDm(); + try { + UnregisterFromExternalDm(); + RegisterToExternalDm(); + } catch (const DfsuException &e) { + LOGE("Reregister devicemagager callback failed."); + } catch (const std::exception &e) { + LOGE("Unexpected Low Level exception"); + } } void DeviceManagerAgent::RegisterToExternalDm()