diff --git a/interfaces/kits/js/file_share/grant_permissions.cpp b/interfaces/kits/js/file_share/grant_permissions.cpp index 7b06a584e77e079b2bc82859c6407770c95ca92e..18910ae63298d80298b21145d4c94d7588c4e905 100644 --- a/interfaces/kits/js/file_share/grant_permissions.cpp +++ b/interfaces/kits/js/file_share/grant_permissions.cpp @@ -76,6 +76,37 @@ static napi_value GetResultData(napi_env env, const vector &results) return res; } +static napi_status GetUriPolicy(napi_env env, napi_value agrv, std::vector &uriPolicies, uint32_t index) +{ + napi_value object; + napi_status status = napi_get_element(env, agrv, index, &object); + if (status != napi_ok) { + LOGE("get element failed"); + return status; + } + napi_value uriValue; + napi_value modeValue; + status = napi_get_named_property(env, object, "uri", &uriValue); + if (status != napi_ok) { + LOGE("get named property failed"); + return status; + } + status = napi_get_named_property(env, object, "operationMode", &modeValue); + if (status != napi_ok) { + LOGE("get named property failed"); + return status; + } + auto [succStr, str, ignore] = NVal(env, uriValue).ToUTF8String(); + auto [succMode, mode] = NVal(env, modeValue).ToUint32(); + if (!succStr || !succMode) { + LOGE("the argument error, succStr = %{public}d, succMode = %{public}d", succStr, succMode); + return napi_invalid_arg; + } + UriPolicyInfo uriPolicy {.uri = str.get(), .mode = mode}; + uriPolicies.emplace_back(uriPolicy); + return napi_ok; +} + static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector &uriPolicies) { uint32_t count; @@ -94,32 +125,11 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector< if (status != napi_ok) { return status; } - napi_value object; - status = napi_get_element(env, agrv, i, &object); + status = GetUriPolicy(env, agrv, uriPolicies, i); if (status != napi_ok) { - LOGE("get element failed"); + napi_close_handle_scope(env, scope); return status; } - napi_value uriValue; - napi_value modeValue; - status = napi_get_named_property(env, object, "uri", &uriValue); - if (status != napi_ok) { - LOGE("get named property failed"); - return status; - } - status = napi_get_named_property(env, object, "operationMode", &modeValue); - if (status != napi_ok) { - LOGE("get named property failed"); - return status; - } - auto [succStr, str, ignore] = NVal(env, uriValue).ToUTF8String(); - auto [succMode, mode] = NVal(env, modeValue).ToUint32(); - if (!succStr || !succMode) { - LOGE("the argument error"); - return napi_invalid_arg; - } - UriPolicyInfo uriPolicy {.uri = str.get(), .mode = mode}; - uriPolicies.emplace_back(uriPolicy); status = napi_close_handle_scope(env, scope); if (status != napi_ok) { return status; @@ -142,6 +152,38 @@ static napi_status CheckPathArray(napi_env env, napi_value agrv, uint32_t &count return napi_ok; } +static napi_status GetPathPolicy(napi_env env, napi_value agrv, std::vector &pathPolicies, + uint32_t index) +{ + napi_value object; + napi_status status = napi_get_element(env, agrv, index, &object); + if (status != napi_ok) { + LOGE("get element failed"); + return status; + } + napi_value pathValue; + napi_value modeValue; + status = napi_get_named_property(env, object, "path", &pathValue); + if (status != napi_ok) { + LOGE("get named property failed"); + return status; + } + status = napi_get_named_property(env, object, "operationMode", &modeValue); + if (status != napi_ok) { + LOGE("get named property failed"); + return status; + } + auto [succStr, str, ignore] = NVal(env, pathValue).ToUTF8String(); + auto [succMode, mode] = NVal(env, modeValue).ToUint32(); + if (!succStr || !succMode) { + LOGE("the argument error, succStr = %{public}d, succMode = %{public}d", succStr, succMode); + return napi_invalid_arg; + } + PathPolicyInfo pathPolicy {.path = str.get(), .mode = mode}; + pathPolicies.emplace_back(pathPolicy); + return napi_ok; +} + static napi_status GetPathPoliciesArg(napi_env env, napi_value agrv, std::vector &pathPolicies) { uint32_t count; @@ -156,32 +198,11 @@ static napi_status GetPathPoliciesArg(napi_env env, napi_value agrv, std::vector LOGE("open handle scope failed"); return status; } - napi_value object; - status = napi_get_element(env, agrv, i, &object); + status = GetPathPolicy(env, agrv, pathPolicies, i); if (status != napi_ok) { - LOGE("get element failed"); + napi_close_handle_scope(env, scope); return status; } - napi_value pathValue; - napi_value modeValue; - status = napi_get_named_property(env, object, "path", &pathValue); - if (status != napi_ok) { - LOGE("get named property failed"); - return status; - } - status = napi_get_named_property(env, object, "operationMode", &modeValue); - if (status != napi_ok) { - LOGE("get named property failed"); - return status; - } - auto [succStr, str, ignore] = NVal(env, pathValue).ToUTF8String(); - auto [succMode, mode] = NVal(env, modeValue).ToUint32(); - if (!succStr || !succMode) { - LOGE("the argument error"); - return napi_invalid_arg; - } - PathPolicyInfo pathPolicy {.path = str.get(), .mode = mode}; - pathPolicies.emplace_back(pathPolicy); status = napi_close_handle_scope(env, scope); if (status != napi_ok) { return status;