From f874e68fd8fb9ef9ecca84abf662632ce5f57083 Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Wed, 5 Mar 2025 09:14:14 +0800 Subject: [PATCH] add code Signed-off-by: lanhaoyu --- access_token.gni | 6 + bundle.json | 6 +- frameworks/js/arkts/BUILD.gn | 21 ++++ .../arkts/privacy/aniprivacyManager/BUILD.gn | 57 ++++++++++ .../arkts/privacy/permissions.ets | 36 ++++++ .../arkts/privacy/privacyManager_ani.ets | 71 ++++++++++++ .../aniprivacyManager/arktsconfig.json | 21 ++++ .../native/ani_privacyManager.cpp | 103 ++++++++++++++++++ 8 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 frameworks/js/arkts/BUILD.gn create mode 100644 frameworks/js/arkts/privacy/aniprivacyManager/BUILD.gn create mode 100644 frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/permissions.ets create mode 100644 frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/privacyManager_ani.ets create mode 100644 frameworks/js/arkts/privacy/aniprivacyManager/arktsconfig.json create mode 100644 frameworks/js/arkts/privacy/aniprivacyManager/native/ani_privacyManager.cpp diff --git a/access_token.gni b/access_token.gni index 3c8f3ea55..dee74ca93 100644 --- a/access_token.gni +++ b/access_token.gni @@ -34,6 +34,12 @@ if (vendor_config_exist) { access_token_impl_include_dirs = [] } +if (product_name != "qemu-arm-linux-min") { + qemu_disable = true +} else { + qemu_disable = false +} + if (!defined(global_parts_info) || defined(global_parts_info.distributedhardware_device_manager)) { token_sync_enable = true diff --git a/bundle.json b/bundle.json index 207d968ef..3a739cbd8 100644 --- a/bundle.json +++ b/bundle.json @@ -57,7 +57,8 @@ "selinux_adapter", "sqlite", "window_manager", - "zlib" + "zlib", + "runtime_core" ], "third_party": [] }, @@ -83,6 +84,9 @@ "//base/security/access_token/services/common:accesstoken_common" ] }, + "sub_component": [ + "//base/security/access_token/frameworks/js/arkts:arkts_package" + ], "inner_kits": [ { "name": "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", diff --git a/frameworks/js/arkts/BUILD.gn b/frameworks/js/arkts/BUILD.gn new file mode 100644 index 000000000..d5cb9ece6 --- /dev/null +++ b/frameworks/js/arkts/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2025 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("//base/security/access_token/access_token.gni") + +group("arkts_package") { + deps = [] + if (qemu_disable) { + deps += [ "privacy/aniprivacyManager:ani_privacyManager" ] + } +} diff --git a/frameworks/js/arkts/privacy/aniprivacyManager/BUILD.gn b/frameworks/js/arkts/privacy/aniprivacyManager/BUILD.gn new file mode 100644 index 000000000..6e83f987e --- /dev/null +++ b/frameworks/js/arkts/privacy/aniprivacyManager/BUILD.gn @@ -0,0 +1,57 @@ +# Copyright (c) 2025 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("//base/security/access_token/access_token.gni") +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") + +ohos_shared_library("aniprivacyManager") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "${access_token_path}/interfaces/innerkits/accesstoken/include", + "${access_token_path}/interfaces/innerkits/privacy/include", + "${access_token_path}/ani/include", + ] + sources = [ "native/ani_privacyManager.cpp" ] + + deps = [ "${access_token_path}/interfaces/innerkits/privacy:libprivacy_sdk" ] + + external_deps = [ "runtime_core:ani" ] + + part_name = "access_token" + subsystem_name = "security" +} + +if (qemu_disable) { + generate_static_abc("privacyManager_abc") { + arktsconfig = "arktsconfig.json" + dst_file = "$target_out_dir/privacyManager_ani.abc" + out_puts = [ "$target_out_dir/privacyManager_ani.abc" ] + } +} + +group("ani_privacyManager") { + deps = [] + if (qemu_disable) { + deps += [ + ":aniprivacyManager", + ":privacyManager_abc", + ] + } +} diff --git a/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/permissions.ets b/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/permissions.ets new file mode 100644 index 000000000..c06103b96 --- /dev/null +++ b/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/permissions.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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. + */ + +/** + * @file Defines all permissions. + * @kit AbilityKit + */ + +/** + * Indicates permissions. + * + * @typedef { string } + * @syscap SystemCapability.Security.AccessToken + * @since 9 + */ + /** + * Indicates permissions. + * + * @typedef { string } + * @syscap SystemCapability.Security.AccessToken + * @atomicservice + * @since 11 + */ +export type Permissions = string; \ No newline at end of file diff --git a/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/privacyManager_ani.ets b/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/privacyManager_ani.ets new file mode 100644 index 000000000..eb8e3f4ee --- /dev/null +++ b/frameworks/js/arkts/privacy/aniprivacyManager/arkts/privacy/privacyManager_ani.ets @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 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 { Permissions } from './permissions'; + + + +export class BusinessError { + code: number; + data: T; + } + + type AsyncCallback = (err: BusinessError, data: T) => void; + + class Result{ + result:T; + error:E; + constructor(result: T , error: E) { + this.result = result; + this.error = error; + } + } + export namespace privacyManager { + export enum PermissionUsedType { + + NORMAL_TYPE = 0, + + PICKER_TYPE = 1, + + SECURITY_COMPONENT_TYPE = 2 + } + + export class AddPermissionUsedRecordOptions { + usedType: PermissionUsedType; + } + + export class PrivacyManagerInner { + + static { loadLibrary("aniprivacyManager.z") } + native addPermissionUsedRecordSync(tokenID: int, permissionName: Permissions, successCount: int, failCount: int, options: AddPermissionUsedRecordOptions): int; + + } + + export function addPermissionUsedRecord(tokenID: int, permissionName: string, successCount: int, failCount: int, options: AddPermissionUsedRecordOptions): Promise { + let p = new Promise((resolve: (v: int) => void, reject: (error: Object) => void) => { + let p1 = taskpool.execute((): int => { return new PrivacyManagerInner().addPermissionUsedRecordSync(tokenID, permissionName, successCount, failCount, options) }); + p1.then((e: NullishType) => { + console.log("P1 then e = " + e + "."); + let r: Int = e as Int; + resolve(r.intValue()); + }, (err: Object): void => { + console.log("Test Promise. The promise shouldn`t be rejected."); + }); + } + ); + return p; + } +} + diff --git a/frameworks/js/arkts/privacy/aniprivacyManager/arktsconfig.json b/frameworks/js/arkts/privacy/aniprivacyManager/arktsconfig.json new file mode 100644 index 000000000..ecc4ddf00 --- /dev/null +++ b/frameworks/js/arkts/privacy/aniprivacyManager/arktsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "package": "", + "baseUrl": ".", + "outDir": "./out", + "paths": { + "std": [ + "../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/std" + ], + "escompat": [ + "../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/escompat" + ], + "@kit.privacyManager_ani": [ + "./arkts/privacy/privacyManager_ani.ets" + ] + } + }, + "include": [ + "./arkts/privacy/*.ets" + ] +} \ No newline at end of file diff --git a/frameworks/js/arkts/privacy/aniprivacyManager/native/ani_privacyManager.cpp b/frameworks/js/arkts/privacy/aniprivacyManager/native/ani_privacyManager.cpp new file mode 100644 index 000000000..5da128c18 --- /dev/null +++ b/frameworks/js/arkts/privacy/aniprivacyManager/native/ani_privacyManager.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 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 + +#include "ani.h" +#include "privacy_kit.h" + +constexpr int64_t ERRCODE_2 = 2; +constexpr int64_t ERRCODE_3 = 3; +constexpr int64_t ERRCODE_9 = 9; + +static int AddPermissionUsedRecordSync( + ani_int tokenID, std::string permissionName, ani_int successCount, ani_int failCount, ani_int ntype) +{ + OHOS::Security::AccessToken::AddPermParamInfo info; + info.tokenId = tokenID; + info.permissionName = permissionName; + info.successCount = successCount; + info.failCount = failCount; + info.type = (OHOS::Security::AccessToken::PermissionUsedType)ntype; + std::cout << "call addPermissionUsedRecord:" << info.type << std::endl; + auto retCode = OHOS::Security::AccessToken::PrivacyKit::AddPermissionUsedRecord(info); + std::cout << "call addPermissionUsedRecord " << retCode << std::endl; + return retCode; +} + +static ani_int AddPermissionUsedRecord([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, + ani_int tokenID, ani_string permissionName, ani_int successCount, ani_int failCount, ani_object options) +{ + ani_size strSize; + env->String_GetUTF8Size(permissionName, &strSize); + + std::vector buffer(strSize + 1); + char *utf8_buffer = buffer.data(); + + ani_size bytes_written = 0; + env->String_GetUTF8(permissionName, utf8_buffer, strSize + 1, &bytes_written); + + utf8_buffer[bytes_written] = '\0'; + std::string content = std::string(utf8_buffer); + std::cout << "CallMeWithOptionalString Get " << content << std::endl; + + ani_boolean isUndefined; + env->Reference_IsUndefined(options, &isUndefined); + ani_int type = 0; + + ani_int usedTypeRef; + if (ANI_OK != env->Object_GetFieldByName_Int(options, "usedType", &usedTypeRef)) { + std::cerr << "Object_GetFieldByName_Ref Faild" << std::endl; + } + + std::cerr << "Object_GetFieldByName_Ref usedTypeRef:" << usedTypeRef << std::endl; + type = usedTypeRef; + + std::cout << "CallMeWithOptionalDouble get double " << type << std::endl; + + return AddPermissionUsedRecordSync(tokenID, content, successCount, failCount, type); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + std::cout << "ANI_Constructor called" << std::endl; + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + std::cerr << "Unsupported ANI_VERSION_1" << std::endl; + return (ani_status)ERRCODE_9; + } + + static const char *className = "LprivacyManager_ani/privacyManager/PrivacyManagerInner;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + std::cerr << "Not found '" << className << "'" << std::endl; + return (ani_status)ERRCODE_2; + } + + std::cout << "array methods called" << std::endl; + std::array methods = { + ani_native_function{ "addPermissionUsedRecordSync", + "ILstd/core/String;IILprivacyManager_ani/privacyManager/AddPermissionUsedRecordOptions;:I", + reinterpret_cast(AddPermissionUsedRecord) }, + }; + + if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { + std::cerr << "Cannot bind native methods to '" << className << "'" << std::endl; + return (ani_status)ERRCODE_3; + }; + + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file -- Gitee