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