From 98bd026d2f4aff70c2e66581853b710c2db6a372 Mon Sep 17 00:00:00 2001 From: chennian Date: Mon, 19 Sep 2022 10:31:39 +0800 Subject: [PATCH] =?UTF-8?q?NAPI=E6=95=B4=E6=94=B9=20Signed-off-by:chennian?= =?UTF-8?q??= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- interfaces/kits/accesstoken/BUILD.gn | 5 +- .../accesstoken/napi/include/napi_atmanager.h | 23 +- .../napi/include/napi_context_common.h | 37 ++ .../accesstoken/napi/src/napi_atmanager.cpp | 463 ++++++++---------- .../napi/src/napi_context_common.cpp | 39 ++ interfaces/kits/common/include/napi_common.h | 31 +- interfaces/kits/common/src/napi_common.cpp | 99 ++-- .../privacy/napi/src/napi_context_common.cpp | 12 +- .../src/permission_record_manager_napi.cpp | 276 ++++++----- .../sensitive/sensitive_resource_manager.cpp | 2 + 10 files changed, 537 insertions(+), 450 deletions(-) create mode 100644 interfaces/kits/accesstoken/napi/include/napi_context_common.h create mode 100644 interfaces/kits/accesstoken/napi/src/napi_context_common.cpp diff --git a/interfaces/kits/accesstoken/BUILD.gn b/interfaces/kits/accesstoken/BUILD.gn index 566139e17..289639a49 100644 --- a/interfaces/kits/accesstoken/BUILD.gn +++ b/interfaces/kits/accesstoken/BUILD.gn @@ -23,7 +23,10 @@ ohos_shared_library("libabilityaccessctrl") { "//foundation/arkui/napi/interfaces/kits", ] - sources = [ "//base/security/access_token/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp" ] + sources = [ + "//base/security/access_token/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp", + "//base/security/access_token/interfaces/kits/accesstoken/napi/src/napi_context_common.cpp", + ] deps = [ "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", diff --git a/interfaces/kits/accesstoken/napi/include/napi_atmanager.h b/interfaces/kits/accesstoken/napi/include/napi_atmanager.h index 976eeda60..27aae3e3a 100644 --- a/interfaces/kits/accesstoken/napi/include/napi_atmanager.h +++ b/interfaces/kits/accesstoken/napi/include/napi_atmanager.h @@ -24,6 +24,7 @@ #include "access_token.h" #include "accesstoken_kit.h" #include "napi_common.h" +#include "napi_context_common.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "perm_state_change_callback_customize.h" @@ -33,8 +34,6 @@ namespace Security { namespace AccessToken { const int AT_PERM_OPERA_FAIL = -1; const int AT_PERM_OPERA_SUCC = 0; -const int VERIFY_OR_FLAG_INPUT_MAX_VALUES = 2; -const int GRANT_OR_REVOKE_INPUT_MAX_VALUES = 4; enum PermissionStateChangeType { PERMISSION_REVOKED_OPER = 0, @@ -79,17 +78,13 @@ struct UnregisterPermStateChangeInfo : public PermStateChangeContext { PermStateChangeScope scopeInfo; }; -struct AtManagerAsyncContext { - napi_env env = nullptr; +struct AtManagerAsyncContext : public AtManagerAsyncWorkData { + explicit AtManagerAsyncContext(napi_env env) : AtManagerAsyncWorkData(env) {} + uint32_t tokenId = 0; - char permissionName[ VALUE_BUFFER_SIZE ] = { 0 }; - size_t pNameLen = 0; - int flag = 0; - int result = AT_PERM_OPERA_FAIL; // default failed - - napi_deferred deferred = nullptr; // promise handle - napi_ref callbackRef = nullptr; // callback handle - napi_async_work work = nullptr; // work handle + std::string permissionName; + int32_t flag = 0; + int32_t result = AT_PERM_OPERA_FAIL; }; class NapiAtManager { @@ -106,11 +101,11 @@ private: static napi_value GetPermissionFlags(napi_env env, napi_callback_info info); static napi_value GetVersion(napi_env env, napi_callback_info info); - static void ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, + static bool ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, AtManagerAsyncContext& asyncContext); static void VerifyAccessTokenExecute(napi_env env, void *data); static void VerifyAccessTokenComplete(napi_env env, napi_status status, void *data); - static void ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, + static bool ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, AtManagerAsyncContext& asyncContext); static void GrantUserGrantedPermissionExecute(napi_env env, void *data); static void GrantUserGrantedPermissionComplete(napi_env env, napi_status status, void *data); diff --git a/interfaces/kits/accesstoken/napi/include/napi_context_common.h b/interfaces/kits/accesstoken/napi/include/napi_context_common.h new file mode 100644 index 000000000..2c23e682c --- /dev/null +++ b/interfaces/kits/accesstoken/napi/include/napi_context_common.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERFACES_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H +#define INTERFACES_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi_common.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +struct AtManagerAsyncWorkData { + explicit AtManagerAsyncWorkData(napi_env env); + virtual ~AtManagerAsyncWorkData(); + + napi_env env = nullptr; + napi_async_work work = nullptr; + napi_deferred deferred = nullptr; + napi_ref callbackRef = nullptr; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif /* INTERFACES_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H */ diff --git a/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp b/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp index b88c10015..6c270d513 100644 --- a/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp +++ b/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp @@ -34,6 +34,9 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenAbilityAccessCtrl" }; +static constexpr int32_t VERIFY_OR_FLAG_INPUT_MAX_PARAMS = 2; +static constexpr int32_t GRANT_OR_REVOKE_INPUT_MAX_PARAMS = 4; +static constexpr int32_t ON_OFF_MAX_PARAMS = 4; static bool ConvertPermStateChangeInfo(napi_env env, napi_value value, const PermStateChangeInfo& result) { @@ -60,11 +63,11 @@ static void UvQueueWorkPermStateChanged(uv_work_t* work, int status) RegisterPermStateChangeWorker* registerPermStateChangeData = reinterpret_cast(work->data); std::unique_ptr workPtr {registerPermStateChangeData}; - napi_value result[ARGS_ONE] = {nullptr}; + napi_value result = {nullptr}; NAPI_CALL_RETURN_VOID(registerPermStateChangeData->env, - napi_create_array(registerPermStateChangeData->env, &result[PARAM0])); + napi_create_array(registerPermStateChangeData->env, &result)); if (!ConvertPermStateChangeInfo(registerPermStateChangeData->env, - result[PARAM0], registerPermStateChangeData->result)) { + result, registerPermStateChangeData->result)) { ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertPermStateChangeInfo failed"); return; } @@ -78,7 +81,7 @@ static void UvQueueWorkPermStateChanged(uv_work_t* work, int status) napi_get_reference_value(registerPermStateChangeData->env, registerPermStateChangeData->ref, &callback)); NAPI_CALL_RETURN_VOID(registerPermStateChangeData->env, napi_call_function(registerPermStateChangeData->env, - undefined, callback, ARGS_ONE, &result[PARAM0], &resultout)); + undefined, callback, 1, &result, &resultout)); ACCESSTOKEN_LOG_DEBUG(LABEL, "UvQueueWorkPermStateChanged end"); }; } // namespace @@ -210,17 +213,18 @@ napi_value NapiAtManager::JsConstructor(napi_env env, napi_callback_info cbinfo) ACCESSTOKEN_LOG_ERROR(LABEL, "objectInfo is nullptr"); return nullptr; } - if (napi_wrap(env, thisVar, objectInfo, [](napi_env env, void* data, void* hint) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "delete accesstoken kit"); - if (data != nullptr) { - AccessTokenKit* objectInfo = (AccessTokenKit*) data; - delete objectInfo; - } - }, nullptr, nullptr) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_wrap failed"); - return nullptr; - } + std::unique_ptr objPtr {objectInfo}; + NAPI_CALL(env, napi_wrap(env, thisVar, objectInfo, + [](napi_env env, void* data, void* hint) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "delete accesstoken kit"); + if (data != nullptr) { + AccessTokenKit* objectInfo = (AccessTokenKit*)data; + delete objectInfo; + } + }, + nullptr, nullptr)); + objPtr.release(); return thisVar; } @@ -231,62 +235,49 @@ napi_value NapiAtManager::CreateAtManager(napi_env env, napi_callback_info cbInf napi_value instance = nullptr; napi_value cons = nullptr; - if (napi_get_reference_value(env, atManagerRef_, &cons) != napi_ok) { - return nullptr; - } - + NAPI_CALL(env, napi_get_reference_value(env, atManagerRef_, &cons)); ACCESSTOKEN_LOG_DEBUG(LABEL, "Get a reference to the global variable atManagerRef_ complete"); - if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) { - return nullptr; - } + NAPI_CALL(env, napi_new_instance(env, cons, 0, nullptr, &instance)); ACCESSTOKEN_LOG_DEBUG(LABEL, "New the js instance complete"); - return instance; } -void NapiAtManager::ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, +bool NapiAtManager::ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, AtManagerAsyncContext& asyncContext) { - size_t argc = VERIFY_OR_FLAG_INPUT_MAX_VALUES; + size_t argc = VERIFY_OR_FLAG_INPUT_MAX_PARAMS; - napi_value argv[VERIFY_OR_FLAG_INPUT_MAX_VALUES] = { 0 }; + napi_value argv[VERIFY_OR_FLAG_INPUT_MAX_PARAMS] = { nullptr }; napi_value thisVar = nullptr; void *data = nullptr; - - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data), false); asyncContext.env = env; - - // parse input tokenId and permissionName - for (size_t i = 0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); - - if (valueType == napi_number) { - napi_get_value_uint32(env, argv[i], &(asyncContext.tokenId)); // get tokenId - } else if (valueType == napi_string) { - napi_get_value_string_utf8(env, argv[i], asyncContext.permissionName, - VALUE_BUFFER_SIZE + 1, &(asyncContext.pNameLen)); // get permissionName - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "Type matching failed"); - return; - } + // 0: the first parameter of argv + if (!ParseUint32(env, argv[0], asyncContext.tokenId)) { + return false; } - asyncContext.result = AT_PERM_OPERA_SUCC; + // 1: the second parameter of argv + if (!ParseString(env, argv[1], asyncContext.permissionName)) { + return false; + } ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenID = %{public}d, permissionName = %{public}s", asyncContext.tokenId, - asyncContext.permissionName); + asyncContext.permissionName.c_str()); + return true; } void NapiAtManager::VerifyAccessTokenExecute(napi_env env, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } - // use innerkit class method to verify permission asyncContext->result = AccessTokenKit::VerifyAccessToken(asyncContext->tokenId, asyncContext->permissionName); } @@ -294,52 +285,48 @@ void NapiAtManager::VerifyAccessTokenExecute(napi_env env, void *data) void NapiAtManager::VerifyAccessTokenComplete(napi_env env, napi_status status, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } + std::unique_ptr context {asyncContext}; napi_value result; ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenId = %{public}d, permissionName = %{public}s, verify result = %{public}d.", - asyncContext->tokenId, asyncContext->permissionName, asyncContext->result); + asyncContext->tokenId, asyncContext->permissionName.c_str(), asyncContext->result); - // only resolve, no reject currently - napi_create_int32(env, asyncContext->result, &result); // verify result - napi_resolve_deferred(env, asyncContext->deferred, result); - - // after return the result, free resources - napi_delete_async_work(env, asyncContext->work); - delete asyncContext; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &result)); // verify result + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, result)); } napi_value NapiAtManager::VerifyAccessToken(napi_env env, napi_callback_info info) { ACCESSTOKEN_LOG_DEBUG(LABEL, "VerifyAccessToken begin."); - auto *asyncContext = new AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } - ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext); - if (asyncContext->result == AT_PERM_OPERA_FAIL) { - delete asyncContext; + std::unique_ptr context {asyncContext}; + if (!ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext)) { return nullptr; } - // after get input, keep result default failed - asyncContext->result = AT_PERM_OPERA_FAIL; - napi_value result = nullptr; - napi_create_promise(env, &(asyncContext->deferred), &result); // create delay promise object + NAPI_CALL(env, napi_create_promise(env, &(asyncContext->deferred), &result)); - napi_value resource = nullptr; // resource name - napi_create_string_utf8(env, "VerifyAccessToken", NAPI_AUTO_LENGTH, &resource); + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "VerifyAccessToken", NAPI_AUTO_LENGTH, &resource)); - napi_create_async_work( // define work - env, nullptr, resource, VerifyAccessTokenExecute, VerifyAccessTokenComplete, - reinterpret_cast(asyncContext), &(asyncContext->work)); - napi_queue_async_work(env, asyncContext->work); // add async work handle to the napi queue and wait for result + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resource, + VerifyAccessTokenExecute, VerifyAccessTokenComplete, + reinterpret_cast(asyncContext), &(asyncContext->work))); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); ACCESSTOKEN_LOG_DEBUG(LABEL, "VerifyAccessToken end."); - + context.release(); return result; } @@ -347,76 +334,73 @@ napi_value NapiAtManager::VerifyAccessTokenSync(napi_env env, napi_callback_info { ACCESSTOKEN_LOG_DEBUG(LABEL, "VerifyAccessToken begin."); - auto *asyncContext = new AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } - ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext); - if (asyncContext->result == AT_PERM_OPERA_FAIL) { - delete asyncContext; + std::unique_ptr context {asyncContext}; + if (!ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext)) { return nullptr; } - // use innerkit class method to verify permission asyncContext->result = AccessTokenKit::VerifyAccessToken(asyncContext->tokenId, asyncContext->permissionName); napi_value result = nullptr; - napi_create_int32(env, asyncContext->result, &result); // verify result + NAPI_CALL(env, napi_create_int32(env, asyncContext->result, &result)); ACCESSTOKEN_LOG_DEBUG(LABEL, "VerifyAccessToken end."); - return result; } -void NapiAtManager::ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, +bool NapiAtManager::ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, AtManagerAsyncContext& asyncContext) { - size_t argc = GRANT_OR_REVOKE_INPUT_MAX_VALUES; - - napi_value argv[GRANT_OR_REVOKE_INPUT_MAX_VALUES] = {nullptr}; + size_t argc = GRANT_OR_REVOKE_INPUT_MAX_PARAMS; + napi_value argv[GRANT_OR_REVOKE_INPUT_MAX_PARAMS] = {nullptr}; napi_value thisVar = nullptr; void *data = nullptr; - - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data), false); asyncContext.env = env; + // 0: the first parameter of argv + if (!ParseUint32(env, argv[0], asyncContext.tokenId)) { + return false; + } - // parse input tokenId and permissionName - for (size_t i = 0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); - - if ((i == 0) && (valueType == napi_number)) { - napi_get_value_uint32(env, argv[i], &(asyncContext.tokenId)); // get tokenId - } else if (valueType == napi_string) { - napi_get_value_string_utf8(env, argv[i], asyncContext.permissionName, - VALUE_BUFFER_SIZE + 1, &(asyncContext.pNameLen)); // get permissionName - } else if (valueType == napi_number) { - napi_get_value_int32(env, argv[i], &(asyncContext.flag)); // get flag - } else if (valueType == napi_function) { - napi_create_reference(env, argv[i], 1, &asyncContext.callbackRef); // get probably callback - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "Type matching failed"); - return; - } + // 1: the second parameter of argv + if (!ParseString(env, argv[1], asyncContext.permissionName)) { + return false; + } + + // 2: the third parameter of argv + if (!ParseInt32(env, argv[2], asyncContext.flag)) { + return false; } - asyncContext.result = AT_PERM_OPERA_SUCC; + if (argc == GRANT_OR_REVOKE_INPUT_MAX_PARAMS) { + // 3: the fourth parameter of argv + if (!ParseCallback(env, argv[3], asyncContext.callbackRef)) { + return false; + } + } ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenID = %{public}d, permissionName = %{public}s, flag = %{public}d", - asyncContext.tokenId, asyncContext.permissionName, asyncContext.flag); + asyncContext.tokenId, asyncContext.permissionName.c_str(), asyncContext.flag); + return true; } void NapiAtManager::GrantUserGrantedPermissionExecute(napi_env env, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } PermissionDef permissionDef; - // struct init, can not use = { 0 } or memset otherwise program crashdump permissionDef.grantMode = 0; permissionDef.availableLevel = APL_NORMAL; permissionDef.provisionEnable = false; @@ -424,15 +408,12 @@ void NapiAtManager::GrantUserGrantedPermissionExecute(napi_env env, void *data) permissionDef.labelId = 0; permissionDef.descriptionId = 0; - // use innerkit class method to check if the permission grantmode is USER_GRANT-0 - int ret = AccessTokenKit::GetDefPermission(asyncContext->permissionName, permissionDef); - if (ret) { - // this means permission is undefined, return failed + if (AccessTokenKit::GetDefPermission(asyncContext->permissionName, permissionDef) != AT_PERM_OPERA_SUCC) { return; } - ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName = %{public}s, grantmode = %{public}d.", asyncContext->permissionName, - permissionDef.grantMode); + ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName = %{public}s, grantmode = %{public}d.", + asyncContext->permissionName.c_str(), permissionDef.grantMode); // only user_grant permission can use innerkit class method to grant permission, system_grant return failed if (permissionDef.grantMode == USER_GRANT) { @@ -441,51 +422,47 @@ void NapiAtManager::GrantUserGrantedPermissionExecute(napi_env env, void *data) ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenId = %{public}d, permissionName = %{public}s, flag = %{public}d, grant result = %{public}d.", - asyncContext->tokenId, asyncContext->permissionName, asyncContext->flag, asyncContext->result); + asyncContext->tokenId, asyncContext->permissionName.c_str(), asyncContext->flag, asyncContext->result); } } void NapiAtManager::GrantUserGrantedPermissionComplete(napi_env env, napi_status status, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); - napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; // for AsyncCallback + if (asyncContext == nullptr) { + return; + } + napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; - // no err, results[0] remain nullptr - napi_create_int32(env, asyncContext->result, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + std::unique_ptr callbackPtr {asyncContext}; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &results[ASYNC_CALL_BACK_VALUES_NUM - 1])); - if (asyncContext->deferred) { - // promise type, no reject currently - napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + if (asyncContext->deferred != nullptr) { + NAPI_CALL_RETURN_VOID( + env, napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1])); } else { - // callback type napi_value callback = nullptr; - napi_value thisValue = nullptr; // recv napi value - napi_value thatValue = nullptr; // result napi value + napi_value thisValue = nullptr; + napi_value thatValue = nullptr; - // set call function params->napi_call_function(env, recv, func, argc, argv, result) - napi_get_undefined(env, &thisValue); // can not null otherwise js code can not get return - napi_create_int32(env, 0, &thatValue); // can not null otherwise js code can not get return - napi_get_reference_value(env, asyncContext->callbackRef, &callback); - napi_call_function(env, thisValue, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &thatValue); - napi_delete_reference(env, asyncContext->callbackRef); // release callback handle + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &thisValue)); + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, 0, &thatValue)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncContext->callbackRef, &callback)); + NAPI_CALL_RETURN_VOID(env, + napi_call_function(env, thisValue, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &thatValue)); } - - // after return the result, free resources - napi_delete_async_work(env, asyncContext->work); - delete asyncContext; } napi_value NapiAtManager::GetVersion(napi_env env, napi_callback_info info) { ACCESSTOKEN_LOG_DEBUG(LABEL, "GetVersion begin."); - auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } std::unique_ptr context {asyncContext}; - asyncContext->result = AT_PERM_OPERA_FAIL; napi_value result = nullptr; NAPI_CALL(env, napi_create_promise(env, &(asyncContext->deferred), &result)); @@ -505,6 +482,9 @@ napi_value NapiAtManager::GetVersion(napi_env env, napi_callback_info info) void NapiAtManager::GetVersionExecute(napi_env env, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } asyncContext->result = AccessTokenKit::GetVersion(); ACCESSTOKEN_LOG_DEBUG(LABEL, "version result = %{public}d.", asyncContext->result); } @@ -512,66 +492,64 @@ void NapiAtManager::GetVersionExecute(napi_env env, void *data) void NapiAtManager::GetVersionComplete(napi_env env, napi_status status, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } + std::unique_ptr context {asyncContext}; napi_value result; ACCESSTOKEN_LOG_DEBUG(LABEL, "version result = %{public}d.", asyncContext->result); NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &result)); NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, result)); - - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, asyncContext->work)); - delete asyncContext; } napi_value NapiAtManager::GrantUserGrantedPermission(napi_env env, napi_callback_info info) { ACCESSTOKEN_LOG_DEBUG(LABEL, "GrantUserGrantedPermission begin."); - auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); // for async work deliver data if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } - ParseInputGrantOrRevokePermission(env, info, *asyncContext); - if (asyncContext->result == AT_PERM_OPERA_FAIL) { - delete asyncContext; + std::unique_ptr context {asyncContext}; + if (!ParseInputGrantOrRevokePermission(env, info, *asyncContext)) { return nullptr; } - // after get input, keep result default failed - asyncContext->result = AT_PERM_OPERA_FAIL; - napi_value result = nullptr; if (asyncContext->callbackRef == nullptr) { - // when callback null, create delay promise object for returning result in async work complete function - napi_create_promise(env, &(asyncContext->deferred), &result); + NAPI_CALL(env, napi_create_promise(env, &(asyncContext->deferred), &result)); } else { - // callback not null, use callback type to return result - napi_get_undefined(env, &result); + NAPI_CALL(env, napi_get_undefined(env, &result)); } - napi_value resource = nullptr; // resource name - napi_create_string_utf8(env, "GrantUserGrantedPermission", NAPI_AUTO_LENGTH, &resource); + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "GrantUserGrantedPermission", NAPI_AUTO_LENGTH, &resource)); - napi_create_async_work( // define work - env, nullptr, resource, GrantUserGrantedPermissionExecute, GrantUserGrantedPermissionComplete, - reinterpret_cast(asyncContext), &(asyncContext->work)); + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resource, + GrantUserGrantedPermissionExecute, GrantUserGrantedPermissionComplete, + reinterpret_cast(asyncContext), &(asyncContext->work))); - napi_queue_async_work(env, asyncContext->work); // add async work handle to the napi queue and wait for result + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); ACCESSTOKEN_LOG_DEBUG(LABEL, "GrantUserGrantedPermission end."); - + context.release(); return result; } void NapiAtManager::RevokeUserGrantedPermissionExecute(napi_env env, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } PermissionDef permissionDef; - // struct init, can not use = { 0 } or memset otherwise program crashdump permissionDef.grantMode = 0; permissionDef.availableLevel = APL_NORMAL; permissionDef.provisionEnable = false; @@ -579,15 +557,12 @@ void NapiAtManager::RevokeUserGrantedPermissionExecute(napi_env env, void *data) permissionDef.labelId = 0; permissionDef.descriptionId = 0; - // use innerkit class method to check if the permission grantmode is USER_GRANT-0 - int ret = AccessTokenKit::GetDefPermission(asyncContext->permissionName, permissionDef); - if (ret) { - // this means permission is undefined, return failed + if (AccessTokenKit::GetDefPermission(asyncContext->permissionName, permissionDef) != AT_PERM_OPERA_SUCC) { return; } - ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName = %{public}s, grantmode = %{public}d.", asyncContext->permissionName, - permissionDef.grantMode); + ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName = %{public}s, grantmode = %{public}d.", + asyncContext->permissionName.c_str(), permissionDef.grantMode); // only user_grant permission can use innerkit class method to grant permission, system_grant return failed if (permissionDef.grantMode == USER_GRANT) { @@ -596,80 +571,70 @@ void NapiAtManager::RevokeUserGrantedPermissionExecute(napi_env env, void *data) ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenId = %{public}d, permissionName = %{public}s, flag = %{public}d, revoke result = %{public}d.", - asyncContext->tokenId, asyncContext->permissionName, asyncContext->flag, asyncContext->result); + asyncContext->tokenId, asyncContext->permissionName.c_str(), asyncContext->flag, asyncContext->result); } } void NapiAtManager::RevokeUserGrantedPermissionComplete(napi_env env, napi_status status, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); + if (asyncContext == nullptr) { + return; + } napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; // for AsyncCallback - // no err, results[0] remain nullptr - napi_create_int32(env, asyncContext->result, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + std::unique_ptr callbackPtr {asyncContext}; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &results[ASYNC_CALL_BACK_VALUES_NUM - 1])); - if (asyncContext->deferred) { - // promise type, no reject currently - napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + if (asyncContext->deferred != nullptr) { + NAPI_CALL_RETURN_VOID( + env, napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1])); } else { - // callback type napi_value callback = nullptr; - napi_value thisValue = nullptr; // recv napi value - napi_value thatValue = nullptr; // result napi value + napi_value thisValue = nullptr; + napi_value thatValue = nullptr; - // set call function params->napi_call_function(env, recv, func, argc, argv, result) - napi_get_undefined(env, &thisValue); // can not null otherwise js code can not get return - napi_create_int32(env, 0, &thatValue); // can not null otherwise js code can not get return - napi_get_reference_value(env, asyncContext->callbackRef, &callback); - napi_call_function(env, thisValue, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &thatValue); - napi_delete_reference(env, asyncContext->callbackRef); // release callback handle + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &thisValue)); + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, 0, &thatValue)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncContext->callbackRef, &callback)); + NAPI_CALL_RETURN_VOID(env, + napi_call_function(env, thisValue, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &thatValue)); } - - // after return the result, free resources - napi_delete_async_work(env, asyncContext->work); - delete asyncContext; } napi_value NapiAtManager::RevokeUserGrantedPermission(napi_env env, napi_callback_info info) { ACCESSTOKEN_LOG_DEBUG(LABEL, "RevokeUserGrantedPermission begin."); - auto *asyncContext = new AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); // for async work deliver data if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } - ParseInputGrantOrRevokePermission(env, info, *asyncContext); - if (asyncContext->result == AT_PERM_OPERA_FAIL) { - delete asyncContext; + std::unique_ptr context {asyncContext}; + if (!ParseInputGrantOrRevokePermission(env, info, *asyncContext)) { return nullptr; } - // after get input, keep result default failed - asyncContext->result = AT_PERM_OPERA_FAIL; - napi_value result = nullptr; - if (asyncContext->callbackRef == nullptr) { - // when callback null, create delay promise object for returning result in async work complete function - napi_create_promise(env, &(asyncContext->deferred), &result); + NAPI_CALL(env, napi_create_promise(env, &(asyncContext->deferred), &result)); } else { - // callback not null, use callback type to return result - napi_get_undefined(env, &result); + NAPI_CALL(env, napi_get_undefined(env, &result)); } - napi_value resource = nullptr; // resource name - napi_create_string_utf8(env, "RevokeUserGrantedPermission", NAPI_AUTO_LENGTH, &resource); - - napi_create_async_work( // define work - env, nullptr, resource, RevokeUserGrantedPermissionExecute, RevokeUserGrantedPermissionComplete, - reinterpret_cast(asyncContext), &(asyncContext->work)); + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "RevokeUserGrantedPermission", NAPI_AUTO_LENGTH, &resource)); - napi_queue_async_work(env, asyncContext->work); // add async work handle to the napi queue and wait for result + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resource, + RevokeUserGrantedPermissionExecute, RevokeUserGrantedPermissionComplete, + reinterpret_cast(asyncContext), &(asyncContext->work))); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); ACCESSTOKEN_LOG_DEBUG(LABEL, "RevokeUserGrantedPermission end."); - + context.release(); return result; } @@ -677,7 +642,6 @@ void NapiAtManager::GetPermissionFlagsExecute(napi_env env, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); - // use innerkit class method to get permission flag asyncContext->flag = AccessTokenKit::GetPermissionFlag(asyncContext->tokenId, asyncContext->permissionName); } @@ -685,39 +649,34 @@ void NapiAtManager::GetPermissionFlagsExecute(napi_env env, void *data) void NapiAtManager::GetPermissionFlagsComplete(napi_env env, napi_status status, void *data) { AtManagerAsyncContext* asyncContext = reinterpret_cast(data); - napi_value result; + if (asyncContext == nullptr) { + return; + } + napi_value result = nullptr; ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName = %{public}s, tokenId = %{public}d, flag = %{public}d.", - asyncContext->permissionName, asyncContext->tokenId, asyncContext->flag); + asyncContext->permissionName.c_str(), asyncContext->tokenId, asyncContext->flag); - // only resolve, no reject currently - napi_create_int32(env, asyncContext->flag, &result); - napi_resolve_deferred(env, asyncContext->deferred, result); - - // after return the result, free resources - napi_delete_async_work(env, asyncContext->work); - delete asyncContext; + std::unique_ptr callbackPtr {asyncContext}; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->flag, &result)); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, result)); } napi_value NapiAtManager::GetPermissionFlags(napi_env env, napi_callback_info info) { ACCESSTOKEN_LOG_DEBUG(LABEL, "GetPermissionFlags begin."); - auto *asyncContext = new AtManagerAsyncContext(); // for async work deliver data + auto *asyncContext = new (std::nothrow) AtManagerAsyncContext(env); if (asyncContext == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); return nullptr; } - ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext); - if (asyncContext->result == AT_PERM_OPERA_FAIL) { - delete asyncContext; + std::unique_ptr context {asyncContext}; + if (!ParseInputVerifyPermissionOrGetFlag(env, info, *asyncContext)) { return nullptr; } - // after get input, keep result default failed - asyncContext->result = AT_PERM_OPERA_FAIL; - napi_value result = nullptr; napi_create_promise(env, &(asyncContext->deferred), &result); // create delay promise object @@ -730,41 +689,41 @@ napi_value NapiAtManager::GetPermissionFlags(napi_env env, napi_callback_info in napi_queue_async_work(env, asyncContext->work); // add async work handle to the napi queue and wait for result ACCESSTOKEN_LOG_DEBUG(LABEL, "GetPermissionFlags end."); - + context.release(); return result; } bool NapiAtManager::ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, RegisterPermStateChangeInfo& registerPermStateChangeInfo) { - size_t argc = ARGS_FOUR; - napi_value argv[ARGS_FOUR] = {nullptr}; + size_t argc = ON_OFF_MAX_PARAMS; + napi_value argv[ON_OFF_MAX_PARAMS] = {nullptr}; napi_value thisVar = nullptr; napi_ref callback = nullptr; - if (napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_get_cb_info failed"); + NAPI_CALL_BASE(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL), false); + + // 0: the first parameter of argv + std::string type; + if (!ParseString(env, argv[0], type)) { return false; } - std::string type = ParseString(env, argv[PARAM0]); + PermStateChangeScope scopeInfo; - if (!ParseAccessTokenIDArray(env, argv[PARAM1], scopeInfo.tokenIDs)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseAccessTokenIDArray failed"); - return false; - } - if (!ParseStringArray(env, argv[PARAM2], scopeInfo.permList)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseStringArray failed"); + // 1: the second parameter of argv + if (!ParseAccessTokenIDArray(env, argv[1], scopeInfo.tokenIDs)) { return false; } - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[PARAM3], &valueType); // get PRARM[3] type - if (valueType != napi_function) { - ACCESSTOKEN_LOG_ERROR(LABEL, "argv[PARAM3] type matching failed"); + + // 2: the third parameter of argv + if (!ParseStringArray(env, argv[2], scopeInfo.permList)) { return false; } - if (napi_create_reference(env, argv[PARAM3], 1, &callback) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_create_reference failed"); + + // 3: the fourth parameter of argv + if (!ParseCallback(env, argv[3], callback)) { return false; } + AccessTokenKit* accessTokenKitInfo = nullptr; if (napi_unwrap(env, thisVar, reinterpret_cast(&accessTokenKitInfo)) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "napi_unwrap failed"); @@ -817,41 +776,41 @@ napi_value NapiAtManager::RegisterPermStateChangeCallback(napi_env env, napi_cal bool NapiAtManager::ParseInputToUnregister(const napi_env env, napi_callback_info cbInfo, UnregisterPermStateChangeInfo& unregisterPermStateChangeInfo) { - size_t argc = ARGS_FOUR; - napi_value argv[ARGS_FOUR] = {nullptr}; + size_t argc = ON_OFF_MAX_PARAMS; + napi_value argv[ON_OFF_MAX_PARAMS] = {nullptr}; napi_value thisVar = nullptr; napi_ref callback = nullptr; if (napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "napi_get_cb_info failed"); return false; } - std::string type = ParseString(env, argv[PARAM0]); + + // 0: the first parameter of argv + std::string type; + if (!ParseString(env, argv[0], type)) { + return false; + } + PermStateChangeScope scopeInfo; - if (!ParseAccessTokenIDArray(env, argv[PARAM1], scopeInfo.tokenIDs)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseAccessTokenIDArray failed"); + // 1: the second parameter of argv + if (!ParseAccessTokenIDArray(env, argv[1], scopeInfo.tokenIDs)) { return false; } - if (!ParseStringArray(env, argv[PARAM2], scopeInfo.permList)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseStringArray failed"); + + // 2: the third parameter of argv + if (!ParseStringArray(env, argv[2], scopeInfo.permList)) { return false; } - if (argc >= ARGS_FOUR) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[PARAM3], &valueType); // get PRARM[3] type - if (valueType != napi_function) { - ACCESSTOKEN_LOG_ERROR(LABEL, "argv[PARAM3] type matching failed"); - return false; - } - if (napi_create_reference(env, argv[PARAM3], 1, &callback) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_create_reference failed"); + + if (argc == ON_OFF_MAX_PARAMS) { + // 3: the fourth parameter of argv + if (!ParseCallback(env, argv[3], callback)) { return false; } } AccessTokenKit* accessTokenKitInfo = nullptr; - if (napi_unwrap(env, thisVar, reinterpret_cast(&accessTokenKitInfo)) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_unwrap failed"); - return false; - } + NAPI_CALL_BASE(env, napi_unwrap(env, thisVar, reinterpret_cast(&accessTokenKitInfo)), false); + std::sort(scopeInfo.tokenIDs.begin(), scopeInfo.tokenIDs.end()); std::sort(scopeInfo.permList.begin(), scopeInfo.permList.end()); unregisterPermStateChangeInfo.env = env; diff --git a/interfaces/kits/accesstoken/napi/src/napi_context_common.cpp b/interfaces/kits/accesstoken/napi/src/napi_context_common.cpp new file mode 100644 index 000000000..63f3357b2 --- /dev/null +++ b/interfaces/kits/accesstoken/napi/src/napi_context_common.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "napi_context_common.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +AtManagerAsyncWorkData::AtManagerAsyncWorkData(napi_env envValue) +{ + env = envValue; +} + +AtManagerAsyncWorkData::~AtManagerAsyncWorkData() +{ + if (callbackRef != nullptr) { + napi_delete_reference(env, callbackRef); + callbackRef = nullptr; + } + + if (work != nullptr) { + napi_delete_async_work(env, work); + work = nullptr; + } +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/common/include/napi_common.h b/interfaces/kits/common/include/napi_common.h index 114820887..2ad91604a 100644 --- a/interfaces/kits/common/include/napi_common.h +++ b/interfaces/kits/common/include/napi_common.h @@ -23,28 +23,21 @@ namespace OHOS { namespace Security { namespace AccessToken { -const int ARGS_ONE = 1; -const int ARGS_TWO = 2; -const int ARGS_THREE = 3; -const int ARGS_FOUR = 4; -const int ARGS_FIVE = 5; -const int ASYNC_CALL_BACK_VALUES_NUM = 2; -const int PARAM0 = 0; -const int PARAM1 = 1; -const int PARAM2 = 2; -const int PARAM3 = 3; -const int VALUE_BUFFER_SIZE = 256; +constexpr int32_t ASYNC_CALL_BACK_VALUES_NUM = 2; +constexpr int32_t ASYNC_CALL_BACK_PARAM_ERROR = 0; +constexpr int32_t ASYNC_CALL_BACK_PARAM_DATA = 1; -bool ParseBool(const napi_env env, const napi_value value); -int32_t ParseInt32(const napi_env env, const napi_value value); -int64_t ParseInt64(const napi_env env, const napi_value value); -uint32_t ParseUint32(const napi_env env, const napi_value value); -std::string ParseString(const napi_env env, const napi_value value); -bool ParseStringArray(const napi_env env, const napi_value value, std::vector& res); -bool ParseAccessTokenIDArray(const napi_env& env, const napi_value& value, std::vector& res); +bool ParseBool(const napi_env& env, const napi_value& value, bool& result); +bool ParseInt32(const napi_env& env, const napi_value& value, int32_t& result); +bool ParseInt64(const napi_env& env, const napi_value& value, int64_t& result); +bool ParseUint32(const napi_env& env, const napi_value& value, uint32_t& result); +bool ParseString(const napi_env& env, const napi_value& value, std::string& result); +bool ParseStringArray(const napi_env& env, const napi_value& value, std::vector& result); +bool ParseAccessTokenIDArray(const napi_env& env, const napi_value& value, std::vector& result); +bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& result); bool IsArray(const napi_env& env, const napi_value& value); } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif /* INTERFACES_PRIVACY_KITS_NAPI_COMMON_H */ +#endif /* INTERFACES_PRIVACY_KITS_NAPI_COMMON_H */ diff --git a/interfaces/kits/common/src/napi_common.cpp b/interfaces/kits/common/src/napi_common.cpp index 84ba7ccba..860ee6cbe 100644 --- a/interfaces/kits/common/src/napi_common.cpp +++ b/interfaces/kits/common/src/napi_common.cpp @@ -22,7 +22,7 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_PRIVACY, "CommonNapi"}; } // namespace -static bool inline Check(const napi_env env, const napi_value value, const napi_valuetype type) +static bool inline CheckType(const napi_env& env, const napi_value& value, const napi_valuetype& type) { napi_valuetype valuetype = napi_undefined; napi_typeof(env, value, &valuetype); @@ -33,80 +33,75 @@ static bool inline Check(const napi_env env, const napi_value value, const napi_ return true; } -bool ParseBool(const napi_env env, const napi_value value) +bool ParseBool(const napi_env& env, const napi_value& value, bool& result) { - if (!Check(env, value, napi_boolean)) { - return 0; + if (!CheckType(env, value, napi_boolean)) { + return false; } - bool result = 0; + if (napi_get_value_bool(env, value, &result) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value bool"); - return 0; + return false; } - return result; + return true; } -int32_t ParseInt32(const napi_env env, const napi_value value) +bool ParseInt32(const napi_env& env, const napi_value& value, int32_t& result) { - if (!Check(env, value, napi_number)) { - return 0; + if (!CheckType(env, value, napi_number)) { + return false; } - int32_t result = 0; if (napi_get_value_int32(env, value, &result) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value int32"); - return 0; + return false; } - return result; + return true; } -int64_t ParseInt64(const napi_env env, const napi_value value) +bool ParseInt64(const napi_env& env, const napi_value& value, int64_t& result) { - if (!Check(env, value, napi_number)) { - return 0; + if (!CheckType(env, value, napi_number)) { + return false; } - int64_t result = 0; if (napi_get_value_int64(env, value, &result) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value int64"); - return 0; + return false; } - return result; + return true; } -uint32_t ParseUint32(const napi_env env, const napi_value value) +bool ParseUint32(const napi_env& env, const napi_value& value, uint32_t& result) { - if (!Check(env, value, napi_number)) { - return 0; + if (!CheckType(env, value, napi_number)) { + return false; } - uint32_t result = 0; if (napi_get_value_uint32(env, value, &result) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value uint32"); - return 0; + return false; } - return result; + return true; } -std::string ParseString(const napi_env env, const napi_value value) +bool ParseString(const napi_env& env, const napi_value& value, std::string& result) { - if (!Check(env, value, napi_string)) { - return ""; + if (!CheckType(env, value, napi_string)) { + return false; } size_t size; - if (napi_get_value_string_utf8(env, value, nullptr, 0, &size) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get string size"); - return ""; + return false; } - std::string str; - str.reserve(size + 1); - str.resize(size); - if (napi_get_value_string_utf8(env, value, str.data(), size + 1, &size) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get string value"); - return ""; + result.reserve(size + 1); + result.resize(size); + if (napi_get_value_string_utf8(env, value, result.data(), size + 1, &size) != napi_ok) { + ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value string"); + return false; } - return str; + return true; } -bool ParseStringArray(const napi_env env, const napi_value value, std::vector& res) +bool ParseStringArray(const napi_env& env, const napi_value& value, std::vector& result) { if (!IsArray(env, value)) { return false; @@ -124,16 +119,16 @@ bool ParseStringArray(const napi_env env, const napi_value value, std::vector& res) +bool ParseAccessTokenIDArray(const napi_env& env, const napi_value& value, std::vector& result) { uint32_t length = 0; if (!IsArray(env, value)) { @@ -143,7 +138,11 @@ bool ParseAccessTokenIDArray(const napi_env& env, const napi_value& value, std:: napi_value valueArray; for (uint32_t i = 0; i < length; i++) { napi_get_element(env, value, i, &valueArray); - res.emplace_back(ParseUint32(env, valueArray)); + uint32_t res; + if (!ParseUint32(env, valueArray, res)) { + return false; + } + result.emplace_back(res); } return true; }; @@ -157,6 +156,18 @@ bool IsArray(const napi_env& env, const napi_value& value) } return isArray; } + +bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& result) +{ + if (!CheckType(env, value, napi_function)) { + return false; + } + if (napi_create_reference(env, value, 1, &result) != napi_ok) { + ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value callback"); + return false; + } + return true; +} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/interfaces/kits/privacy/napi/src/napi_context_common.cpp b/interfaces/kits/privacy/napi/src/napi_context_common.cpp index d9eae5ebe..3943bffba 100644 --- a/interfaces/kits/privacy/napi/src/napi_context_common.cpp +++ b/interfaces/kits/privacy/napi/src/napi_context_common.cpp @@ -27,12 +27,12 @@ PrivacyAsyncWorkData::PrivacyAsyncWorkData(napi_env envValue) PrivacyAsyncWorkData::~PrivacyAsyncWorkData() { - if (callbackRef) { + if (callbackRef != nullptr) { napi_delete_reference(env, callbackRef); callbackRef = nullptr; } - if (asyncWork) { + if (asyncWork != nullptr) { napi_delete_async_work(env, asyncWork); asyncWork = nullptr; } @@ -107,10 +107,10 @@ void UvQueueWorkActiveStatusChange(uv_work_t* work, int status) std::unique_ptr uvWorkPtr {work}; PermActiveStatusWorker* permActiveStatusData = reinterpret_cast(work->data); std::unique_ptr workPtr {permActiveStatusData}; - napi_value result[ARGS_ONE] = {nullptr}; + napi_value result = nullptr; NAPI_CALL_RETURN_VOID(permActiveStatusData->env, - napi_create_array(permActiveStatusData->env, &result[PARAM0])); - if (!ConvertActiveChangeResponse(permActiveStatusData->env, result[PARAM0], permActiveStatusData->result)) { + napi_create_array(permActiveStatusData->env, &result)); + if (!ConvertActiveChangeResponse(permActiveStatusData->env, result, permActiveStatusData->result)) { ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertActiveChangeResponse failed"); return; } @@ -122,7 +122,7 @@ void UvQueueWorkActiveStatusChange(uv_work_t* work, int status) NAPI_CALL_RETURN_VOID(permActiveStatusData->env, napi_get_reference_value(permActiveStatusData->env, permActiveStatusData->ref, &callback)); NAPI_CALL_RETURN_VOID(permActiveStatusData->env, - napi_call_function(permActiveStatusData->env, undefined, callback, ARGS_ONE, &result[PARAM0], &resultout)); + napi_call_function(permActiveStatusData->env, undefined, callback, 1, &result, &resultout)); } bool ConvertActiveChangeResponse(napi_env env, napi_value value, const ActiveChangeResponse& result) diff --git a/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp b/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp index 88876515b..afc405a54 100644 --- a/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp +++ b/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp @@ -26,55 +26,81 @@ namespace Security { namespace AccessToken { std::mutex g_lockForPermActiveChangeSubscribers; std::vector g_permActiveChangeSubscribers; -static const size_t MAX_CALLBACK_SIZE = 200; +static constexpr size_t MAX_CALLBACK_SIZE = 200; +static constexpr int32_t ADD_PERMISSION_RECORD_MAX_PARAMS = 5; +static constexpr int32_t GET_PERMISSION_RECORD_MAX_PARAMS = 2; +static constexpr int32_t ON_OFF_MAX_PARAMS = 3; +static constexpr int32_t START_STOP_MAX_PARAMS = 3; + namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermissionRecordManagerNapi"}; } // namespace -static void ParseAddPermissionRecord( +static bool ParseAddPermissionRecord( const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) { - size_t argc = ARGS_FIVE; - napi_value argv[ARGS_FIVE] = { 0 }; + size_t argc = ADD_PERMISSION_RECORD_MAX_PARAMS; + napi_value argv[ADD_PERMISSION_RECORD_MAX_PARAMS] = { 0 }; napi_value thisVar = nullptr; void* data = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data), false); asyncContext.env = env; - asyncContext.tokenId = ParseUint32(env, argv[PARAM0]); - asyncContext.permissionName = ParseString(env, argv[PARAM1]); - asyncContext.successCount = ParseInt32(env, argv[PARAM2]); - asyncContext.failCount = ParseInt32(env, argv[PARAM3]); - if (argc == ARGS_FIVE) { - napi_valuetype valueType = napi_undefined; - NAPI_CALL_RETURN_VOID(env, napi_typeof(env, argv[ARGS_FIVE - 1], &valueType)); - if (valueType == napi_function) { - NAPI_CALL_RETURN_VOID(env, - napi_create_reference(env, argv[ARGS_FIVE - 1], 1, &asyncContext.callbackRef)); + + // 0: the first parameter of argv + if (!ParseUint32(env, argv[0], asyncContext.tokenId)) { + return false; + } + + // 1: the second parameter of argv + if (!ParseString(env, argv[1], asyncContext.permissionName)) { + return false; + } + + // 2: the third parameter of argv + if (!ParseInt32(env, argv[2], asyncContext.successCount)) { + return false; + } + + // 3: the fourth parameter of argv + if (!ParseInt32(env, argv[3], asyncContext.failCount)) { + return false; + } + if (argc == ADD_PERMISSION_RECORD_MAX_PARAMS) { + // 4: : the fifth parameter of argv + if (!ParseCallback(env, argv[4], asyncContext.callbackRef)) { + return false; } } + return true; } -static void ParseStartAndStopUsingPermission( +static bool ParseStartAndStopUsingPermission( const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) { - size_t argc = ARGS_THREE; - napi_value argv[ARGS_THREE] = { 0 }; + size_t argc = START_STOP_MAX_PARAMS; + napi_value argv[START_STOP_MAX_PARAMS] = { 0 }; napi_value thisVar = nullptr; void* data = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data), false); asyncContext.env = env; - asyncContext.tokenId = ParseUint32(env, argv[PARAM0]); - asyncContext.permissionName = ParseString(env, argv[PARAM1]); - if (argc == ARGS_THREE) { - napi_valuetype valueType = napi_undefined; - NAPI_CALL_RETURN_VOID(env, napi_typeof(env, argv[ARGS_THREE - 1], &valueType)) ; - if (valueType == napi_function) { - NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, - argv[ARGS_THREE - 1], 1, &asyncContext.callbackRef)); + // 0: the first parameter of argv + if (!ParseUint32(env, argv[0], asyncContext.tokenId)) { + return false; + } + + // 1: the second parameter of argv + if (!ParseString(env, argv[1], asyncContext.permissionName)) { + return false; + } + if (argc == START_STOP_MAX_PARAMS) { + // 2: the third parameter of argv + if (!ParseCallback(env, argv[2], asyncContext.callbackRef)) { + return false; } } + return true; } static void ConvertDetailUsedRecord(napi_env env, napi_value value, const UsedRecordDetail& detailRecord) @@ -199,61 +225,89 @@ static void ProcessRecordResult(napi_env env, napi_value value, const Permission NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "bundleRecords", objBundleRecords)); } -static void ParseGetPermissionUsedRecords( - const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) +static bool ParseRequest(const napi_env& env, const napi_value& value, PermissionUsedRequest& request) { - size_t argc = ARGS_TWO; - napi_value argv[ARGS_TWO] = { 0 }; - napi_value thisVar = nullptr; - void* data = nullptr; - napi_valuetype valuetype = napi_undefined; - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - napi_typeof(env, argv[PARAM0], &valuetype); - if (valuetype != napi_object) { - return; + napi_valuetype valueType; + napi_typeof(env, value, &valueType); + if (valueType != napi_object) { + return false; } napi_value property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "tokenId", &property)); - asyncContext.request.tokenId = ParseUint32(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "tokenId", &property), false); + if (!ParseUint32(env, property, request.tokenId)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "isRemote", &property)) ; - asyncContext.request.isRemote = ParseBool(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "isRemote", &property), false) ; + if (!ParseBool(env, property, request.isRemote)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "deviceId", &property)); - asyncContext.request.deviceId = ParseString(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "deviceId", &property), false); + if (!ParseString(env, property, request.deviceId)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "bundleName", &property)); - asyncContext.request.bundleName = ParseString(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "bundleName", &property), false); + if (!ParseString(env, property, request.bundleName)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "beginTime", &property)); - asyncContext.request.beginTimeMillis = ParseInt64(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "beginTime", &property), false); + if (!ParseInt64(env, property, request.beginTimeMillis)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "endTime", &property)); - asyncContext.request.endTimeMillis = ParseInt64(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "endTime", &property), false); + if (!ParseInt64(env, property, request.endTimeMillis)) { + return false; + } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "permissionNames", &property)); - if (!ParseStringArray(env, property, asyncContext.request.permissionList)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseStringArray failed"); - return; + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "permissionNames", &property), false); + if (!ParseStringArray(env, property, request.permissionList)) { + return false; } property = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv[PARAM0], "flag", &property)); - asyncContext.request.flag = (PermissionUsageFlagEnum)ParseInt32(env, property); + NAPI_CALL_BASE(env, napi_get_named_property(env, value, "flag", &property), false); + int32_t flag; + if (!ParseInt32(env, property, flag)) { + return false; + } + request.flag = static_cast(flag); + + return true; +} + +static bool ParseGetPermissionUsedRecords( + const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) +{ + size_t argc = GET_PERMISSION_RECORD_MAX_PARAMS; + napi_value argv[GET_PERMISSION_RECORD_MAX_PARAMS] = { 0 }; + napi_value thisVar = nullptr; + void* data = nullptr; + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data), false); asyncContext.env = env; - if (argc == ARGS_TWO) { - NAPI_CALL_RETURN_VOID(env, napi_typeof(env, argv[ARGS_TWO - 1], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, argv[ARGS_TWO - 1], 1, &asyncContext.callbackRef)); + + // 0: the first parameter of argv + if (!ParseRequest(env, argv[0], asyncContext.request)) { + return false; + } + + if (argc == GET_PERMISSION_RECORD_MAX_PARAMS) { + // 1: the second parameter of argv + if (!ParseCallback(env, argv[1], asyncContext.callbackRef)) { + return false; } } + return true; } static void AddPermissionUsedRecordExecute(napi_env env, void* data) @@ -279,11 +333,10 @@ static void AddPermissionUsedRecordComplete(napi_env env, napi_status status, vo std::unique_ptr callbackPtr {asyncContext}; napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; - NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, - asyncContext->retCode, &results[PARAM1])); - if (asyncContext->deferred) { + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_PARAM_DATA])); + if (asyncContext->deferred != nullptr) { NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, - results[PARAM1])); + results[ASYNC_CALL_BACK_PARAM_DATA])); } else { napi_value callback = nullptr; napi_value callResult = nullptr; @@ -307,7 +360,9 @@ napi_value AddPermissionUsedRecord(napi_env env, napi_callback_info cbinfo) } std::unique_ptr callbackPtr {asyncContext}; - ParseAddPermissionRecord(env, cbinfo, *asyncContext); + if (!ParseAddPermissionRecord(env, cbinfo, *asyncContext)) { + return nullptr; + } napi_value result = nullptr; if (asyncContext->callbackRef == nullptr) { @@ -354,11 +409,10 @@ static void StartUsingPermissionComplete(napi_env env, napi_status status, void* std::unique_ptr callbackPtr {asyncContext}; napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; - NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, - &results[PARAM1])); - if (asyncContext->deferred) { + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_PARAM_DATA])); + if (asyncContext->deferred != nullptr) { NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, - results[PARAM1])); + results[ASYNC_CALL_BACK_PARAM_DATA])); } else { napi_value callback = nullptr; napi_value callResult = nullptr; @@ -381,7 +435,9 @@ napi_value StartUsingPermission(napi_env env, napi_callback_info cbinfo) } std::unique_ptr callbackPtr {asyncContext}; - ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext); + if (!ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext)) { + return nullptr; + } napi_value result = nullptr; if (asyncContext->callbackRef == nullptr) { @@ -428,11 +484,10 @@ static void StopUsingPermissionComplete(napi_env env, napi_status status, void* std::unique_ptr callbackPtr {asyncContext}; napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; - NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, - &results[PARAM1])); - if (asyncContext->deferred) { + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_PARAM_DATA])); + if (asyncContext->deferred != nullptr) { NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, - results[PARAM1])); + results[ASYNC_CALL_BACK_PARAM_DATA])); } else { napi_value callback = nullptr; napi_value callResult = nullptr; @@ -456,7 +511,9 @@ napi_value StopUsingPermission(napi_env env, napi_callback_info cbinfo) } std::unique_ptr callbackPtr {asyncContext}; - ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext); + if (!ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext)) { + return nullptr; + } napi_value result = nullptr; if (asyncContext->callbackRef == nullptr) { @@ -502,12 +559,11 @@ static void GetPermissionUsedRecordsComplete(napi_env env, napi_status status, v std::unique_ptr callbackPtr {asyncContext}; napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; - NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->retCode, &results[0])); - NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &results[PARAM1])); - ProcessRecordResult(env, results[PARAM1], asyncContext->result); - if (asyncContext->deferred) { + NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &results[ASYNC_CALL_BACK_PARAM_DATA])); + ProcessRecordResult(env, results[ASYNC_CALL_BACK_PARAM_DATA], asyncContext->result); + if (asyncContext->deferred != nullptr) { NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, - results[PARAM1])); + results[ASYNC_CALL_BACK_PARAM_DATA])); } else { napi_value callback = nullptr; napi_value callResult = nullptr; @@ -530,7 +586,9 @@ napi_value GetPermissionUsedRecords(napi_env env, napi_callback_info cbinfo) } std::unique_ptr callbackPtr {asyncContext}; - ParseGetPermissionUsedRecords(env, cbinfo, *asyncContext); + if (!ParseGetPermissionUsedRecords(env, cbinfo, *asyncContext)) { + return nullptr; + } napi_value result = nullptr; if (asyncContext->callbackRef == nullptr) { @@ -557,32 +615,27 @@ napi_value GetPermissionUsedRecords(napi_env env, napi_callback_info cbinfo) static bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, RegisterPermActiveChangeContext& registerPermActiveChangeContext) { - size_t argc = ARGS_THREE; - napi_value argv[ARGS_THREE] = {nullptr}; + size_t argc = ON_OFF_MAX_PARAMS; + napi_value argv[ON_OFF_MAX_PARAMS] = {nullptr}; napi_value thisVar = nullptr; napi_ref callback = nullptr; if (napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "napi_get_cb_info failed"); return false; } - std::string type = ParseString(env, argv[PARAM0]); - std::vector permList; - if (!ParseStringArray(env, argv[PARAM1], permList)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseStringArray failed"); + std::string type; + // 0: the first parameter of argv + if (!ParseString(env, argv[0], type)) { return false; } - std::sort(permList.begin(), permList.end()); - napi_valuetype valueType = napi_undefined; - if (napi_typeof(env, argv[PARAM2], &valueType) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "get napi type failed"); - return false; - } // get PRARM[2] callback type - if (valueType != napi_function) { - ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + std::vector permList; + // 1: the second parameter of argv + if (!ParseStringArray(env, argv[1], permList)) { return false; } - if (napi_create_reference(env, argv[PARAM2], 1, &callback) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_create_reference failed"); + std::sort(permList.begin(), permList.end()); + // 2: the third parameter of argv + if (!ParseCallback(env, argv[2], callback)) { return false; } registerPermActiveChangeContext.env = env; @@ -597,33 +650,28 @@ static bool ParseInputToRegister(const napi_env env, const napi_callback_info cb static bool ParseInputToUnregister(const napi_env env, const napi_callback_info cbInfo, UnregisterPermActiveChangeContext& unregisterPermActiveChangeContext) { - size_t argc = ARGS_THREE; - napi_value argv[ARGS_THREE] = {nullptr}; + size_t argc = ON_OFF_MAX_PARAMS; + napi_value argv[ON_OFF_MAX_PARAMS] = {nullptr}; napi_value thisVar = nullptr; napi_ref callback = nullptr; if (napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, NULL) != napi_ok) { ACCESSTOKEN_LOG_ERROR(LABEL, "napi_get_cb_info failed"); return false; } - std::string type = ParseString(env, argv[PARAM0]); + std::string type; + // 0: the first parameter of argv + if (!ParseString(env, argv[0], type)) { + return false; + } + // 1: the second parameter of argv std::vector permList; - if (!ParseStringArray(env, argv[PARAM1], permList)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "ParseStringArray failed"); + if (!ParseStringArray(env, argv[1], permList)) { return false; } std::sort(permList.begin(), permList.end()); - napi_valuetype valueType = napi_undefined; - if (argc >= ARGS_THREE) { - if (napi_typeof(env, argv[PARAM2], &valueType) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "get napi type failed"); - return false; - } // get PRARM[2] callback type - if (valueType != napi_function) { - ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); - return false; - } - if (napi_create_reference(env, argv[PARAM2], 1, &callback) != napi_ok) { - ACCESSTOKEN_LOG_ERROR(LABEL, "napi_create_reference failed"); + if (argc == ON_OFF_MAX_PARAMS) { + // 2: the first parameter of argv + if (!ParseCallback(env, argv[2], callback)) { return false; } } diff --git a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp index b53b061ab..82bbe8b47 100644 --- a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp +++ b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp @@ -93,7 +93,9 @@ bool SensitiveResourceManager::GetAppStatus(const std::string& pkgName, int32_t& std::vector foreGroundAppList; appMgrProxy->GetForegroundApplications(foreGroundAppList); + ACCESSTOKEN_LOG_ERROR(LABEL, "chennian %{public}s ", foreGroundAppList.size()); for (const auto& foreGroundApp : foreGroundAppList) { + ACCESSTOKEN_LOG_ERROR(LABEL, "chennian %{public}s ", foreGroundApp.bundleName.c_str()); if (foreGroundApp.bundleName == pkgName) { status = PERM_ACTIVE_IN_FOREGROUND; return true; -- Gitee