• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_SETTINGS_H
17 #define NAPI_SETTINGS_H
18 
19 #include <vector>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "napi/native_node_api.h"
24 #include "uri.h"
25 
26 #include "data_ability_helper.h"
27 #include "data_ability_predicates.h"
28 #include "datashare_helper.h"
29 #include "datashare_predicates.h"
30 
31 #define ARGS_ONE 1
32 #define ARGS_TWO 2
33 #define ARGS_THREE 3
34 #define ARGS_FOUR 4
35 #define ARGS_FIVE 5
36 
37 #define PARAM0 0
38 #define PARAM1 1
39 #define PARAM2 2
40 #define PARAM3 3
41 #define PARAM4 4
42 
43 enum CallType {
44     INVALID_CALL,
45     STAGE_SYNC,
46     STAGE_CALLBACK,
47     STAGE_CALLBACK_SPECIFIC,
48     STAGE_PROMISE,
49     STAGE_PROMISE_SPECIFIC,
50     FA_SYNC,
51     FA_CALLBACK,
52     FA_PROMISE
53 };
54 
55 struct AsyncCallbackInfo {
56     napi_env env;
57     napi_async_work asyncWork;
58     napi_deferred deferred;
59     napi_ref callbackRef;
60     std::shared_ptr<OHOS::AppExecFwk::DataAbilityHelper> dataAbilityHelper;
61     std::string key;
62     std::string value;
63     std::string uri;
64     CallType callType;
65     std::string tableName;
66     int status;
67     std::shared_ptr<OHOS::DataShare::DataShareHelper> dataShareHelper = nullptr;
68     bool useNonSilent;
69 };
70 
71 namespace OHOS {
72 namespace Settings {
73 /**
74  * @brief Wrap void to js value.
75  * ability_context
76  * @param env the environment that the Node-API call is invoked under
77  * @return napi_value napi_value after wrapped
78  */
79 napi_value wrap_void_to_js(napi_env env);
80 
81 /**
82  * @brief Wrap string to js value.
83  *
84  * @param env the environment that the Node-API call is invoked under
85  * @param value string value to be wrap
86  * @return napi_value js value after wrapped
87  */
88 napi_value wrap_string_to_js(napi_env env, const std::string &value);
89 
90 /**
91  * @brief Wrap bool to js value.
92  *
93  * @param env the environment that the Node-API call is invoked under
94  * @param value bool value to be wrap
95  * @return napi_value js value after wrapped
96  */
97 napi_value wrap_bool_to_js(napi_env env, bool value);
98 
99 /**
100  * @brief Unwrap string from js value.
101  *
102  * @param env the environment that the Node-API call is invoked under
103  * @param param js value to unwrap
104  * @param showLog log is need to show, default true
105  * @param anonymousLog log is need anonymous, default false
106  * @return std::string string value after unwrapped
107  */
108 std::string unwrap_string_from_js(napi_env env, napi_value param, bool showLog = true, bool anonymousLog = false);
109 
110 /**
111  * @brief anonymous log.
112  *
113  * @param log original log
114  * @return std::string string value after anonymous
115  */
116 std::string anonymous_log(std::string log);
117 
118 /**
119  * @brief getUri NAPI implementation.
120  * @param env the environment that the Node-API call is invoked under
121  * @param info the callback info passed into the callback function
122  * @return napi_value the return value from NAPI C++ to JS for the module.
123  */
124 napi_value napi_get_uri(napi_env env, napi_callback_info info);
125 
126 /**
127  * @brief getUriSync NAPI implementation.
128  * @param env the environment that the Node-API call is invoked under
129  * @param info the callback info passed into the callback function
130  * @return napi_value the return value from NAPI C++ to JS for the module.
131  */
132 napi_value napi_get_uri_sync(napi_env env, napi_callback_info info);
133 
134 /**
135  * @brief getValue NAPI implementation.
136  * @param env the environment that the Node-API call is invoked under
137  * @param info the callback info passed into the callback function
138  * @return napi_value the return value from NAPI C++ to JS for the module.
139  */
140 napi_value napi_get_value(napi_env env, napi_callback_info info);
141 napi_value napi_get_value_ext(napi_env env, napi_callback_info info, const bool stageMode);
142 
143 /**
144  * @brief getValueSync NAPI implementation.
145  * @param env the environment that the Node-API call is invoked under
146  * @param info the callback info passed into the callback function
147  * @return napi_value the return value from NAPI C++ to JS for the module.
148  */
149 napi_value napi_get_value_sync(napi_env env, napi_callback_info info);
150 
151 /**
152  * @brief setValue NAPI implementation.
153  * @param env the environment that the Node-API call is invoked under
154  * @param info the callback info passed into the callback function
155  * @return napi_value the return value from NAPI C++ to JS for the module.
156  */
157 napi_value napi_set_value(napi_env env, napi_callback_info info);
158 napi_value napi_set_value_ext(napi_env env, napi_callback_info info, const bool stageMode);
159 
160 /**
161  * @brief setValueSync NAPI implementation.
162  * @param env the environment that the Node-API call is invoked under
163  * @param info the callback info passed into the callback function
164  * @return napi_value the return value from NAPI C++ to JS for the module.
165  */
166 napi_value napi_set_value_sync(napi_env env, napi_callback_info info);
167 
168 /**
169  * @brief initNapiClass NAPI implementation.
170  * @param env the environment that the Node-API call is invoked under
171  * @param exports the class property
172  * @return napi_value the return value from NAPI C++ to JS for the module.
173  */
174 napi_value InitNapiClass(napi_env env, napi_value exports);
175 
176 /**
177  * @brief enableAirplaneMode NAPI implementation.
178  * @param env the environment that the Node-API call is invoked under
179  * @param info the callback info passed into the callback function
180  * @return napi_value the return value from NAPI C++ to JS for the module.
181  */
182 napi_value napi_enable_airplane_mode(napi_env env, napi_callback_info info);
183 
184 /**
185  * @brief canShowFloating NAPI implementation.
186  * @param env the environment that the Node-API call is invoked under
187  * @param info the callback info passed into the callback function
188  * @return napi_value the return value from NAPI C++ to JS for the module.
189  */
190 napi_value napi_can_show_floating(napi_env env, napi_callback_info info);
191 std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr);
192 std::string GetProxyUriStr(std::string tableName, std::string idStr);
193 bool IsTableNameInvalid(std::string tableName);
194 std::shared_ptr<DataShare::DataShareHelper> getDataShareHelper(
195     napi_env env, const napi_value context, const bool stageMode, std::string tableName = "global",
196     AsyncCallbackInfo *data = nullptr);
197 napi_value napi_get_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args);
198 napi_value napi_set_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args);
199 napi_value napi_register_key_observer(napi_env env, napi_callback_info info);
200 napi_value napi_unregister_key_observer(napi_env env, napi_callback_info info);
201 }  // namespace Settings
202 }  // namespace OHOS
203 #endif  //  NAPI_SETTINGS_H
204