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 "napi_session.h"
17
18 #include "node_api.h"
19
20 #include "base_client.h"
21 #include "napi_common_define.h"
22 #include "napi_common_utils.h"
23
24 using namespace std;
25
26 namespace OHOS::UpdateEngine {
27 uint32_t g_sessionId = 0;
28
NapiSession(BaseClient * client,SessionParams & sessionParams,size_t argc,size_t callbackNumber)29 NapiSession::NapiSession(BaseClient *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber)
30 : sessionId(++g_sessionId), client_(client), sessionParams_(sessionParams), totalArgc_(argc),
31 callbackNumber_(callbackNumber) {}
32
CreateWorkerName(napi_env env) const33 napi_value NapiSession::CreateWorkerName(napi_env env) const
34 {
35 napi_value workName;
36 std::string name = "Async Work" + std::to_string(sessionId);
37 napi_status status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &workName);
38 PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to worker name");
39 return workName;
40 }
41
StartWork(napi_env env,const napi_value * args,DoWorkFunction worker,void * context)42 napi_value NapiSession::StartWork(napi_env env, const napi_value *args, DoWorkFunction worker, void *context)
43 {
44 ENGINE_LOGI("StartWork type: %{public}d", CAST_INT(sessionParams_.type));
45 doWorker_ = worker;
46 context_ = context;
47 return StartWork(env, sessionParams_.callbackStartIndex, args);
48 }
49
ExecuteWork(napi_env env)50 void NapiSession::ExecuteWork(napi_env env)
51 {
52 if (doWorker_ != nullptr) {
53 #ifndef UPDATER_UT
54 if (sessionParams_.isNeedBusinessError) {
55 workResult_ = doWorker_(&businessError_);
56 } else {
57 workResult_ = doWorker_(context_);
58 }
59 ENGINE_LOGI("UpdateSession::ExecuteWork workResult : %{public}d", workResult_);
60 if (sessionParams_.isAsyncCompleteWork && IsWorkExecuteSuccess()) {
61 // 异步搜包完成,需要把businessError设置进来或者超时,才能结束等待
62 std::unique_lock<std::mutex> lock(conditionVariableMutex_);
63 auto now = std::chrono::system_clock::now();
64 conditionVariable_.wait_until(lock, now + 40000ms, [this] { return asyncExecuteComplete_; });
65 ENGINE_LOGI("UpdateSession::ExecuteWork asyncExcuteComplete : %{public}s",
66 asyncExecuteComplete_ ? "true" : "false");
67 if (!asyncExecuteComplete_) {
68 businessError_.errorNum = CallResult::TIME_OUT;
69 }
70 }
71 #else
72 doWorker_(context_);
73 #endif
74 }
75 }
76
77 // JS thread, which is used to notify the JS page upon completion of the operation.
CompleteWork(napi_env env,napi_status status,void * data)78 void NapiSession::CompleteWork(napi_env env, napi_status status, void *data)
79 {
80 auto sess = reinterpret_cast<NapiSession *>(data);
81 PARAM_CHECK(sess != nullptr && sess->GetNapiClient() != nullptr, return, "Session is null pointer");
82 sess->CompleteWork(env, status);
83 // If the share ptr is used, you can directly remove the share ptr.
84 BaseClient *client = sess->GetNapiClient();
85 if (client != nullptr) {
86 client->RemoveSession(sess->GetSessionId());
87 }
88 }
89
90 // The C++ thread executes the synchronization operation. After the synchronization is complete,
91 // the CompleteWork is called to notify the JS page of the completion of the operation.
ExecuteWork(napi_env env,void * data)92 void NapiSession::ExecuteWork(napi_env env, void *data)
93 {
94 auto sess = reinterpret_cast<NapiSession *>(data);
95 PARAM_CHECK(sess != nullptr, return, "sess is null");
96 sess->ExecuteWork(env);
97 }
98
GetSessionFuncParameter(std::string & funcName,std::string & permissionName)99 void NapiSession::GetSessionFuncParameter(std::string &funcName, std::string &permissionName)
100 {
101 funcName = GetFunctionName();
102 permissionName = GetFunctionPermissionName();
103 }
104 } // namespace OHOS::UpdateEngine