• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_single_kv_store.h"
19 #include "log_print.h"
20 #include "types.h"
21 
22 using namespace OHOS::DistributedKv;
23 namespace OHOS::DistributedKVStore {
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     ASSERT(status == napi_ok, "int32_t to napi_value failed!", status);
29     status = napi_set_named_property(env, obj, name.c_str(), property);
30     ASSERT(status == napi_ok, "napi_set_named_property failed!", status);
31     return status;
32 }
33 
ExportConstants(napi_env env)34 static napi_value ExportConstants(napi_env env)
35 {
36     constexpr int32_t MAX_KEY_LENGTH = 1024;
37     constexpr int32_t MAX_VALUE_LENGTH = 4194303;
38     constexpr int32_t MAX_KEY_LENGTH_DEVICE = 896;
39     constexpr int32_t MAX_STORE_ID_LENGTH = 128;
40     constexpr int32_t MAX_QUERY_LENGTH = 512000;
41     constexpr int32_t MAX_BATCH_SIZE = 128;
42 
43     napi_value constants = nullptr;
44     napi_status status = napi_create_object(env, &constants);
45     if (status != napi_ok) {
46         return nullptr;
47     }
48     SetNamedProperty(env, constants, "MAX_KEY_LENGTH", MAX_KEY_LENGTH);
49     SetNamedProperty(env, constants, "MAX_VALUE_LENGTH", MAX_VALUE_LENGTH);
50     SetNamedProperty(env, constants, "MAX_KEY_LENGTH_DEVICE", MAX_KEY_LENGTH_DEVICE);
51     SetNamedProperty(env, constants, "MAX_STORE_ID_LENGTH", MAX_STORE_ID_LENGTH);
52     SetNamedProperty(env, constants, "MAX_QUERY_LENGTH", MAX_QUERY_LENGTH);
53     SetNamedProperty(env, constants, "MAX_BATCH_SIZE", MAX_BATCH_SIZE);
54     napi_object_freeze(env, constants);
55     return constants;
56 }
57 
ExportValueType(napi_env env)58 static napi_value ExportValueType(napi_env env)
59 {
60     napi_value valueType = nullptr;
61     napi_status status = napi_create_object(env, &valueType);
62     if (status != napi_ok) {
63         return nullptr;
64     }
65     SetNamedProperty(env, valueType, "STRING", (int32_t)JSUtil::STRING);
66     SetNamedProperty(env, valueType, "INTEGER", (int32_t)JSUtil::INTEGER);
67     SetNamedProperty(env, valueType, "FLOAT", (int32_t)JSUtil::FLOAT);
68     SetNamedProperty(env, valueType, "BYTE_ARRAY", (int32_t)JSUtil::BYTE_ARRAY);
69     SetNamedProperty(env, valueType, "BOOLEAN", (int32_t)JSUtil::BOOLEAN);
70     SetNamedProperty(env, valueType, "DOUBLE", (int32_t)JSUtil::DOUBLE);
71     napi_object_freeze(env, valueType);
72     return valueType;
73 }
74 
ExportSyncMode(napi_env env)75 static napi_value ExportSyncMode(napi_env env)
76 {
77     napi_value syncMode = nullptr;
78     napi_status status = napi_create_object(env, &syncMode);
79     if (status != napi_ok) {
80         return nullptr;
81     }
82     SetNamedProperty(env, syncMode, "PULL_ONLY", (int32_t)SyncMode::PULL);
83     SetNamedProperty(env, syncMode, "PUSH_ONLY", (int32_t)SyncMode::PUSH);
84     SetNamedProperty(env, syncMode, "PUSH_PULL", (int32_t)SyncMode::PUSH_PULL);
85     napi_object_freeze(env, syncMode);
86     return syncMode;
87 }
88 
ExportSubscribeType(napi_env env)89 static napi_value ExportSubscribeType(napi_env env)
90 {
91     napi_value subscribeType = nullptr;
92     napi_status status = napi_create_object(env, &subscribeType);
93     if (status != napi_ok) {
94         return nullptr;
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_status status = napi_create_object(env, &kvStoreType);
107     if (status != napi_ok) {
108         return nullptr;
109     }
110     SetNamedProperty(env, kvStoreType, "DEVICE_COLLABORATION", (int32_t)KvStoreType::DEVICE_COLLABORATION);
111     SetNamedProperty(env, kvStoreType, "SINGLE_VERSION", (int32_t)KvStoreType::SINGLE_VERSION);
112     napi_object_freeze(env, kvStoreType);
113     return kvStoreType;
114 }
115 
ExportSecurityLevel(napi_env env)116 static napi_value ExportSecurityLevel(napi_env env)
117 {
118     napi_value securityLevel = nullptr;
119     napi_status status = napi_create_object(env, &securityLevel);
120     if (status != napi_ok) {
121         return nullptr;
122     }
123     SetNamedProperty(env, securityLevel, "S1", (int32_t)SecurityLevel::S1);
124     SetNamedProperty(env, securityLevel, "S2", (int32_t)SecurityLevel::S2);
125     SetNamedProperty(env, securityLevel, "S3", (int32_t)SecurityLevel::S3);
126     SetNamedProperty(env, securityLevel, "S4", (int32_t)SecurityLevel::S4);
127     napi_object_freeze(env, securityLevel);
128     return securityLevel;
129 }
130 
InitConstProperties(napi_env env,napi_value exports)131 napi_status InitConstProperties(napi_env env, napi_value exports)
132 {
133     const napi_property_descriptor properties[] = {
134         DECLARE_NAPI_PROPERTY("Constants", ExportConstants(env)),
135         DECLARE_NAPI_PROPERTY("ValueType", ExportValueType(env)),
136         DECLARE_NAPI_PROPERTY("SyncMode", ExportSyncMode(env)),
137         DECLARE_NAPI_PROPERTY("SubscribeType", ExportSubscribeType(env)),
138         DECLARE_NAPI_PROPERTY("KVStoreType", ExportKVStoreType(env)),
139         DECLARE_NAPI_PROPERTY("SecurityLevel", ExportSecurityLevel(env)),
140     };
141     size_t count = sizeof(properties) / sizeof(properties[0]);
142 
143     return napi_define_properties(env, exports, count, properties);
144 }
145 } // namespace OHOS::DistributedKVStore
146