diff --git a/frameworks/js/napi/quick_fix/BUILD.gn b/frameworks/js/napi/quick_fix/BUILD.gn index 9e1c07cdc1..b8d43ceff5 100644 --- a/frameworks/js/napi/quick_fix/BUILD.gn +++ b/frameworks/js/napi/quick_fix/BUILD.gn @@ -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", ] diff --git a/frameworks/js/napi/quick_fix/js_application_quick_fix_info.cpp b/frameworks/js/napi/quick_fix/js_application_quick_fix_info.cpp new file mode 100644 index 0000000000..8b2be9d8a7 --- /dev/null +++ b/frameworks/js/napi/quick_fix/js_application_quick_fix_info.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(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 &hqfInfos) +{ + NativeValue *arrayValue = engine.CreateArray(hqfInfos.size()); + NativeArray *array = ConvertNativeValueTo(arrayValue); + uint32_t index = 0; + for (const auto &hqfInfo : hqfInfos) { + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = ConvertNativeValueTo(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 diff --git a/frameworks/js/napi/quick_fix/js_application_quick_fix_info.h b/frameworks/js/napi/quick_fix/js_application_quick_fix_info.h new file mode 100644 index 0000000000..b599d1142d --- /dev/null +++ b/frameworks/js/napi/quick_fix/js_application_quick_fix_info.h @@ -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 &hqfInfos); +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_JS_APPLICATION_QUICK_FIX_INFO_H \ No newline at end of file diff --git a/frameworks/js/napi/quick_fix/js_quick_fix_manager.cpp b/frameworks/js/napi/quick_fix/js_quick_fix_manager.cpp index 43b1ecd9db..4d0a84cd1b 100644 --- a/frameworks/js/napi/quick_fix/js_quick_fix_manager.cpp +++ b/frameworks/js/napi/quick_fix/js_quick_fix_manager.cpp @@ -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(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(&engine), + reinterpret_cast(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::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.");