• 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 "GdbJSUtil"
16 #include "napi_gdb_js_utils.h"
17 
18 #include "gdb_utils.h"
19 #include "js_ability.h"
20 #include "securec.h"
21 
22 namespace OHOS::AppDataMgrJsKit::JSUtils {
23 constexpr int MAX_PATH_LENGTH = 1024;
24 
GetCurrentAbilityParam(napi_env env,napi_value jsValue,ContextParam & param)25 int32_t GetCurrentAbilityParam(napi_env env, napi_value jsValue, ContextParam &param)
26 {
27     std::shared_ptr<Context> context = JSAbility::GetCurrentAbility(env, jsValue);
28     if (context == nullptr) {
29         return napi_invalid_arg;
30     }
31     param.baseDir = context->GetDatabaseDir();
32     param.moduleName = context->GetModuleName();
33     param.area = context->GetArea();
34     param.bundleName = context->GetBundleName();
35     param.isSystemApp = context->IsSystemAppCalled();
36     return napi_ok;
37 }
38 
39 template<>
Convert2Value(napi_env env,napi_value jsValue,ContextParam & param)40 int32_t Convert2Value(napi_env env, napi_value jsValue, ContextParam &param)
41 {
42     if (jsValue == nullptr) {
43         LOG_INFO("hasProp is false -> fa stage");
44         param.isStageMode = false;
45         return GetCurrentAbilityParam(env, jsValue, param);
46     }
47 
48     int32_t status = GetNamedProperty(env, jsValue, "stageMode", param.isStageMode);
49     ASSERT(status == napi_ok, "get stageMode param failed", napi_invalid_arg);
50     if (!param.isStageMode) {
51         LOG_WARN("isStageMode is false -> fa stage");
52         return GetCurrentAbilityParam(env, jsValue, param);
53     }
54     LOG_DEBUG("stage mode branch");
55     status = GetNamedProperty(env, jsValue, "databaseDir", param.baseDir);
56     ASSERT(status == napi_ok, "get databaseDir failed.", napi_invalid_arg);
57     status = GetNamedProperty(env, jsValue, "area", param.area, true);
58     ASSERT(status == napi_ok, "get area failed.", napi_invalid_arg);
59 
60     napi_value hapInfo = nullptr;
61     GetNamedProperty(env, jsValue, "currentHapModuleInfo", hapInfo);
62     if (hapInfo != nullptr) {
63         status = GetNamedProperty(env, hapInfo, "name", param.moduleName);
64         ASSERT(status == napi_ok, "get currentHapModuleInfo.name failed.", napi_invalid_arg);
65     }
66 
67     napi_value appInfo = nullptr;
68     GetNamedProperty(env, jsValue, "applicationInfo", appInfo);
69     if (appInfo != nullptr) {
70         status = GetNamedProperty(env, appInfo, "name", param.bundleName);
71         ASSERT(status == napi_ok, "get applicationInfo.name failed.", napi_invalid_arg);
72         status = GetNamedProperty(env, appInfo, "systemApp", param.isSystemApp, true);
73         ASSERT(status == napi_ok, "get applicationInfo.systemApp failed.", napi_invalid_arg);
74         int32_t hapVersion = JSAbility::GetHapVersion(env, jsValue);
75         JSUtils::SetHapVersion(hapVersion);
76     }
77     return napi_ok;
78 }
79 
80 template<>
Convert2Value(napi_env env,napi_value jsValue,StoreConfig & config)81 int32_t Convert2Value(napi_env env, napi_value jsValue, StoreConfig &config)
82 {
83     std::string name;
84     auto status = GetNamedProperty(env, jsValue, "name", name);
85     ASSERT(OK == status, "get name failed.", napi_invalid_arg);
86     config.SetName(name);
87 
88     int32_t securityLevel;
89     status = GetNamedProperty(env, jsValue, "securityLevel", securityLevel);
90     ASSERT(OK == status, "get securityLevel failed.", napi_invalid_arg);
91     config.SetSecurityLevel(securityLevel);
92 
93     bool isEncrypt = false;
94     status = GetNamedProperty(env, jsValue, "encrypt", isEncrypt, true);
95     ASSERT(OK == status, "get encrypt failed.", napi_invalid_arg);
96     config.SetEncryptStatus(isEncrypt);
97     return napi_ok;
98 }
99 
100 template<>
Convert2JSValue(napi_env env,const std::shared_ptr<Result> & result)101 napi_value Convert2JSValue(napi_env env, const std::shared_ptr<Result> &result)
102 {
103     std::vector<napi_property_descriptor> descriptors = {
104         DECLARE_JS_PROPERTY(env, "records", result->GetAllData()),
105     };
106 
107     napi_value object = nullptr;
108     NAPI_CALL_RETURN_ERR(
109         napi_create_object_with_properties(env, &object, descriptors.size(), descriptors.data()), object);
110     return object;
111 }
112 
113 template<>
Convert2JSValue(napi_env env,const std::shared_ptr<Vertex> & vertex)114 napi_value Convert2JSValue(napi_env env, const std::shared_ptr<Vertex> &vertex)
115 {
116     std::vector<napi_property_descriptor> descriptors = {
117         DECLARE_JS_PROPERTY(env, "vid", vertex->GetId()),
118         DECLARE_JS_PROPERTY(env, "labels", vertex->GetLabels()),
119         DECLARE_JS_PROPERTY(env, "properties", vertex->GetProperties()),
120     };
121 
122     napi_value object = nullptr;
123     NAPI_CALL_RETURN_ERR(
124         napi_create_object_with_properties(env, &object, descriptors.size(), descriptors.data()), object);
125     return object;
126 }
127 
128 template<>
Convert2JSValue(napi_env env,const std::shared_ptr<Edge> & edge)129 napi_value Convert2JSValue(napi_env env, const std::shared_ptr<Edge> &edge)
130 {
131     std::vector<napi_property_descriptor> descriptors = {
132         DECLARE_JS_PROPERTY(env, "eid", edge->GetId()),
133         DECLARE_JS_PROPERTY(env, "type", edge->GetLabel()),
134         DECLARE_JS_PROPERTY(env, "startVid", edge->GetSourceId()),
135         DECLARE_JS_PROPERTY(env, "endVid", edge->GetTargetId()),
136         DECLARE_JS_PROPERTY(env, "properties", edge->GetProperties()),
137     };
138 
139     napi_value object = nullptr;
140     NAPI_CALL_RETURN_ERR(
141         napi_create_object_with_properties(env, &object, descriptors.size(), descriptors.data()), object);
142     return object;
143 }
144 
145 template<>
Convert2JSValue(napi_env env,const std::shared_ptr<PathSegment> & pathSegment)146 napi_value Convert2JSValue(napi_env env, const std::shared_ptr<PathSegment> &pathSegment)
147 {
148     std::vector<napi_property_descriptor> descriptors = {
149         DECLARE_JS_PROPERTY(env, "start", pathSegment->GetSourceVertex()),
150         DECLARE_JS_PROPERTY(env, "end", pathSegment->GetTargetVertex()),
151         DECLARE_JS_PROPERTY(env, "edge", pathSegment->GetEdge()),
152     };
153 
154     napi_value object = nullptr;
155     NAPI_CALL_RETURN_ERR(
156         napi_create_object_with_properties(env, &object, descriptors.size(), descriptors.data()), object);
157     return object;
158 }
159 
160 template<>
Convert2JSValue(napi_env env,const std::shared_ptr<Path> & path)161 napi_value Convert2JSValue(napi_env env, const std::shared_ptr<Path> &path)
162 {
163     std::vector<napi_property_descriptor> descriptors = {
164         DECLARE_JS_PROPERTY(env, "start", path->GetStart()),
165         DECLARE_JS_PROPERTY(env, "end", path->GetEnd()),
166         DECLARE_JS_PROPERTY(env, "length", path->GetPathLength()),
167         DECLARE_JS_PROPERTY(env, "segments", path->GetSegments()),
168     };
169 
170     napi_value object = nullptr;
171     NAPI_CALL_RETURN_ERR(
172         napi_create_object_with_properties(env, &object, descriptors.size(), descriptors.data()), object);
173     return object;
174 }
175 
GetRealPath(StoreConfig & config,ContextParam & param)176 std::tuple<int32_t, std::shared_ptr<Error>> GetRealPath(StoreConfig &config, ContextParam &param)
177 {
178     CHECK_RETURN_CORE(config.GetName().find(PATH_SPLIT) == std::string::npos, GDB_DO_NOTHING,
179         std::make_tuple(ERR, std::make_shared<ParamError>("StoreConfig.name", "a database name without path.")));
180     std::string databaseDir;
181     databaseDir.append(param.baseDir).append("/gdb");
182     auto errorCode = DistributedDataAip::GdbUtils::CreateDirectory(databaseDir);
183     std::string realPath = databaseDir + "/" + config.GetName();
184     CHECK_RETURN_CORE(errorCode == E_OK && realPath.length() <= MAX_PATH_LENGTH, GDB_DO_NOTHING,
185         std::make_tuple(ERR, std::make_shared<ParamError>("database path", "a valid path.")));
186     config.SetPath(databaseDir);
187     return std::make_tuple(E_OK, nullptr);
188 }
189 }