diff --git a/bundle.json b/bundle.json index 10d428cf5d7fb0f3ae56e184a9d2c738916add5c..7834b42dac96d33dc353f4168c5772cc4612738d 100644 --- a/bundle.json +++ b/bundle.json @@ -51,12 +51,14 @@ "preferences", "sys_installer", "time_service", - "relational_store" + "relational_store", + "runtime_core" ], "third_party": [] }, "build": { "modules": [ + "//base/update/updateservice/frameworks/js/ani:update_framework_taihe", "//base/update/updateservice/frameworks/js/napi/update:update", "//base/update/updateservice/interfaces/inner_api/engine:updateservicekits", "//base/update/updateservice/interfaces/inner_api/modulemgr:update_module_mgr", diff --git a/foundations/model/include/business_error.h b/foundations/model/include/business_error.h index 24a7f5723139cedf58c3d18968d062d9a2f6b5af..2eabd7218dadc786064e1e34f9ff15e01560fb27 100644 --- a/foundations/model/include/business_error.h +++ b/foundations/model/include/business_error.h @@ -39,6 +39,11 @@ struct BusinessError { return *this; } + bool IsSuccess() const + { + return errorNum == CallResult::SUCCESS; + } + BusinessError &AddErrorMessage(int32_t errorCode, const std::string &errorMessage) { ErrorMessage errMsg; diff --git a/frameworks/js/ani/BUILD.gn b/frameworks/js/ani/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5d68d6813610ae0fc23771c36cb4c16ab0cb16fd --- /dev/null +++ b/frameworks/js/ani/BUILD.gn @@ -0,0 +1,114 @@ +# 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/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../updateengine.gni") +import("$updateengine_root_path/foundations/foundations.gni") + +copy_taihe_idl("copy_update_framework") { + sources = [ "idl/ohos.update.taihe" ] +} + +subsystem_name = "updater" +part_name = "update_service" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" +ohos_taihe("run_taihe_update") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_update_framework" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.update.ani.cpp", + "$taihe_generated_file_path/src/ohos.update.abi.c", + ] +} + +ani_includes = [ + "include", + "$updateengine_root_path/frameworks/js/napi/update/include", + "$updateengine_root_path/frameworks/js/napi/update/common/include", +] +ani_includes += foundations_include + + +ani_deps = [ + ":run_taihe_update", + "$updateengine_root_path/foundations:update_foundations", + "$updateengine_root_path/frameworks/js/napi/update:$updateengine_client_library_name", + "$updateengine_root_path/interfaces/inner_api/engine:$updateengine_inner_library_name", +] + +ani_deps +=foundations_deps + + +ani_external_deps =[ + "c_utils:utils", + "runtime_core:ani", + "runtime_core:libarkruntime", + "runtime_core:ani_helpers", + "cJSON:cjson", +] +ani_external_deps += foundations_external_deps + +taihe_shared_library("update_ani") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" + taihe_generated_file_path = "$taihe_generated_file_path" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + sources = get_target_outputs(":run_taihe_update") + include_dirs = ani_includes + sources += [ + "src/ani_base_updater.cpp", + "src/ani_constructor.cpp", + "src/ani_common_conveter.cpp", + "src/ani_local_updater.cpp", + "src/ani_restorer.cpp", + "src/ani_updater.cpp", + "src/taihe_impl.cpp", + ] + deps = ani_deps + external_deps = ani_external_deps +} + +generate_static_abc("update_framework_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.update.ets" ] + is_boot_abc = "True" + dst_file = "$target_out_dir/update_ani.abc" + out_puts = [ "$target_out_dir/update_ani.abc" ] + device_dst_file = "/system/framework/update_ani.abc" + dependencies = [ ":run_taihe_update" ] +} + +ohos_prebuilt_etc("update_framework_etc") { + source = "$target_out_dir/update_ani.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":update_framework_abc" ] +} + +group("update_framework_taihe") { + deps = [ + ":update_framework_etc", + ":update_ani", + ] +} \ No newline at end of file diff --git a/frameworks/js/ani/idl/ohos.update.taihe b/frameworks/js/ani/idl/ohos.update.taihe new file mode 100644 index 0000000000000000000000000000000000000000..7bc3f25c1cd88735a2e3ad1d749be65c17c54f35 --- /dev/null +++ b/frameworks/js/ani/idl/ohos.update.taihe @@ -0,0 +1,342 @@ +/* + * 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. + */ + +@!namespace("@ohos.update", "update") + +@!sts_inject(""" + static { + loadLibrary("update_ani.z") + } +""") + + struct UpgradeInfo { + upgradeApp: String; + businessType: BusinessType; + } + + struct BusinessType { + vendor: BusinessVendor; + subType: BusinessSubType; + } + + struct CheckResult { + isExistNewVersion: bool; + newVersionInfo: NewVersionInfo; + } + + struct NewVersionInfo { + versionDigestInfo: VersionDigestInfo; + versionComponents: Array; + } + + struct VersionDigestInfo { + versionDigest: String; + } + + struct VersionComponent { + componentId: String; + componentType: ComponentType; + upgradeAction: UpgradeAction; + displayVersion: String; + innerVersion: String; + size: i32; + effectiveMode: EffectiveMode; + descriptionInfo: DescriptionInfo; + } + + struct DescriptionOptions { + format: DescriptionFormat; + language: String; + } + + struct ComponentDescription { + componentId: String; + descriptionInfo: DescriptionInfo; + } + + struct DescriptionInfo { + descriptionType: DescriptionType; + content: String; + } + + struct CurrentVersionInfo { + osVersion: String; + deviceName: String; + versionComponents: Array; + } + + struct DownloadOptions { + allowNetwork: NetType; + order: Order; + } + + struct ResumeDownloadOptions { + allowNetwork: NetType; + } + + struct PauseDownloadOptions { + isAllowAutoResume: bool; + } + + struct UpgradeOptions { + order: Order; + } + + struct ClearOptions { + status: UpgradeStatus; + } + + struct UpgradePolicy { + downloadStrategy: bool; + autoUpgradeStrategy: bool; + autoUpgradePeriods: Array; + } + + struct UpgradePeriod { + start: i32; + end: i32; + } + + struct TaskInfo { + existTask: bool; + taskBody: TaskBody; + } + + struct EventInfo { + eventId: EventId; + taskBody: TaskBody; + } + + struct TaskBody { + versionDigestInfo: VersionDigestInfo; + status: UpgradeStatus; + subStatus: i32; + progress: i32; + installMode: i32; + errorMessages: Array; + versionComponents: Array; + } + + struct ErrorMessage { + errorCode: i32; + errorMessage: String; + } + + struct EventClassifyInfo { + eventClassify: EventClassify; + extraInfo: String; + } + + struct UpgradeFile { + fileType: ComponentType; + filePath: String; + } + + enum BusinessVendor : String { + PUBLIC = "public" + } + + enum BusinessSubType : i32 { + FIRMWARE = 1 + } + + enum ComponentType : i32 { + OTA = 1 + } + + enum UpgradeAction : String { + UPGRADE = "upgrade", + RECOVERY = "recovery" + } + + enum EffectiveMode : i32 { + COLD = 1, + LIVE = 2, + LIVE_AND_COLD = 3 + } + + enum DescriptionType : i32 { + CONTENT = 0, + URI = 1 + } + + enum DescriptionFormat : i32 { + STANDARD = 0, + SIMPLIFIED = 1 + } + + enum NetType : i32 { + CELLULAR = 1, + METERED_WIFI = 2, + NOT_METERED_WIFI = 4, + WIFI = 6, + CELLULAR_AND_WIFI = 7 + } + + enum Order : i32 { + DOWNLOAD = 1, + INSTALL = 2, + DOWNLOAD_AND_INSTALL = 3, + APPLY = 4, + INSTALL_AND_APPLY = 6 + } + + enum UpgradeStatus : i32 { + ERROR = -1, + INIT = 0, + CHECKING_VERSION = 10, + CHECK_VERSION_FAIL = 11, + CHECK_VERSION_SUCCESS = 12, + DOWNLOADING = 20, + DOWNLOAD_PAUSE = 21, + DOWNLOAD_CANCEL = 22, + DOWNLOAD_FAIL = 23, + DOWNLOAD_SUCCESS = 24, + VERIFYING = 30, + VERIFY_FAIL = 31, + VERIFY_SUCCESS = 32, + AUTHING = 60, + AUTH_FAIL = 61, + AUTH_SUCCESS = 62, + WAIT_TRANSFER = 68, + TRANSFER_START = 69, + PACKAGE_TRANSING = 70, + PACKAGE_TRANS_FAIL = 71, + PACKAGE_TRANS_SUCCESS = 72, + INSTALLING = 80, + INSTALL_FAIL = 81, + INSTALL_SUCCESS = 82, + INSTALL_PAUSE = 83, + UPDATING = 90, + UPDATE_FAIL = 91, + UPDATE_SUCCESS = 92, + UPGRADE_REBOOT = 93, + UPGRADE_COUNT_DOWN = 100, + UPGRADE_CANCEL = 101 + } + + enum EventClassify : i32 { + TASK = 0x01000000 + } + + enum EventId : i32 { + EVENT_TASK_BASE = 0x01000000, // EventClassify.TASK, + EVENT_TASK_RECEIVE, + EVENT_TASK_CANCEL, + EVENT_DOWNLOAD_WAIT, + EVENT_DOWNLOAD_START, + EVENT_DOWNLOAD_UPDATE, + EVENT_DOWNLOAD_PAUSE, + EVENT_DOWNLOAD_RESUME, + EVENT_DOWNLOAD_SUCCESS, + EVENT_DOWNLOAD_FAIL, + EVENT_UPGRADE_WAIT, + EVENT_UPGRADE_START, + EVENT_UPGRADE_UPDATE, + EVENT_APPLY_WAIT, + EVENT_APPLY_START, + EVENT_UPGRADE_SUCCESS, + EVENT_UPGRADE_FAIL + } + +@!sts_inject("""export type UpgradeTaskCallback = (eventInfo: EventInfo) => void;""") + +interface Updater { + @gen_async("checkNewVersion") + @gen_promise("checkNewVersion") + CheckNewVersionSync(): CheckResult; + + @gen_async("getNewVersionInfo") + @gen_promise("getNewVersionInfo") + GetNewVersionInfoSync(): NewVersionInfo; + + @gen_async("getNewVersionDescription") + @gen_promise("getNewVersionDescription") + GetNewVersionDescriptionSync(versionDigestInfo: VersionDigestInfo, descriptionOptions: DescriptionOptions): Array; + + @gen_async("getCurrentVersionInfo") + @gen_promise("getCurrentVersionInfo") + GetCurrentVersionInfoSync(): CurrentVersionInfo; + + @gen_async("getCurrentVersionDescription") + @gen_promise("getCurrentVersionDescription") + GetCurrentVersionDescriptionSync(descriptionOptions: DescriptionOptions): Array; + + @gen_async("getTaskInfo") + @gen_promise("getTaskInfo") + GetTaskInfoSync(): TaskInfo; + + @gen_async("download") + @gen_promise("download") + DownloadSync(versionDigestInfo: VersionDigestInfo, downloadOptions: DownloadOptions): void; + + @gen_async("resumeDownload") + @gen_promise("resumeDownload") + ResumeDownloadSync(versionDigestInfo: VersionDigestInfo, resumeDownloadOptions: ResumeDownloadOptions): void; + + @gen_async("pauseDownload") + @gen_promise("pauseDownload") + PauseDownloadSync(versionDigestInfo: VersionDigestInfo, pauseDownloadOptions: PauseDownloadOptions): void; + + @gen_async("upgrade") + @gen_promise("upgrade") + UpgradeSync(versionDigestInfo: VersionDigestInfo, upgradeOptions: UpgradeOptions): void; + + @gen_async("clearError") + @gen_promise("clearError") + ClearErrorSync(versionDigestInfo: VersionDigestInfo, clearOptions: ClearOptions): void; + + @gen_async("getUpgradePolicy") + @gen_promise("getUpgradePolicy") + GetUpgradePolicySync(): UpgradePolicy; + + @gen_async("setUpgradePolicy") + @gen_promise("setUpgradePolicy") + SetUpgradePolicySync(policy: UpgradePolicy): void; + + @gen_async("terminateUpgrade") + @gen_promise("terminateUpgrade") + TerminateUpgradeSync():void; + + On(eventClassifyInfo: EventClassifyInfo, taskCallback: (eventInfo: EventInfo) => void): void; + + Off(eventClassifyInfo: EventClassifyInfo, taskCallback:Optional<(eventInfo: EventInfo) => void>): void; + } + + interface Restorer { + @gen_async("factoryReset") + @gen_promise("factoryReset") + FactoryResetSync(): void; + } + + interface LocalUpdater { + @gen_async("verifyUpgradePackage") + @gen_promise("verifyUpgradePackage") + VerifyUpgradePackageSync(upgradeFile: UpgradeFile, certsFile: String): void; + + @gen_async("applyNewVersion") + @gen_promise("applyNewVersion") + ApplyNewVersionSync(upgradeFiles: Array): void; + + On(eventClassifyInfo: EventClassifyInfo, taskCallback: (eventInfo: EventInfo) => void): void; + + Off(eventClassifyInfo: EventClassifyInfo, taskCallback:Optional<(eventInfo: EventInfo) => void>): void; + } + + function GetOnlineUpdater(upgradeInfo: UpgradeInfo): Updater; + + function GetRestorer(): Restorer; + + function GetLocalUpdater(): LocalUpdater; \ No newline at end of file diff --git a/frameworks/js/ani/include/ani_base_updater.h b/frameworks/js/ani/include/ani_base_updater.h new file mode 100644 index 0000000000000000000000000000000000000000..b44ec15f1b063b43e900fb1fb78a3e1943e8a4a2 --- /dev/null +++ b/frameworks/js/ani/include/ani_base_updater.h @@ -0,0 +1,55 @@ +/* + * 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. + */ + +#ifndef ANI_BASE_UPDATER_H +#define ANI_BASE_UPDATER_H + +#include +#include + +#include "ohos.update.proj.hpp" +#include "taihe/callback.hpp" + +#include "business_error.h" +#include "event_info.h" + +namespace OHOS::UpdateEngine { +class AniBaseUpdater { +public: + virtual ~AniBaseUpdater(); + AniBaseUpdater() = default; + + void On(taihe::callback_view callback); + void Off(const taihe::callback &callback); + void OffAll(); + +protected: + virtual void RegisterCallback() {} + virtual std::string GetPermissionName(); + virtual void UnRegisterCallback() {} + bool SetError(int32_t ret, const std::string &funcName, const BusinessError &error); + void CallbackEventInfo(const EventInfo &eventInfo); + +private: + BusinessError GetIpcBusinessError(const std::string &funcName, int32_t ipcRequestCode); + bool IsCommonError(CallResult callResult); + int32_t ConvertToErrorCode(CallResult callResult); + + std::mutex mutex_; + std::vector> callbacks_; +}; +} + +#endif // ANI_BASE_UPDATER_H diff --git a/frameworks/js/ani/include/ani_common_conveter.h b/frameworks/js/ani/include/ani_common_conveter.h new file mode 100644 index 0000000000000000000000000000000000000000..deab9de38e6bb89ba660d1a4f9883f848f4b3830 --- /dev/null +++ b/frameworks/js/ani/include/ani_common_conveter.h @@ -0,0 +1,79 @@ +/* + * 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. + */ + +#ifndef ANI_COMMON_CONVETER_H +#define ANI_COMMON_CONVETER_H + +#include "ohos.update.proj.hpp" + +#include "check_result.h" +#include "clear_options.h" +#include "component_description.h" +#include "current_version_info.h" +#include "description_options.h" +#include "download_options.h" +#include "event_info.h" +#include "pause_download_options.h" +#include "resume_download_options.h" +#include "upgrade_status.h" +#include "task_body.h" +#include "task_info.h" +#include "upgrade_file.h" +#include "upgrade_info.h" +#include "upgrade_options.h" +#include "upgrade_policy.h" + +namespace OHOS::UpdateEngine { +class AniCommonConverter { +public: + static ohos::update::ErrorMessage Converter(const ErrorMessage &error); + static taihe::array Converter(const std::vector &errors); + static ohos::update::TaskInfo Converter(const TaskInfo &info); + static ohos::update::CurrentVersionInfo Converter(const CurrentVersionInfo &info); + static ohos::update::NewVersionInfo Converter(const NewVersionInfo &info); + static ohos::update::CheckResult Converter(const CheckResult &result); + static ohos::update::TaskBody Converter(const TaskBody &taskBody); + static ohos::update::VersionDigestInfo Converter(const VersionDigestInfo &versionDigestInfo); + static ohos::update::UpgradeStatus Converter(const UpgradeStatus &status); + static ohos::update::ComponentType Converter(const ComponentType &componentType); + static ohos::update::DescriptionType Converter(const DescriptionType &descriptionType); + static ohos::update::EffectiveMode Converter(const EffectiveMode &effectiveMode); + static ohos::update::EventId Converter(const EventId &effectiveMode); + static ohos::update::DescriptionInfo Converter(const DescriptionInfo &description); + static ohos::update::UpgradeAction Converter(const std::string &action); + static ohos::update::UpgradePolicy Converter(const UpgradePolicy &upgradePolicy); + static ohos::update::ComponentDescription Converter(const ComponentDescription &componentDescription); + static taihe::array Converter( + const std::vector &componentDescriptions); + static ohos::update::VersionComponent Converter(const VersionComponent &component); + static taihe::array Converter(const std::vector &components); + static ohos::update::UpgradePeriod Converter(const UpgradePeriod &upgradePeriod); + static taihe::array Converter(const std::vector &upgradePeriods); + static ohos::update::EventInfo Converter(const EventInfo &info); + static VersionDigestInfo Converter(const ohos::update::VersionDigestInfo &versionDigestInfo); + static DescriptionOptions Converter(const ohos::update::DescriptionOptions &descriptionOptions); + static DownloadOptions Converter(const ohos::update::DownloadOptions &downloadOptions); + static ResumeDownloadOptions Converter(const ohos::update::ResumeDownloadOptions &resumeDownloadOptions); + static PauseDownloadOptions Converter(const ohos::update::PauseDownloadOptions &pauseDownloadOptions); + static UpgradeOptions Converter(const ohos::update::UpgradeOptions &upgradeOptions); + static ClearOptions Converter(const ohos::update::ClearOptions &clearOptions); + static UpgradeFile Converter(const ohos::update::UpgradeFile &upgradeFile); + static UpgradeInfo Converter(const ohos::update::UpgradeInfo &info); + static BusinessType Converter(const ohos::update::BusinessType &businessType); + static UpgradePeriod Converter(const ohos::update::UpgradePeriod &period); + static UpgradePolicy Converter(const ohos::update::UpgradePolicy &policy); +}; +} +#endif // ANI_COMMON_CONVETER_H diff --git a/frameworks/js/ani/include/ani_local_updater.h b/frameworks/js/ani/include/ani_local_updater.h new file mode 100644 index 0000000000000000000000000000000000000000..9d088b21dad35e53270305c0026a26fef6eab0f6 --- /dev/null +++ b/frameworks/js/ani/include/ani_local_updater.h @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef ANI_LOCAL_UPDATER_H +#define ANI_LOCAL_UPDATER_H + +#include "ohos.update.proj.hpp" + +#include "ani_base_updater.h" + +namespace OHOS::UpdateEngine { +class AniLocalUpdater final : public AniBaseUpdater { +public: + AniLocalUpdater() = default; + + void VerifyUpgradePackageSync(const ohos::update::UpgradeFile &upgradeFile, taihe::string_view certsFile); + void ApplyNewVersionSync(const taihe::array_view &upgradeFiles); +}; +} +#endif // ANI_LOCAL_UPDATER_H diff --git a/frameworks/js/ani/include/ani_restorer.h b/frameworks/js/ani/include/ani_restorer.h new file mode 100644 index 0000000000000000000000000000000000000000..01bdd18709998a6e74ea69b28b62e0db59b5d8f3 --- /dev/null +++ b/frameworks/js/ani/include/ani_restorer.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +#ifndef ANI_RESTORER_H +#define ANI_RESTORER_H + +#include "ani_base_updater.h" + +namespace OHOS::UpdateEngine { +class AniRestorer final : public AniBaseUpdater { +public: + explicit AniRestorer() = default; + ~AniRestorer() override = default; + + void FactoryResetSync(); + +protected: + std::string GetPermissionName() override; +}; +} // namespace OHOS::UpdateEngine +#endif // ANI_RESTORER_H diff --git a/frameworks/js/ani/include/ani_updater.h b/frameworks/js/ani/include/ani_updater.h new file mode 100644 index 0000000000000000000000000000000000000000..85f629072cb864f8bc2eb40ee2fdb32fac513925 --- /dev/null +++ b/frameworks/js/ani/include/ani_updater.h @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#ifndef ANI_UPDATER_H +#define ANI_UPDATER_H + +#include "ohos.update.proj.hpp" +#include "ohos.update.impl.hpp" + +#include "ani_base_updater.h" +#include "upgrade_info.h" + +namespace OHOS::UpdateEngine { +class AniUpdater final : public AniBaseUpdater { +public: + explicit AniUpdater(const UpgradeInfo &info); + + ohos::update::CheckResult CheckNewVersionSync(); + ohos::update::NewVersionInfo GetNewVersionInfoSync(); + taihe::array GetNewVersionDescriptionSync( + const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DescriptionOptions &descriptionOptions); + ohos::update::CurrentVersionInfo GetCurrentVersionInfoSync(); + taihe::array GetCurrentVersionDescriptionSync( + const ohos::update::DescriptionOptions &descriptionOptions); + ohos::update::TaskInfo GetTaskInfoSync(); + void DownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DownloadOptions &downloadOptions); + void ResumeDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ResumeDownloadOptions &resumeDownloadOptions); + void PauseDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::PauseDownloadOptions &pauseDownloadOptions); + void UpgradeSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::UpgradeOptions &upgradeOptions); + void ClearErrorSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ClearOptions &clearOptions); + ohos::update::UpgradePolicy GetUpgradePolicySync(); + void SetUpgradePolicySync(const ohos::update::UpgradePolicy &policy); + void TerminateUpgradeSync(); + +protected: + void RegisterCallback() override; + void UnRegisterCallback() override; + +private: + UpgradeInfo upgradeInfo_; +}; +} + +#endif // ANI_UPDATER_H diff --git a/frameworks/js/ani/src/ani_base_updater.cpp b/frameworks/js/ani/src/ani_base_updater.cpp new file mode 100644 index 0000000000000000000000000000000000000000..121c5b20233dd2c6e27b1fba422aa8e31e9eb2dd --- /dev/null +++ b/frameworks/js/ani/src/ani_base_updater.cpp @@ -0,0 +1,148 @@ +/* + * 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 "ani_base_updater.h" + +#include "taihe/runtime.hpp" + +#include "ani_common_conveter.h" +#include "update_log.h" + +namespace OHOS::UpdateEngine { +AniBaseUpdater::~AniBaseUpdater() +{ + OffAll(); +} + +void AniBaseUpdater::On(taihe::callback_view callback) +{ + std::unique_lock lock(mutex_); + taihe::callback taiheCallback(callback); + if (callbacks_.empty()) { + RegisterCallback(); + } + callbacks_.push_back(taiheCallback); +} + +void AniBaseUpdater::Off([[maybe_unused]] const taihe::callback &callback) +{ + std::unique_lock lock(mutex_); + if (callbacks_.empty()) { + UnRegisterCallback(); + } +} + +void AniBaseUpdater::OffAll() +{ + std::unique_lock lock(mutex_); + callbacks_.clear(); + UnRegisterCallback(); +} + +std::string AniBaseUpdater::GetPermissionName() +{ + return "ohos.permission.UPDATE_SYSTEM"; +} + +bool AniBaseUpdater::SetError(int32_t ret, const std::string &funcName, const BusinessError &error) +{ + if (ret != 0) { + const BusinessError errorInfo = GetIpcBusinessError(funcName, ret); + taihe::set_business_error(ConvertToErrorCode(errorInfo.errorNum), errorInfo.message); + return true; + } + if (!error.IsSuccess()) { + taihe::set_business_error(ConvertToErrorCode(error.errorNum), error.message); + return true; + } + return false; +} + +void AniBaseUpdater::CallbackEventInfo(const EventInfo &eventInfo) +{ + std::unique_lock lock(mutex_); + for ([[maybe_unused]] const auto &callback : callbacks_) { + callback(AniCommonConverter::Converter(eventInfo)); + } +} + +bool AniBaseUpdater::IsCommonError(CallResult callResult) +{ + return callResult == CallResult::UN_SUPPORT || callResult == CallResult::NOT_SYSTEM_APP || + callResult == CallResult::APP_NOT_GRANTED || callResult == CallResult::PARAM_ERR; +} + +int32_t AniBaseUpdater::ConvertToErrorCode(CallResult callResult) +{ + if (IsCommonError(callResult) || callResult == CallResult::SUCCESS) { + return CAST_INT(callResult); + } + constexpr int32_t componentErr = 11500000; + return componentErr + CAST_INT(callResult); +} + +BusinessError AniBaseUpdater::GetIpcBusinessError(const std::string &funcName, int32_t ipcRequestCode) +{ + BusinessError businessError; + std::string msg = "execute error"; + const auto ipcCallResult = static_cast(ipcRequestCode); + const std::string callResultStr = std::to_string(ConvertToErrorCode(ipcCallResult)); + + switch (ipcRequestCode) { + case INT_NOT_SYSTEM_APP: + msg = "BusinessError " + callResultStr + NOT_SYSTEM_APP_INFO.data(); + break; + case INT_APP_NOT_GRANTED: + msg = "BusinessError " + callResultStr + ": Permission denied. An attempt was made to " + funcName + + " forbidden by permission: " + GetPermissionName() + "."; + break; + case INT_CALL_IPC_ERR: + msg = "BusinessError " + callResultStr + ": IPC error."; + break; + case INT_UN_SUPPORT: + msg = "BusinessError " + callResultStr + ": Capability not supported. " + "function " + funcName + + " can not work correctly due to limited device capabilities."; + break; + case INT_PARAM_ERR: + msg = "param error"; + break; + case INT_CALL_FAIL: + msg = "BusinessError " + callResultStr + ": Execute fail."; + break; + case INT_FORBIDDEN: + msg = "BusinessError " + callResultStr + ": Forbidden execution."; + break; + case INT_DEV_UPG_INFO_ERR: + msg = "BusinessError " + callResultStr + ": Device info error."; + break; + case INT_TIME_OUT: + msg = "BusinessError " + callResultStr + ": Execute timeout."; + break; + case INT_DB_ERROR: + msg = "BusinessError " + callResultStr + ": DB error."; + break; + case INT_IO_ERROR: + msg = "BusinessError " + callResultStr + ": IO error."; + break; + case INT_NET_ERROR: + msg = "BusinessError " + callResultStr + ": Network error."; + break; + default: + break; + } + businessError.Build(static_cast(ipcRequestCode), msg); + return businessError; +} +} diff --git a/frameworks/js/ani/src/ani_common_conveter.cpp b/frameworks/js/ani/src/ani_common_conveter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f830fd2ad8f4451908bc0e608a2de9348357d26e --- /dev/null +++ b/frameworks/js/ani/src/ani_common_conveter.cpp @@ -0,0 +1,305 @@ +/* + * 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 "ani_common_conveter.h" + +namespace OHOS::UpdateEngine { +ohos::update::ErrorMessage AniCommonConverter::Converter(const ErrorMessage &error) +{ + return { + .errorCode = static_cast(error.errorCode), + .errorMessage = error.errorMessage + }; +} + +taihe::array AniCommonConverter::Converter(const std::vector &errors) +{ + std::vector taiheErrorMessage; + taiheErrorMessage.reserve(errors.size()); + for (const auto &error : errors) { + taiheErrorMessage.push_back(Converter(error)); + } + return { taiheErrorMessage }; +} + +ohos::update::TaskInfo AniCommonConverter::Converter(const TaskInfo &info) +{ + return { + .existTask = info.existTask, + .taskBody = Converter(info.taskBody) + }; +} + +ohos::update::CurrentVersionInfo AniCommonConverter::Converter(const CurrentVersionInfo &info) +{ + return { + .deviceName = info.deviceName, + .osVersion = info.osVersion, + .versionComponents = Converter(info.versionComponents) + }; +} + +ohos::update::NewVersionInfo AniCommonConverter::Converter(const NewVersionInfo &info) +{ + return { + .versionComponents = Converter(info.versionComponents), + .versionDigestInfo = Converter(info.versionDigestInfo) + }; +} + +ohos::update::CheckResult AniCommonConverter::Converter(const CheckResult &result) +{ + return { + .isExistNewVersion = result.isExistNewVersion, + .newVersionInfo = Converter(result.newVersionInfo) + }; +} + +ohos::update::TaskBody AniCommonConverter::Converter(const TaskBody &taskBody) +{ + ohos::update::TaskBody value = { + .progress = taskBody.progress, + .status = Converter(taskBody.status), + .errorMessages = Converter(taskBody.errorMessages), + .installMode = static_cast(taskBody.installMode), + .subStatus = static_cast(taskBody.subStatus), + .versionComponents = Converter(taskBody.versionComponents), + .versionDigestInfo = Converter(taskBody.versionDigestInfo) + }; + return value; +} + +ohos::update::VersionDigestInfo AniCommonConverter::Converter(const VersionDigestInfo &versionDigestInfo) +{ + return { + .versionDigest = versionDigestInfo.versionDigest + }; +} + +ohos::update::UpgradeStatus AniCommonConverter::Converter(const UpgradeStatus &status) +{ + return ohos::update::UpgradeStatus::from_value(static_cast(status)); +} + +ohos::update::ComponentType AniCommonConverter::Converter(const ComponentType &componentType) +{ + return ohos::update::ComponentType::from_value(static_cast(componentType)); +} + +ohos::update::DescriptionType AniCommonConverter::Converter(const DescriptionType &descriptionType) +{ + return ohos::update::DescriptionType::from_value(static_cast(descriptionType)); +} + +ohos::update::EffectiveMode AniCommonConverter::Converter(const EffectiveMode &effectiveMode) +{ + return ohos::update::EffectiveMode::from_value(static_cast(effectiveMode)); +} + +ohos::update::DescriptionInfo AniCommonConverter::Converter(const DescriptionInfo &description) +{ + return { + .content = description.content, + .descriptionType = Converter(description.descriptionType) + }; +} + +ohos::update::UpgradeAction AniCommonConverter::Converter(const std::string &action) +{ + return ohos::update::UpgradeAction::from_value(action); +} + +ohos::update::UpgradePolicy AniCommonConverter::Converter(const UpgradePolicy &upgradePolicy) +{ + std::vector vectors = { std::begin(upgradePolicy.autoUpgradePeriods), + std::end(upgradePolicy.autoUpgradePeriods) }; + return { + .downloadStrategy = upgradePolicy.downloadStrategy, + .autoUpgradePeriods = Converter(vectors), + .autoUpgradeStrategy = upgradePolicy.autoUpgradeStrategy + }; +} + +ohos::update::ComponentDescription AniCommonConverter::Converter(const ComponentDescription &componentDescription) +{ + return { + .componentId = componentDescription.componentId, + .descriptionInfo = Converter(componentDescription.descriptionInfo) + }; +} + +taihe::array AniCommonConverter::Converter( + const std::vector &componentDescriptions) +{ + std::vector taiheComponents; + taiheComponents.reserve(componentDescriptions.size()); + for (const auto &component : componentDescriptions) { + taiheComponents.push_back(Converter(component)); + } + return { taiheComponents }; +} + +ohos::update::VersionComponent AniCommonConverter::Converter(const VersionComponent &component) +{ + return { + .size = static_cast(component.size), + .componentId = component.componentId, + .componentType = Converter(static_cast(component.componentType)), + .descriptionInfo = Converter(component.descriptionInfo), + .displayVersion = component.displayVersion, + .effectiveMode = Converter(static_cast(component.effectiveMode)), + .innerVersion = component.innerVersion, + .upgradeAction = Converter(component.upgradeAction) + }; +} + +taihe::array AniCommonConverter::Converter( + const std::vector &components) +{ + std::vector taiheComponents; + taiheComponents.reserve(components.size()); + for (const auto &component : components) { + taiheComponents.push_back(Converter(component)); + } + return { taiheComponents }; +} + +ohos::update::UpgradePeriod AniCommonConverter::Converter(const UpgradePeriod &upgradePeriod) +{ + return { + .start = static_cast(upgradePeriod.start), + .end = static_cast(upgradePeriod.end) + }; +} + +taihe::array AniCommonConverter::Converter( + const std::vector &upgradePeriods) +{ + std::vector taiheUpgradePeriods; + taiheUpgradePeriods.reserve(upgradePeriods.size()); + for (const auto &period : upgradePeriods) { + taiheUpgradePeriods.push_back(Converter(period)); + } + return { taiheUpgradePeriods }; +} + +VersionDigestInfo AniCommonConverter::Converter(const ohos::update::VersionDigestInfo &versionDigestInfo) +{ + VersionDigestInfo res; + res.versionDigest = versionDigestInfo.versionDigest; + return res; +} + +DescriptionOptions AniCommonConverter::Converter(const ohos::update::DescriptionOptions &descriptionOptions) +{ + DescriptionOptions res; + res.format = static_cast(descriptionOptions.format.get_value()); + res.language = descriptionOptions.language; + return res; +} + +DownloadOptions AniCommonConverter::Converter(const ohos::update::DownloadOptions &downloadOptions) +{ + DownloadOptions res; + res.allowNetwork = static_cast(downloadOptions.allowNetwork.get_value()); + res.order = static_cast(downloadOptions.order.get_value()); + return res; +} + +ResumeDownloadOptions AniCommonConverter::Converter(const ohos::update::ResumeDownloadOptions &resumeDownloadOptions) +{ + ResumeDownloadOptions res; + res.allowNetwork = static_cast(resumeDownloadOptions.allowNetwork.get_value()); + return res; +} + +PauseDownloadOptions AniCommonConverter::Converter(const ohos::update::PauseDownloadOptions &pauseDownloadOptions) +{ + PauseDownloadOptions res; + res.isAllowAutoResume = pauseDownloadOptions.isAllowAutoResume; + return res; +} + +UpgradeOptions AniCommonConverter::Converter(const ohos::update::UpgradeOptions &upgradeOptions) +{ + UpgradeOptions res; + res.order = static_cast(upgradeOptions.order.get_value()); + return res; +} + +ClearOptions AniCommonConverter::Converter(const ohos::update::ClearOptions &clearOptions) +{ + ClearOptions res; + res.status = static_cast(clearOptions.status.get_value()); + return res; +} + +UpgradeFile AniCommonConverter::Converter(const ohos::update::UpgradeFile &upgradeFile) +{ + UpgradeFile res; + res.filePath = upgradeFile.filePath; + res.fileType = static_cast(upgradeFile.fileType.get_value()); + return res; +} + +UpgradeInfo AniCommonConverter::Converter(const ohos::update::UpgradeInfo &info) +{ + UpgradeInfo upgradeInfo; + upgradeInfo.upgradeApp = info.upgradeApp; + upgradeInfo.businessType = Converter(info.businessType); + return upgradeInfo; +} + +BusinessType AniCommonConverter::Converter(const ohos::update::BusinessType &businessType) +{ + BusinessType business; + business.vendor = businessType.vendor; + business.subType = static_cast(businessType.subType.get_value()); + return business; +} + +UpgradePeriod AniCommonConverter::Converter(const ohos::update::UpgradePeriod &period) +{ + UpgradePeriod upgradePeriod; + upgradePeriod.start = period.start; + upgradePeriod.end = period.end; + return upgradePeriod; +} + +UpgradePolicy AniCommonConverter::Converter(const ohos::update::UpgradePolicy &policy) +{ + UpgradePolicy upgradePolicy; + upgradePolicy.downloadStrategy = policy.downloadStrategy; + upgradePolicy.autoUpgradeStrategy = policy.autoUpgradeStrategy; + const std::size_t range = std::min(policy.autoUpgradePeriods.size(), std::size(upgradePolicy.autoUpgradePeriods)); + for (std::size_t index = 0; index < range; ++index) { + upgradePolicy.autoUpgradePeriods[index] = Converter(policy.autoUpgradePeriods[index]); + } + return upgradePolicy; +} + +ohos::update::EventId AniCommonConverter::Converter(const EventId &effectiveMode) +{ + return ohos::update::EventId::from_value(static_cast(effectiveMode)); +} + +ohos::update::EventInfo AniCommonConverter::Converter(const EventInfo &info) +{ + return { + .eventId = Converter(info.eventId), + .taskBody = Converter(info.taskBody) + }; +} +} diff --git a/frameworks/js/ani/src/ani_constructor.cpp b/frameworks/js/ani/src/ani_constructor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38a6db1481040e6d16fa25f513df908e6a05f67a --- /dev/null +++ b/frameworks/js/ani/src/ani_constructor.cpp @@ -0,0 +1,30 @@ +/* + * 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 "taihe/runtime.hpp" +#include "ohos.update.ani.hpp" + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != ohos::update::ANIRegister(env)) { + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/frameworks/js/ani/src/ani_local_updater.cpp b/frameworks/js/ani/src/ani_local_updater.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb20bab36d13d47911a2ec23eace16c9e8b40cd8 --- /dev/null +++ b/frameworks/js/ani/src/ani_local_updater.cpp @@ -0,0 +1,47 @@ +/* + * 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 "ani_local_updater.h" + +#include "taihe/runtime.hpp" + +#include "ani_common_conveter.h" +#include "update_service_kits.h" +#include "upgrade_file.h" + +namespace OHOS::UpdateEngine { +void AniLocalUpdater::VerifyUpgradePackageSync(const ohos::update::UpgradeFile &upgradeFile, + taihe::string_view certsFile) +{ + BusinessError error; + const int32_t ret = UpdateServiceKits::GetInstance().VerifyUpgradePackage(std::string(upgradeFile.filePath), + std::string(certsFile), error); + SetError(ret, "verifyUpgradePackage", error); +} + +void AniLocalUpdater::ApplyNewVersionSync(const taihe::array_view &upgradeFiles) +{ + BusinessError error; + UpgradeInfo upgradeInfo; + upgradeInfo.upgradeApp = LOCAL_UPGRADE_INFO; + const std::string miscFile = "/dev/block/by-name/misc"; + std::vector packageNames; + for (const auto &value : upgradeFiles) { + packageNames.emplace_back(AniCommonConverter::Converter(value).filePath); + } + const int32_t ret = UpdateServiceKits::GetInstance().ApplyNewVersion(upgradeInfo, miscFile, packageNames, error); + SetError(ret, "applyNewVersion", error); +} +} diff --git a/frameworks/js/ani/src/ani_restorer.cpp b/frameworks/js/ani/src/ani_restorer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b76d04a0eef6f772c4296998e3c9d64f06c7cd76 --- /dev/null +++ b/frameworks/js/ani/src/ani_restorer.cpp @@ -0,0 +1,32 @@ +/* + * 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 "ani_restorer.h" + +#include "update_service_kits.h" + +namespace OHOS::UpdateEngine { +void AniRestorer::FactoryResetSync() +{ + BusinessError error; + const int32_t ret = UpdateServiceKits::GetInstance().FactoryReset(error); + SetError(ret, "factoryReset", error); +} + +std::string AniRestorer::GetPermissionName() +{ + return "ohos.permission.FACTORY_RESET"; +} +} // namespace OHOS::UpdateEngine diff --git a/frameworks/js/ani/src/ani_updater.cpp b/frameworks/js/ani/src/ani_updater.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48003de94306efd00e42180304b3a5ad858271ad --- /dev/null +++ b/frameworks/js/ani/src/ani_updater.cpp @@ -0,0 +1,191 @@ +/* + * 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 "ani_updater.h" + +#include + +#include "taihe/runtime.hpp" + +#include "ani_common_conveter.h" +#include "task_info.h" +#include "update_service_kits.h" + +namespace OHOS::UpdateEngine { +AniUpdater::AniUpdater(const UpgradeInfo &info) : upgradeInfo_(info) +{ + ENGINE_LOGI("AniUpdater construct upgradeInfo = %{public}s", info.ToString().c_str()); +} + +ohos::update::CheckResult AniUpdater::CheckNewVersionSync() +{ + BusinessError error; + CheckResult result; + const auto ret = UpdateServiceKits::GetInstance().CheckNewVersion(upgradeInfo_, error, result); + SetError(ret, "checkNewVersion", error); + return AniCommonConverter::Converter(result); +} + +ohos::update::NewVersionInfo AniUpdater::GetNewVersionInfoSync() +{ + BusinessError error; + NewVersionInfo info; + const auto ret = UpdateServiceKits::GetInstance().GetNewVersionInfo(upgradeInfo_, info, error); + SetError(ret, "getNewVersionInfo", error); + return AniCommonConverter::Converter(info); +} + +taihe::array AniUpdater::GetNewVersionDescriptionSync( + const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DescriptionOptions &descriptionOptions) +{ + BusinessError error; + VersionDescriptionInfo result; + const auto ret = UpdateServiceKits::GetInstance().GetNewVersionDescription(upgradeInfo_, + AniCommonConverter::Converter(versionDigestInfo), AniCommonConverter::Converter(descriptionOptions), result, + error); + SetError(ret, "getNewVersionDescription", error); + return AniCommonConverter::Converter(result.componentDescriptions); +} + +ohos::update::CurrentVersionInfo AniUpdater::GetCurrentVersionInfoSync() +{ + BusinessError error; + CurrentVersionInfo info; + const auto ret = UpdateServiceKits::GetInstance().GetCurrentVersionInfo(upgradeInfo_, info, error); + SetError(ret, "getCurrentVersionInfo", error); + return AniCommonConverter::Converter(info); +} + +taihe::array AniUpdater::GetCurrentVersionDescriptionSync( + const ohos::update::DescriptionOptions &descriptionOptions) +{ + VersionDescriptionInfo currentVersionDescriptionInfo; + BusinessError error; + const auto ret = UpdateServiceKits::GetInstance().GetCurrentVersionDescription(upgradeInfo_, + AniCommonConverter::Converter(descriptionOptions), currentVersionDescriptionInfo, error); + SetError(ret, "getCurrentVersionDescription", error); + return AniCommonConverter::Converter(currentVersionDescriptionInfo.componentDescriptions); +} + +ohos::update::TaskInfo AniUpdater::GetTaskInfoSync() +{ + TaskInfo taskInfo; + BusinessError businessError; + const auto ret = UpdateServiceKits::GetInstance().GetTaskInfo(upgradeInfo_, taskInfo, businessError); + SetError(ret, "getTaskInfo", businessError); + return AniCommonConverter::Converter(taskInfo); +} + +void AniUpdater::DownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DownloadOptions &downloadOptions) +{ + BusinessError businessError; + const auto ret = + UpdateServiceKits::GetInstance().Download(upgradeInfo_, AniCommonConverter::Converter(versionDigestInfo), + AniCommonConverter::Converter(downloadOptions), businessError); + SetError(ret, "download", businessError); +} + +void AniUpdater::ResumeDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ResumeDownloadOptions &resumeDownloadOptions) +{ + BusinessError businessError; + const auto ret = + UpdateServiceKits::GetInstance().ResumeDownload(upgradeInfo_, AniCommonConverter::Converter(versionDigestInfo), + AniCommonConverter::Converter(resumeDownloadOptions), businessError); + SetError(ret, "resumeDownload", businessError); +} + +void AniUpdater::PauseDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::PauseDownloadOptions &pauseDownloadOptions) +{ + BusinessError businessError; + const auto ret = + UpdateServiceKits::GetInstance().PauseDownload(upgradeInfo_, AniCommonConverter::Converter(versionDigestInfo), + AniCommonConverter::Converter(pauseDownloadOptions), businessError); + SetError(ret, "pauseDownload", businessError); +} + +void AniUpdater::UpgradeSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::UpgradeOptions &upgradeOptions) +{ + BusinessError businessError; + const auto ret = UpdateServiceKits::GetInstance().Upgrade(upgradeInfo_, + AniCommonConverter::Converter(versionDigestInfo), AniCommonConverter::Converter(upgradeOptions), businessError); + SetError(ret, "upgrade", businessError); +} + +void AniUpdater::ClearErrorSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ClearOptions &clearOptions) +{ + BusinessError businessError; + const auto ret = UpdateServiceKits::GetInstance().ClearError(upgradeInfo_, + AniCommonConverter::Converter(versionDigestInfo), AniCommonConverter::Converter(clearOptions), businessError); + SetError(ret, "clearError", businessError); +} + +ohos::update::UpgradePolicy AniUpdater::GetUpgradePolicySync() +{ + BusinessError businessError; + UpgradePolicy policy; + + const auto ret = UpdateServiceKits::GetInstance().GetUpgradePolicy(upgradeInfo_, policy, businessError); + SetError(ret, "getUpgradePolicy", businessError); + return AniCommonConverter::Converter(policy); +} + +void AniUpdater::SetUpgradePolicySync(const ohos::update::UpgradePolicy &policy) +{ + BusinessError businessError; + const auto ret = UpdateServiceKits::GetInstance().SetUpgradePolicy(upgradeInfo_, + AniCommonConverter::Converter(policy), businessError); + SetError(ret, "setUpgradePolicy", businessError); +} + +void AniUpdater::TerminateUpgradeSync() +{ + BusinessError businessError; + const auto ret = UpdateServiceKits::GetInstance().TerminateUpgrade(upgradeInfo_, businessError); + SetError(ret, "TerminateUpgradeSync", businessError); +} + +void AniUpdater::RegisterCallback() +{ + ENGINE_LOGI("AniUpdater::RegisterCallback"); + const UpdateCallbackInfo callback{ [=](const EventInfo &eventInfo) { CallbackEventInfo(eventInfo); } }; + constexpr int32_t sleepDuration = 10; // 回调注册失败后再次尝试之前的等待时间,单位:毫秒 + constexpr int32_t maxRetryTimes = 5; // 回调注册失败最大尝试次数 + int32_t retryTimes = 0; + do { + int32_t ret = UpdateServiceKits::GetInstance().RegisterUpdateCallback(upgradeInfo_, callback); + if (ret == INT_CALL_SUCCESS) { + break; + } + + if (retryTimes++ < maxRetryTimes) { + ENGINE_LOGI("InitCallback fail, will retry after %{public}d milliseconds", sleepDuration); + std::this_thread::sleep_for(std::chrono::milliseconds(sleepDuration)); + } else { + ENGINE_LOGE("InitCallback fail after retry %{public}d times", retryTimes); + } + } while (retryTimes < maxRetryTimes); +} + +void AniUpdater::UnRegisterCallback() +{ + UpdateServiceKits::GetInstance().UnregisterUpdateCallback(upgradeInfo_); +} +} diff --git a/frameworks/js/ani/src/taihe_impl.cpp b/frameworks/js/ani/src/taihe_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36c9bcafd08eb0634e07bfa51f41fef3bfd2ced5 --- /dev/null +++ b/frameworks/js/ani/src/taihe_impl.cpp @@ -0,0 +1,239 @@ +/* + * 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 "ohos.update.proj.hpp" +#include "ohos.update.impl.hpp" +#include "taihe/runtime.hpp" + +#include "ani_common_conveter.h" +#include "ani_local_updater.h" +#include "ani_restorer.h" +#include "ani_updater.h" + +using namespace OHOS::UpdateEngine; +namespace { +class UpdaterImpl { +public: + explicit UpdaterImpl(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {} + + ohos::update::CheckResult CheckNewVersionSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->CheckNewVersionSync(); + } + + ohos::update::NewVersionInfo GetNewVersionInfoSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetNewVersionInfoSync(); + } + + taihe::array GetNewVersionDescriptionSync( + const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DescriptionOptions &descriptionOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetNewVersionDescriptionSync(versionDigestInfo, descriptionOptions); + } + + ohos::update::CurrentVersionInfo GetCurrentVersionInfoSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetCurrentVersionInfoSync(); + } + + taihe::array GetCurrentVersionDescriptionSync( + ohos::update::DescriptionOptions const & descriptionOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetCurrentVersionDescriptionSync(descriptionOptions); + } + + ohos::update::TaskInfo GetTaskInfoSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetTaskInfoSync(); + } + + void DownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::DownloadOptions &downloadOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->DownloadSync(versionDigestInfo, downloadOptions); + } + + void ResumeDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ResumeDownloadOptions &resumeDownloadOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->ResumeDownloadSync(versionDigestInfo, resumeDownloadOptions); + } + + void PauseDownloadSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::PauseDownloadOptions &pauseDownloadOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->PauseDownloadSync(versionDigestInfo, pauseDownloadOptions); + } + + void UpgradeSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::UpgradeOptions &upgradeOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->UpgradeSync(versionDigestInfo, upgradeOptions); + } + + void ClearErrorSync(const ohos::update::VersionDigestInfo &versionDigestInfo, + const ohos::update::ClearOptions &clearOptions) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->ClearErrorSync(versionDigestInfo, clearOptions); + } + + ohos::update::UpgradePolicy GetUpgradePolicySync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + return GetAniUpdater()->GetUpgradePolicySync(); + } + + void SetUpgradePolicySync(const ohos::update::UpgradePolicy &policy) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->SetUpgradePolicySync(policy); + } + + void TerminateUpgradeSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->TerminateUpgradeSync(); + } + + void On(const ohos::update::EventClassifyInfo &eventClassifyInfo, + taihe::callback_view taskCallback) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetAniUpdater()->On(taskCallback); + } + + void Off(const ::ohos::update::EventClassifyInfo &eventClassifyInfo, + taihe::optional_view> taskCallback) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + if (taskCallback) { + GetAniUpdater()->Off(*taskCallback); + return; + } + GetAniUpdater()->OffAll(); + } + +private: + std::shared_ptr GetAniUpdater() + { + if (!aniUpdatersMap_.count(upgradeInfo_)) { + aniUpdatersMap_[upgradeInfo_] = std::make_shared(upgradeInfo_); + } + return aniUpdatersMap_[upgradeInfo_]; + } + + static inline std::map> aniUpdatersMap_; + UpgradeInfo upgradeInfo_; +}; + +class RestorerImpl { +public: + RestorerImpl() = default; + + void FactoryResetSync() + { + ENGINE_LOGI("%{public}s method invoked", __func__); + if (aniRestorer_ == nullptr) { + aniRestorer_ = std::make_shared(); + } + aniRestorer_->FactoryResetSync(); + } + +private: + static inline std::shared_ptr aniRestorer_ = nullptr; +}; + +class LocalUpdaterImpl { +public: + LocalUpdaterImpl() = default; + + void VerifyUpgradePackageSync(const ohos::update::UpgradeFile &upgradeFile, taihe::string_view certsFile) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetLocalUpdater()->VerifyUpgradePackageSync(upgradeFile, certsFile); + } + + void ApplyNewVersionSync(taihe::array_view upgradeFiles) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetLocalUpdater()->ApplyNewVersionSync(upgradeFiles); + } + + void On(const ohos::update::EventClassifyInfo &eventClassifyInfo, + taihe::callback_view taskCallback) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + GetLocalUpdater()->On(taskCallback); + } + + void Off(const ::ohos::update::EventClassifyInfo &eventClassifyInfo, + taihe::optional_view> taskCallback) + { + ENGINE_LOGI("%{public}s method invoked", __func__); + if (taskCallback) { + GetLocalUpdater()->Off(*taskCallback); + return; + } + GetLocalUpdater()->OffAll(); + } + +private: + std::shared_ptr GetLocalUpdater() + { + if (localUpdater_ == nullptr) { + localUpdater_ = std::make_shared(); + } + return localUpdater_; + } + + static inline std::shared_ptr localUpdater_ = nullptr; +}; + +ohos::update::Updater GetOnlineUpdater(const ohos::update::UpgradeInfo &upgradeInfo) +{ + const auto &info = AniCommonConverter::Converter(upgradeInfo); + return taihe::make_holder(info); +} + +ohos::update::Restorer GetRestorer() +{ + return taihe::make_holder(); +} + +ohos::update::LocalUpdater GetLocalUpdater() +{ + return taihe::make_holder(); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_GetOnlineUpdater(GetOnlineUpdater); +TH_EXPORT_CPP_API_GetRestorer(GetRestorer); +TH_EXPORT_CPP_API_GetLocalUpdater(GetLocalUpdater); +// NOLINTEND