• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 useSilent;
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  * @return std::string string value after unwrapped
105  */
106 std::string unwrap_string_from_js(napi_env env, napi_value param);
107 
108 /**
109  * @brief getUri NAPI implementation.
110  * @param env the environment that the Node-API call is invoked under
111  * @param info the callback info passed into the callback function
112  * @return napi_value the return value from NAPI C++ to JS for the module.
113  */
114 napi_value napi_get_uri(napi_env env, napi_callback_info info);
115 
116 /**
117  * @brief getUriSync NAPI implementation.
118  * @param env the environment that the Node-API call is invoked under
119  * @param info the callback info passed into the callback function
120  * @return napi_value the return value from NAPI C++ to JS for the module.
121  */
122 napi_value napi_get_uri_sync(napi_env env, napi_callback_info info);
123 
124 /**
125  * @brief getValue NAPI implementation.
126  * @param env the environment that the Node-API call is invoked under
127  * @param info the callback info passed into the callback function
128  * @return napi_value the return value from NAPI C++ to JS for the module.
129  */
130 napi_value napi_get_value(napi_env env, napi_callback_info info);
131 napi_value napi_get_value_ext(napi_env env, napi_callback_info info, const bool stageMode);
132 
133 /**
134  * @brief getValueSync NAPI implementation.
135  * @param env the environment that the Node-API call is invoked under
136  * @param info the callback info passed into the callback function
137  * @return napi_value the return value from NAPI C++ to JS for the module.
138  */
139 napi_value napi_get_value_sync(napi_env env, napi_callback_info info);
140 
141 /**
142  * @brief setValue NAPI implementation.
143  * @param env the environment that the Node-API call is invoked under
144  * @param info the callback info passed into the callback function
145  * @return napi_value the return value from NAPI C++ to JS for the module.
146  */
147 napi_value napi_set_value(napi_env env, napi_callback_info info);
148 napi_value napi_set_value_ext(napi_env env, napi_callback_info info, const bool stageMode);
149 
150 /**
151  * @brief setValueSync NAPI implementation.
152  * @param env the environment that the Node-API call is invoked under
153  * @param info the callback info passed into the callback function
154  * @return napi_value the return value from NAPI C++ to JS for the module.
155  */
156 napi_value napi_set_value_sync(napi_env env, napi_callback_info info);
157 
158 /**
159  * @brief initNapiClass NAPI implementation.
160  * @param env the environment that the Node-API call is invoked under
161  * @param exports the class property
162  * @return napi_value the return value from NAPI C++ to JS for the module.
163  */
164 napi_value InitNapiClass(napi_env env, napi_value exports);
165 
166 /**
167  * @brief enableAirplaneMode NAPI implementation.
168  * @param env the environment that the Node-API call is invoked under
169  * @param info the callback info passed into the callback function
170  * @return napi_value the return value from NAPI C++ to JS for the module.
171  */
172 napi_value napi_enable_airplane_mode(napi_env env, napi_callback_info info);
173 
174 /**
175  * @brief canShowFloating NAPI implementation.
176  * @param env the environment that the Node-API call is invoked under
177  * @param info the callback info passed into the callback function
178  * @return napi_value the return value from NAPI C++ to JS for the module.
179  */
180 napi_value napi_can_show_floating(napi_env env, napi_callback_info info);
181 std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr);
182 std::string GetProxyUriStr(std::string tableName, std::string idStr);
183 bool IsTableNameInvalid(std::string tableName);
184 std::shared_ptr<DataShare::DataShareHelper> getDataShareHelper(
185     napi_env env, const napi_value context, const bool stageMode, std::string tableName = "global",
186     AsyncCallbackInfo *data = nullptr);
187 napi_value napi_get_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args);
188 napi_value napi_set_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args);
189 napi_value napi_register_key_observer(napi_env env, napi_callback_info info);
190 napi_value napi_unregister_key_observer(napi_env env, napi_callback_info info);
191 }  // namespace Settings
192 }  // namespace OHOS
193 #endif  //  NAPI_SETTINGS_H
194