• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     napi_status 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     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 async to ohos.rpc.MessageOption
66  */
NapiOhosRpcMessageOptionIsAsync(napi_env env,napi_callback_info info)67 napi_value NapiOhosRpcMessageOptionIsAsync(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_unwrap(env, thisVar, (void **)&option);
74     NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
75     int flags = option->GetFlags();
76     napi_value result = nullptr;
77     napi_status status = napi_get_boolean(env, flags != 0 ? true : false, &result);
78     NAPI_ASSERT(env, status == napi_ok, "failed to create boolean value");
79     return result;
80 }
81 
82 /*
83  * Set async to ohos.rpc.MessageOption
84  */
NapiOhosRpcMessageOptionSetAsync(napi_env env,napi_callback_info info)85 napi_value NapiOhosRpcMessageOptionSetAsync(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_boolean, "type mismatch for parameter 1");
95     bool flags = false;
96     napi_status status = napi_get_value_bool(env, argv[0], &flags);
97     NAPI_ASSERT(env, status == napi_ok, "failed to get boolean value");
98     MessageOption *option = nullptr;
99     napi_unwrap(env, thisVar, (void **)&option);
100     NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
101     option->SetFlags(static_cast<int32_t> (flags));
102     napi_value result = nullptr;
103     napi_get_undefined(env, &result);
104     return result;
105 }
106 
107 /*
108  * Get wait time field from ohos.rpc.MessageOption.
109  */
NapiOhosRpcMessageOptionGetWaittime(napi_env env,napi_callback_info info)110 napi_value NapiOhosRpcMessageOptionGetWaittime(napi_env env, napi_callback_info info)
111 {
112     napi_value thisVar = nullptr;
113     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
114     NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
115     MessageOption *option = nullptr;
116     napi_unwrap(env, thisVar, (void **)&option);
117     NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
118     int flags = option->GetWaitTime();
119     napi_value result = nullptr;
120     napi_status status = napi_create_int32(env, flags, &result);
121     NAPI_ASSERT(env, status == napi_ok, "failed to create int32 value");
122     return result;
123 }
124 
125 /*
126  * Set wait time to ohos.rpc.MessageOption
127  */
NapiOhosRpcMessageOptionSetWaittime(napi_env env,napi_callback_info info)128 napi_value NapiOhosRpcMessageOptionSetWaittime(napi_env env, napi_callback_info info)
129 {
130     napi_value thisVar = nullptr;
131     size_t argc = 1;
132     napi_value argv[1] = { 0 };
133     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
134     NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
135     napi_valuetype valueType = napi_null;
136     napi_typeof(env, argv[0], &valueType);
137     NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
138     int32_t waittime = 0;
139     napi_status status = napi_get_value_int32(env, argv[0], &waittime);
140     NAPI_ASSERT(env, status == napi_ok, "failed to get int32 value");
141     MessageOption *option = nullptr;
142     napi_unwrap(env, thisVar, (void **)&option);
143     NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
144     option->SetWaitTime(waittime);
145     napi_value result = nullptr;
146     napi_get_undefined(env, &result);
147     return result;
148 }
149 } // namespace OHOS