1 /*
2  * Copyright (c) 2022-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 #ifndef HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_HOLDER_H
16 #define HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_HOLDER_H
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "hiappevent_base.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 struct AppEventPackage {
AppEventPackageAppEventPackage29     AppEventPackage() : packageId(0), row(0), size(0), data(0), events(0) {}
~AppEventPackageAppEventPackage30     ~AppEventPackage() {}
31     int packageId;
32     int row;
33     int size;
34     std::vector<std::string> data;
35     std::vector<std::shared_ptr<AppEventPack>> events;
36 };
37 
38 class NapiAppEventHolder {
39 public:
40     NapiAppEventHolder(const std::string& name, int64_t observerSeq = -1);
~NapiAppEventHolder()41     ~NapiAppEventHolder() {}
42     static napi_value NapiConstructor(napi_env env, napi_callback_info info);
43     static napi_value NapiExport(napi_env env, napi_value exports);
44     static napi_value NapiSetRow(napi_env env, napi_callback_info info);
45     static napi_value NapiSetSize(napi_env env, napi_callback_info info);
46     static napi_value NapiTakeNext(napi_env env, napi_callback_info info);
47 
48     void SetRow(int row);
49     void SetSize(int size);
50     std::shared_ptr<AppEventPackage> TakeNext();
51 
52 public:
53     static thread_local napi_ref constructor_;
54 
55 private:
56     std::string name_;
57     int takeRow_;
58     int takeSize_;
59     int packageId_;
60     int64_t observerSeq_;
61     bool hasSetRow_;
62     bool hasSetSize_;
63 };
64 } // namespace HiviewDFX
65 } // namespace OHOS
66 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_HOLDER_H
67