nativeapi_kv.cpp 9.1 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nativeapi_kv.h"
#include <securec.h>
#include "ability_env.h"
#include "js_async_work.h"
#include "nativeapi_common.h"
#include "nativeapi_config.h"
#include "nativeapi_kv_impl.h"

namespace OHOS {
namespace ACELite {
namespace {
char g_kvFullPath[FILE_NAME_MAX_LEN + 1] = {0};

L
l00518380 已提交
29
bool IsValidKey(const char* key)
W
wenjun 已提交
30
{
L
l00518380 已提交
31 32 33 34 35
    if (key == nullptr) {
        return false;
    }
    size_t keyLen = strnlen(key, KEY_MAX_LEN + 1);
    if ((keyLen == 0) || (keyLen > KEY_MAX_LEN)) {
W
wenjun 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        return false;
    }
    if (strpbrk(key, "/\\\"*+,:;<=>\?[]|\x7F")) {
        return false;
    }
    return true;
}

int GetFullPath(const char* dataPath, const char* key)
{
    if (!IsValidKey(key) || (dataPath == nullptr)) {
        return ERROR_CODE_PARAM;
    }
    if (memset_s(g_kvFullPath, sizeof(g_kvFullPath), 0x0, sizeof(g_kvFullPath)) != EOK) {
        return ERROR_CODE_GENERAL;
    }
    if (sprintf_s(g_kvFullPath, sizeof(g_kvFullPath), "%s/%s/%s", dataPath, DEFAULT_FOLDER_PATH, key) < 0) {
        return ERROR_CODE_GENERAL;
    }
    return NATIVE_SUCCESS;
}

void GetDefault(const JSIValue thisVal, const JSIValue args)
{
L
l00518380 已提交
60
    char* defaultValue = JSI::GetStringProperty(args, DEFAULT);
W
wenjun 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    JSIValue result;
    if (defaultValue == nullptr) {
        result = JSI::CreateString("");
    } else {
        result = JSI::CreateString(defaultValue);
    }
    JSI::ReleaseString(defaultValue);
    NativeapiCommon::SuccessCallBack(thisVal, args, result);
    JSI::ReleaseValue(result);
}

int GetValueInner(const char* dataPath, const char* key, char* value)
{
    int ret = GetFullPath(dataPath, key);
    if (ret != NATIVE_SUCCESS) {
        return ret;
    }
78
    return GetValue(g_kvFullPath, value);
W
wenjun 已提交
79 80 81 82 83 84 85 86
}

int SetValueInner(const char* dataPath, const char* key, const char* value)
{
    int ret = GetFullPath(dataPath, key);
    if (ret != NATIVE_SUCCESS) {
        return ret;
    }
87
    return SetValue(g_kvFullPath, value);
W
wenjun 已提交
88 89 90 91 92 93 94 95
}

int DeleteValueInner(const char* dataPath, const char* key)
{
    int ret = GetFullPath(dataPath, key);
    if (ret != NATIVE_SUCCESS) {
        return ret;
    }
96
    return DeleteValue(g_kvFullPath);
W
wenjun 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

JSIValue ExecuteAsyncWork(const JSIValue thisVal, const JSIValue* args,
    uint8_t argsNum, AsyncWorkHandler ExecuteFunc, bool flag)
{
    JSIValue undefValue = JSI::CreateUndefined();
    if (!NativeapiCommon::IsValidJSIValue(args, argsNum)) {
        return undefValue;
    }
    FuncParams* params = new FuncParams();
    if (params == nullptr) {
        return undefValue;
    }
    params->thisVal = JSI::AcquireValue(thisVal);
    params->args = JSI::AcquireValue(args[0]);
    params->flag = flag;
    JsAsyncWork::DispatchAsyncWork(ExecuteFunc, reinterpret_cast<void *>(params));
    return undefValue;
}

void ExecuteGet(void* data)
{
    FuncParams* params = reinterpret_cast<FuncParams *>(data);
    if (params == nullptr) {
        return;
    }
    JSIValue args = params->args;
    JSIValue thisVal = params->thisVal;
    char* key = JSI::GetStringProperty(args, KV_KEY);
    const char* dataPath = GetDataPath();
    JSIValue result = JSI::CreateUndefined();
    int ret = ERROR_CODE_GENERAL;
L
l00518380 已提交
129
    char* value = reinterpret_cast<char *>(malloc(VALUE_MAX_LEN + 1));
W
wenjun 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    if (value == nullptr) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        goto EXIT;
    }
    ret = InitKv(dataPath);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        goto EXIT;
    }
    ret = GetValueInner(dataPath, key, value);
    if (ret == ERROR_FR_NO_FILE) {
        GetDefault(thisVal, args);
        goto EXIT;
    }
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        goto EXIT;
    }
    result = JSI::CreateString(value);
    NativeapiCommon::SuccessCallBack(thisVal, args, result);
EXIT:
    free(value);
    JSI::ReleaseString(key);
    JSI::ReleaseValueList(args, thisVal, result, ARGS_END);
    delete params;
}

void ExecuteSet(void* data)
{
    FuncParams* params = reinterpret_cast<FuncParams *>(data);
    if (params == nullptr) {
        return;
    }
    JSIValue args = params->args;
    JSIValue thisVal = params->thisVal;
    char* key = JSI::GetStringProperty(args, KV_KEY);
    char* value = JSI::GetStringProperty(args, KV_VALUE);
    const char* dataPath = GetDataPath();
    int ret = InitKv(dataPath);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        goto EXIT;
    }
    if ((key == nullptr) || !strlen(key)) {
        NativeapiCommon::FailCallBack(thisVal, args, ERROR_CODE_PARAM);
        goto EXIT;
    }
    if ((value == nullptr) || !strlen(value)) {
        DeleteValueInner(dataPath, key);
        NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined());
        goto EXIT;
    }
    ret = SetValueInner(dataPath, key, value);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        goto EXIT;
    }
    NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined());
EXIT:
    JSI::ReleaseString(key);
    JSI::ReleaseString(value);
    JSI::ReleaseValueList(args, thisVal, ARGS_END);
    delete params;
}

void ExecuteDelete(void* data)
{
    FuncParams* params = reinterpret_cast<FuncParams *>(data);
    if (params == nullptr) {
        return;
    }
    JSIValue args = params->args;
    JSIValue thisVal = params->thisVal;
    const char* dataPath = GetDataPath();
    int ret = InitKv(dataPath);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        JSI::ReleaseValueList(args, thisVal, ARGS_END);
        delete params;
        return;
    }
    char* key = JSI::GetStringProperty(args, KV_KEY);
    ret = DeleteValueInner(dataPath, key);
    JSI::ReleaseString(key);
    if ((ret < 0) && (ret != ERROR_FR_NO_FILE)) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        JSI::ReleaseValueList(args, thisVal, ARGS_END);
        delete params;
        return;
    }
    NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined());
    JSI::ReleaseValueList(args, thisVal, ARGS_END);
    delete params;
}

void ExecuteClear(void* data)
{
    FuncParams* params = reinterpret_cast<FuncParams *>(data);
    if (params == nullptr) {
        return;
    }
    JSIValue args = params->args;
    JSIValue thisVal = params->thisVal;
    const char* dataPath = GetDataPath();
    int ret = InitKv(dataPath);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        JSI::ReleaseValueList(args, thisVal, ARGS_END);
        delete params;
        return;
    }
    ret = ClearKVStore(dataPath);
    if (ret != NATIVE_SUCCESS) {
        NativeapiCommon::FailCallBack(thisVal, args, ret);
        JSI::ReleaseValueList(args, thisVal, ARGS_END);
        delete params;
        return;
    }
    NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined());
    JSI::ReleaseValueList(args, thisVal, ARGS_END);
    delete params;
}
}

void InitNativeApiKv(JSIValue exports)
{
    JSI::SetModuleAPI(exports, "get", NativeapiKv::Get);
    JSI::SetModuleAPI(exports, "set", NativeapiKv::Set);
    JSI::SetModuleAPI(exports, "delete", NativeapiKv::Delete);
    JSI::SetModuleAPI(exports, "clear", NativeapiKv::Clear);
}

JSIValue NativeapiKv::Get(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
{
    return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteGet, false);
}

JSIValue NativeapiKv::Set(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
{
    return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteSet, false);
}

JSIValue NativeapiKv::Delete(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
{
    return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteDelete, false);
}

JSIValue NativeapiKv::Clear(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
{
    JSIValue undefValue = JSI::CreateUndefined();
    FuncParams* params = new FuncParams();
    if (params == nullptr) {
        return undefValue;
    }
    params->thisVal = JSI::AcquireValue(thisVal);
    if (NativeapiCommon::IsValidJSIValue(args, argsNum)) {
        params->args = JSI::AcquireValue(args[0]);
    }
    JsAsyncWork::DispatchAsyncWork(reinterpret_cast<AsyncWorkHandler>(ExecuteClear), reinterpret_cast<void *>(params));
    return undefValue;
}
} // ACELite
} // OHOS