1 /*
2 * Copyright (C) 2021 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_message_option.h"
17 #include "ipc_debug.h"
18 #include "log_tags.h"
19
20 namespace OHOS {
21 /*
22 * Get flags field from ohos.rpc.MessageOption.
23 */
NapiOhosRpcMessageOptionGetFlags(napi_env env,napi_callback_info info)24 napi_value NapiOhosRpcMessageOptionGetFlags(napi_env env, napi_callback_info info)
25 {
26 napi_value thisVar = nullptr;
27 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
28 NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
29 MessageOption *option = nullptr;
30 napi_status status = napi_unwrap(env, thisVar, (void **)&option);
31 NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
32 int flags = option->GetFlags();
33 napi_value result = nullptr;
34 status = napi_create_int32(env, flags, &result);
35 NAPI_ASSERT(env, status == napi_ok, "failed to create int32 value");
36 return result;
37 }
38
39 /*
40 * Set flags to ohos.rpc.MessageOption
41 */
NapiOhosRpcMessageOptionSetFlags(napi_env env,napi_callback_info info)42 napi_value NapiOhosRpcMessageOptionSetFlags(napi_env env, napi_callback_info info)
43 {
44 napi_value thisVar = nullptr;
45 size_t argc = 1;
46 napi_value argv[1] = { 0 };
47 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
48 NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
49 napi_valuetype valueType = napi_null;
50 napi_typeof(env, argv[0], &valueType);
51 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
52 int32_t flags = 0;
53 napi_status status = napi_get_value_int32(env, argv[0], &flags);
54 NAPI_ASSERT(env, status == napi_ok, "failed to get int32 value");
55 MessageOption *option = nullptr;
56 status = napi_unwrap(env, thisVar, (void **)&option);
57 NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
58 option->SetFlags(flags);
59 napi_value result = nullptr;
60 napi_get_undefined(env, &result);
61 return result;
62 }
63
64 /*
65 * Get wait time field from ohos.rpc.MessageOption.
66 */
NapiOhosRpcMessageOptionGetWaittime(napi_env env,napi_callback_info info)67 napi_value NapiOhosRpcMessageOptionGetWaittime(napi_env env, napi_callback_info info)
68 {
69 napi_value thisVar = nullptr;
70 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
71 NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
72 MessageOption *option = nullptr;
73 napi_status status = napi_unwrap(env, thisVar, (void **)&option);
74 NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
75 int flags = option->GetWaitTime();
76 napi_value result = nullptr;
77 status = napi_create_int32(env, flags, &result);
78 NAPI_ASSERT(env, status == napi_ok, "failed to create int32 value");
79 return result;
80 }
81
82 /*
83 * Set wait time to ohos.rpc.MessageOption
84 */
NapiOhosRpcMessageOptionSetWaittime(napi_env env,napi_callback_info info)85 napi_value NapiOhosRpcMessageOptionSetWaittime(napi_env env, napi_callback_info info)
86 {
87 napi_value thisVar = nullptr;
88 size_t argc = 1;
89 napi_value argv[1] = { 0 };
90 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
91 NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
92 napi_valuetype valueType = napi_null;
93 napi_typeof(env, argv[0], &valueType);
94 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
95 int32_t waittime = 0;
96 napi_status status = napi_get_value_int32(env, argv[0], &waittime);
97 NAPI_ASSERT(env, status == napi_ok, "failed to get int32 value");
98 MessageOption *option = nullptr;
99 status = napi_unwrap(env, thisVar, (void **)&option);
100 NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
101 option->SetWaitTime(waittime);
102 napi_value result = nullptr;
103 napi_get_undefined(env, &result);
104 return result;
105 }
106 } // namespace OHOS