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 FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_STORAGE_JSSTORAGE_H
17 #define FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_STORAGE_JSSTORAGE_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 
22 #include "securec.h"
23 
24 #include <map>
25 #include <string>
26 
27 namespace StorageBufferSize {
28     constexpr size_t EVENT_TYPE_SIZE = 32;
29     constexpr size_t KEY_BUFFER_SIZE = 32;
30     constexpr size_t VALUE_BUFFER_SIZE = 128;
31 };
32 
33 enum StorageArgIndex {
34     FIRST_ARG = 0,
35     SECOND_ARG,
36     THIRD_ARG,
37     FOURTH_ARG,
38 };
39 
40 enum StorageEvent {
41     STORAGE_EVENT_UNKNOWN = -1,
42     STORAGE_EVENT_CHANGE,
43     STORAGE_EVENT_CLEAR,
44     STORAGE_EVENT_ERROR,
45 };
46 
47 enum GetParaType {
48     GET = 0,
49     SET,
50     DELETE,
51     CLEAR,
52 };
53 
54 /***********************************************
55  * Storage Constructor
56  ***********************************************/
57 
58 struct EventHandler {
59     napi_ref callbackRef = nullptr;
60     EventHandler* next = nullptr;
61 };
62 
63 class EventListener {
64 public:
EventListener()65     EventListener() : type_(nullptr), handlers_(nullptr) {}
66 
~EventListener()67     virtual ~EventListener() {}
68 
69     void Add(napi_env env, napi_value handler);
70 
71     void Del(napi_env env, napi_value handler);
72 
73     void Clear(napi_env env);
74 
75     const char* type_;
76     EventHandler* handlers_;
77 
78 protected:
79     EventHandler* Find(napi_env env, napi_value handler);
80 };
81 
82 class StorageObjectInfo {
83 public:
84     explicit StorageObjectInfo(napi_env env);
85 
86     virtual ~StorageObjectInfo();
87 
88     void On(const char* type, napi_value handler);
89 
90     void Off(const char* type, napi_value handler = nullptr);
91 
92     void Emit(napi_value thisArg, const char* type);
93 
94 protected:
95     StorageEvent Find(const char* type) const;
96 
97 private:
98     napi_env env_;
99     EventListener listeners_[3];
100 };
101 
102 struct StorageAsyncContext {
103     napi_env env = nullptr;
104     napi_async_work work = nullptr;
105 
106     char key[StorageBufferSize::KEY_BUFFER_SIZE] = { 0 };
107     size_t keyLen = 0;
108     char value[StorageBufferSize::VALUE_BUFFER_SIZE] = { 0 };
109     size_t valueLen = 0;
110     napi_deferred deferred = nullptr;
111     napi_ref callbackRef = nullptr;
112 
113     int status = 0;
114     StorageObjectInfo* objectInfo = nullptr;
115 };
116 
117 /***********************************************
118  * Method in Storage
119  ***********************************************/
120 
121 bool GetParameOfGet(napi_env env, size_t argc, napi_value *argv, StorageAsyncContext *asyncContext);
122 bool JSStorageGetParameterFromArg(napi_env env, size_t argc, napi_value *argv,
123                                   StorageAsyncContext *asyncContext, GetParaType paraType);
124 
125 void CreateAsyncWorkOfGet(napi_env env, StorageAsyncContext *asyncContext, napi_value resource, napi_value result);
126 void CreateAsyncWorkOfSet(napi_env env, StorageAsyncContext *asyncContext, napi_value resource, napi_value result);
127 void CreateAsyncWorkOfDelete(napi_env env, StorageAsyncContext *asyncContext, napi_value resource, napi_value result);
128 void CreateAsyncWorkOfClear(napi_env env, StorageAsyncContext *asyncContext, napi_value resource, napi_value result);
129 
130 #endif