1 /*
2  * Copyright (c) 2023 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 #ifndef BASE_PROMISE_SESSION_H
17 #define BASE_PROMISE_SESSION_H
18 
19 #include "napi/native_api.h"
20 #include "node_api.h"
21 
22 #include "napi_session.h"
23 
24 namespace OHOS::UpdateEngine {
25 template <typename RESULT> class BasePromiseSession : public NapiSession {
26 public:
27     BasePromiseSession(BaseClient *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0)
28         : NapiSession(client, sessionParams, argc, callbackNumber) {}
29 
30     ~BasePromiseSession() override = default;
31 
StartWork(napi_env env,size_t startIndex,const napi_value * args)32     napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override
33     {
34         PARAM_CHECK_NAPI_CALL(env, args != nullptr, return nullptr, "Invalid para");
35         napi_value workName = CreateWorkerName(env);
36         PARAM_CHECK_NAPI_CALL(env, workName != nullptr, return nullptr, "Failed to worker name");
37 
38         napi_value promise;
39         napi_status status = napi_create_promise(env, &deferred_, &promise);
40         PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_create_promise");
41 
42         // Create an asynchronous call.
43         status = napi_create_async_work(env, nullptr, workName, NapiSession::ExecuteWork, NapiSession::CompleteWork,
44             this, &(worker_));
45         PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_create_async_work");
46         // Put the thread in the task execution queue.
47         status = napi_queue_async_work_with_qos(env, worker_, napi_qos_default);
48         PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_queue_async_work");
49         return promise;
50     }
51 
NotifyJS(napi_env env,napi_value thisVar,const RESULT & result)52     void NotifyJS(napi_env env, napi_value thisVar, const RESULT &result)
53     {
54         int32_t errorNum = static_cast<int32_t>(result.businessError.errorNum);
55         if (errorNum != 0) {
56             ENGINE_LOGI("BasePromiseSession NotifyJS errorNum:%{public}d", errorNum);
57         }
58 
59         // Get the return result.
60         napi_value processResult = nullptr;
61         BusinessError businessError;
62         GetBusinessError(businessError, result);
63         if (businessError.errorNum == CallResult::SUCCESS) {
64             result.buildJSObject(env, processResult, result);
65             napi_resolve_deferred(env, deferred_, processResult);
66         } else {
67             NapiCommonUtils::BuildBusinessError(env, processResult, businessError);
68             napi_reject_deferred(env, deferred_, processResult);
69         }
70         napi_delete_async_work(env, worker_);
71         worker_ = nullptr;
72     }
73 
CompleteWork(napi_env env,napi_status status)74     virtual void CompleteWork(napi_env env, napi_status status) override {}
75 
76 protected:
77     napi_async_work worker_ = nullptr;
78     napi_deferred deferred_ = nullptr;
79 };
80 } // namespace OHOS::UpdateEngine
81 #endif // BASE_PROMISE_SESSION_H