deferred link napi支持

Signed-off-by: woohoa <wanghuan36@huawei.com>
This commit is contained in:
15950533375 2024-10-29 17:22:39 +08:00
parent 81a4da440a
commit ba1b080516
24 changed files with 507 additions and 93 deletions

View File

@ -26,7 +26,8 @@ group("app_domain_verify_packages") {
"interfaces/inner_api/client:app_domain_verify_agent_client",
"interfaces/inner_api/client:app_domain_verify_mgr_client",
"interfaces/inner_api/common:app_domain_verify_common",
"interfaces/kits/js:appdomainverify_napi",
"interfaces/kits/js/app_domain_verify:appdomainverify_napi",
"interfaces/kits/js/deferred_link:deferredlink_napi",
"profile:bundlemanager_app_domain_verify_sa_profiles",
"services:app_domain_verify_agent_service",
"services:app_domain_verify_mgr_service",

View File

@ -22,6 +22,9 @@ app_domain_verify_root_path = "//foundation/bundlemanager/app_domain_verify"
app_domain_verify_client_path =
"${app_domain_verify_root_path}/interfaces/inner_api/client"
app_domain_verify_kits_path =
"${app_domain_verify_root_path}/interfaces/kits/js"
app_domain_verify_service_path = "${app_domain_verify_root_path}/services"
app_domain_verify_common_path =

View File

@ -320,13 +320,6 @@ int AppDomainVerifyMgrClient::QueryAssociatedBundleNames(
int AppDomainVerifyMgrClient::GetDeferredLink(std::string& link)
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_CLIENT, "called");
std::string bundleName;
if(!BundleInfoQuery::GetBundleNameSelf(bundleName)){
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_CLIENT, "can not get bundleName");
return CommonErrorCode::E_INTERNAL_ERR;
}
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_CLIENT, "bundleInfo %{public}s", bundleName.c_str());
std::lock_guard<std::mutex> autoLock(proxyLock_);
if (IsServiceAvailable()) {
return appDomainVerifyMgrServiceProxy_->GetDeferredLink(link);

View File

@ -15,7 +15,7 @@
#ifndef APP_DOMAIN_VERIFY_COMM_DEFINE_H
#define APP_DOMAIN_VERIFY_COMM_DEFINE_H
namespace OHOS::AppDomainVerify {
enum CommonErrorCode {
enum CommonErrorCode: uint32_t {
E_OK = 0,
E_PERMISSION_DENIED = 201,
E_IS_NOT_SYS_APP = 202,

View File

@ -17,6 +17,8 @@ config("app_domain_verify_mgr_napi_config") {
visibility = [ ":*" ]
include_dirs = [
"include",
"${app_domain_verify_kits_path}/event/include",
"${app_domain_verify_kits_path}/napi_utils/include",
"${app_domain_verify_client_path}/include",
"${app_domain_verify_common_path}/include",
"${app_domain_verify_common_path}/include/zidl",
@ -41,9 +43,10 @@ config("app_domain_verify_mgr_napi_config") {
ohos_shared_library("appdomainverify_napi") {
branch_protector_ret = "pac_ret"
sources = [
"src/api_event_reporter.cpp",
"${app_domain_verify_kits_path}/event/src/api_event_reporter.cpp",
"${app_domain_verify_kits_path}/event/src/config_parser.cpp",
"${app_domain_verify_kits_path}/napi_utils/src/napi_value_utils.cpp",
"src/app_domain_verify_manager_napi.cpp",
"src/config_parser.cpp",
"src/native_module.cpp",
]
public_configs = [ ":app_domain_verify_mgr_napi_config" ]

View File

@ -20,55 +20,14 @@
#include "app_domain_verify_mgr_client.h"
#include "comm_define.h"
#include "api_event_reporter.h"
#include "napi_value_utils.h"
namespace OHOS::AppDomainVerify {
constexpr int32_t API_SUCCESS = 0;
constexpr int32_t API_FAIL = 1;
constexpr int32_t MAX_STR_INPUT_SIZE = 256;
constexpr int32_t STRING_BUF_MAX_SIZE = 4096;
using namespace Dfx;
std::map<CommonErrorCode, const char*> ErrCodeMap = { { CommonErrorCode::E_PERMISSION_DENIED, "Permission denied." },
{ CommonErrorCode::E_IS_NOT_SYS_APP, "System API accessed by non-system app." },
{ CommonErrorCode::E_PARAM_ERROR, "Parameter error." }, { CommonErrorCode::E_INTERNAL_ERR, "Internal error." } };
static std::string GetString(napi_env env, napi_value value)
{
std::unique_ptr<char[]> valueBuf = std::make_unique<char[]>(STRING_BUF_MAX_SIZE);
size_t size = 0;
NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, valueBuf.get(), STRING_BUF_MAX_SIZE, &size), "");
std::string result = std::string(valueBuf.get(), size);
return result;
}
static napi_value BuildString(const napi_env& env, const std::string& data)
{
napi_value result;
NAPI_CALL_BASE(env, napi_create_string_utf8(env, data.c_str(), NAPI_AUTO_LENGTH, &result), nullptr);
return result;
}
static napi_value BuildStringArray(const napi_env& env, const std::vector<std::string>& data)
{
napi_value arr;
NAPI_CALL_BASE(env, napi_create_array(env, &arr), nullptr);
size_t index = 0;
for (auto&& str : data) {
napi_value value = BuildString(env, str);
NAPI_CALL_BASE(env, napi_set_element(env, arr, index++, value), nullptr);
}
return arr;
}
static bool CheckInput(const std::string& input)
{
if (input.empty() || input.size() > MAX_STR_INPUT_SIZE) {
return false;
}
return true;
}
static napi_value BuildError(const napi_env& env, CommonErrorCode errorCode)
{
auto ret = napi_throw_error(env, std::to_string(errorCode).c_str(), ErrCodeMap[errorCode]);
if (ret != napi_status::napi_ok) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "throw err failed.");
}
return nullptr;
}
napi_value QueryAssociatedDomains(napi_env env, napi_callback_info info)
{
size_t argc = 1;
@ -79,14 +38,14 @@ napi_value QueryAssociatedDomains(napi_env env, napi_callback_info info)
std::string bundleName = GetString(env, args[0]);
if (!CheckInput(bundleName)) {
reporter.WriteEndEvent(API_FAIL, CommonErrorCode::E_PARAM_ERROR);
return BuildError(env, CommonErrorCode::E_PARAM_ERROR);
return BuildError(env, CommonErrorCode::E_PARAM_ERROR, ErrCodeMap[CommonErrorCode::E_PARAM_ERROR]);
}
std::vector<std::string> domains;
auto ret = AppDomainVerifyMgrClient::GetInstance()->QueryAssociatedDomains(bundleName, domains);
if (ret != 0) {
if (ErrCodeMap.count(static_cast<CommonErrorCode>(ret)) != 0) {
reporter.WriteEndEvent(API_FAIL, static_cast<CommonErrorCode>(ret));
return BuildError(env, static_cast<CommonErrorCode>(ret));
return BuildError(env, static_cast<CommonErrorCode>(ret), ErrCodeMap[static_cast<CommonErrorCode>(ret)]);
} else {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "unknown error:%{public}d.", ret);
reporter.WriteEndEvent(API_FAIL, ret);
@ -106,14 +65,14 @@ napi_value QueryAssociatedBundleNames(napi_env env, napi_callback_info info)
std::string domain = GetString(env, args[0]);
if (!CheckInput(domain)) {
reporter.WriteEndEvent(API_FAIL, CommonErrorCode::E_PARAM_ERROR);
return BuildError(env, CommonErrorCode::E_PARAM_ERROR);
return BuildError(env, CommonErrorCode::E_PARAM_ERROR, ErrCodeMap[CommonErrorCode::E_PARAM_ERROR]);
}
std::vector<std::string> bundleNames;
auto ret = AppDomainVerifyMgrClient::GetInstance()->QueryAssociatedBundleNames(domain, bundleNames);
if (ret != 0) {
if (ErrCodeMap.count(static_cast<CommonErrorCode>(ret)) != 0) {
reporter.WriteEndEvent(API_FAIL, static_cast<CommonErrorCode>(ret));
return BuildError(env, static_cast<CommonErrorCode>(ret));
return BuildError(env, static_cast<CommonErrorCode>(ret), ErrCodeMap[static_cast<CommonErrorCode>(ret)]);
} else {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "unknown error:%{public}d.", ret);
reporter.WriteEndEvent(API_FAIL, ret);

View File

@ -0,0 +1,77 @@
# Copyright (C) 2024 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")
import("//foundation/bundlemanager/app_domain_verify/app_domain_verify.gni")
config("app_domain_verify_deferred_link_config") {
visibility = [ ":*" ]
include_dirs = [
"include",
"${app_domain_verify_kits_path}/event/include",
"${app_domain_verify_kits_path}/napi_utils/include",
"${app_domain_verify_client_path}/include",
"${app_domain_verify_common_path}/include",
"${app_domain_verify_common_path}/include/zidl",
]
configs = [ "//build/config/compiler:exceptions" ]
ldflags = [ "-Wl,--exclude-libs=ALL" ]
cflags = [
"-fvisibility=hidden",
"-fdata-sections",
"-ffunction-sections",
"-fstack-protector-strong",
"-D_FORTIFY_SOURCE=2",
"-Os",
]
cflags_cc = [
"-fvisibility-inlines-hidden",
"-Os",
]
}
ohos_shared_library("deferredlink_napi") {
branch_protector_ret = "pac_ret"
sources = [
"${app_domain_verify_kits_path}/event/src/api_event_reporter.cpp",
"${app_domain_verify_kits_path}/event/src/config_parser.cpp",
"${app_domain_verify_kits_path}/napi_utils/src/napi_async_utils.cpp",
"${app_domain_verify_kits_path}/napi_utils/src/napi_value_utils.cpp",
"src/deferred_link_napi.cpp",
"src/native_module.cpp",
]
public_configs = [ ":app_domain_verify_deferred_link_config" ]
deps = [
"${app_domain_verify_client_path}:app_domain_verify_mgr_client",
"${app_domain_verify_common_path}:app_domain_verify_common",
]
external_deps = [
"ability_base:want",
"bundle_framework:appexecfwk_base",
"c_utils:utils",
"hiappevent:hiappevent_innerapi",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
defines = []
if (build_variant == "user") {
defines += [ "IS_RELEASE_VERSION" ]
}
relative_install_dir = "module/bundle"
subsystem_name = "bundlemanager"
part_name = "app_domain_verify"
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 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 APP_DOMAIN_VERIFY_DEFERRED_LINK_NAPI_H
#define APP_DOMAIN_VERIFY_DEFERRED_LINK_NAPI_H
#include "napi/native_api.h"
#include "napi/native_node_api.h"
namespace OHOS::AppDomainVerify {
napi_value GetDeferredLink(napi_env env, napi_callback_info info);
}
#endif // APP_DOMAIN_VERIFY_DEFERRED_LINK_NAPI_H

View File

@ -0,0 +1,115 @@
/*
* Copyright (C) 2024 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 <memory>
#include <map>
#include "deferred_link_napi.h"
#include "app_domain_verify_hilog.h"
#include "app_domain_verify_mgr_client.h"
#include "comm_define.h"
#include "api_event_reporter.h"
#include "napi_value_utils.h"
#include "napi_async_utils.h"
namespace OHOS::AppDomainVerify {
using namespace Dfx;
namespace {
constexpr const char* GET_DEFERRED_LINK = "GetDeferredLink";
}
std::map<CommonErrorCode, const char*> ErrCodeMap = { { CommonErrorCode::E_INTERNAL_ERR, "Internal error." } };
struct GetDeferredLinkCallbackInfo : public BaseCallbackInfo {
GetDeferredLinkCallbackInfo(napi_env napiEnv, std::unique_ptr<Dfx::ApiEventReporter> reporter)
: BaseCallbackInfo(napiEnv), apiReporter(std::move(reporter))
{
}
std::unique_ptr<Dfx::ApiEventReporter> apiReporter;
std::string deferred_link;
};
void GetDeferredLinkExec(napi_env env, void* data)
{
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called");
auto* asyncCallbackInfo = reinterpret_cast<GetDeferredLinkCallbackInfo*>(data);
if (asyncCallbackInfo == nullptr) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "asyncCallbackInfo is null");
return;
}
std::string deferredLink;
asyncCallbackInfo->err = AppDomainVerifyMgrClient::GetInstance()->GetDeferredLink(deferredLink);
asyncCallbackInfo->deferred_link = deferredLink;
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called end");
}
void GetDeferredLinkComplete(napi_env env, napi_status status, void* data)
{
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called");
auto* asyncCallbackInfo = reinterpret_cast<GetDeferredLinkCallbackInfo*>(data);
if (asyncCallbackInfo == nullptr) {
APP_DOMAIN_VERIFY_HILOGE(
APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "asyncCallbackInfo is null in %{public}s", __func__);
return;
}
std::unique_ptr<GetDeferredLinkCallbackInfo> callbackPtr{ asyncCallbackInfo };
napi_value result[ARGS_SIZE_TWO] = { nullptr };
if (asyncCallbackInfo->err == SUCCESS) {
NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0]));
result[ARGS_SIZE_ONE] = BuildString(env, asyncCallbackInfo->deferred_link);
} else {
result[0] = BuildError(env, CommonErrorCode::E_INTERNAL_ERR, "internal error.");
}
if (asyncCallbackInfo->deferred) {
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "is deferred");
if (asyncCallbackInfo->err == SUCCESS) {
NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[ARGS_SIZE_ONE]));
} else {
NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]));
}
} else {
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "is callback");
napi_value callback = nullptr;
napi_value placeHolder = nullptr;
NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback));
NAPI_CALL_RETURN_VOID(
env, napi_call_function(env, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &placeHolder));
}
callbackPtr->apiReporter->WriteEndEvent(API_SUCCESS, asyncCallbackInfo->err);
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called end");
}
napi_value GetDeferredLink(napi_env env, napi_callback_info info)
{
std::unique_ptr<Dfx::ApiEventReporter> reporter = std::make_unique<Dfx::ApiEventReporter>("GetDeferredLink");
auto* asyncCallbackInfo = new (std::nothrow) GetDeferredLinkCallbackInfo(env, std::move(reporter));
if (asyncCallbackInfo == nullptr) {
return nullptr;
}
std::unique_ptr<GetDeferredLinkCallbackInfo> callbackPtr{ asyncCallbackInfo };
size_t argc = 1;
napi_value args[1] = { nullptr };
NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), nullptr);
napi_valuetype valueType = napi_undefined;
napi_typeof(env, args[0], &valueType);
if (valueType == napi_function) {
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "is callback");
NAPI_CALL(env, napi_create_reference(env, args[0], 1, &asyncCallbackInfo->callback));
}
auto promise = AsyncCallNativeMethod<GetDeferredLinkCallbackInfo>(
env, asyncCallbackInfo, GET_DEFERRED_LINK, GetDeferredLinkExec, GetDeferredLinkComplete);
callbackPtr.release();
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "GetDeferredLink end");
return promise;
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 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/native_api.h"
#include "napi/native_node_api.h"
#include "deferred_link_napi.h"
namespace OHOS {
namespace AppDomainVerify {
static napi_value AppDomainVerifyExport(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "getDeferredLink", nullptr, GetDeferredLink, nullptr, nullptr, nullptr, napi_default, nullptr },
};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
return exports;
}
static napi_module bundle_manager_module = { .nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = AppDomainVerifyExport,
.nm_modname = "bundle.deferredLink",
.nm_priv = ((void*)0),
.reserved = { 0 } };
extern "C" __attribute__((constructor)) void BundleManagerRegister(void)
{
napi_module_register(&bundle_manager_module);
}
}
}

View File

@ -17,6 +17,8 @@
#include <string>
namespace OHOS::AppDomainVerify::Dfx {
constexpr int32_t API_SUCCESS = 0;
constexpr int32_t API_FAIL = 1;
class ApiEventReporter {
public:
explicit ApiEventReporter(const std::string& apiName);

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2024 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 APP_DOMAIN_VERIFY_NAPI_ASYNC_UTILS_H
#define APP_DOMAIN_VERIFY_NAPI_ASYNC_UTILS_H
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "app_domain_verify_hilog.h"
namespace OHOS::AppDomainVerify {
constexpr int SUCCESS = 0;
constexpr int ARGS_SIZE_ZERO = 0;
constexpr int ARGS_SIZE_ONE = 1;
constexpr int ARGS_SIZE_TWO = 2;
struct AsyncWorkData {
explicit AsyncWorkData(napi_env napiEnv) : env(napiEnv){};
virtual ~AsyncWorkData();
napi_env env;
napi_async_work asyncWork = nullptr;
napi_deferred deferred = nullptr;
napi_ref callback = nullptr;
};
struct BaseCallbackInfo : public AsyncWorkData {
explicit BaseCallbackInfo(napi_env napiEnv) : AsyncWorkData(napiEnv)
{
}
int32_t err = 0;
std::string message;
};
template <typename T>
static napi_value AsyncCallNativeMethod(napi_env env, T* asyncCallbackInfo, const std::string& methodName,
void (*execFunc)(napi_env, void*), void (*completeFunc)(napi_env, napi_status, void*))
{
if (asyncCallbackInfo == nullptr) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "asyncCallbackInfo is null");
return nullptr;
}
napi_value promise = nullptr;
if (asyncCallbackInfo->callback == nullptr) {
NAPI_CALL(env, napi_create_promise(env, &asyncCallbackInfo->deferred, &promise));
} else {
NAPI_CALL(env, napi_get_undefined(env, &promise));
}
napi_value resource = nullptr;
NAPI_CALL(env, napi_create_string_utf8(env, methodName.c_str(), NAPI_AUTO_LENGTH, &resource));
NAPI_CALL(env,
napi_create_async_work(env, nullptr, resource, execFunc, completeFunc,
reinterpret_cast<void*>(asyncCallbackInfo), &asyncCallbackInfo->asyncWork));
NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
return promise;
}
}
#endif // APP_DOMAIN_VERIFY_NAPI_ASYNC_UTILS_H

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2024 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 APP_DOMAIN_VERIFY_NAPI_VALUE_UTIL_H
#define APP_DOMAIN_VERIFY_NAPI_VALUE_UTIL_H
#include <memory>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "app_domain_verify_hilog.h"
#include "comm_define.h"
namespace OHOS::AppDomainVerify {
std::string GetString(napi_env env, napi_value value);
napi_value BuildString(const napi_env& env, const std::string& data);
napi_value BuildStringArray(const napi_env& env, const std::vector<std::string>& data);
bool CheckInput(const std::string& input);
napi_value BuildError(const napi_env& env, uint32_t errorCode, const char* errorMsg);
}
#endif // APP_DOMAIN_VERIFY_NAPI_VALUE_UTIL_H

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 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_async_utils.h"
namespace OHOS::AppDomainVerify {
AsyncWorkData::~AsyncWorkData()
{
if (callback) {
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "AsyncWorkData::~AsyncWorkData delete callback");
napi_delete_reference(env, callback);
callback = nullptr;
}
if (asyncWork) {
APP_DOMAIN_VERIFY_HILOGD(
APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "AsyncWorkData::~AsyncWorkData delete asyncWork");
napi_delete_async_work(env, asyncWork);
asyncWork = nullptr;
}
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 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_value_utils.h"
namespace OHOS::AppDomainVerify {
constexpr int32_t MAX_STR_INPUT_SIZE = 256;
constexpr int32_t STRING_BUF_MAX_SIZE = 4096;
std::string GetString(napi_env env, napi_value value)
{
std::unique_ptr<char[]> valueBuf = std::make_unique<char[]>(STRING_BUF_MAX_SIZE);
size_t size = 0;
NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, valueBuf.get(), STRING_BUF_MAX_SIZE, &size), "");
std::string result = std::string(valueBuf.get(), size);
return result;
}
napi_value BuildString(const napi_env& env, const std::string& data)
{
napi_value result;
NAPI_CALL_BASE(env, napi_create_string_utf8(env, data.c_str(), NAPI_AUTO_LENGTH, &result), nullptr);
return result;
}
napi_value BuildStringArray(const napi_env& env, const std::vector<std::string>& data)
{
napi_value arr;
NAPI_CALL_BASE(env, napi_create_array(env, &arr), nullptr);
size_t index = 0;
for (auto&& str : data) {
napi_value value = BuildString(env, str);
NAPI_CALL_BASE(env, napi_set_element(env, arr, index++, value), nullptr);
}
return arr;
}
bool CheckInput(const std::string& input)
{
if (input.empty() || input.size() > MAX_STR_INPUT_SIZE) {
return false;
}
return true;
}
napi_value BuildError(const napi_env& env, uint32_t errorCode, const char* errorMsg)
{
auto ret = napi_throw_error(env, std::to_string(errorCode).c_str(), errorMsg);
if (ret != napi_status::napi_ok) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "throw err failed.");
}
return nullptr;
}
}

View File

@ -44,14 +44,14 @@ public:
/**
* PutDeferredLink
* @descrition put deferred link info
* @param domain the domain of url.
* @param url the url to open.
* @param info the link.
*/
void PutDeferredLink(const DeferredLinkInfo& info);
/**
* GetDeferredLink
* @descrition get deferred link within domains and bundleName
* @param bundleName the bundleName to filter link info.
* @param domains the domains to filter link info.
* @return url the deferred url to open.
*/
@ -70,7 +70,7 @@ private:
* cache list
* @descrition list contains deferred link info, newly in front, older in back.
*/
std::list<DeferredLinkInfo> caches;
std::list<DeferredLinkInfo> caches_;
std::mutex cachesMutex_;
std::shared_ptr<AppExecFwk::EventHandler> ageHandler_;
std::shared_ptr<AppExecFwk::EventRunner> ageRunner_;

View File

@ -345,7 +345,7 @@ int AppDomainVerifyMgrService::GetDeferredLink(std::string& link)
return CommonErrorCode::E_OK;
} else {
APP_DOMAIN_VERIFY_HILOGW(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not get associate domains");
return CommonErrorCode::E_INTERNAL_ERR;
return CommonErrorCode::E_OK;
}
} else {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not get bundleName.");

View File

@ -29,7 +29,7 @@ void DeferredLinkMgr::PutDeferredLink(const DeferredLinkInfo& info)
CheckRemoveExistedUnlocked(info);
CheckFullUnlocked(info);
caches.push_front(info);
caches_.push_front(info);
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "info domain:%{private}s, url:%{private}s.",
info.domain.c_str(), info.url.c_str());
}
@ -42,7 +42,7 @@ std::string DeferredLinkMgr::GetDeferredLink(const std::string& bundleName, cons
std::unique_lock<std::mutex> lock(cachesMutex_);
std::list<DeferredLinkInfo> destination;
// find links in bundle's domain and can match bundle's ability, then remove all of them.
caches.remove_if([this, &bundleName, &domainSet, &destination](const DeferredLinkInfo& linkInfo) {
caches_.remove_if([this, &bundleName, &domainSet, &destination](const DeferredLinkInfo& linkInfo) {
if (domainSet.count(linkInfo.domain) != 0 && CanMatchAbility(bundleName, linkInfo.url)) {
// keep newly in front
destination.push_back(linkInfo);
@ -73,14 +73,14 @@ void DeferredLinkMgr::AgeCacheProcess()
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "age func in.");
std::unique_lock<std::mutex> lock(cachesMutex_);
int64_t now = GetSecondsSince1970ToNow();
caches.remove_if([now](const DeferredLinkInfo& linkInfo) {
caches_.remove_if([now](const DeferredLinkInfo& linkInfo) {
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE,
"url:%{public}s, now:%{public}lld, timestamp%{public}lld.", linkInfo.url.c_str(), now, linkInfo.timeStamp);
return now - linkInfo.timeStamp >= MAX_CACHE_TIME;
});
if (!caches.empty()) {
if (!caches_.empty()) {
APP_DOMAIN_VERIFY_HILOGD(
APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "post continue age task, remain size:%{public}zu.", caches.size());
APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "post continue age task, remain size:%{public}zu.", caches_.size());
PostAgeCacheTask();
} else {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "age task end.");
@ -88,19 +88,19 @@ void DeferredLinkMgr::AgeCacheProcess()
}
void DeferredLinkMgr::CheckStartTimerUnlocked()
{
if (caches.empty()) {
if (caches_.empty()) {
PostAgeCacheTask();
}
}
void DeferredLinkMgr::CheckFullUnlocked(const DeferredLinkInfo& info)
{
if (caches.size() == MAX_CACHE_SIZE) {
caches.pop_back();
if (caches_.size() == MAX_CACHE_SIZE) {
caches_.pop_back();
}
}
void DeferredLinkMgr::CheckRemoveExistedUnlocked(const DeferredLinkInfo& info)
{
caches.remove_if([&info](const DeferredLinkInfo& curInfo) {
caches_.remove_if([&info](const DeferredLinkInfo& curInfo) {
if (curInfo.url == info.url && curInfo.domain == info.domain) {
return true;
}

View File

@ -30,8 +30,6 @@ constexpr const char* BUNDLE_DOMAIN_WRONG = "https://www.openharmony_wrong.cn";
constexpr const char* BUNDLE_URL = "https://www.openharmony.cn/100";
constexpr const char* BUNDLE_URL_NEW = "https://www.openharmony.cn/new";
constexpr const char* TASK_ID = "age";
constexpr int32_t DELAY_TIME = 120000; // 2min
constexpr int64_t MAX_CACHE_TIME = 1200000; // 10min
constexpr int MAX_CACHE_SIZE = 50;
class DeferredLinkMgrTest : public testing::Test {
public:
@ -70,9 +68,9 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkPutTest001, TestSize.Level0)
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches.front().domain == BUNDLE_DOMAIN);
EXPECT_TRUE(deferredLinkMgr.caches.front().url == BUNDLE_URL);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.front().domain == BUNDLE_DOMAIN);
EXPECT_TRUE(deferredLinkMgr.caches_.front().url == BUNDLE_URL);
}
/**
@ -91,9 +89,9 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkPutTest002, TestSize.Level0)
.timeStamp = GetSecondsSince1970ToNow() });
}
EXPECT_TRUE(deferredLinkMgr.caches.size() == MAX_CACHE_SIZE);
EXPECT_EQ(deferredLinkMgr.caches.back().domain, BUNDLE_DOMAIN + std::to_string(1));
EXPECT_EQ(deferredLinkMgr.caches.front().domain, BUNDLE_DOMAIN + std::to_string(MAX_CACHE_SIZE));
EXPECT_TRUE(deferredLinkMgr.caches_.size() == MAX_CACHE_SIZE);
EXPECT_EQ(deferredLinkMgr.caches_.back().domain, BUNDLE_DOMAIN + std::to_string(1));
EXPECT_EQ(deferredLinkMgr.caches_.front().domain, BUNDLE_DOMAIN + std::to_string(MAX_CACHE_SIZE));
}
/**
@ -113,9 +111,9 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkPutTest003, TestSize.Level0)
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 2);
EXPECT_EQ(deferredLinkMgr.caches.back().domain, BUNDLE_DOMAIN + std::to_string(1));
EXPECT_EQ(deferredLinkMgr.caches.front().domain, BUNDLE_DOMAIN);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 2);
EXPECT_EQ(deferredLinkMgr.caches_.back().domain, BUNDLE_DOMAIN + std::to_string(1));
EXPECT_EQ(deferredLinkMgr.caches_.front().domain, BUNDLE_DOMAIN);
}
/**
@ -132,7 +130,7 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest001, TestSize.Level0)
deferredLinkMgr.abilityFilter_ = filter;
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
std::vector<std::string> domains;
domains.emplace_back(BUNDLE_DOMAIN);
@ -140,7 +138,10 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest001, TestSize.Level0)
EXPECT_FALSE(link.empty());
EXPECT_EQ(link, BUNDLE_URL);
EXPECT_TRUE(deferredLinkMgr.caches.empty());
EXPECT_TRUE(deferredLinkMgr.caches_.empty());
auto link1 = deferredLinkMgr.GetDeferredLink(BUNDLE_NAME, domains);
EXPECT_TRUE(link1.empty());
}
/**
@ -157,14 +158,14 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest002, TestSize.Level0)
deferredLinkMgr.abilityFilter_ = filter;
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
std::vector<std::string> domains;
domains.emplace_back(BUNDLE_DOMAIN_WRONG);
auto link = deferredLinkMgr.GetDeferredLink(BUNDLE_NAME, domains);
EXPECT_TRUE(link.empty());
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
}
/**
@ -181,14 +182,14 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest003, TestSize.Level0)
deferredLinkMgr.abilityFilter_ = filter;
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
std::vector<std::string> domains;
domains.emplace_back(BUNDLE_DOMAIN_WRONG);
auto link = deferredLinkMgr.GetDeferredLink(BUNDLE_NAME, domains);
EXPECT_TRUE(link.empty());
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
}
/**
@ -205,14 +206,14 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest004, TestSize.Level0)
deferredLinkMgr.abilityFilter_ = filter;
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
std::vector<std::string> domains;
domains.emplace_back(BUNDLE_DOMAIN);
auto link = deferredLinkMgr.GetDeferredLink(BUNDLE_NAME_WRONG, domains);
EXPECT_TRUE(link.empty());
EXPECT_TRUE(deferredLinkMgr.caches.size() == 1);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 1);
}
/**
@ -231,7 +232,7 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest005, TestSize.Level0)
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL, .timeStamp = GetSecondsSince1970ToNow() });
deferredLinkMgr.PutDeferredLink(
{ .domain = BUNDLE_DOMAIN, .url = BUNDLE_URL_NEW, .timeStamp = GetSecondsSince1970ToNow() });
EXPECT_TRUE(deferredLinkMgr.caches.size() == 2);
EXPECT_TRUE(deferredLinkMgr.caches_.size() == 2);
std::vector<std::string> domains;
domains.emplace_back(BUNDLE_DOMAIN);
@ -239,7 +240,10 @@ HWTEST_F(DeferredLinkMgrTest, DeferredLinkGetTest005, TestSize.Level0)
EXPECT_FALSE(link.empty());
EXPECT_EQ(link, BUNDLE_URL_NEW);
EXPECT_TRUE(deferredLinkMgr.caches.empty());
EXPECT_TRUE(deferredLinkMgr.caches_.empty());
auto link1 = deferredLinkMgr.GetDeferredLink(BUNDLE_NAME, domains);
EXPECT_TRUE(link1.empty());
}
/**