1 /*
2  * Copyright (c) 2024 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 "iupdater.h"
17 
18 #include "napi_common_define.h"
19 #include "update_session.h"
20 
21 namespace OHOS::UpdateEngine {
On(napi_env env,napi_callback_info info)22 napi_value IUpdater::On(napi_env env, napi_callback_info info)
23 {
24     size_t argc = MAX_ARGC;
25     napi_value args[MAX_ARGC] = { 0 };
26     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
27     PARAM_CHECK_NAPI_CALL(env, status == napi_ok && argc >= ARG_NUM_TWO, return nullptr, "Error get cb info");
28 
29     bool isCallerValid = NapiCommonUtils::IsCallerValid();
30     PARAM_CHECK_NAPI_CALL(env, isCallerValid, NapiCommonUtils::NapiThrowNotSystemAppError(env);
31         return nullptr, "Caller not system app.");
32 
33     EventClassifyInfo eventClassifyInfo;
34     ClientStatus ret = ClientHelper::GetEventClassifyInfoFromArg(env, args[0], eventClassifyInfo);
35     std::vector<std::pair<std::string, std::string>> paramInfos;
36     paramInfos.push_back({ "eventClassifyInfo", "EventClassifyInfo" });
37     PARAM_CHECK_NAPI_CALL(env, ret == ClientStatus::CLIENT_SUCCESS,
38         NapiCommonUtils::NapiThrowParamError(env, paramInfos);
39         return nullptr, "Error get eventClassifyInfo");
40     PARAM_CHECK(sessionsMgr_->FindSessionByHandle(env, eventClassifyInfo, args[1]) == nullptr, return nullptr,
41         "Handle has been sub");
42 
43     SessionParams sessionParams(SessionType::SESSION_SUBSCRIBE, CALLBACK_POSITION_TWO);
44     std::shared_ptr<BaseSession> sess = std::make_shared<UpdateListener>(this, sessionParams, argc, false);
45     PARAM_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create listener");
46 
47     sessionsMgr_->AddSession(sess);
48     napi_value retValue = sess->StartWork(
49         env, args,
50         [&](void *context) -> int {
51             RegisterCallback();
52             return 0;
53         },
54         nullptr);
55     PARAM_CHECK(retValue != nullptr, sessionsMgr_->RemoveSession(sess->GetSessionId()); return nullptr,
56         "Failed to SubscribeEvent.");
57     return retValue;
58 }
59 
Off(napi_env env,napi_callback_info info)60 napi_value IUpdater::Off(napi_env env, napi_callback_info info)
61 {
62     size_t argc = MAX_ARGC;
63     napi_value args[MAX_ARGC] = { 0 };
64     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
65     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
66 
67     bool isCallerValid = NapiCommonUtils::IsCallerValid();
68     PARAM_CHECK_NAPI_CALL(env, isCallerValid, NapiCommonUtils::NapiThrowNotSystemAppError(env);
69         return nullptr, "Caller not system app.");
70 
71     EventClassifyInfo eventClassifyInfo;
72     ClientStatus ret = ClientHelper::GetEventClassifyInfoFromArg(env, args[0], eventClassifyInfo);
73     std::vector<std::pair<std::string, std::string>> paramInfos;
74     paramInfos.push_back({ "eventClassifyInfo", "EventClassifyInfo" });
75     PARAM_CHECK_NAPI_CALL(env, ret == ClientStatus::CLIENT_SUCCESS,
76         NapiCommonUtils::NapiThrowParamError(env, paramInfos);
77         return nullptr, "Error get eventClassifyInfo");
78 
79     napi_value handle = nullptr;
80     if (argc >= ARG_NUM_TWO) {
81         ret = NapiCommonUtils::IsTypeOf(env, args[1], napi_function);
82         std::vector<std::pair<std::string, std::string>> paramErrors;
83         paramErrors.push_back({ "callback", "napi_function" });
84         PARAM_CHECK_NAPI_CALL(env, ret == ClientStatus::CLIENT_SUCCESS,
85             NapiCommonUtils::NapiThrowParamError(env, paramErrors);
86             return nullptr, "invalid type");
87         handle = args[1];
88     }
89     sessionsMgr_->Unsubscribe(eventClassifyInfo, handle);
90     UnRegisterCallback();
91     napi_value result;
92     napi_create_int32(env, 0, &result);
93     return result;
94 }
95 
GetUpdateResult(uint32_t type,UpdateResult & result)96 void IUpdater::GetUpdateResult(uint32_t type, UpdateResult &result)
97 {
98     result.buildJSObject = ClientHelper::BuildUndefinedStatus;
99 }
100 
StartSession(napi_env env,napi_callback_info info,SessionParams & sessionParams,BaseSession::DoWorkFunction function)101 napi_value IUpdater::StartSession(napi_env env, napi_callback_info info, SessionParams &sessionParams,
102     BaseSession::DoWorkFunction function)
103 {
104     size_t argc = MAX_ARGC;
105     napi_value args[MAX_ARGC] = { 0 };
106     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
107     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
108 
109     ENGINE_LOGI("StartSession type %{public}d argc %{public}zu callbackStartIndex %{public}d",
110         static_cast<int32_t>(sessionParams.type), argc, static_cast<int>(sessionParams.callbackStartIndex));
111     std::shared_ptr<BaseSession> sess = nullptr;
112     if (argc > sessionParams.callbackStartIndex) {
113         sess = std::make_shared<UpdateAsyncession>(this, sessionParams, argc);
114     } else {
115         sess = std::make_shared<UpdatePromiseSession>(this, sessionParams, argc);
116     }
117     PARAM_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
118     sessionsMgr_->AddSession(sess);
119     napi_value retValue = sess->StartWork(env, args, function, nullptr);
120     PARAM_CHECK(retValue != nullptr, sessionsMgr_->RemoveSession(sess->GetSessionId()); return nullptr,
121         "Failed to start worker.");
122     return retValue;
123 }
124 
StartParamErrorSession(napi_env env,napi_callback_info info,CALLBACK_POSITION callbackPosition)125 napi_value IUpdater::StartParamErrorSession(napi_env env, napi_callback_info info, CALLBACK_POSITION callbackPosition)
126 {
127     SessionParams sessionParams(SessionType::SESSION_REPLY_PARAM_ERROR, callbackPosition, true);
128     return StartSession(env, info, sessionParams, [](void *context) -> int {
129             return INT_PARAM_ERR;
130         });
131 }
132 
NotifyEventInfo(const EventInfo & eventInfo)133 void IUpdater::NotifyEventInfo(const EventInfo &eventInfo)
134 {
135     ENGINE_LOGI("NotifyEventInfo 0x%{public}08x", eventInfo.eventId);
136     auto classify = EventClassify::TASK;
137     for (const auto item : g_eventClassifyList) {
138         if (CAST_UINT(eventInfo.eventId) & CAST_UINT(item)) {
139             classify = item;
140             break;
141         }
142     }
143     EventClassifyInfo eventClassifyInfo(classify);
144     sessionsMgr_->Emit(eventClassifyInfo, eventInfo);
145 }
146 } // namespace OHOS::UpdateEngine