!2926 GetApplyedQuickFixInfo napi Interface

Merge pull request !2926 from laoyitong/quickFix
This commit is contained in:
openharmony_ci
2022-09-05 13:10:10 +00:00
committed by Gitee
4 changed files with 132 additions and 0 deletions
+1
View File
@@ -16,6 +16,7 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni")
ohos_shared_library("quickfixmanager_napi") {
sources = [
"js_application_quick_fix_info.cpp",
"js_quick_fix_manager.cpp",
"native_module.cpp",
]
@@ -0,0 +1,53 @@
/*
* 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 "js_application_quick_fix_info.h"
#include "js_runtime_utils.h"
namespace OHOS {
namespace AbilityRuntime {
NativeValue *CreateJsApplicationQuickFixInfo(
NativeEngine &engine, const AAFwk::ApplicationQuickFixInfo &appQuickFixInfo)
{
NativeValue *objValue = engine.CreateObject();
NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
object->SetProperty("bundleName", CreateJsValue(engine, appQuickFixInfo.bundleName));
object->SetProperty("bundleVersionCode", CreateJsValue(engine, appQuickFixInfo.bundleVersionCode));
object->SetProperty("bundleVersionName", CreateJsValue(engine, appQuickFixInfo.bundleVersionName));
object->SetProperty("quickFixVersionCode", CreateJsValue(engine, appQuickFixInfo.appqfInfo.versionCode));
object->SetProperty("quickFixVersionName", CreateJsValue(engine, appQuickFixInfo.appqfInfo.versionName));
object->SetProperty(
"hapModuleQuickFixInfo", CreateJsHapModuleQuickFixInfoArray(engine, appQuickFixInfo.appqfInfo.hqfInfos));
return objValue;
}
NativeValue *CreateJsHapModuleQuickFixInfoArray(NativeEngine &engine, const std::vector<AppExecFwk::HqfInfo> &hqfInfos)
{
NativeValue *arrayValue = engine.CreateArray(hqfInfos.size());
NativeArray *array = ConvertNativeValueTo<NativeArray>(arrayValue);
uint32_t index = 0;
for (const auto &hqfInfo : hqfInfos) {
NativeValue *objValue = engine.CreateObject();
NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
object->SetProperty("moduleName", CreateJsValue(engine, hqfInfo.moduleName));
object->SetProperty("moduleSha256", CreateJsValue(engine, hqfInfo.hapSha256));
object->SetProperty("quickFixFilePath", CreateJsValue(engine, hqfInfo.hqfFilePath));
array->SetElement(index++, objValue);
}
return arrayValue;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -0,0 +1,29 @@
/*
* 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 OHOS_ABILITY_RUNTIME_JS_APPLICATION_QUICK_FIX_INFO_H
#define OHOS_ABILITY_RUNTIME_JS_APPLICATION_QUICK_FIX_INFO_H
#include "native_engine/native_engine.h"
#include "quick_fix_info.h"
namespace OHOS {
namespace AbilityRuntime {
NativeValue *CreateJsApplicationQuickFixInfo(
NativeEngine &engine, const AAFwk::ApplicationQuickFixInfo &appQuickFixInfo);
NativeValue *CreateJsHapModuleQuickFixInfoArray(NativeEngine &engine, const std::vector<AppExecFwk::HqfInfo> &hqfInfos);
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_JS_APPLICATION_QUICK_FIX_INFO_H
@@ -16,6 +16,7 @@
#include "js_quick_fix_manager.h"
#include "hilog_wrapper.h"
#include "js_application_quick_fix_info.h"
#include "js_runtime_utils.h"
#include "napi_common_util.h"
#include "quick_fix_errno_def.h"
@@ -24,6 +25,7 @@
namespace OHOS {
namespace AbilityRuntime {
namespace {
constexpr size_t ARGC_ONE = 1;
constexpr size_t ARGC_TWO = 2;
const char *QUICK_FIX_MANAGER_NAME = "JsQuickFixMgr";
} // namespace
@@ -45,7 +47,54 @@ public:
return (me != nullptr) ? me->OnApplyQuickFix(*engine, *info) : nullptr;
}
static NativeValue *GetApplyedQuickFixInfo(NativeEngine *engine, NativeCallbackInfo *info)
{
JsQuickFixManager *me = CheckParamsAndGetThis<JsQuickFixManager>(engine, info);
return (me != nullptr) ? me->OnGetApplyedQuickFixInfo(*engine, *info) : nullptr;
}
private:
NativeValue *OnGetApplyedQuickFixInfo(NativeEngine &engine, NativeCallbackInfo &info)
{
HILOG_DEBUG("%{public}s is called.", __func__);
AsyncTask::CompleteCallback complete;
do {
if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
complete = [](NativeEngine& engine, AsyncTask& task, int32_t status) {
task.Reject(engine, CreateJsError(engine, AAFwk::QUICK_FIX_INVALID_PARAM,
"wrong parameter number."));
};
break;
}
std::string bundleName;
if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
reinterpret_cast<napi_value>(info.argv[0]), bundleName)) {
complete = [](NativeEngine& engine, AsyncTask& task, int32_t status) {
task.Reject(engine, CreateJsError(engine, AAFwk::QUICK_FIX_INVALID_PARAM,
"bundle name not a string."));
};
break;
}
complete = [bundleName](NativeEngine &engine, AsyncTask &task, int32_t status) {
AppExecFwk::ApplicationQuickFixInfo quickFixInfo;
auto errCode = DelayedSingleton<AAFwk::QuickFixManagerClient>::GetInstance()->GetApplyedQuickFixInfo(
bundleName, quickFixInfo);
if (errCode == 0) {
task.Resolve(engine, CreateJsApplicationQuickFixInfo(engine, quickFixInfo));
} else {
task.Reject(engine, CreateJsError(engine, errCode, "get applyed quickfix info failed."));
}
};
} while (0);
NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[1];
NativeValue *result = nullptr;
AsyncTask::Schedule("JsQuickFixManager::OnGetApplyedQuickFixInfo", engine,
CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
HILOG_DEBUG("OnGetApplyedQuickFixInfo is finished.");
return result;
}
NativeValue *OnApplyQuickFix(NativeEngine &engine, NativeCallbackInfo &info)
{
HILOG_DEBUG("function called.");