diff --git a/interfaces/innerkits/privacy/test/unittest/src/to_string.cpp b/interfaces/innerkits/privacy/test/unittest/src/to_string.cpp deleted file mode 100644 index c9f30da23af29ff61145d7e37a8ae97b4ea5e04f..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/privacy/test/unittest/src/to_string.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 "to_string.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -void ToString::PermissionUsedRecordToString(const PermissionUsedRecord& permissionRecord, std::string& infos) -{ - infos.append(R"( "permissionRecords": [)"); - infos.append("\n"); - - infos.append(R"( "permissionName": ")" + permissionRecord.permissionName + R"(")" + ",\n"); - infos.append(R"( "accessCount": ")" + std::to_string(permissionRecord.accessCount) + R"(")" + ",\n"); - infos.append(R"( "rejectCount": )" + std::to_string(permissionRecord.rejectCount) + ",\n"); - infos.append(R"( "lastAccessTime": )" + std::to_string(permissionRecord.lastAccessTime) + ",\n"); - infos.append(R"( "lastRejectTime": )" + std::to_string(permissionRecord.lastRejectTime) + ",\n"); - infos.append(R"( "lastAccessDuration": )" + std::to_string(permissionRecord.lastAccessDuration) + ",\n"); - infos.append(" ],\n"); -} - -void ToString::BundleUsedRecordToString(const BundleUsedRecord& bundleRecord, std::string& infos) -{ - infos.append(R"({)"); - infos.append("\n"); - infos.append(R"( "bundleName": )" + bundleRecord.bundleName + ",\n"); - infos.append(R"( "deviceId": )" + bundleRecord.deviceId + ",\n"); - - for (auto perm : bundleRecord.permissionRecords) { - ToString::PermissionUsedRecordToString(perm, infos); - } - - infos.append("}"); -} - -void ToString::PermissionUsedResultToString(const PermissionUsedResult& result, std::string& infos) -{ - infos.append(R"("beginTimeMillis": )" + std::to_string(result.beginTimeMillis) + ",\n"); - infos.append(R"("endTimeMillis": )" + std::to_string(result.endTimeMillis) + ",\n"); - - for (auto res : result.bundleRecords) { - ToString::BundleUsedRecordToString(res, infos); - } - infos.append("\n"); -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/BUILD.gn b/interfaces/kits/BUILD.gn index 39aa55613fbf4910372b0c60076471c61c2e8310..2afd3b89be0a5f5c1ac5e548e9a54fc16a5d783f 100644 --- a/interfaces/kits/BUILD.gn +++ b/interfaces/kits/BUILD.gn @@ -16,6 +16,9 @@ import("//build/ohos.gni") group("napi_packages") { deps = [] if (support_jsapi) { - deps += [ "//base/security/access_token/interfaces/kits/accesstoken:libabilityaccessctrl" ] + deps += [ + "//base/security/access_token/interfaces/kits/accesstoken:libabilityaccessctrl", + "//base/security/access_token/interfaces/kits/privacy:libprivacymanager", + ] } } diff --git a/interfaces/kits/privacy/BUILD.gn b/interfaces/kits/privacy/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..30d1c8322eedf71709c74e47820c9e3a195a3ea0 --- /dev/null +++ b/interfaces/kits/privacy/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2021-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. + +import("//build/ohos.gni") + +ohos_shared_library("libprivacymanager") { + include_dirs = [ + "napi/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", + "//foundation/arkui/napi/interfaces/inner_api", + "//foundation/arkui/napi/interfaces/kits", + "//utils/native/base/include", + ] + + sources = [ + "napi/src/native_module.cpp", + "napi/src/permission_record_manager_napi.cpp", + ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/privacy:libprivacy_sdk", + "//foundation/arkui/napi:ace_napi", + "//utils/native/base:utils", + ] + cflags_cc = [ "-DHILOG_ENABLE" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + relative_install_dir = "module" + subsystem_name = "security" + part_name = "access_token" +} diff --git a/interfaces/innerkits/privacy/test/unittest/src/to_string.h b/interfaces/kits/privacy/napi/include/native_module.h similarity index 54% rename from interfaces/innerkits/privacy/test/unittest/src/to_string.h rename to interfaces/kits/privacy/napi/include/native_module.h index 3d96c712d7eec579476e970d435e90a2cd52f342..63aac29f7b7c5093f3471ab786c69f335132227d 100644 --- a/interfaces/innerkits/privacy/test/unittest/src/to_string.h +++ b/interfaces/kits/privacy/napi/include/native_module.h @@ -12,24 +12,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef INTERFACES_PRIVACY_KITS_NATIVE_MODULE_H_ +#define INTERFACES_PRIVACY_KITS_NATIVE_MODULE_H_ -#ifndef TO_STRING_H -#define TO_STRING_H +#include +#include +#include +#include -#include -#include "permission_used_request.h" -#include "permission_used_result.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" namespace OHOS { namespace Security { namespace AccessToken { -class ToString { -public: - static void PermissionUsedRecordToString(const PermissionUsedRecord& permissionRecord, std::string& infos); - static void BundleUsedRecordToString(const BundleUsedRecord& bundleRecord, std::string& infos); - static void PermissionUsedResultToString(const PermissionUsedResult& result, std::string& infos); -}; -} // namespace AccessToken -} // namespace Security -} // namespace OHOS -#endif // TO_STRING_H +/* + * function for module exports + */ +static napi_value Init(napi_env env, napi_value exports); +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif /* INTERFACES_PRIVACY_KITS_NATIVE_MODULE_H_ */ diff --git a/interfaces/kits/privacy/napi/include/permission_record_manager_napi.h b/interfaces/kits/privacy/napi/include/permission_record_manager_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..716a599aac3631b3c7346db969894d9403f280ec --- /dev/null +++ b/interfaces/kits/privacy/napi/include/permission_record_manager_napi.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021-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_KITS_PERMISSION_USED_MANAGER_NAPI_H_ +#define INTERFACES_KITS_PERMISSION_USED_MANAGER_NAPI_H_ + +#include + +#include "access_token.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#include "permission_used_request.h" +#include "permission_used_result.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +struct RecordManagerAsyncContext { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callbackRef = nullptr; + + AccessTokenID tokenId = 0; + std::string permissionName; + int32_t successCount; + int32_t failCount; + PermissionUsedRequest request; + PermissionUsedResult result; + + int32_t retCode; +}; + +napi_value AddPermissionUsedRecord(napi_env env, napi_callback_info cbinfo); +napi_value StartUsingPermission(napi_env env, napi_callback_info cbinfo); +napi_value StopUsingPermission(napi_env env, napi_callback_info cbinfo); +napi_value GetPermissionUsedRecords(napi_env env, napi_callback_info cbinfo); +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif /* INTERFACES_KITS_PERMISSION_USED_MANAGER_NAPI_H_ */ diff --git a/interfaces/kits/privacy/napi/src/native_module.cpp b/interfaces/kits/privacy/napi/src/native_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b893f9169fec1b4503355bb141b7a9ad25586b0 --- /dev/null +++ b/interfaces/kits/privacy/napi/src/native_module.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021-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 "native_module.h" +#include "permission_record_manager_napi.h" +#include "permission_used_request.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +EXTERN_C_START +/* + * function for module exports + */ +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor descriptor[] = { + DECLARE_NAPI_FUNCTION("addPermissionUsedRecord", AddPermissionUsedRecord), + DECLARE_NAPI_FUNCTION("startUsingPermission", StartUsingPermission), + DECLARE_NAPI_FUNCTION("stopUsingPermission", StopUsingPermission), + DECLARE_NAPI_FUNCTION("getPermissionUsedRecords", GetPermissionUsedRecords) + }; + + napi_define_properties(env, exports, sizeof(descriptor) / sizeof(descriptor[0]), descriptor); + + napi_value permissionUsageFlag = nullptr; + napi_create_object(env, &permissionUsageFlag); + + napi_value prop = nullptr; + napi_create_int32(env, FLAG_PERMISSION_USAGE_SUMMARY, &prop); + napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_SUMMARY", prop); + + prop = nullptr; + napi_create_int32(env, FLAG_PERMISSION_USAGE_DETAIL, &prop); + napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_DETAIL", prop); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("PermissionUsageFlag ", permissionUsageFlag), + }; + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(exportFuncs[0]), exportFuncs); + + return exports; +} +EXTERN_C_END + +/* + * Module define + */ +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "privacyManager", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +/* + * Module register function + */ +extern "C" __attribute__((constructor)) void RegisterPrivacyModule(void) +{ + napi_module_register(&_module); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp b/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cfa0089f8e5ed085477094de81e6461fa4839a1 --- /dev/null +++ b/interfaces/kits/privacy/napi/src/permission_record_manager_napi.cpp @@ -0,0 +1,610 @@ +/* + * Copyright (c) 2021-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 "permission_record_manager_napi.h" + +#include "privacy_kit.h" +#include "accesstoken_log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermissionRecordManagerNapi"}; +} // namespace + +namespace { +const int ARGS_TWO = 2; +const int ARGS_THREE = 3; +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; +}; + +static bool ParseBool(const napi_env env, const napi_value value) +{ + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, value, &valuetype); + if (valuetype != napi_boolean) { + ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + return 0; + } + bool result = 0; + if (napi_get_value_bool(env, value, &result) != napi_ok) { + ACCESSTOKEN_LOG_ERROR(LABEL, "cannot get value bool"); + return 0; + } + return result; +} + +static int32_t ParseInt32(const napi_env env, const napi_value value) +{ + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, value, &valuetype); + if (valuetype != napi_number) { + ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + return 0; + } + 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 result; +} + +static int64_t ParseInt64(const napi_env env, const napi_value value) +{ + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, value, &valuetype); + if (valuetype != napi_number) { + ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + return 0; + } + 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 result; +} + +static uint32_t ParseUint32(const napi_env env, const napi_value value) +{ + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, value, &valuetype); + if (valuetype != napi_number) { + ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + return 0; + } + 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 result; +} + +static std::string ParseString(const napi_env env, const napi_value value) +{ + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, value, &valuetype); + if (valuetype != napi_string) { + ACCESSTOKEN_LOG_ERROR(LABEL, "value type dismatch"); + return ""; + } + std::string str; + 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 ""; + } + + 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 ""; + } + return str; +} + +static std::vector ParseStringArray(const napi_env env, const napi_value value) +{ + std::vector res; + uint32_t length = 0; + napi_valuetype valuetype = napi_undefined; + + napi_get_array_length(env, value, &length); + ACCESSTOKEN_LOG_DEBUG(LABEL, "ParseStringArray array length=%{public}d", length); + + napi_value valueArray; + for (uint32_t i = 0; i < length; i++) { + napi_get_element(env, value, i, &valueArray); + + napi_typeof(env, valueArray, &valuetype); + if (valuetype == napi_string) { + res.emplace_back(ParseString(env, valueArray)); + } + } + return res; +} + + +static void ParseAddPermissionRecord( + const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "ParseAddPermissionRecord begin."); + size_t argc = ARGS_FIVE; + napi_value argv[ARGS_FIVE] = { 0 }; + napi_value thisVar = nullptr; + void* data = nullptr; + + NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + 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_typeof(env, argv[ARGS_FIVE - 1], &valueType); + if (valueType == napi_function) { + napi_create_reference(env, argv[ARGS_FIVE - 1], 1, &asyncContext.callbackRef); // get probably callback + } + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID = %{public}d, permissionName = %{public}s", asyncContext.tokenId, + asyncContext.permissionName.c_str()); +} + +static void ParseStartAndStopUsingPermission( + const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) +{ + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = { 0 }; + napi_value thisVar = nullptr; + void* data = nullptr; + + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + + 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_typeof(env, argv[ARGS_THREE - 1], &valueType); + if (valueType == napi_function) { + napi_create_reference(env, argv[ARGS_THREE - 1], 1, &asyncContext.callbackRef); // get probably callback + } + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenID = %{public}d, permissionName = %{public}s", asyncContext.tokenId, + asyncContext.permissionName.c_str()); +} + +static void ConvertDetailUsedRecord(napi_env env, napi_value value, const UsedRecordDetail& detailRecord) +{ + napi_value nStatus; + napi_create_int32(env, detailRecord.status, &nStatus); + napi_set_named_property(env, value, "status", nStatus); + ACCESSTOKEN_LOG_INFO(LABEL, "status=%{public}d", detailRecord.status); + + napi_value nTimestamp; + napi_create_int64(env, detailRecord.timestamp, &nTimestamp); + napi_set_named_property(env, value, "timestamp", nTimestamp); + + napi_value nAccessDuration; + napi_create_int64(env, detailRecord.accessDuration, &nAccessDuration); + napi_set_named_property(env, value, "accessDuration", nAccessDuration); +} + +static void ConvertPermissionUsedRecord(napi_env env, napi_value value, const PermissionUsedRecord& permissionRecord) +{ + napi_value nPermissionName; + napi_create_string_utf8(env, permissionRecord.permissionName.c_str(), NAPI_AUTO_LENGTH, &nPermissionName); + napi_set_named_property(env, value, "permissionName", nPermissionName); + ACCESSTOKEN_LOG_INFO(LABEL, "permissionName=%{public}s", permissionRecord.permissionName.c_str()); + + napi_value nAccessCount; + napi_create_int32(env, permissionRecord.accessCount, &nAccessCount); + napi_set_named_property(env, value, "accessCount", nAccessCount); + ACCESSTOKEN_LOG_INFO(LABEL, "accessCount=%{public}d", permissionRecord.accessCount); + + napi_value nRejectCount; + napi_create_int32(env, permissionRecord.rejectCount, &nRejectCount); + napi_set_named_property(env, value, "rejectCount", nRejectCount); + ACCESSTOKEN_LOG_INFO(LABEL, "rejectCount=%{public}d", permissionRecord.rejectCount); + + napi_value nLastAccessTime; + napi_create_int64(env, permissionRecord.lastAccessTime, &nLastAccessTime); + napi_set_named_property(env, value, "lastAccessTime", nLastAccessTime); + + napi_value nLastRejectTime; + napi_create_int64(env, permissionRecord.lastRejectTime, &nLastRejectTime); + napi_set_named_property(env, value, "lastRejectTime", nLastRejectTime); + + napi_value nLastAccessDuration; + napi_create_int64(env, permissionRecord.lastAccessDuration, &nLastAccessDuration); + napi_set_named_property(env, value, "lastAccessDuration", nLastAccessDuration); + + size_t index = 0; + napi_value objAccessRecords; + napi_create_array(env, &objAccessRecords); + for (const auto& accRecord : permissionRecord.accessRecords) { + napi_value objAccessRecord; + napi_create_object(env, &objAccessRecord); + ConvertDetailUsedRecord(env, objAccessRecord, accRecord); + napi_set_element(env, objAccessRecords, index, objAccessRecord); + index++; + } + napi_set_named_property(env, value, "accessRecords", objAccessRecords); + + index = 0; + napi_value objRejectRecords; + napi_create_array(env, &objRejectRecords); + for (const auto& rejRecord : permissionRecord.rejectRecords) { + napi_value objRejectRecord; + napi_create_object(env, &objRejectRecord); + ConvertDetailUsedRecord(env, objRejectRecord, rejRecord); + napi_set_element(env, objRejectRecords, index, objRejectRecord); + index++; + } + napi_set_named_property(env, value, "rejectRecords", objRejectRecords); +} + +static void ConvertBundleUsedRecord(napi_env env, napi_value value, const BundleUsedRecord& bundleRecord) +{ + napi_value nTokenId; + napi_create_int32(env, bundleRecord.tokenId, &nTokenId); + napi_set_named_property(env, value, "tokenId", nTokenId); + ACCESSTOKEN_LOG_INFO(LABEL, "tokenId=%{public}d", bundleRecord.tokenId); + + napi_value nIsRemote; + napi_create_int32(env, bundleRecord.isRemote, &nIsRemote); + napi_set_named_property(env, value, "isRemote", nIsRemote); + ACCESSTOKEN_LOG_INFO(LABEL, "isRemote=%{public}d", bundleRecord.isRemote); + + napi_value nDeviceId; + napi_create_string_utf8(env, bundleRecord.deviceId.c_str(), NAPI_AUTO_LENGTH, &nDeviceId); + napi_set_named_property(env, value, "deviceId", nDeviceId); + ACCESSTOKEN_LOG_INFO(LABEL, "deviceId=%{public}s", bundleRecord.deviceId.c_str()); + + napi_value nBundleName; + napi_create_string_utf8(env, bundleRecord.bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName); + napi_set_named_property(env, value, "bundleName", nBundleName); + ACCESSTOKEN_LOG_INFO(LABEL, "bundleName=%{public}s", bundleRecord.bundleName.c_str()); + + size_t index = 0; + napi_value objPermissionRecords; + napi_create_array(env, &objPermissionRecords); + for (const auto& permRecord : bundleRecord.permissionRecords) { + napi_value objPermissionRecord; + napi_create_object(env, &objPermissionRecord); + ConvertPermissionUsedRecord(env, objPermissionRecord, permRecord); + napi_set_element(env, objPermissionRecords, index, objPermissionRecord); + index++; + } + napi_set_named_property(env, value, "permissionRecords", objPermissionRecords); +} + +static void ProcessRecordResult(napi_env env, napi_value value, const PermissionUsedResult& result) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "ProcessRecordResult."); + + napi_value nBeginTimestamp; + napi_create_int64(env, result.beginTimeMillis, &nBeginTimestamp); + napi_set_named_property(env, value, "beginTime", nBeginTimestamp); + + napi_value nEndTimestamp; + napi_create_int64(env, result.endTimeMillis, &nEndTimestamp); + napi_set_named_property(env, value, "endTime", nEndTimestamp); + + size_t index = 0; + napi_value objBundleRecords; + napi_create_array(env, &objBundleRecords); + for (const auto& bundleRecord : result.bundleRecords) { + napi_value objBundleRecord; + napi_create_object(env, &objBundleRecord); + ConvertBundleUsedRecord(env, objBundleRecord, bundleRecord); + napi_set_element(env, objBundleRecords, index, objBundleRecord); + index++; + } + napi_set_named_property(env, value, "bundleRecords", objBundleRecords); +} + +static void ParseGetPermissionUsedRecords( + const napi_env env, const napi_callback_info info, RecordManagerAsyncContext& asyncContext) +{ + 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[0], &valuetype); + if (valuetype != napi_object) { + return; + } + napi_value property = nullptr; + if (napi_ok != napi_get_named_property(env, argv[0], "tokenId", &property)) { + return; + } + asyncContext.request.tokenId = ParseUint32(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "isRemote", &property)) { + return; + } + asyncContext.request.isRemote = ParseBool(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "deviceId", &property)) { + return; + } + asyncContext.request.deviceId = ParseString(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "bundleName", &property)) { + return; + } + asyncContext.request.bundleName = ParseString(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "beginTime", &property)) { + return; + } + asyncContext.request.beginTimeMillis = ParseInt64(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "endTime", &property)) { + return; + } + asyncContext.request.endTimeMillis = ParseInt64(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "permissionNames", &property)) { + return; + } + asyncContext.request.permissionList = ParseStringArray(env, property); + if (napi_ok != napi_get_named_property(env, argv[0], "flag", &property)) { + return; + } + asyncContext.request.flag = (PermissionUsageFlagEnum)ParseInt32(env, property); + + asyncContext.env = env; + if (argc == ARGS_TWO) { + napi_typeof(env, argv[ARGS_TWO - 1], &valuetype); + if (valuetype == napi_function) { + napi_create_reference(env, argv[ARGS_TWO - 1], 1, &asyncContext.callbackRef); + } + } +} + +napi_value AddPermissionUsedRecord(napi_env env, napi_callback_info cbinfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "AddPermissionUsedRecord begin."); + + auto *asyncContext = new RecordManagerAsyncContext(); + if (asyncContext == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); + return nullptr; + } + ParseAddPermissionRecord(env, cbinfo, *asyncContext); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &(asyncContext->deferred), &result); + } else { + napi_get_undefined(env, &result); + } + + napi_value resource = nullptr; + napi_create_string_utf8(env, "AddPermissionUsedRecord", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + asyncContext->retCode = PrivacyKit::AddPermissionUsedRecord(asyncContext->tokenId, + asyncContext->permissionName, asyncContext->successCount, asyncContext->failCount); + }, + [](napi_env env, napi_status status, void *data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; + napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + if (asyncContext->deferred) { + napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_value undefine = nullptr; + napi_get_undefined(env, &undefine); + napi_create_int32(env, 0, &callResult); + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, undefine, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->asyncWork); + delete asyncContext; + }, + reinterpret_cast(asyncContext), + &(asyncContext->asyncWork)); + napi_queue_async_work(env, asyncContext->asyncWork); + ACCESSTOKEN_LOG_DEBUG(LABEL, "AddPermissionUsedRecord end."); + return result; +} + +napi_value StartUsingPermission(napi_env env, napi_callback_info cbinfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "StartUsingPermission begin."); + + auto *asyncContext = new RecordManagerAsyncContext(); + if (asyncContext == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); + return nullptr; + } + ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &(asyncContext->deferred), &result); + } else { + napi_get_undefined(env, &result); + } + + napi_value resource = nullptr; + napi_create_string_utf8(env, "StartUsingPermission", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + asyncContext->retCode = PrivacyKit::StartUsingPermission(asyncContext->tokenId, + asyncContext->permissionName); + }, + [](napi_env env, napi_status status, void *data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; + napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + if (asyncContext->deferred) { + napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_value undefine = nullptr; + napi_get_undefined(env, &undefine); + napi_create_int32(env, 0, &callResult); + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, undefine, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->asyncWork); + delete asyncContext; + }, + reinterpret_cast(asyncContext), + &(asyncContext->asyncWork)); + napi_queue_async_work(env, asyncContext->asyncWork); + ACCESSTOKEN_LOG_DEBUG(LABEL, "StartUsingPermission end."); + return result; +} + +napi_value StopUsingPermission(napi_env env, napi_callback_info cbinfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "StopUsingPermission begin."); + + auto *asyncContext = new RecordManagerAsyncContext(); + if (asyncContext == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); + return nullptr; + } + ParseStartAndStopUsingPermission(env, cbinfo, *asyncContext); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &(asyncContext->deferred), &result); + } else { + napi_get_undefined(env, &result); + } + + napi_value resource = nullptr; + napi_create_string_utf8(env, "StopUsingPermission", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + asyncContext->retCode = PrivacyKit::StopUsingPermission(asyncContext->tokenId, + asyncContext->permissionName); + }, + [](napi_env env, napi_status status, void *data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; + napi_create_int32(env, asyncContext->retCode, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + if (asyncContext->deferred) { + napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_value undefine = nullptr; + napi_get_undefined(env, &undefine); + napi_create_int32(env, 0, &callResult); + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, undefine, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->asyncWork); + delete asyncContext; + }, + reinterpret_cast(asyncContext), + &(asyncContext->asyncWork)); + napi_queue_async_work(env, asyncContext->asyncWork); + ACCESSTOKEN_LOG_DEBUG(LABEL, "StopUsingPermission end."); + return result; +} + +napi_value GetPermissionUsedRecords(napi_env env, napi_callback_info cbinfo) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "GetPermissionUsedRecords begin."); + + auto *asyncContext = new RecordManagerAsyncContext(); + if (asyncContext == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new struct fail."); + return nullptr; + } + ParseGetPermissionUsedRecords(env, cbinfo, *asyncContext); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &(asyncContext->deferred), &result); + } else { + napi_get_undefined(env, &result); + } + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetPermissionUsedRecords", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + asyncContext->retCode = PrivacyKit::GetPermissionUsedRecords(asyncContext->request, asyncContext->result); + }, + [](napi_env env, napi_status status, void *data) { + RecordManagerAsyncContext* asyncContext = reinterpret_cast(data); + napi_value results[ASYNC_CALL_BACK_VALUES_NUM] = {nullptr}; + napi_create_int32(env, asyncContext->retCode, &results[0]); + napi_create_array(env, &results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + ProcessRecordResult(env, results[ASYNC_CALL_BACK_VALUES_NUM - 1], asyncContext->result); + if (asyncContext->deferred) { + napi_resolve_deferred(env, asyncContext->deferred, results[ASYNC_CALL_BACK_VALUES_NUM - 1]); + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_value undefine = nullptr; + napi_get_undefined(env, &undefine); + napi_create_int32(env, 0, &callResult); + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, undefine, callback, ASYNC_CALL_BACK_VALUES_NUM, results, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->asyncWork); + delete asyncContext; + }, + reinterpret_cast(asyncContext), + &(asyncContext->asyncWork)); + napi_queue_async_work(env, asyncContext->asyncWork); + ACCESSTOKEN_LOG_DEBUG(LABEL, "StopUsingPermission end."); + return result; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/include/database/data_translator.h b/services/privacymanager/include/database/data_translator.h index e1775cd625c58416b174928bc3480fd4fe4434d1..1dbdfa3c5fe0f4ffa8edb9975b2cabafd294b6f0 100644 --- a/services/privacymanager/include/database/data_translator.h +++ b/services/privacymanager/include/database/data_translator.h @@ -28,7 +28,7 @@ namespace AccessToken { class DataTranslator final { public: static int32_t TranslationIntoGenericValues(const PermissionUsedRequest& request, - GenericValues& visitorGenericValues, GenericValues& andGenericValues, GenericValues& orGenericValues); + GenericValues& visitorGenericValues, GenericValues& andGenericValues, GenericValues& orGenericValues); static int32_t TranslationGenericValuesIntoPermissionUsedRecord( const GenericValues& inGenericValues, PermissionUsedRecord& permissionRecord); }; diff --git a/tools/accesstoken/BUILD.gn b/tools/accesstoken/BUILD.gn index 269b6616c85c5fdfb3b2cfa3e4b1741e3c6056e2..e24569afd7b8beea9b35b148b0411c08a1e2073d 100644 --- a/tools/accesstoken/BUILD.gn +++ b/tools/accesstoken/BUILD.gn @@ -16,10 +16,11 @@ import("//foundation/ability/ability_runtime/aafwk.gni") ohos_source_set("tools_atm_source_set") { include_dirs = [ - "//base/security/access_token/tools/accesstoken/include", + "include", "//base/security/access_token/frameworks/common/include", "${aafwk_path}/tools/aa/include", "//base/security/access_token/interfaces/innerkits/accesstoken/main/cpp/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", "${services_path}/common/include", ] @@ -33,6 +34,7 @@ ohos_source_set("tools_atm_source_set") { deps = [ "${aafwk_path}/tools/aa:aa", "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", + "//base/security/access_token/interfaces/innerkits/privacy:libprivacy_sdk", "//utils/native/base:utils", ] diff --git a/tools/accesstoken/include/atm_command.h b/tools/accesstoken/include/atm_command.h index d8617b51a69c1f9ebdc9e999d6d65fa8719a1410..7ff2ec3192af7ef326ab49666e5c8237a0f5d92f 100644 --- a/tools/accesstoken/include/atm_command.h +++ b/tools/accesstoken/include/atm_command.h @@ -22,10 +22,17 @@ namespace OHOS { namespace Security { namespace AccessToken { const std::string TOOLS_NAME = "atm"; -const std::string HELP_MSG = "usage: atm \n" +const std::string HELP_MSG = "usage: atm