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 #define LOG_TAG "ConstProperties"
16 #include "js_const_properties.h"
17 #include "js_util.h"
18 #include "js_kv_store.h"
19 #include "log_print.h"
20 #include "types.h"
21
22 using namespace OHOS::DistributedKv;
23 namespace OHOS::DistributedData {
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)24 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
25 {
26 napi_value property = nullptr;
27 napi_status status = napi_create_int32(env, value, &property);
28 CHECK_RETURN(status == napi_ok, "napi_create_int32 failed!", status);
29 status = napi_set_named_property(env, obj, name.c_str(), property);
30 CHECK_RETURN(status == napi_ok, "napi_set_named_property failed!", status);
31 return status;
32 }
33
ExportUserType(napi_env env)34 static napi_value ExportUserType(napi_env env)
35 {
36 constexpr int32_t SAME_USER_ID = 0;
37
38 napi_value userType = nullptr;
39 napi_create_object(env, &userType);
40 SetNamedProperty(env, userType, "SAME_USER_ID", SAME_USER_ID);
41 napi_object_freeze(env, userType);
42 return userType;
43 }
44
ExportConstants(napi_env env)45 static napi_value ExportConstants(napi_env env)
46 {
47 constexpr int32_t MAX_KEY_LENGTH = 1024;
48 constexpr int32_t MAX_VALUE_LENGTH = 4194303;
49 constexpr int32_t MAX_KEY_LENGTH_DEVICE = 896;
50 constexpr int32_t MAX_STORE_ID_LENGTH = 128;
51 constexpr int32_t MAX_QUERY_LENGTH = 512000;
52 constexpr int32_t MAX_BATCH_SIZE = 128;
53
54 napi_value constants = nullptr;
55 napi_create_object(env, &constants);
56 SetNamedProperty(env, constants, "MAX_KEY_LENGTH", MAX_KEY_LENGTH);
57 SetNamedProperty(env, constants, "MAX_VALUE_LENGTH", MAX_VALUE_LENGTH);
58 SetNamedProperty(env, constants, "MAX_KEY_LENGTH_DEVICE", MAX_KEY_LENGTH_DEVICE);
59 SetNamedProperty(env, constants, "MAX_STORE_ID_LENGTH", MAX_STORE_ID_LENGTH);
60 SetNamedProperty(env, constants, "MAX_QUERY_LENGTH", MAX_QUERY_LENGTH);
61 SetNamedProperty(env, constants, "MAX_BATCH_SIZE", MAX_BATCH_SIZE);
62 napi_object_freeze(env, constants);
63 return constants;
64 }
65
ExportValueType(napi_env env)66 static napi_value ExportValueType(napi_env env)
67 {
68 napi_value valueType = nullptr;
69 napi_create_object(env, &valueType);
70 SetNamedProperty(env, valueType, "STRING", (int32_t)JSUtil::STRING);
71 SetNamedProperty(env, valueType, "INTEGER", (int32_t)JSUtil::INTEGER);
72 SetNamedProperty(env, valueType, "FLOAT", (int32_t)JSUtil::FLOAT);
73 SetNamedProperty(env, valueType, "BYTE_ARRAY", (int32_t)JSUtil::BYTE_ARRAY);
74 SetNamedProperty(env, valueType, "BOOLEAN", (int32_t)JSUtil::BOOLEAN);
75 SetNamedProperty(env, valueType, "DOUBLE", (int32_t)JSUtil::DOUBLE);
76 napi_object_freeze(env, valueType);
77 return valueType;
78 }
79
ExportSyncMode(napi_env env)80 static napi_value ExportSyncMode(napi_env env)
81 {
82 napi_value syncMode = nullptr;
83 napi_create_object(env, &syncMode);
84 SetNamedProperty(env, syncMode, "PULL_ONLY", (int32_t)SyncMode::PULL);
85 SetNamedProperty(env, syncMode, "PUSH_ONLY", (int32_t)SyncMode::PUSH);
86 SetNamedProperty(env, syncMode, "PUSH_PULL", (int32_t)SyncMode::PUSH_PULL);
87 napi_object_freeze(env, syncMode);
88 return syncMode;
89 }
90
ExportSubscribeType(napi_env env)91 static napi_value ExportSubscribeType(napi_env env)
92 {
93 napi_value subscribeType = nullptr;
94 napi_create_object(env, &subscribeType);
95
96 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_LOCAL", (int32_t)SUBSCRIBE_LOCAL);
97 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_REMOTE", (int32_t)SUBSCRIBE_REMOTE);
98 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_ALL", (int32_t)SUBSCRIBE_LOCAL_REMOTE);
99 napi_object_freeze(env, subscribeType);
100 return subscribeType;
101 }
102
ExportKVStoreType(napi_env env)103 static napi_value ExportKVStoreType(napi_env env)
104 {
105 napi_value kvStoreType = nullptr;
106 napi_create_object(env, &kvStoreType);
107 SetNamedProperty(env, kvStoreType, "DEVICE_COLLABORATION", (int32_t)KvStoreType::DEVICE_COLLABORATION);
108 SetNamedProperty(env, kvStoreType, "SINGLE_VERSION", (int32_t)KvStoreType::SINGLE_VERSION);
109 SetNamedProperty(env, kvStoreType, "MULTI_VERSION", (int32_t)KvStoreType::MULTI_VERSION);
110 napi_object_freeze(env, kvStoreType);
111 return kvStoreType;
112 }
113
ExportSecurityLevel(napi_env env)114 static napi_value ExportSecurityLevel(napi_env env)
115 {
116 napi_value securityLevel = nullptr;
117 napi_create_object(env, &securityLevel);
118 SetNamedProperty(env, securityLevel, "NO_LEVEL", (int32_t)SecurityLevel::NO_LABEL);
119 SetNamedProperty(env, securityLevel, "S0", (int32_t)SecurityLevel::S0);
120 SetNamedProperty(env, securityLevel, "S1", (int32_t)SecurityLevel::S1);
121 SetNamedProperty(env, securityLevel, "S2", (int32_t)SecurityLevel::S2);
122 SetNamedProperty(env, securityLevel, "S3", (int32_t)SecurityLevel::S3);
123 SetNamedProperty(env, securityLevel, "S4", (int32_t)SecurityLevel::S4);
124 napi_object_freeze(env, securityLevel);
125 return securityLevel;
126 }
127
InitConstProperties(napi_env env,napi_value exports)128 napi_status InitConstProperties(napi_env env, napi_value exports)
129 {
130 const napi_property_descriptor properties[] = {
131 DECLARE_NAPI_PROPERTY("UserType", ExportUserType(env)),
132 DECLARE_NAPI_PROPERTY("Constants", ExportConstants(env)),
133 DECLARE_NAPI_PROPERTY("ValueType", ExportValueType(env)),
134 DECLARE_NAPI_PROPERTY("SyncMode", ExportSyncMode(env)),
135 DECLARE_NAPI_PROPERTY("SubscribeType", ExportSubscribeType(env)),
136 DECLARE_NAPI_PROPERTY("KVStoreType", ExportKVStoreType(env)),
137 DECLARE_NAPI_PROPERTY("SecurityLevel", ExportSecurityLevel(env)),
138 };
139 size_t count = sizeof(properties) / sizeof(properties[0]);
140
141 return napi_define_properties(env, exports, count, properties);
142 }
143 } // namespace OHOS::DistributedData
144