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/native_api.h"
17 #include "napi/native_node_api.h"
18 #include "common_types.h"
19 
20 namespace OHOS::CommonType {
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)21 static napi_status SetNamedProperty(napi_env env, napi_value &obj, const std::string &name,
22     int32_t value)
23 {
24     napi_value property = nullptr;
25     napi_status status = napi_create_int32(env, value, &property);
26     if (status != napi_ok) {
27         return status;
28     }
29     status = napi_set_named_property(env, obj, name.c_str(), property);
30     return status;
31 }
32 
ExportAssetStatus(napi_env env)33 static napi_value ExportAssetStatus(napi_env env)
34 {
35     napi_value assetStatus = nullptr;
36     napi_create_object(env, &assetStatus);
37     SetNamedProperty(env, assetStatus, "ASSET_NORMAL", AssetValue::STATUS_NORMAL);
38     SetNamedProperty(env, assetStatus, "ASSET_INSERT", AssetValue::STATUS_INSERT);
39     SetNamedProperty(env, assetStatus, "ASSET_UPDATE", AssetValue::STATUS_UPDATE);
40     SetNamedProperty(env, assetStatus, "ASSET_DELETE", AssetValue::STATUS_DELETE);
41     SetNamedProperty(env, assetStatus, "ASSET_ABNORMAL", AssetValue::STATUS_ABNORMAL);
42     SetNamedProperty(env, assetStatus, "ASSET_DOWNLOADING", AssetValue::STATUS_DOWNLOADING);
43     napi_object_freeze(env, assetStatus);
44     return assetStatus;
45 }
46 }
47 
CommonTypeExport(napi_env env,napi_value exports)48 static napi_value CommonTypeExport(napi_env env, napi_value exports)
49 {
50     napi_status status;
51     const napi_property_descriptor desc[] = {
52         DECLARE_NAPI_PROPERTY("AssetStatus", OHOS::CommonType::ExportAssetStatus(env)),
53     };
54 
55     status = napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
56     if (status != napi_ok) {
57         return nullptr;
58     }
59     return exports;
60 }
61 
62 static napi_module storageModule = {
63     .nm_version = 1,
64     .nm_flags = 0,
65     .nm_filename = nullptr,
66     .nm_register_func = CommonTypeExport,
67     .nm_modname = "data.commonType",
68     .nm_priv = ((void *)0),
69     .reserved = { 0 },
70 };
71 
RegisterModule()72 static __attribute__((constructor)) void RegisterModule()
73 {
74     napi_module_register(&storageModule);
75 }