• 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 HIDEBUG_ASYNCTASK_H_
17 #define HIDEBUG_ASYNCTASK_H_
18 
19 #include "napi/native_api.h"
20 
21 #include <cstdint>
22 #include <functional>
23 #include <string>
24 #include <type_traits>
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 
29 napi_value CreateErrorMessage(napi_env env, const std::string& msg);
30 
31 napi_value CreateErrorMessage(napi_env env, const std::string& errCode, const std::string& msg);
32 
33 napi_value CreateUndefined(napi_env env);
34 
35 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType);
36 
37 bool GetNapiArrayLength(napi_env env, napi_value value, uint32_t& ret);
38 
39 bool GetNapiInt32Value(napi_env env, napi_value value, int32_t& ret);
40 
41 bool GetNapiUint32Value(napi_env env, napi_value value, uint32_t& ret);
42 
43 bool GetNapiDoubleValue(napi_env env, napi_value value, double& ret);
44 
45 bool GetNapiBoolValue(napi_env env, napi_value value, bool& ret);
46 
47 bool GetNapiStringValue(napi_env env, napi_value value, std::string& ret, size_t maxSize);
48 
49 bool GetNapiObjectProperty(napi_env env, napi_value value, const std::string& propertyName, napi_value& ret);
50 
51 bool GetTheOnlyStringParam(napi_env env, napi_callback_info info, std::string &fileName);
52 
53 std::string GetFileNameParam(napi_env env, napi_callback_info info);
54 class AsyncTask {
55 public:
AsyncTask(const std::string & resourceName)56     explicit AsyncTask(const std::string& resourceName): resourceName_(resourceName) {};
57     virtual ~AsyncTask() = default;
58 
59     template<typename T, typename = typename std::enable_if<std::is_base_of<AsyncTask, T>::value>::type>
60     static napi_value GetPromise(napi_env env, std::function<void(T*)> setReqParam = [](T*) {})
61     {
62         napi_value promise = nullptr;
63         T* asyncTask = new (std::nothrow) T();
64         if (asyncTask == nullptr) {
65             return nullptr;
66         }
67         setReqParam(asyncTask);
68         if (!asyncTask->CreatePromise(env, promise)) {
69             delete asyncTask;
70             return nullptr;
71         }
72         return promise;
73     };
74 
75 protected:
76     napi_async_work worker_ = nullptr;
77     napi_deferred deferred_ = nullptr;
78     std::string resourceName_;
79     virtual void Work(napi_env env) = 0;
80     virtual void Done(napi_env env, napi_status status) = 0;
81 
82 private:
83     bool CreatePromise(napi_env env, napi_value& promise);
84     static void ExecuteCallBack(napi_env env, void* data);
85     static void CompletedCallBack(napi_env env, napi_status status, void* data);
86 };
87 }
88 }
89 
90 #endif //HIDEBUG_ASYNCTASK_H_
91