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 #include "datetime_manager_addon.h"
16 #include "edm_log.h"
17
18 using namespace OHOS::EDM;
19
Init(napi_env env,napi_value exports)20 napi_value DatetimeManagerAddon::Init(napi_env env, napi_value exports)
21 {
22 napi_property_descriptor property[] = {
23 DECLARE_NAPI_FUNCTION("setDateTime", SetDateTime),
24 };
25 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
26 return exports;
27 }
28
SetDateTime(napi_env env,napi_callback_info info)29 napi_value DatetimeManagerAddon::SetDateTime(napi_env env, napi_callback_info info)
30 {
31 EDMLOGI("NAPI_SetDateTime called");
32 size_t argc = ARGS_SIZE_THREE;
33 napi_value argv[ARGS_SIZE_THREE] = {nullptr};
34 napi_value thisArg = nullptr;
35 void *data = nullptr;
36 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
37 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error");
38 bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object) &&
39 MatchValueType(env, argv[ARR_INDEX_ONE], napi_number);
40 if (argc > ARGS_SIZE_TWO) {
41 matchFlag = matchFlag && MatchValueType(env, argv[ARR_INDEX_TWO], napi_function);
42 }
43 ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error");
44 auto asyncCallbackInfo = new (std::nothrow) AsyncSetDateTimeCallbackInfo();
45 if (asyncCallbackInfo == nullptr) {
46 return nullptr;
47 }
48 std::unique_ptr<AsyncSetDateTimeCallbackInfo> callbackPtr {asyncCallbackInfo};
49 bool ret = ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]);
50 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error");
51 EDMLOGD("SetDateTime: asyncCallbackInfo->elementName.bundlename %{public}s, "
52 "asyncCallbackInfo->abilityname:%{public}s",
53 asyncCallbackInfo->elementName.GetBundleName().c_str(),
54 asyncCallbackInfo->elementName.GetAbilityName().c_str());
55 ret = ParseLong(env, asyncCallbackInfo->time, argv[ARR_INDEX_ONE]);
56 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "time param error");
57 if (argc > ARGS_SIZE_TWO) {
58 EDMLOGD("NAPI_SetDateTime argc == ARGS_SIZE_THREE");
59 napi_create_reference(env, argv[ARR_INDEX_TWO], NAPI_RETURN_ONE, &asyncCallbackInfo->callback);
60 }
61
62 napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "SetDateTime",
63 NativeSetDateTime, NativeVoidCallbackComplete);
64 callbackPtr.release();
65 return asyncWorkReturn;
66 }
67
NativeSetDateTime(napi_env env,void * data)68 void DatetimeManagerAddon::NativeSetDateTime(napi_env env, void *data)
69 {
70 EDMLOGI("NAPI_NativeSetDateTime called");
71 if (data == nullptr) {
72 EDMLOGE("data is nullptr");
73 return;
74 }
75 AsyncSetDateTimeCallbackInfo *asyncCallbackInfo = static_cast<AsyncSetDateTimeCallbackInfo *>(data);
76 auto dateTimeManagerProxy_ = DatetimeManagerProxy::GetDatetimeManagerProxy();
77 if (dateTimeManagerProxy_ == nullptr) {
78 EDMLOGE("can not get DatetimeManagerProxy");
79 return;
80 }
81 asyncCallbackInfo->ret = dateTimeManagerProxy_->SetDateTime(asyncCallbackInfo->elementName,
82 asyncCallbackInfo->time);
83 }
84
85 static napi_module g_dateTimeManagerModule = {
86 .nm_version = 1,
87 .nm_flags = 0,
88 .nm_filename = nullptr,
89 .nm_register_func = DatetimeManagerAddon::Init,
90 .nm_modname = "enterprise.dateTimeManager",
91 .nm_priv = ((void *)0),
92 .reserved = { 0 },
93 };
94
DateTimeManagerRegister()95 extern "C" __attribute__((constructor)) void DateTimeManagerRegister()
96 {
97 napi_module_register(&g_dateTimeManagerModule);
98 }