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 }; 69 70 namespace OHOS { 71 namespace Settings { 72 /** 73 * @brief Wrap void to js value. 74 * ability_context 75 * @param env the environment that the Node-API call is invoked under 76 * @return napi_value napi_value after wrapped 77 */ 78 napi_value wrap_void_to_js(napi_env env); 79 80 /** 81 * @brief Wrap string to js value. 82 * 83 * @param env the environment that the Node-API call is invoked under 84 * @param value string value to be wrap 85 * @return napi_value js value after wrapped 86 */ 87 napi_value wrap_string_to_js(napi_env env, const std::string &value); 88 89 /** 90 * @brief Wrap bool to js value. 91 * 92 * @param env the environment that the Node-API call is invoked under 93 * @param value bool value to be wrap 94 * @return napi_value js value after wrapped 95 */ 96 napi_value wrap_bool_to_js(napi_env env, bool value); 97 98 /** 99 * @brief Unwrap string from js value. 100 * 101 * @param env the environment that the Node-API call is invoked under 102 * @param param js value to unwrap 103 * @return std::string string value after unwrapped 104 */ 105 std::string unwrap_string_from_js(napi_env env, napi_value param); 106 107 /** 108 * @brief getUri NAPI implementation. 109 * @param env the environment that the Node-API call is invoked under 110 * @param info the callback info passed into the callback function 111 * @return napi_value the return value from NAPI C++ to JS for the module. 112 */ 113 napi_value napi_get_uri(napi_env env, napi_callback_info info); 114 115 /** 116 * @brief getUriSync NAPI implementation. 117 * @param env the environment that the Node-API call is invoked under 118 * @param info the callback info passed into the callback function 119 * @return napi_value the return value from NAPI C++ to JS for the module. 120 */ 121 napi_value napi_get_uri_sync(napi_env env, napi_callback_info info); 122 123 /** 124 * @brief getValue NAPI implementation. 125 * @param env the environment that the Node-API call is invoked under 126 * @param info the callback info passed into the callback function 127 * @return napi_value the return value from NAPI C++ to JS for the module. 128 */ 129 napi_value napi_get_value(napi_env env, napi_callback_info info); 130 napi_value napi_get_value_ext(napi_env env, napi_callback_info info, const bool stageMode); 131 132 /** 133 * @brief getValueSync NAPI implementation. 134 * @param env the environment that the Node-API call is invoked under 135 * @param info the callback info passed into the callback function 136 * @return napi_value the return value from NAPI C++ to JS for the module. 137 */ 138 napi_value napi_get_value_sync(napi_env env, napi_callback_info info); 139 140 /** 141 * @brief setValue NAPI implementation. 142 * @param env the environment that the Node-API call is invoked under 143 * @param info the callback info passed into the callback function 144 * @return napi_value the return value from NAPI C++ to JS for the module. 145 */ 146 napi_value napi_set_value(napi_env env, napi_callback_info info); 147 napi_value napi_set_value_ext(napi_env env, napi_callback_info info, const bool stageMode); 148 149 /** 150 * @brief setValueSync NAPI implementation. 151 * @param env the environment that the Node-API call is invoked under 152 * @param info the callback info passed into the callback function 153 * @return napi_value the return value from NAPI C++ to JS for the module. 154 */ 155 napi_value napi_set_value_sync(napi_env env, napi_callback_info info); 156 157 /** 158 * @brief initNapiClass NAPI implementation. 159 * @param env the environment that the Node-API call is invoked under 160 * @param exports the class property 161 * @return napi_value the return value from NAPI C++ to JS for the module. 162 */ 163 napi_value InitNapiClass(napi_env env, napi_value exports); 164 165 /** 166 * @brief enableAirplaneMode NAPI implementation. 167 * @param env the environment that the Node-API call is invoked under 168 * @param info the callback info passed into the callback function 169 * @return napi_value the return value from NAPI C++ to JS for the module. 170 */ 171 napi_value napi_enable_airplane_mode(napi_env env, napi_callback_info info); 172 173 /** 174 * @brief canShowFloating NAPI implementation. 175 * @param env the environment that the Node-API call is invoked under 176 * @param info the callback info passed into the callback function 177 * @return napi_value the return value from NAPI C++ to JS for the module. 178 */ 179 napi_value napi_can_show_floating(napi_env env, napi_callback_info info); 180 std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr); 181 std::string GetProxyUriStr(std::string tableName, std::string idStr); 182 bool IsTableNameInvalid(std::string tableName); 183 std::shared_ptr<DataShare::DataShareHelper> getDataShareHelper( 184 napi_env env, const napi_value context, const bool stageMode, std::string tableName = "global"); 185 napi_value napi_get_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args); 186 napi_value napi_set_value_sync_ext(bool stageMode, size_t argc, napi_env env, napi_value* args); 187 napi_value napi_register_key_observer(napi_env env, napi_callback_info info); 188 napi_value napi_unregister_key_observer(napi_env env, napi_callback_info info); 189 } // namespace Settings 190 } // namespace OHOS 191 #endif // NAPI_SETTINGS_H 192