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