1 /*
2 * Copyright (c) 2022 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 #include "local_updater.h"
16 #include "napi_util.h"
17 #include "update_service_kits.h"
18 #include "update_session.h"
19
20 namespace OHOS {
21 namespace UpdateEngine {
22 const std::string MISC_FILE = "/dev/block/by-name/misc";
NapiVerifyUpgradePackage(napi_env env,napi_callback_info info)23 napi_value LocalUpdater::Napi::NapiVerifyUpgradePackage(napi_env env, napi_callback_info info)
24 {
25 CLIENT_LOGI("LocalUpdater::Napi::NapiVerifyUpgradePackage");
26 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
27 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
28 return localUpdater->VerifyUpgradePackage(env, info);
29 }
30
NapiApplyNewVersion(napi_env env,napi_callback_info info)31 napi_value LocalUpdater::Napi::NapiApplyNewVersion(napi_env env, napi_callback_info info)
32 {
33 CLIENT_LOGI("LocalUpdater::Napi::NapiApplyNewVersion");
34 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
35 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
36 return localUpdater->ApplyNewVersion(env, info);
37 }
38
NapiOn(napi_env env,napi_callback_info info)39 napi_value LocalUpdater::Napi::NapiOn(napi_env env, napi_callback_info info)
40 {
41 CLIENT_LOGI("LocalUpdater::Napi::NapiOn");
42 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
43 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
44 return localUpdater->On(env, info);
45 }
46
NapiOff(napi_env env,napi_callback_info info)47 napi_value LocalUpdater::Napi::NapiOff(napi_env env, napi_callback_info info)
48 {
49 CLIENT_LOGI("LocalUpdater::Napi::NapiOff");
50 LocalUpdater* localUpdater = UnwrapJsObject<LocalUpdater>(env, info);
51 PARAM_CHECK_NAPI_CALL(env, localUpdater != nullptr, return nullptr, "Error get localUpdater");
52 return localUpdater->Off(env, info);
53 }
54
LocalUpdater(napi_env env,napi_value thisVar)55 LocalUpdater::LocalUpdater(napi_env env, napi_value thisVar)
56 {
57 napi_ref thisReference = nullptr;
58 napi_create_reference(env, thisVar, 1, &thisReference);
59 sessionsMgr_ = std::make_shared<SessionManager>(env, thisReference);
60 CLIENT_LOGI("LocalUpdater::LocalUpdater");
61 }
62
Init()63 void LocalUpdater::Init()
64 {
65 PARAM_CHECK(!isInit_, return, "local updater has init.");
66 UpdateCallbackInfo callback {
67 [](const BusinessError &businessError, const CheckResultEx &checkResultEx) {},
68 [this](const EventInfo &eventInfo) {
69 NotifyEventInfo(eventInfo);
70 },
71 };
72 CLIENT_LOGI("LocalUpdater::Init");
73 UpgradeInfo upgradeInfo;
74 upgradeInfo.upgradeApp = LOCAL_UPGRADE_INFO;
75 UpdateServiceKits::GetInstance().RegisterUpdateCallback(upgradeInfo, callback);
76 isInit_ = true;
77 }
78
VerifyUpgradePackage(napi_env env,napi_callback_info info)79 napi_value LocalUpdater::VerifyUpgradePackage(napi_env env, napi_callback_info info)
80 {
81 size_t argc = MAX_ARGC;
82 napi_value args[MAX_ARGC] = {0};
83 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
84 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
85 PARAM_CHECK_NAPI_CALL(env, argc >= ARG_NUM_TWO, return nullptr, "Error get cb info");
86
87 UpgradeFile upgradeFile;
88 ClientStatus ret = ClientHelper::GetUpgradeFileFromArg(env, args[0], upgradeFile);
89 if (ret != ClientStatus::CLIENT_SUCCESS) {
90 CLIENT_LOGE("VerifyUpgradePackage error, get upgradeFile fail");
91 return StartParamErrorSession(env, info, CALLBACK_POSITION_THREE);
92 }
93
94 std::string certsFile;
95 int32_t result = NapiUtil::GetString(env, args[1], certsFile);
96 if (result != CAST_INT(ClientStatus::CLIENT_SUCCESS)) {
97 CLIENT_LOGE("VerifyUpgradePackage error, get certsFile fail");
98 return StartParamErrorSession(env, info, CALLBACK_POSITION_THREE);
99 }
100
101 SessionParams sessionParams(SessionType::SESSION_VERIFY_PACKAGE, CALLBACK_POSITION_THREE, true);
102 napi_value retValue = StartSession(env, info, sessionParams,
103 [upgradeFile, certsFile](SessionType type, void *context) -> int {
104 CLIENT_LOGI("VerifyUpdatePackage StartWork %s, %s", upgradeFile.filePath.c_str(), certsFile.c_str());
105 BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
106 return UpdateServiceKits::GetInstance().VerifyUpgradePackage(upgradeFile.filePath, certsFile,
107 *businessError);
108 });
109 PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to VerifyUpgradePackage.");
110 return retValue;
111 }
112
ApplyNewVersion(napi_env env,napi_callback_info info)113 napi_value LocalUpdater::ApplyNewVersion(napi_env env, napi_callback_info info)
114 {
115 size_t argc = MAX_ARGC;
116 napi_value args[MAX_ARGC] = {0};
117 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
118 PARAM_CHECK_NAPI_CALL(env, status == napi_ok && argc >= ARG_NUM_ONE, return nullptr, "Error get cb info");
119
120 std::vector<UpgradeFile> upgradeFiles;
121 ClientStatus ret = ClientHelper::GetUpgradeFilesFromArg(env, args[0], upgradeFiles);
122 if ((ret != ClientStatus::CLIENT_SUCCESS) || (upgradeFiles.size() == 0)) {
123 CLIENT_LOGE("ApplyNewVersion error, get GetUpgradeFilesFromArg fail");
124 return StartParamErrorSession(env, info, CALLBACK_POSITION_TWO);
125 }
126
127 SessionParams sessionParams(SessionType::SESSION_APPLY_NEW_VERSION, CALLBACK_POSITION_TWO, true);
128 napi_value retValue = StartSession(env, info, sessionParams,
129 [upgradeFiles](SessionType type, void *context) -> int {
130 CLIENT_LOGI("ApplyNewVersion %s", upgradeFiles[0].filePath.c_str());
131 BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
132 UpgradeInfo upgradeInfo;
133 upgradeInfo.upgradeApp = LOCAL_UPGRADE_INFO;
134 return UpdateServiceKits::GetInstance().ApplyNewVersion(upgradeInfo, MISC_FILE, upgradeFiles[0].filePath,
135 *businessError);
136 });
137 PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to ApplyNewVersion");
138 return retValue;
139 }
140 } // namespace UpdateEngine
141 } // namespace OHOS
142