• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "GdbStoreHelperProxy"
16 #include "napi_gdb_store_helper.h"
17 
18 #include <regex>
19 
20 #include "db_trace.h"
21 #include "gdb_helper.h"
22 #include "logger.h"
23 #include "napi_gdb_context.h"
24 #include "napi_gdb_js_utils.h"
25 #include "napi_gdb_store.h"
26 
27 namespace OHOS::GraphStoreJsKit {
28 using namespace OHOS::DistributedDataAip;
29 
30 static constexpr int MAX_GDB_DB_NAME_LENGTH = 128;
31 
IsValidDbName(const std::string & name)32 bool IsValidDbName(const std::string &name)
33 {
34     if (name.empty() || name.size() > MAX_GDB_DB_NAME_LENGTH) {
35         return false;
36     }
37     const std::regex pattern("^[a-zA-Z0-9_]+$");
38     return std::regex_match(name, pattern);
39 }
40 
GetStore(napi_env env,napi_callback_info info)41 napi_value GetStore(napi_env env, napi_callback_info info)
42 {
43     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
44     auto context = std::make_shared<GdbStoreContext>();
45     auto input = [context, info](napi_env env, size_t argc, napi_value *argv, napi_value self) {
46         CHECK_RETURN_SET_E(argc == 2, std::make_shared<ParamNumError>(" 2 "));
47         int errCode = AppDataMgrJsKit::JSUtils::Convert2Value(env, argv[0], context->param);
48         CHECK_RETURN_SET_E(OK == errCode, std::make_shared<ParamError>("Illegal context."));
49         CHECK_RETURN_SET_E(context->param.isSystemApp, std::make_shared<NonSystemError>());
50         errCode = AppDataMgrJsKit::JSUtils::Convert2Value(env, argv[1], context->config);
51         CHECK_RETURN_SET_E(OK == errCode, std::make_shared<ParamError>("Illegal StoreConfig or name."));
52         CHECK_RETURN_SET_E(IsValidDbName(context->config.GetName()), std::make_shared<ParamError>("Illegal name."));
53         auto [code, err] = AppDataMgrJsKit::JSUtils::GetRealPath(context->config, context->param);
54         CHECK_RETURN_SET_E(OK == code, err);
55         context->config.SetBundleName(context->param.bundleName);
56     };
57     auto exec = [context]() -> int {
58         context->gdbStore = GDBHelper::GetDBStore(context->config, context->intOutput);
59         return OK;
60     };
61     auto output = [context](napi_env env, napi_value &result) {
62         CHECK_RETURN_SET_E(context->intOutput == OK, std::make_shared<InnerError>(context->intOutput));
63         result = GdbStoreProxy::NewInstance(env, context->gdbStore, context->param.isSystemApp);
64         CHECK_RETURN_SET_E(result != nullptr, std::make_shared<InnerError>(E_INNER_ERROR));
65     };
66     context->SetAction(env, info, input, exec, output);
67 
68     CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK);
69     return ASYNC_CALL(env, context);
70 }
71 
DeleteStore(napi_env env,napi_callback_info info)72 napi_value DeleteStore(napi_env env, napi_callback_info info)
73 {
74     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
75     auto context = std::make_shared<GdbStoreContext>();
76     auto input = [context, info](napi_env env, size_t argc, napi_value *argv, napi_value self) {
77         CHECK_RETURN_SET_E(argc == 2, std::make_shared<ParamNumError>(" 2 "));
78         int errCode = AppDataMgrJsKit::JSUtils::Convert2Value(env, argv[0], context->param);
79         CHECK_RETURN_SET_E(OK == errCode, std::make_shared<ParamError>("Illegal context."));
80         CHECK_RETURN_SET_E(context->param.isSystemApp, std::make_shared<NonSystemError>());
81         errCode = AppDataMgrJsKit::JSUtils::Convert2Value(env, argv[1], context->config);
82         CHECK_RETURN_SET_E(OK == errCode, std::make_shared<ParamError>("Illegal StoreConfig or name."));
83         CHECK_RETURN_SET_E(IsValidDbName(context->config.GetName()), std::make_shared<ParamError>("Illegal name."));
84         auto [code, err] = AppDataMgrJsKit::JSUtils::GetRealPath(context->config, context->param);
85         CHECK_RETURN_SET_E(OK == code, err);
86     };
87     auto exec = [context]() -> int {
88         context->intOutput = GDBHelper::DeleteDBStore(context->config);
89         return OK;
90     };
91     auto output = [context](napi_env env, napi_value &result) {
92         CHECK_RETURN_SET_E(context->intOutput == OK, std::make_shared<InnerError>(context->intOutput));
93     };
94     context->SetAction(env, info, input, exec, output);
95 
96     CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK);
97     return ASYNC_CALL(env, context);
98 }
99 
InitGdbHelper(napi_env env,napi_value exports)100 napi_value InitGdbHelper(napi_env env, napi_value exports)
101 {
102     napi_property_descriptor properties[] = {
103         DECLARE_NAPI_FUNCTION_WITH_DATA("getStore", GetStore, AIP_ASYNC),
104         DECLARE_NAPI_FUNCTION_WITH_DATA("deleteStore", DeleteStore, AIP_ASYNC),
105     };
106     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
107     return exports;
108 }
109 } // namespace OHOS::GraphStoreJsKit