• 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 #ifndef NAPI_SESSION_H
17 #define NAPI_SESSION_H
18 
19 #include <condition_variable>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "base_client.h"
27 #include "base_session.h"
28 #include "napi_common_define.h"
29 #include "napi_common_utils.h"
30 #include "napi_structs_base.h"
31 
32 namespace OHOS::UpdateEngine {
33 class NapiSession : public BaseSession {
34 public:
35     NapiSession(BaseClient *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber);
36 
~NapiSession()37     virtual ~NapiSession() {}
38 
39     napi_value StartWork(napi_env env, const napi_value *args, DoWorkFunction worker, void *context) override;
40 
GetNapiClient()41     BaseClient* GetNapiClient() const
42     {
43         return client_;
44     }
45 
GetType()46     uint32_t GetType() const override
47     {
48         return sessionParams_.type;
49     }
50 
GetSessionId()51     uint32_t GetSessionId() const override
52     {
53         return sessionId;
54     }
55 
OnAsyncComplete(const BusinessError & businessError)56     void OnAsyncComplete(const BusinessError &businessError) final
57     {
58         std::lock_guard<std::mutex> lock(conditionVariableMutex_);
59         businessError_ = businessError;
60         asyncExecuteComplete_ = true;
61         conditionVariable_.notify_all();
62     }
63 
CompleteWork(napi_env env,napi_status status)64     virtual void CompleteWork(napi_env env, napi_status status) {}
65     virtual void ExecuteWork(napi_env env);
66     virtual napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) = 0;
67 
68     static void CompleteWork(napi_env env, napi_status status, void *data);
69     static void ExecuteWork(napi_env env, void *data);
70 
IsAsyncCompleteWork()71     bool IsAsyncCompleteWork() override
72     {
73         return false;
74     }
75 
76 protected:
77     napi_value CreateWorkerName(napi_env env) const;
78 
IsWorkExecuteSuccess()79     bool IsWorkExecuteSuccess() const
80     {
81         return workResult_ == INT_CALL_SUCCESS;
82     }
83 
GetFunctionName()84     virtual std::string GetFunctionName()
85     {
86         return "";
87     }
88 
GetFunctionPermissionName()89     virtual std::string GetFunctionPermissionName()
90     {
91         return "";
92     }
93 
BuildWorkBusinessErr(BusinessError & businessError)94     void BuildWorkBusinessErr(BusinessError &businessError)
95     {
96         std::string msg = "execute error";
97         std::string funcName;
98         std::string permissionName;
99         switch (workResult_) {
100             case INT_NOT_SYSTEM_APP:
101                 msg = "BusinessError " + std::to_string(workResult_) + ": Caller not system app.";
102                 break;
103             case INT_APP_NOT_GRANTED:
104                 GetSessionFuncParameter(funcName, permissionName);
105                 msg = "BusinessError " + std::to_string(workResult_) + ": Permission denied. An attempt was made to " +
106                       funcName + " forbidden by permission: " + permissionName + ".";
107                 break;
108             case INT_CALL_IPC_ERR:
109                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": IPC error.";
110                 break;
111             case INT_UN_SUPPORT:
112                 GetSessionFuncParameter(funcName, permissionName);
113                 msg = "BusinessError " + std::to_string(workResult_) + ": Capability not supported. " +
114                       "function " + funcName + " can not work correctly due to limited device capabilities.";
115                 break;
116             case INT_PARAM_ERR:
117                 msg = "param error";
118                 break;
119             case INT_CALL_FAIL:
120                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": Execute fail.";
121                 break;
122             case INT_FORBIDDEN:
123                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": Forbidden execution.";
124                 break;
125             case INT_DEV_UPG_INFO_ERR:
126                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": Device info error.";
127                 break;
128             case INT_TIME_OUT:
129                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": Execute timeout.";
130                 break;
131             case INT_DB_ERROR:
132                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": DB error.";
133                 break;
134             case INT_IO_ERROR:
135                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": IO error.";
136                 break;
137             case INT_NET_ERROR:
138                 msg = "BusinessError " + std::to_string(COMPONENT_ERR + workResult_) + ": Network error.";
139                 break;
140             default:
141                 break;
142         }
143         businessError.Build(static_cast<CallResult>(workResult_), msg);
144     }
145 
GetBusinessError(BusinessError & businessError,const NapiResult & result)146     void GetBusinessError(BusinessError &businessError, const NapiResult &result)
147     {
148         if (IsWorkExecuteSuccess()) {
149             businessError = result.businessError;
150         } else {
151             BuildWorkBusinessErr(businessError);
152         }
153     }
154 
155 protected:
156     uint32_t sessionId { 0 };
157     BaseClient *client_ = nullptr;
158     BusinessError businessError_ {};
159     SessionParams sessionParams_ {};
160     int32_t workResult_ = INT_CALL_SUCCESS;
161     size_t totalArgc_ = 0;
162     size_t callbackNumber_ = 0;
163     void* context_ {};
164     DoWorkFunction doWorker_ {};
165     std::condition_variable conditionVariable_;
166     std::mutex conditionVariableMutex_;
167     bool asyncExecuteComplete_ = false;
168 
169 private:
170     void GetSessionFuncParameter(std::string &funcName, std::string &permissionName);
171 };
172 } // namespace OHOS::UpdateEngine
173 #endif // NAPI_SESSION_H