• 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 "ability_manager.h"
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 #include <securec.h>
20 #include <assert.h>
21 #include "ability_errors.h"
22 
23 #define GET_PARAMS(env, info, num) \
24     size_t argc = num;             \
25     napi_value argv[num];          \
26     napi_value thisVar;            \
27     void* data;                    \
28     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
29 
30 typedef struct _SetTimeAsyncContext {
31     napi_env env;
32     napi_async_work work;
33 
34     int64_t time;
35     napi_deferred deferred;
36     napi_ref callbackRef;
37 
38     int status;
39 } SetTimeAsyncContext;
40 
JSAafwkStartAbility(napi_env env,napi_callback_info info)41 static int JSAafwkStartAbility(napi_env env, napi_callback_info info)
42 {
43     napi_status status;
44     size_t argc = 1;
45     napi_value args[1] = {0};;
46     status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
47     assert(status == napi_ok);
48 
49     napi_valuetype types[1];
50     status = napi_typeof(env, args[0], types);
51     assert(status == napi_ok);
52     assert(argc == 1 && types[0] == napi_object);
53 
54     Want want;
55     if (memset_s(&want, sizeof(Want), 0x00, sizeof(Want)) != 0) {
56         return MEMORY_MALLOC_ERROR;
57     }
58     if (GetWantFromNapiValue(env, args[0], want) == false) {
59         return PARAM_CHECK_ERROR;
60     }
61     StartAbility(&want);
62     ClearWant(&want);
63 }
64 
JSAafwkStopAbility(napi_env env,napi_callback_info info)65 static int JSAafwkStopAbility(napi_env env, napi_callback_info info)
66 {
67     napi_status status;
68     size_t argc = 1;
69     napi_value args[1] = {0};;
70     status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
71     assert(status == napi_ok);
72 
73     napi_valuetype types[1];
74     status = napi_typeof(env, args[0], types);
75     assert(status == napi_ok);
76     assert(argc == 1 && types[0] == napi_object);
77 
78     Want want;
79     if (memset_s(&want, sizeof(Want), 0x00, sizeof(Want)) != 0) {
80         return MEMORY_MALLOC_ERROR;
81     }
82     if (GetWantFromNapiValue(env, args[0], want) == false) {
83         return PARAM_CHECK_ERROR;
84     }
85     StopAbility(&want);
86     ClearWant(&want);
87 }
88 
GetWantFromNapiValue(napi_env env,napi_value args,Want & want)89 static bool GetWantFromNapiValue(napi_env env, napi_value args, Want& want)
90 {
91     ElementName element;
92     if (memset_s(&element, sizeof(ElementName), 0x00, sizeof(ElementName)) != 0) {
93         return MEMORY_MALLOC_ERROR;
94     }
95 
96     napi_value data;
97     napi_get_named_property(env, args, "want_param", &data);
98 
99     // napi_has_named_property(env, arg, "audioSourceType", &bIsPresent);
100     napi_value elementName;
101     if (napi_get_named_property(env, args, "elementName", &elementName) != napi_ok) {
102         return COMMAND_ERROR;
103     }
104 
105     napi_value napi_deviceId;
106     napi_get_named_property(env, elementName, "deviceId", &napi_deviceId);
107     char *deviceId = nullptr;
108     GetCharPointerArgument(env, napi_deviceId, deviceId);
109     SetElementDeviceID(&element, deviceId);
110     free(deviceId);
111     deviceId = nullptr;
112 
113 
114     napi_value napi_bundleName;
115     napi_get_named_property(env, elementName, "bundleName", &napi_bundleName);
116     char *bundleName = nullptr;
117     GetCharPointerArgument(env, napi_bundleName, bundleName);
118     SetElementBundleName(&element, bundleName);
119     free(bundleName);
120     bundleName = nullptr;
121 
122     napi_value napi_abilityName;
123     napi_get_named_property(env, elementName, "abilityName", &napi_abilityName);
124     char *abilityName = nullptr;
125     GetCharPointerArgument(env, napi_abilityName, abilityName);
126     SetElementAbilityName(&element, abilityName);
127     free(abilityName);
128     abilityName = nullptr;
129 
130     SetWantData(&want, (void *)data, sizeof(data));
131     SetWantElement(&want, element);
132     ClearElement(&element);
133 }
134 
135 // Function to read string argument from napi_value
GetCharPointerArgument(napi_env env,napi_value value,char * result)136 static bool GetCharPointerArgument(napi_env env, napi_value value, char* result)
137 {
138     napi_status status;
139     size_t bufLength = 0;
140     result = nullptr;
141     bool ret = false;
142     // get buffer length first and get buffer based on length
143     status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
144     if (status == napi_ok && bufLength > 0) {
145         // Create a buffer and create std::string later from it
146         result = (char *) malloc((bufLength + 1) * sizeof(char));
147         if (result != nullptr) {
148             status = napi_get_value_string_utf8(env, value, result, bufLength + 1, &bufLength);
149             if (status == napi_ok) {
150                 ret = true;
151             }
152         }
153     }
154     return ret;
155 }
156 
157 EXTERN_C_START
AafwkExport(napi_env env,napi_value exports)158 napi_value AafwkExport(napi_env env, napi_value exports)
159 {
160     static napi_property_descriptor desc[] = {
161         DECLARE_NAPI_FUNCTION("startAbility", JSAafwkStartAbility),
162         DECLARE_NAPI_FUNCTION("stopAbility", JSAafwkStopAbility),
163     };
164     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
165     return exports;
166 }
167 EXTERN_C_END
168 
169 static napi_module aafwk_module = {
170     .nm_version = 1,
171     .nm_flags = 0,
172     .nm_filename = nullptr,
173     .nm_register_func = AafwkExport,
174     .nm_modname = "aafwk",
175     .nm_priv = ((void*)0),
176     .reserved = {0}
177 };
178 
AafwkRegister()179 extern "C" __attribute__((constructor)) void AafwkRegister()
180 {
181     napi_module_register(&aafwk_module);
182 }