• 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 
16 #include "napi_rdb_const_properties.h"
17 #include "rdb_common.h"
18 #include "rdb_store.h"
19 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
20 #include "rdb_store_config.h"
21 #include "rdb_types.h"
22 
23 using OHOS::DistributedRdb::SyncMode;
24 using OHOS::DistributedRdb::SubscribeMode;
25 #endif
26 using OHOS::NativeRdb::SecurityLevel;
27 using OHOS::NativeRdb::ConflictResolution;
28 
29 #define SET_NAPI_PROPERTY(object, propName, value)                        \
30 {                                                                         \
31     (void) SetNamedProperty(env, (object), (propName), (int32_t)(value)); \
32 }
33 
34 namespace OHOS::RelationalStoreJsKit {
35 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)36 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
37 {
38     napi_value property = nullptr;
39     napi_status status = napi_create_int32(env, value, &property);
40     if (status != napi_ok) {
41         return status;
42     }
43     status = napi_set_named_property(env, obj, name.c_str(), property);
44     if (status != napi_ok) {
45         return status;
46     }
47     return status;
48 }
49 
ExportSyncMode(napi_env env)50 static napi_value ExportSyncMode(napi_env env)
51 {
52     napi_value syncMode = nullptr;
53     napi_create_object(env, &syncMode);
54 
55     SET_NAPI_PROPERTY(syncMode, "SYNC_MODE_PUSH", SyncMode::PUSH);
56     SET_NAPI_PROPERTY(syncMode, "SYNC_MODE_PULL", SyncMode::PULL);
57     SET_NAPI_PROPERTY(syncMode, "SYNC_MODE_TIME_FIRST", SyncMode::TIME_FIRST);
58     SET_NAPI_PROPERTY(syncMode, "SYNC_MODE_NATIVE_FIRST", SyncMode::NATIVE_FIRST);
59     SET_NAPI_PROPERTY(syncMode, "SYNC_MODE_CLOUD_FIRST", SyncMode::CLOUD_FIRST);
60     napi_object_freeze(env, syncMode);
61     return syncMode;
62 }
63 
ExportSubscribeType(napi_env env)64 static napi_value ExportSubscribeType(napi_env env)
65 {
66     napi_value subscribeType = nullptr;
67     napi_create_object(env, &subscribeType);
68 
69     SET_NAPI_PROPERTY(subscribeType, "SUBSCRIBE_TYPE_REMOTE", SubscribeMode::REMOTE);
70     SET_NAPI_PROPERTY(subscribeType, "SUBSCRIBE_TYPE_CLOUD", SubscribeMode::CLOUD);
71     SET_NAPI_PROPERTY(subscribeType, "SUBSCRIBE_TYPE_CLOUD_DETAILS", SubscribeMode::CLOUD_DETAIL);
72     napi_object_freeze(env, subscribeType);
73     return subscribeType;
74 }
75 
ExportSecurityLevel(napi_env env)76 static napi_value ExportSecurityLevel(napi_env env)
77 {
78     napi_value securityLevel = nullptr;
79     napi_create_object(env, &securityLevel);
80 
81     SET_NAPI_PROPERTY(securityLevel, "S1", SecurityLevel::S1);
82     SET_NAPI_PROPERTY(securityLevel, "S2", SecurityLevel::S2);
83     SET_NAPI_PROPERTY(securityLevel, "S3", SecurityLevel::S3);
84     SET_NAPI_PROPERTY(securityLevel, "S4", SecurityLevel::S4);
85     napi_object_freeze(env, securityLevel);
86     return securityLevel;
87 }
88 #endif
89 
ExportProgress(napi_env env)90 static napi_value ExportProgress(napi_env env)
91 {
92     napi_value progress = nullptr;
93     napi_create_object(env, &progress);
94 
95     SET_NAPI_PROPERTY(progress, "SYNC_BEGIN", 0);
96     SET_NAPI_PROPERTY(progress, "SYNC_IN_PROGRESS", 1);
97     SET_NAPI_PROPERTY(progress, "SYNC_FINISH", 2);
98     napi_object_freeze(env, progress);
99     return progress;
100 }
101 
ExportProgressCode(napi_env env)102 static napi_value ExportProgressCode(napi_env env)
103 {
104     napi_value progressCode = nullptr;
105     napi_create_object(env, &progressCode);
106 
107     SET_NAPI_PROPERTY(progressCode, "SUCCESS", 0);
108     SET_NAPI_PROPERTY(progressCode, "UNKNOWN_ERROR", 1);
109     SET_NAPI_PROPERTY(progressCode, "NETWORK_ERROR", 2);
110     SET_NAPI_PROPERTY(progressCode, "CLOUD_DISABLED", 3);
111     SET_NAPI_PROPERTY(progressCode, "LOCKED_BY_OTHERS", 4);
112     SET_NAPI_PROPERTY(progressCode, "RECORD_LIMIT_EXCEEDED", 5);
113     SET_NAPI_PROPERTY(progressCode, "NO_SPACE_FOR_ASSET", 6);
114     napi_object_freeze(env, progressCode);
115     return progressCode;
116 }
117 
ExportDistributedType(napi_env env)118 static napi_value ExportDistributedType(napi_env env)
119 {
120     napi_value distributedType = nullptr;
121     napi_create_object(env, &distributedType);
122 
123     SET_NAPI_PROPERTY(distributedType, "DISTRIBUTED_DEVICE", 0);
124     SET_NAPI_PROPERTY(distributedType, "DISTRIBUTED_CLOUD", 1);
125     napi_object_freeze(env, distributedType);
126     return distributedType;
127 }
128 
ExportChangeType(napi_env env)129 static napi_value ExportChangeType(napi_env env)
130 {
131     napi_value changeType = nullptr;
132     napi_create_object(env, &changeType);
133 
134     SET_NAPI_PROPERTY(changeType, "DATA_CHANGE", DistributedRdb::Origin::BASIC_DATA);
135     SET_NAPI_PROPERTY(changeType, "ASSET_CHANGE", DistributedRdb::Origin::ASSET_DATA);
136     napi_object_freeze(env, changeType);
137     return changeType;
138 }
139 
ExportAssetStatus(napi_env env)140 static napi_value ExportAssetStatus(napi_env env)
141 {
142     napi_value assetStatus = nullptr;
143     napi_create_object(env, &assetStatus);
144 
145     SET_NAPI_PROPERTY(assetStatus, "ASSET_NORMAL", NativeRdb::AssetValue::STATUS_NORMAL);
146     SET_NAPI_PROPERTY(assetStatus, "ASSET_INSERT", NativeRdb::AssetValue::STATUS_INSERT);
147     SET_NAPI_PROPERTY(assetStatus, "ASSET_UPDATE", NativeRdb::AssetValue::STATUS_UPDATE);
148     SET_NAPI_PROPERTY(assetStatus, "ASSET_DELETE", NativeRdb::AssetValue::STATUS_DELETE);
149     SET_NAPI_PROPERTY(assetStatus, "ASSET_ABNORMAL", NativeRdb::AssetValue::STATUS_ABNORMAL);
150     SET_NAPI_PROPERTY(assetStatus, "ASSET_DOWNLOADING", NativeRdb::AssetValue::STATUS_DOWNLOADING);
151     napi_object_freeze(env, assetStatus);
152     return assetStatus;
153 }
154 
ExportConflictResolution(napi_env env)155 static napi_value ExportConflictResolution(napi_env env)
156 {
157     napi_value conflictResolution = nullptr;
158     napi_create_object(env, &conflictResolution);
159 
160     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_NONE", ConflictResolution::ON_CONFLICT_NONE);
161     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_ROLLBACK", ConflictResolution::ON_CONFLICT_ROLLBACK);
162     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_ABORT", ConflictResolution::ON_CONFLICT_ABORT);
163     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_FAIL", ConflictResolution::ON_CONFLICT_FAIL);
164     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_IGNORE", ConflictResolution::ON_CONFLICT_IGNORE);
165     SET_NAPI_PROPERTY(conflictResolution, "ON_CONFLICT_REPLACE", ConflictResolution::ON_CONFLICT_REPLACE);
166 
167     napi_object_freeze(env, conflictResolution);
168     return conflictResolution;
169 }
170 
InitConstProperties(napi_env env,napi_value exports)171 napi_status InitConstProperties(napi_env env, napi_value exports)
172 {
173     const napi_property_descriptor properties[] = {
174         DECLARE_NAPI_PROPERTY("ConflictResolution", ExportConflictResolution(env)),
175 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
176         DECLARE_NAPI_PROPERTY("SyncMode", ExportSyncMode(env)),
177         DECLARE_NAPI_PROPERTY("SubscribeType", ExportSubscribeType(env)),
178         DECLARE_NAPI_PROPERTY("SecurityLevel", ExportSecurityLevel(env)),
179 #endif
180         DECLARE_NAPI_PROPERTY("Progress", ExportProgress(env)),
181         DECLARE_NAPI_PROPERTY("ProgressCode", ExportProgressCode(env)),
182         DECLARE_NAPI_PROPERTY("DistributedType", ExportDistributedType(env)),
183         DECLARE_NAPI_PROPERTY("AssetStatus", ExportAssetStatus(env)),
184         DECLARE_NAPI_PROPERTY("ChangeType", ExportChangeType(env)),
185     };
186 
187     size_t count = sizeof(properties) / sizeof(properties[0]);
188     return napi_define_properties(env, exports, count, properties);
189 }
190 } // namespace OHOS::RelationalStoreJsKit