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
16 #define LOG_TAG "DataShareConstProperties"
17
18 #include "napi_datashare_const_properties.h"
19
20 #include <string>
21
22 #include "datashare_helper.h"
23 #include "napi_base_context.h"
24 #include "napi_common_util.h"
25
26 namespace OHOS::DataShare {
27
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)28 static napi_status SetNamedProperty(napi_env env, napi_value &obj, const std::string &name, int32_t value)
29 {
30 napi_value property = nullptr;
31 napi_status status = napi_create_int32(env, value, &property);
32 NAPI_ASSERT_BASE(env, status == napi_ok, "int32_t to napi_value failed!", status);
33 napi_set_named_property(env, obj, name.c_str(), property);
34 return status;
35 }
36
ExportChangeType(napi_env env)37 static napi_value ExportChangeType(napi_env env)
38 {
39 napi_value changeType = nullptr;
40 napi_create_object(env, &changeType);
41 SetNamedProperty(env, changeType, "INSERT", static_cast<int32_t>(DataShareObserver::ChangeType::INSERT));
42 SetNamedProperty(env, changeType, "DELETE", static_cast<int32_t>(DataShareObserver::ChangeType::DELETE));
43 SetNamedProperty(env, changeType, "UPDATE", static_cast<int32_t>(DataShareObserver::ChangeType::UPDATE));
44 napi_object_freeze(env, changeType);
45 return changeType;
46 }
47
ExportSubscriptionType(napi_env env)48 static napi_value ExportSubscriptionType(napi_env env)
49 {
50 napi_value SubscriptionType = nullptr;
51 napi_create_object(env, &SubscriptionType);
52 SetNamedProperty(env, SubscriptionType, "SUBSCRIPTION_TYPE_EXACT_URI",
53 static_cast<int32_t>(DataShareObserver::SubscriptionType::SUBSCRIPTION_TYPE_EXACT_URI));
54 napi_object_freeze(env, SubscriptionType);
55 return SubscriptionType;
56 }
InitConstProperties(napi_env env,napi_value exports)57 napi_status InitConstProperties(napi_env env, napi_value exports)
58 {
59 const napi_property_descriptor properties[] = {
60 DECLARE_NAPI_PROPERTY("ChangeType", ExportChangeType(env)),
61 DECLARE_NAPI_PROPERTY("SubscriptionType", ExportSubscriptionType(env)),
62 };
63 size_t count = sizeof(properties) / sizeof(properties[0]);
64
65 return napi_define_properties(env, exports, count, properties);
66 }
67 } // namespace OHOS::DataShare