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