• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_helper.h"
20 #include "update_session.h"
21 
22 namespace OHOS::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         NapiCommonUtils::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<BaseSession> 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(
46         env, args,
47         [&](void *context) -> int {
48             RegisterCallback();
49             return 0;
50         },
51         nullptr);
52     PARAM_CHECK(retValue != nullptr, sessionsMgr_->RemoveSession(sess->GetSessionId()); return nullptr,
53         "Failed to SubscribeEvent.");
54     return retValue;
55 }
56 
Off(napi_env env,napi_callback_info info)57 napi_value IUpdater::Off(napi_env env, napi_callback_info info)
58 {
59     size_t argc = MAX_ARGC;
60     napi_value args[MAX_ARGC] = { 0 };
61     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
62     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
63 
64     EventClassifyInfo eventClassifyInfo;
65     ClientStatus ret = ClientHelper::GetEventClassifyInfoFromArg(env, args[0], eventClassifyInfo);
66     std::vector<std::pair<std::string, std::string>> paramInfos;
67     paramInfos.push_back({ "eventClassifyInfo", "EventClassifyInfo" });
68     PARAM_CHECK_NAPI_CALL(env, ret == ClientStatus::CLIENT_SUCCESS,
69         NapiCommonUtils::NapiThrowParamError(env, paramInfos);
70         return nullptr, "Error get eventClassifyInfo");
71 
72     napi_value handle = nullptr;
73     if (argc >= ARG_NUM_TWO) {
74         ret = NapiCommonUtils::IsTypeOf(env, args[1], napi_function);
75         std::vector<std::pair<std::string, std::string>> paramErrors;
76         paramErrors.push_back({ "callback", "napi_function" });
77         PARAM_CHECK_NAPI_CALL(env, ret == ClientStatus::CLIENT_SUCCESS,
78             NapiCommonUtils::NapiThrowParamError(env, paramErrors);
79             return nullptr, "invalid type");
80         handle = args[1];
81     }
82     sessionsMgr_->Unsubscribe(eventClassifyInfo, handle);
83     UnRegisterCallback();
84     napi_value result;
85     napi_create_int32(env, 0, &result);
86     return result;
87 }
88 
GetUpdateResult(uint32_t type,UpdateResult & result)89 void IUpdater::GetUpdateResult(uint32_t type, UpdateResult &result)
90 {
91     result.buildJSObject = ClientHelper::BuildUndefinedStatus;
92 }
93 
StartSession(napi_env env,napi_callback_info info,SessionParams & sessionParams,BaseSession::DoWorkFunction function)94 napi_value IUpdater::StartSession(napi_env env, napi_callback_info info, SessionParams &sessionParams,
95     BaseSession::DoWorkFunction function)
96 {
97     size_t argc = MAX_ARGC;
98     napi_value args[MAX_ARGC] = { 0 };
99     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
100     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
101 
102     ENGINE_LOGI("StartSession type %{public}d argc %{public}zu callbackStartIndex %{public}d",
103         static_cast<int32_t>(sessionParams.type), argc, static_cast<int>(sessionParams.callbackStartIndex));
104     std::shared_ptr<BaseSession> sess = nullptr;
105     if (argc > sessionParams.callbackStartIndex) {
106         sess = std::make_shared<UpdateAsyncession>(this, sessionParams, argc);
107     } else {
108         sess = std::make_shared<UpdatePromiseSession>(this, sessionParams, argc);
109     }
110     PARAM_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
111     sessionsMgr_->AddSession(sess);
112     napi_value retValue = sess->StartWork(env, args, function, nullptr);
113     PARAM_CHECK(retValue != nullptr, sessionsMgr_->RemoveSession(sess->GetSessionId()); return nullptr,
114         "Failed to start worker.");
115     return retValue;
116 }
117 
StartParamErrorSession(napi_env env,napi_callback_info info,CALLBACK_POSITION callbackPosition)118 napi_value IUpdater::StartParamErrorSession(napi_env env, napi_callback_info info, CALLBACK_POSITION callbackPosition)
119 {
120     SessionParams sessionParams(SessionType::SESSION_REPLY_PARAM_ERROR, callbackPosition, true);
121     return StartSession(env, info, sessionParams, [](void *context) -> int {
122             return INT_PARAM_ERR;
123         });
124 }
125 
NotifyEventInfo(const EventInfo & eventInfo)126 void IUpdater::NotifyEventInfo(const EventInfo &eventInfo)
127 {
128     ENGINE_LOGI("NotifyEventInfo 0x%{public}08x", eventInfo.eventId);
129     auto classify = EventClassify::TASK;
130     for (const auto item : g_eventClassifyList) {
131         if (CAST_UINT(eventInfo.eventId) & CAST_UINT(item)) {
132             classify = item;
133             break;
134         }
135     }
136     EventClassifyInfo eventClassifyInfo(classify);
137     sessionsMgr_->Emit(eventClassifyInfo, eventInfo);
138 }
139 } // namespace OHOS::UpdateEngine