1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "local_updater.h"
17
18 #include "napi_common_utils.h"
19 #include "update_define.h"
20 #include "update_service_kits.h"
21
22 namespace OHOS::UpdateEngine {
23 const std::string MISC_FILE = "/dev/block/by-name/misc";
24
NapiVerifyUpgradePackage(napi_env env,napi_callback_info info)25 napi_value LocalUpdater::Napi::NapiVerifyUpgradePackage(napi_env env, napi_callback_info info)
26 {
27 ENGINE_LOGI("LocalUpdater::Napi::NapiVerifyUpgradePackage");
28 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
29 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
30 return localUpdater->VerifyUpgradePackage(env, info);
31 }
32
NapiApplyNewVersion(napi_env env,napi_callback_info info)33 napi_value LocalUpdater::Napi::NapiApplyNewVersion(napi_env env, napi_callback_info info)
34 {
35 ENGINE_LOGI("LocalUpdater::Napi::NapiApplyNewVersion");
36 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
37 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
38 return localUpdater->ApplyNewVersion(env, info);
39 }
40
NapiOn(napi_env env,napi_callback_info info)41 napi_value LocalUpdater::Napi::NapiOn(napi_env env, napi_callback_info info)
42 {
43 ENGINE_LOGI("LocalUpdater::Napi::NapiOn");
44 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
45 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
46 return localUpdater->On(env, info);
47 }
48
NapiOff(napi_env env,napi_callback_info info)49 napi_value LocalUpdater::Napi::NapiOff(napi_env env, napi_callback_info info)
50 {
51 ENGINE_LOGI("LocalUpdater::Napi::NapiOff");
52 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
53 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
54 return localUpdater->Off(env, info);
55 }
56
LocalUpdater(napi_env env,napi_value thisVar)57 LocalUpdater::LocalUpdater(napi_env env, napi_value thisVar)
58 {
59 napi_ref thisReference = nullptr;
60 constexpr int32_t refCount = 1; // 新引用的初始引用计数
61 napi_create_reference(env, thisVar, refCount, &thisReference);
62 sessionsMgr_ = std::make_shared<SessionManager>(env, thisReference);
63 ENGINE_LOGI("LocalUpdater::LocalUpdater");
64 }
65
Init()66 void LocalUpdater::Init()
67 {
68 PARAM_CHECK(!isInit_, return, "local updater has init.");
69 UpdateCallbackInfo callback {
70 [this](const EventInfo &eventInfo) {
71 NotifyEventInfo(eventInfo);
72 },
73 };
74 ENGINE_LOGI("LocalUpdater::Init");
75 UpgradeInfo upgradeInfo;
76 upgradeInfo.upgradeApp = LOCAL_UPGRADE_INFO;
77 UpdateServiceKits::GetInstance().RegisterUpdateCallback(upgradeInfo, callback);
78 isInit_ = true;
79 }
80
VerifyUpgradePackage(napi_env env,napi_callback_info info)81 napi_value LocalUpdater::VerifyUpgradePackage(napi_env env, napi_callback_info info)
82 {
83 size_t argc = MAX_ARGC;
84 napi_value args[MAX_ARGC] = { 0 };
85 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
86 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
87 PARAM_CHECK_NAPI_CALL(env, argc >= ARG_NUM_TWO, return nullptr, "Error get cb info");
88
89 UpgradeFile upgradeFile;
90 ClientStatus ret = ClientHelper::GetUpgradeFileFromArg(env, args[0], upgradeFile);
91 if (ret != ClientStatus::CLIENT_SUCCESS) {
92 ENGINE_LOGE("VerifyUpgradePackage error, get upgradeFile fail");
93 return StartParamErrorSession(env, info, CALLBACK_POSITION_THREE);
94 }
95
96 std::string certsFile;
97 int32_t result = NapiCommonUtils::GetString(env, args[1], certsFile);
98 if (result != CAST_INT(ClientStatus::CLIENT_SUCCESS)) {
99 ENGINE_LOGE("VerifyUpgradePackage error, get certsFile fail");
100 return StartParamErrorSession(env, info, CALLBACK_POSITION_THREE);
101 }
102
103 SessionParams sessionParams(SessionType::SESSION_VERIFY_PACKAGE, CALLBACK_POSITION_THREE, true);
104 napi_value retValue = StartSession(env, info, sessionParams,
105 [upgradeFile, certsFile](void *context) -> int {
106 ENGINE_LOGI("VerifyUpdatePackage StartWork %s, %s", upgradeFile.filePath.c_str(), certsFile.c_str());
107 BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
108 return UpdateServiceKits::GetInstance().VerifyUpgradePackage(upgradeFile.filePath, certsFile,
109 *businessError);
110 });
111 PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to VerifyUpgradePackage.");
112 return retValue;
113 }
114
ApplyNewVersion(napi_env env,napi_callback_info info)115 napi_value LocalUpdater::ApplyNewVersion(napi_env env, napi_callback_info info)
116 {
117 size_t argc = MAX_ARGC;
118 napi_value args[MAX_ARGC] = { 0 };
119 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
120 PARAM_CHECK_NAPI_CALL(env, status == napi_ok && argc >= ARG_NUM_ONE, return nullptr, "Error get cb info");
121
122 std::vector<UpgradeFile> upgradeFiles;
123 ClientStatus ret = ClientHelper::GetUpgradeFilesFromArg(env, args[0], upgradeFiles);
124 if ((ret != ClientStatus::CLIENT_SUCCESS) || (upgradeFiles.size() == 0)) {
125 ENGINE_LOGE("ApplyNewVersion error, get GetUpgradeFilesFromArg fail");
126 return StartParamErrorSession(env, info, CALLBACK_POSITION_TWO);
127 }
128
129 SessionParams sessionParams(SessionType::SESSION_APPLY_NEW_VERSION, CALLBACK_POSITION_TWO, true);
130 napi_value retValue = StartSession(env, info, sessionParams,
131 [upgradeFiles](void *context) -> int {
132 ENGINE_LOGI("ApplyNewVersion %s", upgradeFiles[0].filePath.c_str());
133 BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
134 UpgradeInfo upgradeInfo;
135 upgradeInfo.upgradeApp = LOCAL_UPGRADE_INFO;
136 return UpdateServiceKits::GetInstance().ApplyNewVersion(upgradeInfo, MISC_FILE, upgradeFiles[0].filePath,
137 *businessError);
138 });
139 PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to ApplyNewVersion");
140 return retValue;
141 }
142 } // namespace OHOS::UpdateEngine